mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-01-18 16:27:34 +01:00
[ion/utf8] next/previousGlyphPosition methods and tests
This commit is contained in:
committed by
EmilieNumworks
parent
fb80cd3271
commit
1043426c40
@@ -38,6 +38,8 @@ public:
|
||||
}
|
||||
CodePoint nextCodePoint();
|
||||
CodePoint previousCodePoint();
|
||||
const char * nextGlyphPosition();
|
||||
const char * previousGlyphPosition();
|
||||
const char * stringPosition() const { return m_stringPosition; }
|
||||
static size_t CharSizeOfCodePoint(CodePoint c);
|
||||
static size_t CodePointToChars(CodePoint c, char * buffer, size_t bufferSize);
|
||||
|
||||
@@ -63,6 +63,30 @@ CodePoint UTF8Decoder::previousCodePoint() {
|
||||
return CodePoint(result);
|
||||
}
|
||||
|
||||
const char * UTF8Decoder::nextGlyphPosition() {
|
||||
assert(*m_stringPosition != 0 && (m_stringPosition == m_string || *(m_stringPosition - 1) != 0));
|
||||
CodePoint followingCodePoint = nextCodePoint();
|
||||
const char * resultGlyphPosition = m_stringPosition;
|
||||
followingCodePoint = nextCodePoint();
|
||||
while (followingCodePoint != UCodePointNull && followingCodePoint.isCombining()) {
|
||||
resultGlyphPosition = m_stringPosition;
|
||||
followingCodePoint = nextCodePoint();
|
||||
}
|
||||
m_stringPosition = resultGlyphPosition;
|
||||
return resultGlyphPosition;
|
||||
}
|
||||
|
||||
const char * UTF8Decoder::previousGlyphPosition() {
|
||||
assert(m_stringPosition > m_string);
|
||||
CodePoint previousCP = previousCodePoint();
|
||||
const char * resultGlyphPosition = m_stringPosition;
|
||||
while (m_stringPosition > m_string && previousCP.isCombining()) {
|
||||
previousCP = previousCodePoint();
|
||||
resultGlyphPosition = m_stringPosition;
|
||||
}
|
||||
return resultGlyphPosition;
|
||||
}
|
||||
|
||||
size_t UTF8Decoder::CharSizeOfCodePoint(CodePoint c) {
|
||||
if (c <= 0x7F) {
|
||||
return 1;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include <quiz.h>
|
||||
#include <ion/unicode/utf8_decoder.h>
|
||||
#include <ion/unicode/utf8_helper.h>
|
||||
|
||||
void assert_decodes_to(const char * string, CodePoint c) {
|
||||
UTF8Decoder d(string);
|
||||
@@ -12,6 +13,18 @@ void assert_previous_code_point_is_to(const char * string, const char * stringPo
|
||||
quiz_assert(d.previousCodePoint() == c);
|
||||
}
|
||||
|
||||
void assert_code_point_at_next_glyph_position_is(const char * string, CodePoint c) {
|
||||
UTF8Decoder d(string);
|
||||
d.nextGlyphPosition();
|
||||
quiz_assert(d.nextCodePoint() == c);
|
||||
}
|
||||
|
||||
void assert_code_point_at_previous_glyph_position_is(const char * string, const char * stringPosition, CodePoint c) {
|
||||
UTF8Decoder d(string, stringPosition);
|
||||
d.previousGlyphPosition();
|
||||
quiz_assert(d.nextCodePoint() == c);
|
||||
}
|
||||
|
||||
QUIZ_CASE(ion_utf8_decode_forward) {
|
||||
assert_decodes_to("\x20", 0x20);
|
||||
assert_decodes_to("\xC2\xA2", 0xA2);
|
||||
@@ -25,3 +38,16 @@ QUIZ_CASE(ion_utf8_decode_backwards) {
|
||||
assert_previous_code_point_is_to(a, a+4, *(a+3));
|
||||
assert_previous_code_point_is_to(a, a+6, *(a+5));
|
||||
}
|
||||
|
||||
QUIZ_CASE(ion_utf8_decoder_next_glyph) {
|
||||
const char * string = u8"a\u0065\u0301i";
|
||||
assert_code_point_at_next_glyph_position_is(string, 'e');
|
||||
assert_code_point_at_next_glyph_position_is(string+1, 'i');
|
||||
}
|
||||
|
||||
QUIZ_CASE(ion_utf8_decoder_previous_glyph) {
|
||||
const char * string = u8"a\u0065\u0301i";
|
||||
const char * iPosition = UTF8Helper::CodePointSearch(string, 'i');
|
||||
assert_code_point_at_previous_glyph_position_is(string, iPosition, 'e');
|
||||
assert_code_point_at_previous_glyph_position_is(string,string+1, 'a');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user