From 74c161c576c60e061753458a8a5e08395ee22048 Mon Sep 17 00:00:00 2001 From: Ferdinand Majerech Date: Tue, 29 Jul 2014 23:21:07 +0200 Subject: [PATCH] validateUTF8NoGC now calculates the number of characters in passed string. --- source/dyaml/nogcutil.d | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/source/dyaml/nogcutil.d b/source/dyaml/nogcutil.d index d96cb1c..15a965c 100644 --- a/source/dyaml/nogcutil.d +++ b/source/dyaml/nogcutil.d @@ -231,6 +231,8 @@ struct ValidateResult { /// Is the validated string valid? bool valid; + /// Number of characters in the string. + size_t characterCount; /// If the string is not valid, error message with details is here. string msg; /// If the string is not valid, the first invalid sequence of bytes is here. @@ -252,11 +254,13 @@ private: ValidateResult validateUTF8NoGC(const(char[]) str) @trusted pure nothrow @nogc { immutable len = str.length; + size_t characterCount; outer: for (size_t index = 0; index < len; ) { if(str[index] < 0x80) { - index++; + ++index; + ++characterCount; continue; } @@ -329,6 +333,7 @@ ValidateResult validateUTF8NoGC(const(char[]) str) @trusted pure nothrow @nogc if (!isValidDchar(d)) { return invalidUTF(pstr[0 .. length]); } } + ++characterCount; index += i + 1; static if(i == 3) { @@ -341,7 +346,7 @@ ValidateResult validateUTF8NoGC(const(char[]) str) @trusted pure nothrow @nogc return invalidUTF(pstr[0 .. length]); } - return ValidateResult(true); + return ValidateResult(true, characterCount); } /// @nogc version of std.utf.decode() for (char[]), but assumes str is valid UTF-8.