Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::Utf8Proc Class Reference

Static Public Member Functions

static UnicodeType Analyze (const char *s, size_t len, UnicodeInvalidReason *invalid_reason=nullptr, size_t *invalid_pos=nullptr)
 Distinguishes ASCII, Valid UTF8 and Invalid UTF8 strings.
static char * Normalize (const char *s, size_t len)
 Performs UTF NFC normalization of string, return value needs to be free'd.
static bool IsValid (const char *s, size_t len)
 Returns whether or not the UTF8 string is valid.
static void MakeValid (char *s, size_t len, char special_flag='?')
 Makes Invalid Unicode valid by replacing invalid parts with a given character.
static std::string RemoveInvalid (const char *s, size_t len)
 Creates a new string with invalid UTF-8 characters removed.
static size_t NextGraphemeCluster (const char *s, size_t len, size_t pos)
 Returns the position (in bytes) of the next grapheme cluster.
static size_t PreviousGraphemeCluster (const char *s, size_t len, size_t pos)
 Returns the position (in bytes) of the previous grapheme cluster.
static bool CodepointToUtf8 (int cp, int &sz, char *c)
 Transform a codepoint to utf8 and writes it to "c", sets "sz" to the size of the codepoint.
static int CodepointLength (int cp)
 Returns the codepoint length in bytes when encoded in UTF8.
static int32_t UTF8ToCodepoint (const char *c, int &sz)
 Transform a UTF8 string to a codepoint; returns the codepoint and writes the length of the codepoint (in UTF8) to sz.
static size_t RenderWidth (const char *s, size_t len, size_t pos)
 Returns the render width of a single character in a string.
static size_t RenderWidth (const std::string &str)
static int32_t CodepointToUpper (int32_t codepoint)
static int32_t CodepointToLower (int32_t codepoint)
static GraphemeIterator GraphemeClusters (const char *s, size_t len)
 Constructs a class that can be iterated over to fetch grapheme clusters in a string.
static size_t GraphemeCount (const char *s, size_t len)
 Returns the number of grapheme clusters in a string.

Member Function Documentation

◆ Analyze()

UnicodeType duckdb::Utf8Proc::Analyze ( const char * s,
size_t len,
UnicodeInvalidReason * invalid_reason = nullptr,
size_t * invalid_pos = nullptr )
static

Distinguishes ASCII, Valid UTF8 and Invalid UTF8 strings.

