mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-01-19 00:37:25 +01:00
[ion/unicode] StringGlyphLength method
This commit is contained in:
committed by
EmilieNumworks
parent
1e64db68ef
commit
50bbae5e06
@@ -34,8 +34,9 @@ tests += $(addprefix ion/test/,\
|
||||
keyboard.cpp\
|
||||
storage.cpp\
|
||||
utf8_decoder.cpp\
|
||||
utf8_helper.cpp\
|
||||
)
|
||||
|
||||
ifdef ION_STORAGE_LOG
|
||||
SFLAGS += -DION_STORAGE_LOG=1
|
||||
endif
|
||||
endif
|
||||
|
||||
@@ -68,6 +68,10 @@ int RemovePreviousCodePoint(const char * text, char * location, CodePoint * c);
|
||||
const char * CodePointAtGlyphOffset(const char * buffer, int position);
|
||||
size_t GlyphOffsetAtCodePoint(const char * buffer, const char * position);
|
||||
|
||||
/* Return the number of glyphs in a string.
|
||||
* For instance, strlen("∑") = 3 but StringGlyphLength("∑") = 1 */
|
||||
size_t StringGlyphLength(const char * s, int maxSize = -1);
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -326,5 +326,20 @@ size_t GlyphOffsetAtCodePoint(const char * buffer, const char * position) {
|
||||
return glyphIndex;
|
||||
}
|
||||
|
||||
size_t StringGlyphLength(const char * s, int maxSize) {
|
||||
if (maxSize == 0) {
|
||||
return 0;
|
||||
}
|
||||
UTF8Decoder decoder(s);
|
||||
CodePoint codePoint = decoder.nextCodePoint();
|
||||
size_t glyphIndex = 0;
|
||||
while (codePoint != UCodePointNull && (maxSize < 0 || ((decoder.stringPosition() - s) <= maxSize))) {
|
||||
if (!codePoint.isCombining()) {
|
||||
glyphIndex++;
|
||||
}
|
||||
codePoint = decoder.nextCodePoint();
|
||||
}
|
||||
return glyphIndex;
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
14
ion/test/utf8_helper.cpp
Normal file
14
ion/test/utf8_helper.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#include <quiz.h>
|
||||
#include <ion/unicode/utf8_helper.h>
|
||||
|
||||
void assert_string_glyph_length_is(const char * string, int maxSize, size_t result) {
|
||||
quiz_assert(UTF8Helper::StringGlyphLength(string, maxSize) == result);
|
||||
}
|
||||
|
||||
QUIZ_CASE(ion_utf8_helper_glyph_length) {
|
||||
assert_string_glyph_length_is("123", -1, 3);
|
||||
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);
|
||||
}
|
||||
Reference in New Issue
Block a user