From 872de8deef877b8b7e0c97ddb6cafd8e0dc3fbc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Fri, 14 Jun 2019 13:50:30 +0200 Subject: [PATCH] [ion/utf8_helper] Fix StringGlyphLength --- ion/src/shared/unicode/utf8_helper.cpp | 9 ++++++--- ion/test/utf8_helper.cpp | 3 ++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/ion/src/shared/unicode/utf8_helper.cpp b/ion/src/shared/unicode/utf8_helper.cpp index a972fc079..2b4391f7d 100644 --- a/ion/src/shared/unicode/utf8_helper.cpp +++ b/ion/src/shared/unicode/utf8_helper.cpp @@ -340,13 +340,16 @@ size_t StringGlyphLength(const char * s, int maxSize) { return 0; } UTF8Decoder decoder(s); - CodePoint codePoint = decoder.nextCodePoint(); + CodePoint codePoint = 0; size_t glyphIndex = 0; - while (codePoint != UCodePointNull && (maxSize < 0 || ((decoder.stringPosition() - s) <= maxSize))) { + while (maxSize < 0 || ((decoder.stringPosition() - s) < maxSize)) { + codePoint = decoder.nextCodePoint(); + if (codePoint == UCodePointNull) { + break; + } if (!codePoint.isCombining()) { glyphIndex++; } - codePoint = decoder.nextCodePoint(); } return glyphIndex; } diff --git a/ion/test/utf8_helper.cpp b/ion/test/utf8_helper.cpp index 975b5cff1..23912121f 100644 --- a/ion/test/utf8_helper.cpp +++ b/ion/test/utf8_helper.cpp @@ -10,5 +10,6 @@ QUIZ_CASE(ion_utf8_helper_glyph_length) { assert_string_glyph_length_is("1ᴇ3", -1, 3); assert_string_glyph_length_is("∑∫𝐢", -1, 3); assert_string_glyph_length_is("123", 2, 2); - assert_string_glyph_length_is("1ᴇ3", 2, 1); + uint8_t testString[] = {'a', 'b', 'c', 0b11111111, 0b11111111, 0}; // Malformed utf-8 string + assert_string_glyph_length_is((const char *)testString, 3, 3); }