507298 {
507299 UnicodeType type = UnicodeType::ASCII;
507300
507301 static constexpr uint64_t MASK = 0x8080808080808080U;
507302 for (size_t i = 0; i < len;) {
507303 // Check 8 bytes at a time until we hit non-ASCII
507304 for (; i + sizeof(uint64_t) <= len; i += sizeof(uint64_t)) {
507305 if (Load<uint64_t>(const_data_ptr_cast(s + i)) & MASK) {
507306 break; // Non-ASCII in the next 8 bytes
507307 }
507308 }
507309 // Check 1 byte at a time for the next 8 bytes
507310 const auto end = MinValue(i + sizeof(uint64_t), len);
507311 for (; i < end; i++) {
507312 int c = (int)s[i];
507313 if ((c & 0x80) == 0) {
507314 continue;
507315 }
507316 int first_pos_seq = i;
507317
507318 if ((c & 0xE0) == 0xC0) {
507319 /* 2 byte sequence */
507320 int utf8char = c & 0x1F;
507321 type = UTF8ExtraByteLoop<1, 0x000780>(first_pos_seq, utf8char, i, s, len, invalid_reason, invalid_pos);
507322 } else if ((c & 0xF0) == 0xE0) {
507323 /* 3 byte sequence */
507324 int utf8char = c & 0x0F;
507325 type = UTF8ExtraByteLoop<2, 0x00F800>(first_pos_seq, utf8char, i, s, len, invalid_reason, invalid_pos);
507326 } else if ((c & 0xF8) == 0xF0) {
507327 /* 4 byte sequence */
507328 int utf8char = c & 0x07;
507329 type = UTF8ExtraByteLoop<3, 0x1F0000>(first_pos_seq, utf8char, i, s, len, invalid_reason, invalid_pos);
507330 } else {
507331 /* invalid UTF-8 start byte */
507332 AssignInvalidUTF8Reason(invalid_reason, invalid_pos, i, UnicodeInvalidReason::BYTE_MISMATCH);
507333 return UnicodeType::INVALID;
507334 }
507335 if (type == UnicodeType::INVALID) {
507336 return type;
507337 }
507338 }
507339 }
507340 return type;
::uint64_t uint64_t
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Normalize()

char * duckdb::Utf8Proc::Normalize ( const char * s,
size_t len )
static

Performs UTF NFC normalization of string, return value needs to be free'd.

507378 {
507379 assert(s);
507380 assert(Utf8Proc::Analyze(s, len) != UnicodeType::INVALID);
507381 return (char *)utf8proc_NFC((const utf8proc_uint8_t *)s, len);
static UnicodeType Analyze(const char *s, size_t len, UnicodeInvalidReason *invalid_reason=nullptr, size_t *invalid_pos=nullptr)
Distinguishes ASCII, Valid UTF8 and Invalid UTF8 strings.
Definition duckdb.cpp:507297
#define assert(x)
Definition duckdb.cpp:486071
UTF8PROC_DLLEXPORT utf8proc_uint8_t * utf8proc_NFC(const utf8proc_uint8_t *str, utf8proc_ssize_t strlen)
Definition duckdb.cpp:507178
Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsValid()

bool duckdb::Utf8Proc::IsValid ( const char * s,
size_t len )
static

Returns whether or not the UTF8 string is valid.

507384 {
507385 return Utf8Proc::Analyze(s, len) != UnicodeType::INVALID;
Here is the call graph for this function:
Here is the caller graph for this function:

◆ MakeValid()

void duckdb::Utf8Proc::MakeValid ( char * s,
size_t len,
char special_flag = '?' )
static

Makes Invalid Unicode valid by replacing invalid parts with a given character.

507343 {
507344 D_ASSERT(special_flag <= 127);
507345 UnicodeType type = UnicodeType::ASCII;
507346 for (size_t i = 0; i < len; i++) {
507347 int c = (int)s[i];
507348 if ((c & 0x80) == 0) {
507349 continue;
507350 }
507351 int first_pos_seq = i;
507352 if ((c & 0xE0) == 0xC0) {
507353 /* 2 byte sequence */
507354 int utf8char = c & 0x1F;
507355 type = UTF8ExtraByteLoop<1, 0x000780>(first_pos_seq, utf8char, i, s, len, nullptr, nullptr);
507356 } else if ((c & 0xF0) == 0xE0) {
507357 /* 3 byte sequence */
507358 int utf8char = c & 0x0F;
507359 type = UTF8ExtraByteLoop<2, 0x00F800>(first_pos_seq, utf8char, i, s, len, nullptr, nullptr);
507360 } else if ((c & 0xF8) == 0xF0) {
507361 /* 4 byte sequence */
507362 int utf8char = c & 0x07;
507363 type = UTF8ExtraByteLoop<3, 0x1F0000>(first_pos_seq, utf8char, i, s, len, nullptr, nullptr);
507364 } else {
507365 /* invalid UTF-8 start byte */
507366 s[i] = special_flag; // Rewrite invalid byte
507367 }
507368 if (type == UnicodeType::INVALID) {
507369 for (size_t j = first_pos_seq; j <= i; j++) {
507370 s[j] = special_flag; // Rewrite each byte of the invalid sequence
507371 }
507372 type = UnicodeType::ASCII;
507373 }
507374 }
507375 D_ASSERT(Utf8Proc::IsValid(s, len));
static bool IsValid(const char *s, size_t len)
Returns whether or not the UTF8 string is valid.
Definition duckdb.cpp:507383
Here is the call graph for this function:
Here is the caller graph for this function:

◆ RemoveInvalid()

std::string duckdb::Utf8Proc::RemoveInvalid ( const char * s,
size_t len )
static

Creates a new string with invalid UTF-8 characters removed.

507388 {
507389 std::string result;
507390 result.reserve(len); // Reserve the maximum possible size
507391
507392 for (size_t i = 0; i < len; i++) {
507393 int c = (int)s[i];
507394 if ((c & 0x80) == 0) {
507395 // ASCII character - always valid
507396 result.push_back(s[i]);
507397 continue;
507398 }
507399
507400 int first_pos_seq = i;
507401 if ((c & 0xE0) == 0xC0) {
507402 /* 2 byte sequence */
507403 int utf8char = c & 0x1F;
507404 UTF8ExtraByteLoop<1, 0x000780>(first_pos_seq, utf8char, i, s, len, nullptr, nullptr);
507405 } else if ((c & 0xF0) == 0xE0) {
507406 /* 3 byte sequence */
507407 int utf8char = c & 0x0F;
507408 UTF8ExtraByteLoop<2, 0x00F800>(first_pos_seq, utf8char, i, s, len, nullptr, nullptr);
507409 } else if ((c & 0xF8) == 0xF0) {
507410 /* 4 byte sequence */
507411 int utf8char = c & 0x07;
507412 UTF8ExtraByteLoop<3, 0x1F0000>(first_pos_seq, utf8char, i, s, len, nullptr, nullptr);
507413 } else {
507414 // invalid, do not write to output
507415 continue;
507416 }
507417
507418 // If we get here, the sequence is valid, so add all bytes of the sequence to result
507419 for (size_t j = first_pos_seq; j <= i; j++) {
507420 result.push_back(s[j]);
507421 }
507422 }
507423
507424 D_ASSERT(Utf8Proc::IsValid(result.c_str(), result.size()));
507425 return result;
Here is the call graph for this function:
Here is the caller graph for this function:

◆ NextGraphemeCluster()

size_t duckdb::Utf8Proc::NextGraphemeCluster ( const char * s,
size_t len,
size_t pos )
static

Returns the position (in bytes) of the next grapheme cluster.

507428 {
507429 int sz;
507430 auto prev_codepoint = Utf8Proc::UTF8ToCodepoint(s + cpos, sz);
507431 utf8proc_int32_t state = 0;
507432 while (true) {
507433 cpos += sz;
507434 if (cpos >= len) {
507435 return cpos;
507436 }
507437 auto next_codepoint = Utf8Proc::UTF8ToCodepoint(s + cpos, sz);
507438 if (utf8proc_grapheme_break_stateful(prev_codepoint, next_codepoint, &state)) {
507439 // found a grapheme break here
507440 return cpos;
507441 }
507442 // not a grapheme break, move on to next codepoint
507443 prev_codepoint = next_codepoint;
507444 }
static int32_t UTF8ToCodepoint(const char *c, int &sz)
Transform a UTF8 string to a codepoint; returns the codepoint and writes the length of the codepoint ...
Definition duckdb.cpp:507584
UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_grapheme_break_stateful(utf8proc_int32_t codepoint1, utf8proc_int32_t codepoint2, utf8proc_int32_t *state)
Definition duckdb.cpp:506734
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PreviousGraphemeCluster()

size_t duckdb::Utf8Proc::PreviousGraphemeCluster ( const char * s,
size_t len,
size_t pos )
static

Returns the position (in bytes) of the previous grapheme cluster.

507522 {
507523 if (!Utf8Proc::IsValid(s, len)) {
507524 return cpos - 1;
507525 }
507526 size_t current_pos = 0;
507527 while (true) {
507528 size_t new_pos = NextGraphemeCluster(s, len, current_pos);
507529 if (new_pos <= current_pos || new_pos >= cpos) {
507530 return current_pos;
507531 }
507532 current_pos = new_pos;
507533 }
static size_t NextGraphemeCluster(const char *s, size_t len, size_t pos)
Returns the position (in bytes) of the next grapheme cluster.
Definition duckdb.cpp:507427
Here is the call graph for this function:

◆ CodepointToUtf8()

bool duckdb::Utf8Proc::CodepointToUtf8 ( int cp,
int & sz,
char * c )
static

Transform a codepoint to utf8 and writes it to "c", sets "sz" to the size of the codepoint.

507536 {
507537 if (cp <= 0x7F) {
507538 sz = 1;
507539 c[0] = cp;
507540 } else if (cp <= 0x7FF) {
507541 sz = 2;
507542 c[0] = (cp >> 6) + 192;
507543 c[1] = (cp & 63) + 128;
507544 } else if (0xd800 <= cp && cp <= 0xdfff) {
507545 sz = -1;
507546 // invalid block of utf
507547 return false;
507548 } else if (cp <= 0xFFFF) {
507549 sz = 3;
507550 c[0] = (cp >> 12) + 224;
507551 c[1] = ((cp >> 6) & 63) + 128;
507552 c[2] = (cp & 63) + 128;
507553 } else if (cp <= 0x10FFFF) {
507554 sz = 4;
507555 c[0] = (cp >> 18) + 240;
507556 c[1] = ((cp >> 12) & 63) + 128;
507557 c[2] = ((cp >> 6) & 63) + 128;
507558 c[3] = (cp & 63) + 128;
507559 } else {
507560 sz = -1;
507561 return false;
507562 }
507563 return true;

◆ CodepointLength()

int duckdb::Utf8Proc::CodepointLength ( int cp)
static

Returns the codepoint length in bytes when encoded in UTF8.

507566 {
507567 if (cp <= 0x7F) {
507568 return 1;
507569 }
507570 if (cp <= 0x7FF) {
507571 return 2;
507572 }
507573 if (0xd800 <= cp && cp <= 0xdfff) {
507574 throw InternalException("invalid code point detected in Utf8Proc::CodepointLength (0xd800 to 0xdfff), likely due to invalid UTF-8");
507575 }
507576 if (cp <= 0xFFFF) {
507577 return 3;
507578 }
507579 if (cp <= 0x10FFFF) {
507580 return 4;
507581 }
507582 throw InternalException("invalid code point detected in Utf8Proc::CodepointLength, likely due to invalid UTF-8");

◆ UTF8ToCodepoint()

int32_t duckdb::Utf8Proc::UTF8ToCodepoint ( const char * c,
int & sz )
static

Transform a UTF8 string to a codepoint; returns the codepoint and writes the length of the codepoint (in UTF8) to sz.

507585 {
507586 // from http://www.zedwood.com/article/cpp-utf8-char-to-codepoint
507587 auto u = reinterpret_cast<const unsigned char *>(u_input);
507588 unsigned char u0 = u[0];
507589 if (u0 <= 127) {
507590 sz = 1;
507591 return u0;
507592 }
507593 unsigned char u1 = u[1];
507594 if (u0 >= 192 && u0 <= 223) {
507595 sz = 2;
507596 return (u0 - 192) * 64 + (u1 - 128);
507597 }
507598 if (u[0] == 0xed && (u[1] & 0xa0) == 0xa0) {
507599 throw InternalException("invalid code point detected in Utf8Proc::UTF8ToCodepoint (0xd800 to 0xdfff), likely due to invalid UTF-8");
507600 }
507601 unsigned char u2 = u[2];
507602 if (u0 >= 224 && u0 <= 239) {
507603 sz = 3;
507604 return (u0 - 224) * 4096 + (u1 - 128) * 64 + (u2 - 128);
507605 }
507606 unsigned char u3 = u[3];
507607 if (u0 >= 240 && u0 <= 247) {
507608 sz = 4;
507609 return (u0 - 240) * 262144 + (u1 - 128) * 4096 + (u2 - 128) * 64 + (u3 - 128);
507610 }
507611 throw InternalException("invalid code point detected in Utf8Proc::UTF8ToCodepoint, likely due to invalid UTF-8");
Here is the caller graph for this function:

◆ RenderWidth() [1/2]

size_t duckdb::Utf8Proc::RenderWidth ( const char * s,
size_t len,
size_t pos )
static

Returns the render width of a single character in a string.

507614 {
507615 int sz;
507616 auto codepoint = Utf8Proc::UTF8ToCodepoint(s + pos, sz);
507617 auto properties = duckdb::utf8proc_get_property(codepoint);
507618 return properties->charwidth;
UTF8PROC_DLLEXPORT const utf8proc_property_t * utf8proc_get_property(utf8proc_int32_t codepoint)
Definition duckdb.cpp:506631
Here is the call graph for this function:

◆ RenderWidth() [2/2]

size_t duckdb::Utf8Proc::RenderWidth ( const std::string & str)
static
507621 {
507622 size_t render_width = 0;
507623 for (auto cluster : Utf8Proc::GraphemeClusters(str.c_str(), str.size())) {
507624 // use the width of the first codepoint in the grapheme cluster
507625 // combining marks, ZWJ, variation selectors, etc. have charwidth 0
507626 // and multi-codepoint clusters (e.g. ZWJ emoji sequences) should only
507627 // count the base character's width, not the sum of all codepoints
507628 render_width += Utf8Proc::RenderWidth(str.c_str(), str.size(), cluster.start);
507629 }
507630 return render_width;
static size_t RenderWidth(const char *s, size_t len, size_t pos)
Returns the render width of a single character in a string.
Definition duckdb.cpp:507613
static GraphemeIterator GraphemeClusters(const char *s, size_t len)
Constructs a class that can be iterated over to fetch grapheme clusters in a string.
Definition duckdb.cpp:507466

◆ CodepointToUpper()

int32_t duckdb::Utf8Proc::CodepointToUpper ( int32_t codepoint)
static
507456 {
507457 return utf8proc_toupper(codepoint);
UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_toupper(utf8proc_int32_t c)
Definition duckdb.cpp:506794

◆ CodepointToLower()

int32_t duckdb::Utf8Proc::CodepointToLower ( int32_t codepoint)
static
507460 {
507461 return utf8proc_tolower(codepoint);
UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_tolower(utf8proc_int32_t c)
Definition duckdb.cpp:506788

◆ GraphemeClusters()

GraphemeIterator duckdb::Utf8Proc::GraphemeClusters ( const char * s,
size_t len )
static

Constructs a class that can be iterated over to fetch grapheme clusters in a string.

507467 {
507468 return GraphemeIterator(s, len);
Here is the caller graph for this function:

◆ GraphemeCount()

size_t duckdb::Utf8Proc::GraphemeCount ( const char * s,
size_t len )
static

Returns the number of grapheme clusters in a string.

507447 {
507448 size_t num_characters = 0;
507449 for (auto cluster : Utf8Proc::GraphemeClusters(input_data, input_size)) {
507450 (void)cluster;
507451 num_characters++;
507452 }
507453 return num_characters;
Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following file: