From 6a77dbc6f68d62d76f7d08c1bde7e5724985cb96 Mon Sep 17 00:00:00 2001 From: yaito3014 Date: Tue, 17 Feb 2026 11:01:19 +0900 Subject: [PATCH 01/28] Refactor --- CMakeLists.txt | 67 +++---- README.md | 318 ++++++++++++++++---------------- source/utf8.h | 11 +- source/utf8/checked.h | 85 ++++----- source/utf8/core.h | 103 ++++++----- source/utf8/cpp11.h | 9 +- source/utf8/cpp17.h | 9 +- source/utf8/cpp20.h | 9 +- source/utf8/unchecked.h | 77 ++++---- tests/CMakeLists.txt | 16 +- tests/negative.cpp | 2 +- tests/test_checked_api.h | 10 +- tests/test_checked_iterator.h | 16 +- tests/test_cpp11.cpp | 4 +- tests/test_cpp17.cpp | 2 +- tests/test_cpp20.cpp | 4 +- tests/test_unchecked_api.h | 76 ++++---- tests/test_unchecked_iterator.h | 16 +- utf8cppConfig.cmake.in | 8 - 19 files changed, 408 insertions(+), 434 deletions(-) delete mode 100644 utf8cppConfig.cmake.in diff --git a/CMakeLists.txt b/CMakeLists.txt index b161449..345467a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,56 +1,31 @@ -cmake_minimum_required (VERSION 3.14...3.27) -project (utf8cpp - VERSION 4.0.9 - LANGUAGES CXX - DESCRIPTION "C++ portable library for working with utf-8 encoding") +cmake_minimum_required(VERSION 3.24) -add_library(${PROJECT_NAME} INTERFACE) - -include(GNUInstallDirs) - -target_include_directories(utf8cpp INTERFACE - "$" - $ -) - -include(CMakePackageConfigHelpers) -write_basic_package_version_file( - "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" - VERSION ${PROJECT_VERSION} - COMPATIBILITY SameMajorVersion - ARCH_INDEPENDENT +project( + iris_utf8cpp + VERSION 5.0.0 + LANGUAGES CXX + DESCRIPTION "C++ portable library for working with utf-8 encoding" ) -install(TARGETS ${PROJECT_NAME} - EXPORT ${PROJECT_NAME}Targets - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} - RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} - PUBLIC_HEADER DESTINATION include COMPONENT Development - BUNDLE DESTINATION bin COMPONENT Runtime -) +add_library(${PROJECT_NAME} INTERFACE) -configure_package_config_file( - "${PROJECT_SOURCE_DIR}/${PROJECT_NAME}Config.cmake.in" - "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" - INSTALL_DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake +target_sources( + ${PROJECT_NAME} + INTERFACE + FILE_SET HEADERS + TYPE HEADERS + BASE_DIRS source + FILES + source/utf8/core.hpp + source/utf8/cpp11.hpp + source/utf8/cpp17.hpp + source/utf8/cpp20.hpp + source/utf8/checked.hpp + source/utf8.hpp ) -install(EXPORT ${PROJECT_NAME}Targets - FILE ${PROJECT_NAME}Targets.cmake - NAMESPACE ${PROJECT_NAME}:: - DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake) - -install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" - "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" - DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake) - -install(FILES ${PROJECT_SOURCE_DIR}/source/utf8.h DESTINATION include/utf8cpp) -install(DIRECTORY ${PROJECT_SOURCE_DIR}/source/utf8 DESTINATION - include/utf8cpp) +option(UTF8CPP_ENABLE_TESTS "Build and enable tests" ${PROJECT_IS_TOP_LEVEL}) -option(UTF8CPP_ENABLE_TESTS "Build and enable tests" OFF) if(UTF8CPP_ENABLE_TESTS) - enable_testing() add_subdirectory(tests) endif() diff --git a/README.md b/README.md index 37f3de7..2a2516d 100644 --- a/README.md +++ b/README.md @@ -24,86 +24,86 @@ I came up with a small, C++98 compatible generic library in order to handle UTF- - [Alternatives](#alternatives) * [Reference](#reference) + [Functions From utf8 Namespace](#functions-from-utf8-namespace) - - [utf8::append](#utf8append) + - [utflib::append](#utf8append) * [octet_iterator append(utfchar32_t cp, octet_iterator result)](#octet_iterator-appendutfchar32_t-cp-octet_iterator-result) * [void append(utfchar32_t cp, std::string& s);](#void-appendutfchar32_t-cp-stdstring-s) - - [utf8::append16](#utf8append16) + - [utflib::append16](#utf8append16) * [word_iterator append16(utfchar32_t cp, word_iterator result)](#word_iterator-append16utfchar32_t-cp-word_iterator-result) * [void append(utfchar32_t cp, std::u16string& s)](#void-appendutfchar32_t-cp-stdu16string-s) - - [utf8::next](#utf8next) - - [utf8::next16](#utf8next16) - - [utf8::peek_next](#utf8peek_next) - - [utf8::prior](#utf8prior) - - [utf8::advance](#utf8advance) - - [utf8::distance](#utf8distance) - - [utf8::utf16to8](#utf8utf16to8) + - [utflib::next](#utf8next) + - [utflib::next16](#utf8next16) + - [utflib::peek_next](#utf8peek_next) + - [utflib::prior](#utf8prior) + - [utflib::advance](#utf8advance) + - [utflib::distance](#utf8distance) + - [utflib::utf16to8](#utf8utf16to8) * [octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result)](#octet_iterator-utf16to8-u16bit_iterator-start-u16bit_iterator-end-octet_iterator-result) * [std::string utf16to8(const std::u16string& s)](#stdstring-utf16to8const-stdu16string-s) * [std::string utf16to8(std::u16string_view s)](#stdstring-utf16to8stdu16string_view-s) - - [utf8::utf16tou8](#utf8utf16tou8) + - [utflib::utf16tou8](#utf8utf16tou8) * [std::u8string utf16tou8(const std::u16string& s)](#stdu8string-utf16tou8const-stdu16string-s) * [std::u8string utf16tou8(const std::u16string_view& s)](#stdu8string-utf16tou8const-stdu16string_view-s) - - [utf8::utf8to16](#utf8utf8to16) + - [utflib::utf8to16](#utf8utf8to16) * [u16bit_iterator utf8to16 (octet_iterator start, octet_iterator end, u16bit_iterator result)](#u16bit_iterator-utf8to16-octet_iterator-start-octet_iterator-end-u16bit_iterator-result) * [std::u16string utf8to16(const std::string& s)](#stdu16string-utf8to16const-stdstring-s) * [std::u16string utf8to16(std::string_view s)](#stdu16string-utf8to16stdstring_view-s) * [std::u16string utf8to16(std::u8string& s)](#stdu16string-utf8to16stdu8string-s) * [std::u16string utf8to16(std::u8string_view& s)](#stdu16string-utf8to16stdu8string_view-s) - - [utf8::utf32to8](#utf8utf32to8) + - [utflib::utf32to8](#utf8utf32to8) * [octet_iterator utf32to8 (u32bit_iterator start, u32bit_iterator end, octet_iterator result)](#octet_iterator-utf32to8-u32bit_iterator-start-u32bit_iterator-end-octet_iterator-result) * [std::string utf32to8(const std::u32string& s)](#stdstring-utf32to8const-stdu32string-s) * [std::u8string utf32to8(const std::u32string& s)](#stdu8string-utf32to8const-stdu32string-s) * [std::u8string utf32to8(const std::u32string_view& s)](#stdu8string-utf32to8const-stdu32string_view-s) * [std::string utf32to8(const std::u32string& s)](#stdstring-utf32to8const-stdu32string-s-1) * [std::string utf32to8(std::u32string_view s)](#stdstring-utf32to8stdu32string_view-s) - - [utf8::utf8to32](#utf8utf8to32) + - [utflib::utf8to32](#utf8utf8to32) * [u32bit_iterator utf8to32 (octet_iterator start, octet_iterator end, u32bit_iterator result)](#u32bit_iterator-utf8to32-octet_iterator-start-octet_iterator-end-u32bit_iterator-result) * [std::u32string utf8to32(const std::u8string& s)](#stdu32string-utf8to32const-stdu8string-s) * [std::u32string utf8to32(const std::u8string_view& s)](#stdu32string-utf8to32const-stdu8string_view-s) * [std::u32string utf8to32(const std::string& s)](#stdu32string-utf8to32const-stdstring-s) * [std::u32string utf8to32(std::string_view s)](#stdu32string-utf8to32stdstring_view-s) - - [utf8::find_invalid](#utf8find_invalid) + - [utflib::find_invalid](#utf8find_invalid) * [octet_iterator find_invalid(octet_iterator start, octet_iterator end)](#octet_iterator-find_invalidoctet_iterator-start-octet_iterator-end) * [const char* find_invalid(const char* str)](#const-char-find_invalidconst-char-str) * [std::size_t find_invalid(const std::string& s)](#stdsize_t-find_invalidconst-stdstring-s) * [std::size_t find_invalid(std::string_view s)](#stdsize_t-find_invalidstdstring_view-s) - - [utf8::is_valid](#utf8is_valid) + - [utflib::is_valid](#utf8is_valid) * [bool is_valid(octet_iterator start, octet_iterator end)](#bool-is_validoctet_iterator-start-octet_iterator-end) * [bool is_valid(const char* str)](#bool-is_validconst-char-str) * [bool is_valid(const std::string& s)](#bool-is_validconst-stdstring-s) * [bool is_valid(std::string_view s)](#bool-is_validstdstring_view-s) - - [utf8::replace_invalid](#utf8replace_invalid) + - [utflib::replace_invalid](#utf8replace_invalid) * [output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out, utfchar32_t replacement)](#output_iterator-replace_invalidoctet_iterator-start-octet_iterator-end-output_iterator-out-utfchar32_t-replacement) * [std::string replace_invalid(const std::string& s, utfchar32_t replacement)](#stdstring-replace_invalidconst-stdstring-s-utfchar32_t-replacement) * [std::string replace_invalid(std::string_view s, char32_t replacement)](#stdstring-replace_invalidstdstring_view-s-char32_t-replacement) - - [utf8::starts_with_bom](#utf8starts_with_bom) + - [utflib::starts_with_bom](#utf8starts_with_bom) * [bool starts_with_bom (octet_iterator it, octet_iterator end)](#bool-starts_with_bom-octet_iterator-it-octet_iterator-end) * [bool starts_with_bom(const std::string& s)](#bool-starts_with_bomconst-stdstring-s) * [bool starts_with_bom(std::string_view s)](#bool-starts_with_bomstdstring_view-s) + [Types From utf8 Namespace](#types-from-utf8-namespace) - - [utf8::exception](#utf8exception) - - [utf8::invalid_code_point](#utf8invalid_code_point) - - [utf8::invalid_utf8](#utf8invalid_utf8) - - [utf8::invalid_utf16](#utf8invalid_utf16) - - [utf8::not_enough_room](#utf8not_enough_room) - - [utf8::iterator](#utf8iterator) + - [utflib::exception](#utf8exception) + - [utflib::invalid_code_point](#utf8invalid_code_point) + - [utflib::invalid_utf8](#utf8invalid_utf8) + - [utflib::invalid_utf16](#utf8invalid_utf16) + - [utflib::not_enough_room](#utf8not_enough_room) + - [utflib::iterator](#utf8iterator) * [Member functions](#member-functions) - + [Functions From utf8::unchecked Namespace](#functions-from-utf8unchecked-namespace) - - [utf8::unchecked::append](#utf8uncheckedappend) - - [utf8::unchecked::append16](#utf8uncheckedappend16) - - [utf8::unchecked::next](#utf8uncheckednext) - - [utf8::next16](#utf8next16-1) - - [utf8::unchecked::peek_next](#utf8uncheckedpeek_next) - - [utf8::unchecked::prior](#utf8uncheckedprior) - - [utf8::unchecked::advance](#utf8uncheckedadvance) - - [utf8::unchecked::distance](#utf8uncheckeddistance) - - [utf8::unchecked::utf16to8](#utf8uncheckedutf16to8) - - [utf8::unchecked::utf8to16](#utf8uncheckedutf8to16) - - [utf8::unchecked::utf32to8](#utf8uncheckedutf32to8) - - [utf8::unchecked::utf8to32](#utf8uncheckedutf8to32) - - [utf8::unchecked::replace_invalid](#utf8uncheckedreplace_invalid) - + [Types From utf8::unchecked Namespace](#types-from-utf8unchecked-namespace) - - [utf8::iterator](#utf8iterator-1) + + [Functions From utflib::unchecked Namespace](#functions-from-utf8unchecked-namespace) + - [utflib::unchecked::append](#utf8uncheckedappend) + - [utflib::unchecked::append16](#utf8uncheckedappend16) + - [utflib::unchecked::next](#utf8uncheckednext) + - [utflib::next16](#utf8next16-1) + - [utflib::unchecked::peek_next](#utf8uncheckedpeek_next) + - [utflib::unchecked::prior](#utf8uncheckedprior) + - [utflib::unchecked::advance](#utf8uncheckedadvance) + - [utflib::unchecked::distance](#utf8uncheckeddistance) + - [utflib::unchecked::utf16to8](#utf8uncheckedutf16to8) + - [utflib::unchecked::utf8to16](#utf8uncheckedutf8to16) + - [utflib::unchecked::utf32to8](#utf8uncheckedutf32to8) + - [utflib::unchecked::utf8to32](#utf8uncheckedutf8to32) + - [utflib::unchecked::replace_invalid](#utf8uncheckedreplace_invalid) + + [Types From utflib::unchecked Namespace](#types-from-utf8unchecked-namespace) + - [utflib::iterator](#utf8iterator-1) * [Member functions](#member-functions-1) @@ -153,33 +153,33 @@ int main(int argc, char** argv) string line; // Play with all the lines in the file while (getline(fs8, line)) { - // check for invalid utf-8 (for a simple yes/no check, there is also utf8::is_valid function) + // check for invalid utf-8 (for a simple yes/no check, there is also utflib::is_valid function) #if __cplusplus >= 201103L // C++ 11 or later - auto end_it = utf8::find_invalid(line.begin(), line.end()); + auto end_it = utflib::find_invalid(line.begin(), line.end()); #else - string::iterator end_it = utf8::find_invalid(line.begin(), line.end()); + string::iterator end_it = utflib::find_invalid(line.begin(), line.end()); #endif // C++ 11 if (end_it != line.end()) { cout << "Invalid UTF-8 encoding detected at line " << line_count << "\n"; cout << "This part is fine: " << string(line.begin(), end_it) << "\n"; } // Get the line length (at least for the valid part) - int length = utf8::distance(line.begin(), end_it); + int length = utflib::distance(line.begin(), end_it); cout << "Length of line " << line_count << " is " << length << "\n"; // Convert it to utf-16 #if __cplusplus >= 201103L // C++ 11 or later - u16string utf16line = utf8::utf8to16(line); + u16string utf16line = utflib::utf8to16(line); #else vector utf16line; - utf8::utf8to16(line.begin(), end_it, back_inserter(utf16line)); + utflib::utf8to16(line.begin(), end_it, back_inserter(utf16line)); #endif // C++ 11 // And back to utf-8; #if __cplusplus >= 201103L // C++ 11 or later - string utf8line = utf8::utf16to8(utf16line); + string utf8line = utflib::utf16to8(utf16line); #else string utf8line; - utf8::utf16to8(utf16line.begin(), utf16line.end(), back_inserter(utf8line)); + utflib::utf16to8(utf16line.begin(), utf16line.end(), back_inserter(utf8line)); #endif // C++ 11 // Confirm that the conversion went OK: if (utf8line != string(line.begin(), end_it)) @@ -192,25 +192,25 @@ int main(int argc, char** argv) } ``` -In the previous code sample, for each line we performed a detection of invalid UTF-8 sequences with `find_invalid`; the number of characters (more precisely - the number of Unicode code points, including the end of line and even BOM if there is one) in each line was determined with a use of `utf8::distance`; finally, we have converted each line to UTF-16 encoding with `utf8to16` and back to UTF-8 with `utf16to8`. +In the previous code sample, for each line we performed a detection of invalid UTF-8 sequences with `find_invalid`; the number of characters (more precisely - the number of Unicode code points, including the end of line and even BOM if there is one) in each line was determined with a use of `utflib::distance`; finally, we have converted each line to UTF-16 encoding with `utf8to16` and back to UTF-8 with `utf16to8`. Note a different pattern of usage for old compilers. For instance, this is how we convert a UTF-8 encoded string to a UTF-16 encoded one with a pre - C++11 compiler: ```cpp vector utf16line; - utf8::utf8to16(line.begin(), end_it, back_inserter(utf16line)); + utflib::utf8to16(line.begin(), end_it, back_inserter(utf16line)); ``` With a more modern compiler, the same operation would look like: ```cpp - u16string utf16line = utf8::utf8to16(line); + u16string utf16line = utflib::utf8to16(line); ``` If `__cplusplus` macro points to a C++ 11 or later, the library exposes API that takes into account C++ standard Unicode strings and move semantics. With an older compiler, it is still possible to use the same functionality, just in a little less convenient way In case you do not trust the `__cplusplus` macro or, for instance, do not want to include -the C++ 11 helper functions even with a modern compiler, define `UTF_CPP_CPLUSPLUS` macro +the C++ 11 helper functions even with a modern compiler, define `IRIS_UTFLIB_CPP_CPLUSPLUS` macro before including `utf8.h` and assign it a value for the standard you want to use - the values are the same as for the `__cplusplus` macro. This can be also useful with compilers that are conservative in setting the `__cplusplus` macro even if they have a good support for a recent standard edition - Microsoft's Visual C++ is one example. @@ -228,16 +228,16 @@ bool valid_utf8_file(const char* file_name) istreambuf_iterator it(ifs.rdbuf()); istreambuf_iterator eos; - return utf8::is_valid(it, eos); + return utflib::is_valid(it, eos); } ``` -Because the function `utf8::is_valid()` works with input iterators, we were able to pass an `istreambuf_iterator` to `it` and read the content of the file directly without loading it to the memory first. +Because the function `utflib::is_valid()` works with input iterators, we were able to pass an `istreambuf_iterator` to `it` and read the content of the file directly without loading it to the memory first. Note that other functions that take input iterator arguments can be used in a similar way. For instance, to read the content of a UTF-8 encoded text file and convert the text to UTF-16, just do something like: ```cpp - utf8::utf8to16(it, eos, back_inserter(u16string)); + utflib::utf8to16(it, eos, back_inserter(u16string)); ``` @@ -249,7 +249,7 @@ If we have some text that "probably" contains UTF-8 encoded text and we want to void fix_utf8_string(std::string& str) { std::string temp; - utf8::replace_invalid(str.begin(), str.end(), back_inserter(temp)); + utflib::replace_invalid(str.begin(), str.end(), back_inserter(temp)); str = temp; } ``` @@ -292,7 +292,7 @@ The article presents author's view of the quality of the API design, but also so ### Functions From utf8 Namespace -#### utf8::append +#### utflib::append ##### octet_iterator append(utfchar32_t cp, octet_iterator result) @@ -321,7 +321,7 @@ assert (u[0] == 0xd1 && u[1] == 0x88 && u[2] == 0 && u[3] == 0 && u[4] == 0); Note that `append` does not allocate any memory - it is the burden of the caller to make sure there is enough memory allocated for the operation. To make things more interesting, `append` can add anywhere between 1 and 4 octets to the sequence. In practice, you would most often want to use `std::back_inserter` to ensure that the necessary memory is allocated. -In case of an invalid code point, a `utf8::invalid_code_point` exception is thrown. +In case of an invalid code point, a `utflib::invalid_code_point` exception is thrown. @@ -346,10 +346,10 @@ append(0x0448, u); assert (u[0] == char(0xd1) && u[1] == char(0x88) && u.length() == 2); ``` -In case of an invalid code point, a `utf8::invalid_code_point` exception is thrown. +In case of an invalid code point, a `utflib::invalid_code_point` exception is thrown. -#### utf8::append16 +#### utflib::append16 ##### word_iterator append16(utfchar32_t cp, word_iterator result) @@ -377,7 +377,7 @@ assert (u[0] == 0x0448 && u[1] == 0); Note that `append16` does not allocate any memory - it is the burden of the caller to make sure there is enough memory allocated for the operation. To make things more interesting, `append16` can add either one or two words to the sequence. In practice, you would most often want to use `std::back_inserter` to ensure that the necessary memory is allocated. -In case of an invalid code point, a `utf8::invalid_code_point` exception is thrown. +In case of an invalid code point, a `utflib::invalid_code_point` exception is thrown. @@ -402,11 +402,11 @@ append(0x0448, u); assert (u[0] == 0x0448 && u.length() == 1); ``` -In case of an invalid code point, a `utf8::invalid_code_point` exception is thrown. +In case of an invalid code point, a `utflib::invalid_code_point` exception is thrown. -#### utf8::next +#### utflib::next Available in version 1.0 and later. @@ -419,7 +419,7 @@ utfchar32_t next(octet_iterator& it, octet_iterator end); `octet_iterator`: an input iterator. `it`: a reference to an iterator pointing to the beginning of an UTF-8 encoded code point. After the function returns, it is incremented to point to the beginning of the next code point. -`end`: end of the UTF-8 sequence to be processed. If `it` gets equal to `end` during the extraction of a code point, an `utf8::not_enough_room` exception is thrown. +`end`: end of the UTF-8 sequence to be processed. If `it` gets equal to `end` during the extraction of a code point, an `utflib::not_enough_room` exception is thrown. Return value: the 32 bit representation of the processed UTF-8 code point. Example of use: @@ -434,10 +434,10 @@ assert (w == twochars + 3); This function is typically used to iterate through a UTF-8 encoded string. -In case of an invalid UTF-8 sequence, a `utf8::invalid_utf8` exception is thrown. +In case of an invalid UTF-8 sequence, a `utflib::invalid_utf8` exception is thrown. -#### utf8::next16 +#### utflib::next16 Available in version 4.0 and later. @@ -450,7 +450,7 @@ utfchar32_t next16(word_iterator& it, word_iterator end); `word_iterator`: an input iterator. `it`: a reference to an iterator pointing to the beginning of an UTF-16 encoded code point. After the function returns, it is incremented to point to the beginning of the next code point. -`end`: end of the UTF-16 sequence to be processed. If `it` gets equal to `end` during the extraction of a code point, an `utf8::not_enough_room` exception is thrown. +`end`: end of the UTF-16 sequence to be processed. If `it` gets equal to `end` during the extraction of a code point, an `utflib::not_enough_room` exception is thrown. Return value: the 32 bit representation of the processed UTF-16 code point. Example of use: @@ -465,11 +465,11 @@ assert (w, u + 1); This function is typically used to iterate through a UTF-16 encoded string. -In case of an invalid UTF-16 sequence, a `utf8::invalid_utf8` exception is thrown. +In case of an invalid UTF-16 sequence, a `utflib::invalid_utf8` exception is thrown. -#### utf8::peek_next +#### utflib::peek_next Available in version 2.1 and later. @@ -483,7 +483,7 @@ utfchar32_t peek_next(octet_iterator it, octet_iterator end); `octet_iterator`: an input iterator. `it`: an iterator pointing to the beginning of an UTF-8 encoded code point. -`end`: end of the UTF-8 sequence to be processed. If `it` gets equal to `end` during the extraction of a code point, an `utf8::not_enough_room` exception is thrown. +`end`: end of the UTF-8 sequence to be processed. If `it` gets equal to `end` during the extraction of a code point, an `utflib::not_enough_room` exception is thrown. Return value: the 32 bit representation of the processed UTF-8 code point. Example of use: @@ -496,10 +496,10 @@ assert (cp == 0x65e5); assert (w == twochars); ``` -In case of an invalid UTF-8 sequence, a `utf8::invalid_utf8` exception is thrown. +In case of an invalid UTF-8 sequence, a `utflib::invalid_utf8` exception is thrown. -#### utf8::prior +#### utflib::prior Available in version 1.02 and later. @@ -525,7 +525,7 @@ assert (cp == 0x65e5); assert (w == twochars); ``` -This function has two purposes: one is two iterate backwards through a UTF-8 encoded string. Note that it is usually a better idea to iterate forward instead, since `utf8::next` is faster. The second purpose is to find a beginning of a UTF-8 sequence if we have a random position within a string. Note that in that case `utf8::prior` may not detect an invalid UTF-8 sequence in some scenarios: for instance if there are superfluous trail octets, it will just skip them. +This function has two purposes: one is two iterate backwards through a UTF-8 encoded string. Note that it is usually a better idea to iterate forward instead, since `utflib::next` is faster. The second purpose is to find a beginning of a UTF-8 sequence if we have a random position within a string. Note that in that case `utflib::prior` may not detect an invalid UTF-8 sequence in some scenarios: for instance if there are superfluous trail octets, it will just skip them. `it` will typically point to the beginning of a code point, and `start` will point to the beginning of the string to ensure we don't go backwards too far. `it` is decreased until it points to a lead UTF-8 octet, and then the UTF-8 sequence beginning with that octet is decoded to a 32 bit representation and returned. @@ -534,7 +534,7 @@ In case `start` is reached before a UTF-8 lead octet is hit, or if an invalid UT In case `start` equals `it`, a `not_enough_room` exception is thrown. -#### utf8::advance +#### utflib::advance Available in version 1.0 and later. Advances an iterator by the specified number of code points within an UTF-8 sequence. @@ -548,7 +548,7 @@ void advance (octet_iterator& it, distance_type n, octet_iterator end); `distance_type`: an integral type convertible to `octet_iterator`'s difference type. `it`: a reference to an iterator pointing to the beginning of an UTF-8 encoded code point. After the function returns, it is incremented to point to the nth following code point. `n`: number of code points `it` should be advanced. A negative value means decrement. -`end`: limit of the UTF-8 sequence to be processed. If `n` is positive and `it` gets equal to `end` during the extraction of a code point, an `utf8::not_enough_room` exception is thrown. If `n` is negative and `it` reaches `end` while `it` points t a trail byte of a UTF-8 sequence, a `utf8::invalid_code_point` exception is thrown. +`end`: limit of the UTF-8 sequence to be processed. If `n` is positive and `it` gets equal to `end` during the extraction of a code point, an `utflib::not_enough_room` exception is thrown. If `n` is negative and `it` reaches `end` while `it` points t a trail byte of a UTF-8 sequence, a `utflib::invalid_code_point` exception is thrown. Example of use: @@ -561,10 +561,10 @@ advance (w, -2, twochars); assert (w == twochars); ``` -In case of an invalid code point, a `utf8::invalid_code_point` exception is thrown. +In case of an invalid code point, a `utflib::invalid_code_point` exception is thrown. -#### utf8::distance +#### utflib::distance Available in version 1.0 and later. @@ -584,16 +584,16 @@ Example of use: ```cpp char* twochars = "\xe6\x97\xa5\xd1\x88"; -size_t dist = utf8::distance(twochars, twochars + 5); +size_t dist = utflib::distance(twochars, twochars + 5); assert (dist == 2); ``` This function is used to find the length (in code points) of a UTF-8 encoded string. The reason it is called _distance_, rather than, say, _length_ is mainly because developers are used that _length_ is an O(1) function. Computing the length of an UTF-8 string is a linear operation, and it looked better to model it after `std::distance` algorithm. -In case of an invalid UTF-8 sequence, a `utf8::invalid_utf8` exception is thrown. If `last` does not point to the past-of-end of a UTF-8 sequence, a `utf8::not_enough_room` exception is thrown. +In case of an invalid UTF-8 sequence, a `utflib::invalid_utf8` exception is thrown. If `last` does not point to the past-of-end of a UTF-8 sequence, a `utflib::not_enough_room` exception is thrown. -#### utf8::utf16to8 +#### utflib::utf16to8 ##### octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result) @@ -622,7 +622,7 @@ utf16to8(utf16string, utf16string + 5, back_inserter(utf8result)); assert (utf8result.size() == 10); ``` -In case of invalid UTF-16 sequence, a `utf8::invalid_utf16` exception is thrown. +In case of invalid UTF-16 sequence, a `utflib::invalid_utf16` exception is thrown. @@ -647,7 +647,7 @@ Example of use: assert (u.size() == 10); ``` -In case of invalid UTF-16 sequence, a `utf8::invalid_utf16` exception is thrown. +In case of invalid UTF-16 sequence, a `utflib::invalid_utf16` exception is thrown. ##### std::string utf16to8(std::u16string_view s) @@ -672,10 +672,10 @@ Example of use: assert (u.size() == 10); ``` -In case of invalid UTF-16 sequence, a `utf8::invalid_utf16` exception is thrown. +In case of invalid UTF-16 sequence, a `utflib::invalid_utf16` exception is thrown. -#### utf8::utf16tou8 +#### utflib::utf16tou8 ##### std::u8string utf16tou8(const std::u16string& s) @@ -698,7 +698,7 @@ Example of use: assert (u.size() == 10); ``` -In case of invalid UTF-16 sequence, a `utf8::invalid_utf16` exception is thrown. +In case of invalid UTF-16 sequence, a `utflib::invalid_utf16` exception is thrown. ##### std::u8string utf16tou8(const std::u16string_view& s) @@ -723,10 +723,10 @@ Example of use: assert (u.size() == 10); ``` -In case of invalid UTF-16 sequence, a `utf8::invalid_utf16` exception is thrown. +In case of invalid UTF-16 sequence, a `utflib::invalid_utf16` exception is thrown. -#### utf8::utf8to16 +#### utflib::utf8to16 ##### u16bit_iterator utf8to16 (octet_iterator start, octet_iterator end, u16bit_iterator result) @@ -757,7 +757,7 @@ assert (utf16result[2] == 0xd834); assert (utf16result[3] == 0xdd1e); ``` -In case of an invalid UTF-8 sequence, a `utf8::invalid_utf8` exception is thrown. If `end` does not point to the past-of-end of a UTF-8 sequence, a `utf8::not_enough_room` exception is thrown. +In case of an invalid UTF-8 sequence, a `utflib::invalid_utf8` exception is thrown. If `end` does not point to the past-of-end of a UTF-8 sequence, a `utflib::not_enough_room` exception is thrown. @@ -785,7 +785,7 @@ assert (utf16result[2] == 0xd834); assert (utf16result[3] == 0xdd1e); ``` -In case of an invalid UTF-8 sequence, a `utf8::invalid_utf8` exception is thrown. +In case of an invalid UTF-8 sequence, a `utflib::invalid_utf8` exception is thrown. @@ -812,7 +812,7 @@ assert (utf16result[2] == 0xd834); assert (utf16result[3] == 0xdd1e); ``` -In case of an invalid UTF-8 sequence, a `utf8::invalid_utf8` exception is thrown. +In case of an invalid UTF-8 sequence, a `utflib::invalid_utf8` exception is thrown. @@ -839,7 +839,7 @@ assert (utf16result[2] == 0xd834); assert (utf16result[3] == 0xdd1e); ``` -In case of an invalid UTF-8 sequence, a `utf8::invalid_utf8` exception is thrown. +In case of an invalid UTF-8 sequence, a `utflib::invalid_utf8` exception is thrown. @@ -867,10 +867,10 @@ assert (utf16result[2] == 0xd834); assert (utf16result[3] == 0xdd1e); ``` -In case of an invalid UTF-8 sequence, a `utf8::invalid_utf8` exception is thrown. +In case of an invalid UTF-8 sequence, a `utflib::invalid_utf8` exception is thrown. -#### utf8::utf32to8 +#### utflib::utf32to8 ##### octet_iterator utf32to8 (u32bit_iterator start, u32bit_iterator end, octet_iterator result) @@ -899,7 +899,7 @@ utf32to8(utf32string, utf32string + 3, back_inserter(utf8result)); assert (utf8result.size() == 9); ``` -In case of invalid UTF-32 string, a `utf8::invalid_code_point` exception is thrown. +In case of invalid UTF-32 string, a `utflib::invalid_code_point` exception is thrown. @@ -924,7 +924,7 @@ string utf8result = utf32to8(utf32string); assert (utf8result.size() == 9); ``` -In case of invalid UTF-32 string, a `utf8::invalid_code_point` exception is thrown. +In case of invalid UTF-32 string, a `utflib::invalid_code_point` exception is thrown. ##### std::u8string utf32to8(const std::u32string& s) @@ -948,7 +948,7 @@ u8string utf8result = utf32to8(utf32string); assert (utf8result.size() == 9); ``` -In case of invalid UTF-32 string, a `utf8::invalid_code_point` exception is thrown. +In case of invalid UTF-32 string, a `utflib::invalid_code_point` exception is thrown. @@ -974,7 +974,7 @@ u8string utf8result = utf32to8(utf32stringview); assert (utf8result.size() == 9); ``` -In case of invalid UTF-32 string, a `utf8::invalid_code_point` exception is thrown. +In case of invalid UTF-32 string, a `utflib::invalid_code_point` exception is thrown. @@ -999,7 +999,7 @@ string utf8result = utf32to8(utf32string); assert (utf8result.size() == 9); ``` -In case of invalid UTF-32 string, a `utf8::invalid_code_point` exception is thrown. +In case of invalid UTF-32 string, a `utflib::invalid_code_point` exception is thrown. ##### std::string utf32to8(std::u32string_view s) @@ -1024,11 +1024,11 @@ string utf8result = utf32to8(utf32stringview); assert (utf8result.size() == 9); ``` -In case of invalid UTF-32 string, a `utf8::invalid_code_point` exception is thrown. +In case of invalid UTF-32 string, a `utflib::invalid_code_point` exception is thrown. -#### utf8::utf8to32 +#### utflib::utf8to32 ##### u32bit_iterator utf8to32 (octet_iterator start, octet_iterator end, u32bit_iterator result) @@ -1057,7 +1057,7 @@ utf8to32(twochars, twochars + 5, back_inserter(utf32result)); assert (utf32result.size() == 2); ``` -In case of an invalid UTF-8 sequence, a `utf8::invalid_utf8` exception is thrown. If `end` does not point to the past-of-end of a UTF-8 sequence, a `utf8::not_enough_room` exception is thrown. +In case of an invalid UTF-8 sequence, a `utflib::invalid_utf8` exception is thrown. If `end` does not point to the past-of-end of a UTF-8 sequence, a `utflib::not_enough_room` exception is thrown. @@ -1083,7 +1083,7 @@ u32string utf32result = utf8to32(twochars); assert (utf32result.size() == 2); ``` -In case of an invalid UTF-8 sequence, a `utf8::invalid_utf8` exception is thrown. +In case of an invalid UTF-8 sequence, a `utflib::invalid_utf8` exception is thrown. @@ -1109,7 +1109,7 @@ u32string utf32result = utf8to32(stringview); assert (utf32result.size() == 2); ``` -In case of an invalid UTF-8 sequence, a `utf8::invalid_utf8` exception is thrown. +In case of an invalid UTF-8 sequence, a `utflib::invalid_utf8` exception is thrown. @@ -1134,7 +1134,7 @@ u32string utf32result = utf8to32(twochars); assert (utf32result.size() == 2); ``` -In case of an invalid UTF-8 sequence, a `utf8::invalid_utf8` exception is thrown. +In case of an invalid UTF-8 sequence, a `utflib::invalid_utf8` exception is thrown. ##### std::u32string utf8to32(std::string_view s) @@ -1158,10 +1158,10 @@ u32string utf32result = utf8to32(twochars); assert (utf32result.size() == 2); ``` -In case of an invalid UTF-8 sequence, a `utf8::invalid_utf8` exception is thrown. +In case of an invalid UTF-8 sequence, a `utflib::invalid_utf8` exception is thrown. -#### utf8::find_invalid +#### utflib::find_invalid ##### octet_iterator find_invalid(octet_iterator start, octet_iterator end) @@ -1263,7 +1263,7 @@ assert (invalid == 5); This function is typically used to make sure a UTF-8 string is valid before processing it with other functions. It is especially important to call it if before doing any of the _unchecked_ operations on it. -#### utf8::is_valid +#### utflib::is_valid ##### bool is_valid(octet_iterator start, octet_iterator end) @@ -1366,7 +1366,7 @@ assert (bvalid == false); You may want to use `is_valid` to make sure that a string contains valid UTF-8 text without the need to know where it fails if it is not valid. -#### utf8::replace_invalid +#### utflib::replace_invalid ##### output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out, utfchar32_t replacement) @@ -1459,7 +1459,7 @@ assert(fixed_invalid_sequence, replace_invalid_result); ``` -#### utf8::starts_with_bom +#### utflib::starts_with_bom ##### bool starts_with_bom (octet_iterator it, octet_iterator end) @@ -1549,7 +1549,7 @@ The typical use of this function is to check the first three bytes of a file. If ### Types From utf8 Namespace -#### utf8::exception +#### utflib::exception Available in version 2.3 and later. @@ -1565,13 +1565,13 @@ Example of use: try { code_that_uses_utf_cpp_library(); } -catch(const utf8::exception& utfcpp_ex) { +catch(const utflib::exception& utfcpp_ex) { cerr << utfcpp_ex.what(); } ``` -#### utf8::invalid_code_point +#### utflib::invalid_code_point Available in version 1.0 and later. @@ -1587,7 +1587,7 @@ public: Member function `code_point()` can be used to determine the invalid code point that caused the exception to be thrown. -#### utf8::invalid_utf8 +#### utflib::invalid_utf8 Available in version 1.0 and later. @@ -1603,7 +1603,7 @@ public: Member function `utf8_octet()` can be used to determine the beginning of the byte sequence that caused the exception to be thrown. -#### utf8::invalid_utf16 +#### utflib::invalid_utf16 Available in version 1.0 and later. @@ -1619,7 +1619,7 @@ public: Member function `utf16_word()` can be used to determine the UTF-16 code unit that caused the exception to be thrown. -#### utf8::not_enough_room +#### utflib::not_enough_room Available in version 1.0 and later. @@ -1630,7 +1630,7 @@ class not_enough_room : public exception {}; ``` -#### utf8::iterator +#### utflib::iterator Available in version 2.0 and later. @@ -1668,37 +1668,37 @@ Example of use: ```cpp char* threechars = "\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88"; -utf8::iterator it(threechars, threechars, threechars + 9); -utf8::iterator it2 = it; +utflib::iterator it(threechars, threechars, threechars + 9); +utflib::iterator it2 = it; assert (it2 == it); assert (*it == 0x10346); assert (*(++it) == 0x65e5); assert ((*it++) == 0x65e5); assert (*it == 0x0448); assert (it != it2); -utf8::iterator endit (threechars + 9, threechars, threechars + 9); +utflib::iterator endit (threechars + 9, threechars, threechars + 9); assert (++it == endit); assert (*(--it) == 0x0448); assert ((*it--) == 0x0448); assert (*it == 0x65e5); -assert (--it == utf8::iterator(threechars, threechars, threechars + 9)); +assert (--it == utflib::iterator(threechars, threechars, threechars + 9)); assert (*it == 0x10346); ``` -The purpose of `utf8::iterator` adapter is to enable easy iteration as well as the use of STL algorithms with UTF-8 encoded strings. Increment and decrement operators are implemented in terms of `utf8::next()` and `utf8::prior()` functions. +The purpose of `utflib::iterator` adapter is to enable easy iteration as well as the use of STL algorithms with UTF-8 encoded strings. Increment and decrement operators are implemented in terms of `utflib::next()` and `utflib::prior()` functions. -Note that `utf8::iterator` adapter is a checked iterator. It operates on the range specified in the constructor; any attempt to go out of that range will result in an exception. Even the comparison operators require both iterator object to be constructed against the same range - otherwise an exception is thrown. Typically, the range will be determined by sequence container functions `begin` and `end`, i.e.: +Note that `utflib::iterator` adapter is a checked iterator. It operates on the range specified in the constructor; any attempt to go out of that range will result in an exception. Even the comparison operators require both iterator object to be constructed against the same range - otherwise an exception is thrown. Typically, the range will be determined by sequence container functions `begin` and `end`, i.e.: ```cpp std::string s = "example"; -utf8::iterator i (s.begin(), s.begin(), s.end()); +utflib::iterator i (s.begin(), s.begin(), s.end()); ``` -### Functions From utf8::unchecked Namespace +### Functions From utflib::unchecked Namespace -#### utf8::unchecked::append +#### utflib::unchecked::append Available in version 1.0 and later. @@ -1721,10 +1721,10 @@ unsigned char* end = unchecked::append(0x0448, u); assert (u[0] == 0xd1 && u[1] == 0x88 && u[2] == 0 && u[3] == 0 && u[4] == 0); ``` -This is a faster but less safe version of `utf8::append`. It does not check for validity of the supplied code point, and may produce an invalid UTF-8 sequence. +This is a faster but less safe version of `utflib::append`. It does not check for validity of the supplied code point, and may produce an invalid UTF-8 sequence. -#### utf8::unchecked::append16 +#### utflib::unchecked::append16 Available in version 4.0 and later. @@ -1743,16 +1743,16 @@ Example of use: ```cpp unsigned short u[5] = {0,0}; -utf8::unchecked::append16(0x0448, u); +utflib::unchecked::append16(0x0448, u); assert(u[0], 0x0448); assert(u[1], 0x0000); ``` -This is a faster but less safe version of `utf8::append`. It does not check for validity of the supplied code point, and may produce an invalid UTF-8 sequence. +This is a faster but less safe version of `utflib::append`. It does not check for validity of the supplied code point, and may produce an invalid UTF-8 sequence. -#### utf8::unchecked::next +#### utflib::unchecked::next Available in version 1.0 and later. @@ -1776,10 +1776,10 @@ assert (cp == 0x65e5); assert (w == twochars + 3); ``` -This is a faster but less safe version of `utf8::next`. It does not check for validity of the supplied UTF-8 sequence. +This is a faster but less safe version of `utflib::next`. It does not check for validity of the supplied UTF-8 sequence. -#### utf8::next16 +#### utflib::next16 Available in version 4.0 and later. @@ -1807,11 +1807,11 @@ assert (w, u + 1); This function is typically used to iterate through a UTF-16 encoded string. -This is a faster but less safe version of `utf8::next16`. It does not check for validity of the supplied UTF-8 sequence. +This is a faster but less safe version of `utflib::next16`. It does not check for validity of the supplied UTF-8 sequence. -#### utf8::unchecked::peek_next +#### utflib::unchecked::peek_next Available in version 2.1 and later. @@ -1835,10 +1835,10 @@ assert (cp == 0x65e5); assert (w == twochars); ``` -This is a faster but less safe version of `utf8::peek_next`. It does not check for validity of the supplied UTF-8 sequence. +This is a faster but less safe version of `utflib::peek_next`. It does not check for validity of the supplied UTF-8 sequence. -#### utf8::unchecked::prior +#### utflib::unchecked::prior Available in version 1.02 and later. @@ -1862,10 +1862,10 @@ assert (cp == 0x65e5); assert (w == twochars); ``` -This is a faster but less safe version of `utf8::prior`. It does not check for validity of the supplied UTF-8 sequence and offers no boundary checking. +This is a faster but less safe version of `utflib::prior`. It does not check for validity of the supplied UTF-8 sequence and offers no boundary checking. -#### utf8::unchecked::advance +#### utflib::unchecked::advance Available in version 1.0 and later. @@ -1888,10 +1888,10 @@ unchecked::advance (w, 2); assert (w == twochars + 5); ``` -This is a faster but less safe version of `utf8::advance`. It does not check for validity of the supplied UTF-8 sequence and offers no boundary checking. +This is a faster but less safe version of `utflib::advance`. It does not check for validity of the supplied UTF-8 sequence and offers no boundary checking. -#### utf8::unchecked::distance +#### utflib::unchecked::distance Available in version 1.0 and later. @@ -1910,14 +1910,14 @@ Example of use: ```cpp char* twochars = "\xe6\x97\xa5\xd1\x88"; -size_t dist = utf8::unchecked::distance(twochars, twochars + 5); +size_t dist = utflib::unchecked::distance(twochars, twochars + 5); assert (dist == 2); ``` -This is a faster but less safe version of `utf8::distance`. It does not check for validity of the supplied UTF-8 sequence. +This is a faster but less safe version of `utflib::distance`. It does not check for validity of the supplied UTF-8 sequence. -#### utf8::unchecked::utf16to8 +#### utflib::unchecked::utf16to8 Available in version 1.0 and later. @@ -1942,10 +1942,10 @@ unchecked::utf16to8(utf16string, utf16string + 5, back_inserter(utf8result)); assert (utf8result.size() == 10); ``` -This is a faster but less safe version of `utf8::utf16to8`. It does not check for validity of the supplied UTF-16 sequence. +This is a faster but less safe version of `utflib::utf16to8`. It does not check for validity of the supplied UTF-16 sequence. -#### utf8::unchecked::utf8to16 +#### utflib::unchecked::utf8to16 Available in version 1.0 and later. @@ -1972,10 +1972,10 @@ assert (utf16result[2] == 0xd834); assert (utf16result[3] == 0xdd1e); ``` -This is a faster but less safe version of `utf8::utf8to16`. It does not check for validity of the supplied UTF-8 sequence. +This is a faster but less safe version of `utflib::utf8to16`. It does not check for validity of the supplied UTF-8 sequence. -#### utf8::unchecked::utf32to8 +#### utflib::unchecked::utf32to8 Available in version 1.0 and later. @@ -2000,10 +2000,10 @@ utf32to8(utf32string, utf32string + 3, back_inserter(utf8result)); assert (utf8result.size() == 9); ``` -This is a faster but less safe version of `utf8::utf32to8`. It does not check for validity of the supplied UTF-32 sequence. +This is a faster but less safe version of `utflib::utf32to8`. It does not check for validity of the supplied UTF-32 sequence. -#### utf8::unchecked::utf8to32 +#### utflib::unchecked::utf8to32 Available in version 1.0 and later. @@ -2028,10 +2028,10 @@ unchecked::utf8to32(twochars, twochars + 5, back_inserter(utf32result)); assert (utf32result.size() == 2); ``` -This is a faster but less safe version of `utf8::utf8to32`. It does not check for validity of the supplied UTF-8 sequence. +This is a faster but less safe version of `utflib::utf8to32`. It does not check for validity of the supplied UTF-8 sequence. -#### utf8::unchecked::replace_invalid +#### utflib::unchecked::replace_invalid Available in version 3.1 and later. @@ -2058,7 +2058,7 @@ Example of use: char invalid_sequence[] = "a\x80\xe0\xa0\xc0\xaf\xed\xa0\x80z"; vector replace_invalid_result; unchecked::replace_invalid (invalid_sequence, invalid_sequence + sizeof(invalid_sequence), back_inserter(replace_invalid_result), '?'); -bvalid = utf8::is_valid(replace_invalid_result.begin(), replace_invalid_result.end()); +bvalid = utflib::is_valid(replace_invalid_result.begin(), replace_invalid_result.end()); assert (bvalid); char* fixed_invalid_sequence = "a????z"; assert (std::equal(replace_invalid_result.begin(), replace_invalid_result.end(), fixed_invalid_sequence)); @@ -2066,13 +2066,13 @@ assert (std::equal(replace_invalid_result.begin(), replace_invalid_result.end(), `replace_invalid` does not perform in-place replacement of invalid sequences. Rather, it produces a copy of the original string with the invalid sequences replaced with a replacement marker. Therefore, `out` must not be in the `[start, end]` range. -Unlike `utf8::replace_invalid`, this function does not verify validity of the replacement marker. +Unlike `utflib::replace_invalid`, this function does not verify validity of the replacement marker. -### Types From utf8::unchecked Namespace +### Types From utflib::unchecked Namespace -#### utf8::iterator +#### utflib::iterator Available in version 2.0 and later. @@ -2110,22 +2110,22 @@ Example of use: ```cpp char* threechars = "\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88"; -utf8::unchecked::iterator un_it(threechars); -utf8::unchecked::iterator un_it2 = un_it; +utflib::unchecked::iterator un_it(threechars); +utflib::unchecked::iterator un_it2 = un_it; assert (un_it2 == un_it); assert (*un_it == 0x10346); assert (*(++un_it) == 0x65e5); assert ((*un_it++) == 0x65e5); assert (*un_it == 0x0448); assert (un_it != un_it2); -utf8::::unchecked::iterator un_endit (threechars + 9); +utflib::::unchecked::iterator un_endit (threechars + 9); assert (++un_it == un_endit); assert (*(--un_it) == 0x0448); assert ((*un_it--) == 0x0448); assert (*un_it == 0x65e5); -assert (--un_it == utf8::unchecked::iterator(threechars)); +assert (--un_it == utflib::unchecked::iterator(threechars)); assert (*un_it == 0x10346); ``` -This is an unchecked version of `utf8::iterator`. It is faster in many cases, but offers no validity or range checks. +This is an unchecked version of `utflib::iterator`. It is faster in many cases, but offers no validity or range checks. diff --git a/source/utf8.h b/source/utf8.h index b513530..3ecc6da 100644 --- a/source/utf8.h +++ b/source/utf8.h @@ -1,4 +1,5 @@ // Copyright 2006 Nemanja Trifunovic +// Copyright 2026 The Iris Project Contributors /* Permission is hereby granted, free of charge, to any person or organization @@ -25,19 +26,19 @@ DEALINGS IN THE SOFTWARE. */ -#ifndef UTF8_FOR_CPP_2675DCD0_9480_4c0c_B92A_CC14C027B731 -#define UTF8_FOR_CPP_2675DCD0_9480_4c0c_B92A_CC14C027B731 +#ifndef IRIS_UTFLIB_UTF8_H +#define IRIS_UTFLIB_UTF8_H /* -To control the C++ language version used by the library, you can define UTF_CPP_CPLUSPLUS macro +To control the C++ language version used by the library, you can define IRIS_UTFLIB_CPP_CPLUSPLUS macro and set it to one of the values used by the __cplusplus predefined macro. For instance, - #define UTF_CPP_CPLUSPLUS 199711L + #define IRIS_UTFLIB_CPP_CPLUSPLUS 199711L will cause the UTF-8 CPP library to use only types and language features available in the C++ 98 standard. Some library features will be disabled. -If you leave UTF_CPP_CPLUSPLUS undefined, it will be internally assigned to __cplusplus. +If you leave IRIS_UTFLIB_CPP_CPLUSPLUS undefined, it will be internally assigned to __cplusplus. */ #include "utf8/checked.h" diff --git a/source/utf8/checked.h b/source/utf8/checked.h index 96ceb4d..616b8e6 100644 --- a/source/utf8/checked.h +++ b/source/utf8/checked.h @@ -1,4 +1,5 @@ // Copyright 2006-2016 Nemanja Trifunovic +// Copyright 2026 The Iris Project Contributors /* Permission is hereby granted, free of charge, to any person or organization @@ -25,13 +26,13 @@ DEALINGS IN THE SOFTWARE. */ -#ifndef UTF8_FOR_CPP_CHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731 -#define UTF8_FOR_CPP_CHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731 +#ifndef IRIS_UTFLIB_UTF8_CHECKED_H +#define IRIS_UTFLIB_UTF8_CHECKED_H #include "core.h" #include -namespace utf8 +namespace iris::utflib { // Base for the exceptions that may be thrown from the library class exception : public ::std::exception { @@ -42,7 +43,7 @@ namespace utf8 utfchar32_t cp; public: invalid_code_point(utfchar32_t codepoint) : cp(codepoint) {} - virtual const char* what() const UTF_CPP_NOEXCEPT UTF_CPP_OVERRIDE { return "Invalid code point"; } + virtual const char* what() const IRIS_UTFLIB_CPP_NOEXCEPT IRIS_UTFLIB_CPP_OVERRIDE { return "Invalid code point"; } utfchar32_t code_point() const {return cp;} }; @@ -51,7 +52,7 @@ namespace utf8 public: invalid_utf8 (utfchar8_t u) : u8(u) {} invalid_utf8 (char c) : u8(static_cast(c)) {} - virtual const char* what() const UTF_CPP_NOEXCEPT UTF_CPP_OVERRIDE { return "Invalid UTF-8"; } + virtual const char* what() const IRIS_UTFLIB_CPP_NOEXCEPT IRIS_UTFLIB_CPP_OVERRIDE { return "Invalid UTF-8"; } utfchar8_t utf8_octet() const {return u8;} }; @@ -59,13 +60,13 @@ namespace utf8 utfchar16_t u16; public: invalid_utf16 (utfchar16_t u) : u16(u) {} - virtual const char* what() const UTF_CPP_NOEXCEPT UTF_CPP_OVERRIDE { return "Invalid UTF-16"; } + virtual const char* what() const IRIS_UTFLIB_CPP_NOEXCEPT IRIS_UTFLIB_CPP_OVERRIDE { return "Invalid UTF-16"; } utfchar16_t utf16_word() const {return u16;} }; class not_enough_room : public exception { public: - virtual const char* what() const UTF_CPP_NOEXCEPT UTF_CPP_OVERRIDE { return "Not enough space"; } + virtual const char* what() const IRIS_UTFLIB_CPP_NOEXCEPT IRIS_UTFLIB_CPP_OVERRIDE { return "Not enough space"; } }; /// The library API - functions intended to be called by the users @@ -73,7 +74,7 @@ namespace utf8 template octet_iterator append(utfchar32_t cp, octet_iterator result) { - if (!utf8::internal::is_code_point_valid(cp)) + if (!utflib::internal::is_code_point_valid(cp)) throw invalid_code_point(cp); return internal::append(cp, result); @@ -87,7 +88,7 @@ namespace utf8 template word_iterator append16(utfchar32_t cp, word_iterator result) { - if (!utf8::internal::is_code_point_valid(cp)) + if (!utflib::internal::is_code_point_valid(cp)) throw invalid_code_point(cp); return internal::append16(cp, result); @@ -98,27 +99,27 @@ namespace utf8 { while (start != end) { octet_iterator sequence_start = start; - internal::utf_error err_code = utf8::internal::validate_next(start, end); + internal::utf_error err_code = utflib::internal::validate_next(start, end); switch (err_code) { case internal::UTF8_OK : for (octet_iterator it = sequence_start; it != start; ++it) *out++ = *it; break; case internal::NOT_ENOUGH_ROOM: - out = utf8::append (replacement, out); + out = utflib::append (replacement, out); start = end; break; case internal::INVALID_LEAD: - out = utf8::append (replacement, out); + out = utflib::append (replacement, out); ++start; break; case internal::INCOMPLETE_SEQUENCE: case internal::OVERLONG_SEQUENCE: case internal::INVALID_CODE_POINT: - out = utf8::append (replacement, out); + out = utflib::append (replacement, out); ++start; // just one replacement mark for the sequence - while (start != end && utf8::internal::is_trail(*start)) + while (start != end && utflib::internal::is_trail(*start)) ++start; break; } @@ -129,8 +130,8 @@ namespace utf8 template inline output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out) { - static const utfchar32_t replacement_marker = static_cast(utf8::internal::mask16(0xfffd)); - return utf8::replace_invalid(start, end, out, replacement_marker); + static const utfchar32_t replacement_marker = static_cast(utflib::internal::mask16(0xfffd)); + return utflib::replace_invalid(start, end, out, replacement_marker); } inline std::string replace_invalid(const std::string& s, utfchar32_t replacement) @@ -151,7 +152,7 @@ namespace utf8 utfchar32_t next(octet_iterator& it, octet_iterator end) { utfchar32_t cp = 0; - internal::utf_error err_code = utf8::internal::validate_next(it, end, cp); + internal::utf_error err_code = utflib::internal::validate_next(it, end, cp); switch (err_code) { case internal::UTF8_OK : break; @@ -171,7 +172,7 @@ namespace utf8 utfchar32_t next16(word_iterator& it, word_iterator end) { utfchar32_t cp = 0; - internal::utf_error err_code = utf8::internal::validate_next16(it, end, cp); + internal::utf_error err_code = utflib::internal::validate_next16(it, end, cp); if (err_code == internal::NOT_ENOUGH_ROOM) throw not_enough_room(); return cp; @@ -180,7 +181,7 @@ namespace utf8 template utfchar32_t peek_next(octet_iterator it, octet_iterator end) { - return utf8::next(it, end); + return utflib::next(it, end); } template @@ -192,10 +193,10 @@ namespace utf8 octet_iterator end = it; // Go back until we hit either a lead octet or start - while (utf8::internal::is_trail(*(--it))) + while (utflib::internal::is_trail(*(--it))) if (it == start) throw invalid_utf8(*it); // error - no lead byte in the sequence - return utf8::peek_next(it, end); + return utflib::peek_next(it, end); } template @@ -205,11 +206,11 @@ namespace utf8 if (n < zero) { // backward for (distance_type i = n; i < zero; ++i) - utf8::prior(it, end); + utflib::prior(it, end); } else { // forward for (distance_type i = zero; i < n; ++i) - utf8::next(it, end); + utflib::next(it, end); } } @@ -219,7 +220,7 @@ namespace utf8 { typename std::iterator_traits::difference_type dist; for (dist = 0; first < last; ++dist) - utf8::next(first, last); + utflib::next(first, last); return dist; } @@ -227,12 +228,12 @@ namespace utf8 octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result) { while (start != end) { - utfchar32_t cp = static_cast(utf8::internal::mask16(*start++)); + utfchar32_t cp = static_cast(utflib::internal::mask16(*start++)); // Take care of surrogate pairs first - if (utf8::internal::is_lead_surrogate(cp)) { + if (utflib::internal::is_lead_surrogate(cp)) { if (start != end) { - const utfchar32_t trail_surrogate = static_cast(utf8::internal::mask16(*start++)); - if (utf8::internal::is_trail_surrogate(trail_surrogate)) + const utfchar32_t trail_surrogate = static_cast(utflib::internal::mask16(*start++)); + if (utflib::internal::is_trail_surrogate(trail_surrogate)) cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET; else throw invalid_utf16(static_cast(trail_surrogate)); @@ -242,10 +243,10 @@ namespace utf8 } // Lone trail surrogate - else if (utf8::internal::is_trail_surrogate(cp)) + else if (utflib::internal::is_trail_surrogate(cp)) throw invalid_utf16(static_cast(cp)); - result = utf8::append(cp, result); + result = utflib::append(cp, result); } return result; } @@ -254,7 +255,7 @@ namespace utf8 u16bit_iterator utf8to16 (octet_iterator start, octet_iterator end, u16bit_iterator result) { while (start < end) { - const utfchar32_t cp = utf8::next(start, end); + const utfchar32_t cp = utflib::next(start, end); if (cp > 0xffff) { //make a surrogate pair *result++ = static_cast((cp >> 10) + internal::LEAD_OFFSET); *result++ = static_cast((cp & 0x3ff) + internal::TRAIL_SURROGATE_MIN); @@ -269,7 +270,7 @@ namespace utf8 octet_iterator utf32to8 (u32bit_iterator start, u32bit_iterator end, octet_iterator result) { while (start != end) - result = utf8::append(*(start++), result); + result = utflib::append(*(start++), result); return result; } @@ -278,7 +279,7 @@ namespace utf8 u32bit_iterator utf8to32 (octet_iterator start, octet_iterator end, u32bit_iterator result) { while (start < end) - (*result++) = utf8::next(start, end); + (*result++) = utflib::next(start, end); return result; } @@ -309,7 +310,7 @@ namespace utf8 utfchar32_t operator * () const { octet_iterator temp = it; - return utf8::next(temp, range_end); + return utflib::next(temp, range_end); } bool operator == (const iterator& rhs) const { @@ -323,35 +324,35 @@ namespace utf8 } iterator& operator ++ () { - utf8::next(it, range_end); + utflib::next(it, range_end); return *this; } iterator operator ++ (int) { iterator temp = *this; - utf8::next(it, range_end); + utflib::next(it, range_end); return temp; } iterator& operator -- () { - utf8::prior(it, range_start); + utflib::prior(it, range_start); return *this; } iterator operator -- (int) { iterator temp = *this; - utf8::prior(it, range_start); + utflib::prior(it, range_start); return temp; } }; // class iterator -} // namespace utf8 +} // namespace iris::utflib -#if UTF_CPP_CPLUSPLUS >= 202002L // C++ 20 or later +#if IRIS_UTFLIB_CPP_CPLUSPLUS >= 202002L // C++ 20 or later #include "cpp20.h" -#elif UTF_CPP_CPLUSPLUS >= 201703L // C++ 17 or later +#elif IRIS_UTFLIB_CPP_CPLUSPLUS >= 201703L // C++ 17 or later #include "cpp17.h" -#elif UTF_CPP_CPLUSPLUS >= 201103L // C++ 11 or later +#elif IRIS_UTFLIB_CPP_CPLUSPLUS >= 201103L // C++ 11 or later #include "cpp11.h" #endif // C++ 11 or later diff --git a/source/utf8/core.h b/source/utf8/core.h index 8e128c1..f75b57c 100644 --- a/source/utf8/core.h +++ b/source/utf8/core.h @@ -1,4 +1,5 @@ // Copyright 2006 Nemanja Trifunovic +// Copyright 2026 The Iris Project Contributors /* Permission is hereby granted, free of charge, to any person or organization @@ -25,38 +26,38 @@ DEALINGS IN THE SOFTWARE. */ -#ifndef UTF8_FOR_CPP_CORE_H_2675DCD0_9480_4c0c_B92A_CC14C027B731 -#define UTF8_FOR_CPP_CORE_H_2675DCD0_9480_4c0c_B92A_CC14C027B731 +#ifndef IRIS_UTFLIB_UTF8_CORE_H +#define IRIS_UTFLIB_UTF8_CORE_H #include #include #include // Determine the C++ standard version. -// If the user defines UTF_CPP_CPLUSPLUS, use that. +// If the user defines IRIS_UTFLIB_CPP_CPLUSPLUS, use that. // Otherwise, trust the unreliable predefined macro __cplusplus -#if !defined UTF_CPP_CPLUSPLUS - #define UTF_CPP_CPLUSPLUS __cplusplus +#if !defined IRIS_UTFLIB_CPP_CPLUSPLUS + #define IRIS_UTFLIB_CPP_CPLUSPLUS __cplusplus #endif -#if UTF_CPP_CPLUSPLUS >= 201103L // C++ 11 or later - #define UTF_CPP_OVERRIDE override - #define UTF_CPP_NOEXCEPT noexcept - #define UTF_CPP_STATIC_ASSERT(condition) static_assert(condition, "UTFCPP static assert"); +#if IRIS_UTFLIB_CPP_CPLUSPLUS >= 201103L // C++ 11 or later + #define IRIS_UTFLIB_CPP_OVERRIDE override + #define IRIS_UTFLIB_CPP_NOEXCEPT noexcept + #define IRIS_UTFLIB_CPP_STATIC_ASSERT(condition) static_assert(condition, "UTFCPP static assert"); #else // C++ 98/03 - #define UTF_CPP_OVERRIDE - #define UTF_CPP_NOEXCEPT throw() + #define IRIS_UTFLIB_CPP_OVERRIDE + #define IRIS_UTFLIB_CPP_NOEXCEPT throw() // Not worth simulating static_assert: - #define UTF_CPP_STATIC_ASSERT(condition) (void)(condition); + #define IRIS_UTFLIB_CPP_STATIC_ASSERT(condition) (void)(condition); #endif // C++ 11 or later -namespace utf8 +namespace iris::utflib { // The typedefs for 8-bit, 16-bit and 32-bit code units -#if UTF_CPP_CPLUSPLUS >= 201103L // C++ 11 or later - #if UTF_CPP_CPLUSPLUS >= 202002L // C++ 20 or later +#if IRIS_UTFLIB_CPP_CPLUSPLUS >= 201103L // C++ 11 or later + #if IRIS_UTFLIB_CPP_CPLUSPLUS >= 202002L // C++ 20 or later typedef char8_t utfchar8_t; #else // C++ 11/14/17 typedef unsigned char utfchar8_t; @@ -100,7 +101,7 @@ namespace internal template inline bool is_trail(octet_type oc) { - return ((utf8::internal::mask8(oc) >> 6) == 0x2); + return ((utflib::internal::mask8(oc) >> 6) == 0x2); } inline bool is_lead_surrogate(utfchar32_t cp) @@ -120,7 +121,7 @@ namespace internal inline bool is_code_point_valid(utfchar32_t cp) { - return (cp <= CODE_POINT_MAX && !utf8::internal::is_surrogate(cp)); + return (cp <= CODE_POINT_MAX && !utflib::internal::is_surrogate(cp)); } inline bool is_in_bmp(utfchar32_t cp) @@ -131,7 +132,7 @@ namespace internal template int sequence_length(octet_iterator lead_it) { - const utfchar8_t lead = utf8::internal::mask8(*lead_it); + const utfchar8_t lead = utflib::internal::mask8(*lead_it); if (lead < 0x80) return 1; else if ((lead >> 5) == 0x6) @@ -170,13 +171,13 @@ namespace internal if (++it == end) return NOT_ENOUGH_ROOM; - if (!utf8::internal::is_trail(*it)) + if (!utflib::internal::is_trail(*it)) return INCOMPLETE_SEQUENCE; return UTF8_OK; } - #define UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(IT, END) {utf_error ret = increase_safely(IT, END); if (ret != UTF8_OK) return ret;} + #define IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(IT, END) {utf_error ret = increase_safely(IT, END); if (ret != UTF8_OK) return ret;} /// get_sequence_x functions decode utf-8 sequences of the length x template @@ -185,7 +186,7 @@ namespace internal if (it == end) return NOT_ENOUGH_ROOM; - code_point = static_cast(utf8::internal::mask8(*it)); + code_point = static_cast(utflib::internal::mask8(*it)); return UTF8_OK; } @@ -196,9 +197,9 @@ namespace internal if (it == end) return NOT_ENOUGH_ROOM; - code_point = static_cast(utf8::internal::mask8(*it)); + code_point = static_cast(utflib::internal::mask8(*it)); - UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end) + IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end) code_point = ((code_point << 6) & 0x7ff) + ((*it) & 0x3f); @@ -211,13 +212,13 @@ namespace internal if (it == end) return NOT_ENOUGH_ROOM; - code_point = static_cast(utf8::internal::mask8(*it)); + code_point = static_cast(utflib::internal::mask8(*it)); - UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end) + IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end) - code_point = ((code_point << 12) & 0xffff) + ((utf8::internal::mask8(*it) << 6) & 0xfff); + code_point = ((code_point << 12) & 0xffff) + ((utflib::internal::mask8(*it) << 6) & 0xfff); - UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end) + IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end) code_point = static_cast(code_point + ((*it) & 0x3f)); @@ -230,24 +231,24 @@ namespace internal if (it == end) return NOT_ENOUGH_ROOM; - code_point = static_cast(utf8::internal::mask8(*it)); + code_point = static_cast(utflib::internal::mask8(*it)); - UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end) + IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end) - code_point = ((code_point << 18) & 0x1fffff) + ((utf8::internal::mask8(*it) << 12) & 0x3ffff); + code_point = ((code_point << 18) & 0x1fffff) + ((utflib::internal::mask8(*it) << 12) & 0x3ffff); - UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end) + IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end) - code_point = static_cast(code_point + ((utf8::internal::mask8(*it) << 6) & 0xfff)); + code_point = static_cast(code_point + ((utflib::internal::mask8(*it) << 6) & 0xfff)); - UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end) + IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end) code_point = static_cast(code_point + ((*it) & 0x3f)); return UTF8_OK; } - #undef UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR + #undef IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR template utf_error validate_next(octet_iterator& it, octet_iterator end, utfchar32_t& code_point) @@ -261,7 +262,7 @@ namespace internal utfchar32_t cp = 0; // Determine the sequence length based on the lead octet - const int length = utf8::internal::sequence_length(it); + const int length = utflib::internal::sequence_length(it); // Get trail octets and calculate the code point utf_error err = UTF8_OK; @@ -269,23 +270,23 @@ namespace internal case 0: return INVALID_LEAD; case 1: - err = utf8::internal::get_sequence_1(it, end, cp); + err = utflib::internal::get_sequence_1(it, end, cp); break; case 2: - err = utf8::internal::get_sequence_2(it, end, cp); + err = utflib::internal::get_sequence_2(it, end, cp); break; case 3: - err = utf8::internal::get_sequence_3(it, end, cp); + err = utflib::internal::get_sequence_3(it, end, cp); break; case 4: - err = utf8::internal::get_sequence_4(it, end, cp); + err = utflib::internal::get_sequence_4(it, end, cp); break; } if (err == UTF8_OK) { // Decoding succeeded. Now, security checks... - if (utf8::internal::is_code_point_valid(cp)) { - if (!utf8::internal::is_overlong_sequence(cp, length)){ + if (utflib::internal::is_code_point_valid(cp)) { + if (!utflib::internal::is_overlong_sequence(cp, length)){ // Passed! Return here. code_point = cp; ++it; @@ -306,7 +307,7 @@ namespace internal template inline utf_error validate_next(octet_iterator& it, octet_iterator end) { utfchar32_t ignored; - return utf8::internal::validate_next(it, end, ignored); + return utflib::internal::validate_next(it, end, ignored); } template @@ -314,7 +315,7 @@ namespace internal { // Make sure the iterator dereferences a large enough type typedef typename std::iterator_traits::value_type word_type; - UTF_CPP_STATIC_ASSERT(sizeof(word_type) >= sizeof(utfchar16_t)); + IRIS_UTFLIB_CPP_STATIC_ASSERT(sizeof(word_type) >= sizeof(utfchar16_t)); // Check the edge case: if (it == end) return NOT_ENOUGH_ROOM; @@ -403,7 +404,7 @@ namespace internal // the word_type. template word_iterator append16(utfchar32_t cp, word_iterator result) { - UTF_CPP_STATIC_ASSERT(sizeof(word_type) >= sizeof(utfchar16_t)); + IRIS_UTFLIB_CPP_STATIC_ASSERT(sizeof(word_type) >= sizeof(utfchar16_t)); if (is_in_bmp(cp)) *(result++) = static_cast(cp); else { @@ -443,7 +444,7 @@ namespace internal { octet_iterator result = start; while (result != end) { - utf8::internal::utf_error err_code = utf8::internal::validate_next(result, end); + utflib::internal::utf_error err_code = utflib::internal::validate_next(result, end); if (err_code != internal::UTF8_OK) return result; } @@ -465,12 +466,12 @@ namespace internal template inline bool is_valid(octet_iterator start, octet_iterator end) { - return (utf8::find_invalid(start, end) == end); + return (utflib::find_invalid(start, end) == end); } inline bool is_valid(const char* str) { - return (*(utf8::find_invalid(str)) == '\0'); + return (*(utflib::find_invalid(str)) == '\0'); } inline bool is_valid(const std::string& s) @@ -484,9 +485,9 @@ namespace internal inline bool starts_with_bom (octet_iterator it, octet_iterator end) { return ( - ((it != end) && (utf8::internal::mask8(*it++)) == bom[0]) && - ((it != end) && (utf8::internal::mask8(*it++)) == bom[1]) && - ((it != end) && (utf8::internal::mask8(*it)) == bom[2]) + ((it != end) && (utflib::internal::mask8(*it++)) == bom[0]) && + ((it != end) && (utflib::internal::mask8(*it++)) == bom[1]) && + ((it != end) && (utflib::internal::mask8(*it)) == bom[2]) ); } @@ -494,7 +495,7 @@ namespace internal { return starts_with_bom(s.begin(), s.end()); } -} // namespace utf8 +} // namespace iris::utflib #endif // header guard diff --git a/source/utf8/cpp11.h b/source/utf8/cpp11.h index 691633c..538d818 100644 --- a/source/utf8/cpp11.h +++ b/source/utf8/cpp11.h @@ -1,4 +1,5 @@ // Copyright 2018 Nemanja Trifunovic +// Copyright 2026 The Iris Project Contributors /* Permission is hereby granted, free of charge, to any person or organization @@ -25,12 +26,12 @@ DEALINGS IN THE SOFTWARE. */ -#ifndef UTF8_FOR_CPP_a184c22c_d012_11e8_a8d5_f2801f1b9fd1 -#define UTF8_FOR_CPP_a184c22c_d012_11e8_a8d5_f2801f1b9fd1 +#ifndef IRIS_UTFLIB_UTF8_CPP11_H +#define IRIS_UTFLIB_UTF8_CPP11_H #include "checked.h" -namespace utf8 +namespace iris::utflib { inline void append16(utfchar32_t cp, std::u16string& s) { @@ -64,7 +65,7 @@ namespace utf8 utf8to32(s.begin(), s.end(), std::back_inserter(result)); return result; } -} // namespace utf8 +} // namespace iris::utflib #endif // header guard diff --git a/source/utf8/cpp17.h b/source/utf8/cpp17.h index 0758730..f357d02 100644 --- a/source/utf8/cpp17.h +++ b/source/utf8/cpp17.h @@ -1,4 +1,5 @@ // Copyright 2018 Nemanja Trifunovic +// Copyright 2026 The Iris Project Contributors /* Permission is hereby granted, free of charge, to any person or organization @@ -25,12 +26,12 @@ DEALINGS IN THE SOFTWARE. */ -#ifndef UTF8_FOR_CPP_7e906c01_03a3_4daf_b420_ea7ea952b3c9 -#define UTF8_FOR_CPP_7e906c01_03a3_4daf_b420_ea7ea952b3c9 +#ifndef IRIS_UTFLIB_UTF8_CPP17_H +#define IRIS_UTFLIB_UTF8_CPP17_H #include "cpp11.h" -namespace utf8 +namespace iris::utflib { inline std::string utf16to8(std::u16string_view s) { @@ -90,7 +91,7 @@ namespace utf8 return starts_with_bom(s.begin(), s.end()); } -} // namespace utf8 +} // namespace iris::utflib #endif // header guard diff --git a/source/utf8/cpp20.h b/source/utf8/cpp20.h index 07b61d0..c45f46a 100644 --- a/source/utf8/cpp20.h +++ b/source/utf8/cpp20.h @@ -1,4 +1,5 @@ // Copyright 2022 Nemanja Trifunovic +// Copyright 2026 The Iris Project Contributors /* Permission is hereby granted, free of charge, to any person or organization @@ -25,12 +26,12 @@ DEALINGS IN THE SOFTWARE. */ -#ifndef UTF8_FOR_CPP_207e906c01_03a3_4daf_b420_ea7ea952b3c9 -#define UTF8_FOR_CPP_207e906c01_03a3_4daf_b420_ea7ea952b3c9 +#ifndef IRIS_UTFLIB_UTF8_CPP20_H +#define IRIS_UTFLIB_UTF8_CPP20_H #include "cpp17.h" -namespace utf8 +namespace iris::utflib { inline std::u8string utf16tou8(const std::u16string& s) { @@ -118,7 +119,7 @@ namespace utf8 return starts_with_bom(s.begin(), s.end()); } -} // namespace utf8 +} // namespace iris::utflib #endif // header guard diff --git a/source/utf8/unchecked.h b/source/utf8/unchecked.h index 173d030..1955acb 100644 --- a/source/utf8/unchecked.h +++ b/source/utf8/unchecked.h @@ -1,4 +1,5 @@ // Copyright 2006 Nemanja Trifunovic +// Copyright 2026 The Iris Project Contributors /* Permission is hereby granted, free of charge, to any person or organization @@ -25,12 +26,12 @@ DEALINGS IN THE SOFTWARE. */ -#ifndef UTF8_FOR_CPP_UNCHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731 -#define UTF8_FOR_CPP_UNCHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731 +#ifndef IRIS_UTFLIB_UTF8_UNCHECKED_H +#define IRIS_UTFLIB_UTF8_UNCHECKED_H #include "core.h" -namespace utf8 +namespace iris::utflib { namespace unchecked { @@ -51,27 +52,27 @@ namespace utf8 { while (start != end) { octet_iterator sequence_start = start; - internal::utf_error err_code = utf8::internal::validate_next(start, end); + internal::utf_error err_code = utflib::internal::validate_next(start, end); switch (err_code) { case internal::UTF8_OK : for (octet_iterator it = sequence_start; it != start; ++it) *out++ = *it; break; case internal::NOT_ENOUGH_ROOM: - out = utf8::unchecked::append(replacement, out); + out = utflib::unchecked::append(replacement, out); start = end; break; case internal::INVALID_LEAD: - out = utf8::unchecked::append(replacement, out); + out = utflib::unchecked::append(replacement, out); ++start; break; case internal::INCOMPLETE_SEQUENCE: case internal::OVERLONG_SEQUENCE: case internal::INVALID_CODE_POINT: - out = utf8::unchecked::append(replacement, out); + out = utflib::unchecked::append(replacement, out); ++start; // just one replacement mark for the sequence - while (start != end && utf8::internal::is_trail(*start)) + while (start != end && utflib::internal::is_trail(*start)) ++start; break; } @@ -82,8 +83,8 @@ namespace utf8 template inline output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out) { - static const utfchar32_t replacement_marker = static_cast(utf8::internal::mask16(0xfffd)); - return utf8::unchecked::replace_invalid(start, end, out, replacement_marker); + static const utfchar32_t replacement_marker = static_cast(utflib::internal::mask16(0xfffd)); + return utflib::unchecked::replace_invalid(start, end, out, replacement_marker); } inline std::string replace_invalid(const std::string& s, utfchar32_t replacement) @@ -103,8 +104,8 @@ namespace utf8 template utfchar32_t next(octet_iterator& it) { - utfchar32_t cp = utf8::internal::mask8(*it); - switch (utf8::internal::sequence_length(it)) { + utfchar32_t cp = utflib::internal::mask8(*it); + switch (utflib::internal::sequence_length(it)) { case 1: break; case 2: @@ -113,15 +114,15 @@ namespace utf8 break; case 3: ++it; - cp = ((cp << 12) & 0xffff) + ((utf8::internal::mask8(*it) << 6) & 0xfff); + cp = ((cp << 12) & 0xffff) + ((utflib::internal::mask8(*it) << 6) & 0xfff); ++it; cp = static_cast(cp + ((*it) & 0x3f)); break; case 4: ++it; - cp = ((cp << 18) & 0x1fffff) + ((utf8::internal::mask8(*it) << 12) & 0x3ffff); + cp = ((cp << 18) & 0x1fffff) + ((utflib::internal::mask8(*it) << 12) & 0x3ffff); ++it; - cp = static_cast(cp + ((utf8::internal::mask8(*it) << 6) & 0xfff)); + cp = static_cast(cp + ((utflib::internal::mask8(*it) << 6) & 0xfff)); ++it; cp = static_cast(cp + ((*it) & 0x3f)); break; @@ -133,24 +134,24 @@ namespace utf8 template utfchar32_t peek_next(octet_iterator it) { - return utf8::unchecked::next(it); + return utflib::unchecked::next(it); } template utfchar32_t next16(word_iterator& it) { - utfchar32_t cp = utf8::internal::mask16(*it++); - if (utf8::internal::is_lead_surrogate(cp)) - return (cp << 10) + *it++ + utf8::internal::SURROGATE_OFFSET; + utfchar32_t cp = utflib::internal::mask16(*it++); + if (utflib::internal::is_lead_surrogate(cp)) + return (cp << 10) + *it++ + utflib::internal::SURROGATE_OFFSET; return cp; } template utfchar32_t prior(octet_iterator& it) { - while (utf8::internal::is_trail(*(--it))) ; + while (utflib::internal::is_trail(*(--it))) ; octet_iterator temp = it; - return utf8::unchecked::next(temp); + return utflib::unchecked::next(temp); } template @@ -160,11 +161,11 @@ namespace utf8 if (n < zero) { // backward for (distance_type i = n; i < zero; ++i) - utf8::unchecked::prior(it); + utflib::unchecked::prior(it); } else { // forward for (distance_type i = zero; i < n; ++i) - utf8::unchecked::next(it); + utflib::unchecked::next(it); } } @@ -174,7 +175,7 @@ namespace utf8 { typename std::iterator_traits::difference_type dist; for (dist = 0; first < last; ++dist) - utf8::unchecked::next(first); + utflib::unchecked::next(first); return dist; } @@ -182,15 +183,15 @@ namespace utf8 octet_iterator utf16to8(u16bit_iterator start, u16bit_iterator end, octet_iterator result) { while (start != end) { - utfchar32_t cp = utf8::internal::mask16(*start++); + utfchar32_t cp = utflib::internal::mask16(*start++); // Take care of surrogate pairs first - if (utf8::internal::is_lead_surrogate(cp)) { + if (utflib::internal::is_lead_surrogate(cp)) { if (start == end) return result; - utfchar32_t trail_surrogate = utf8::internal::mask16(*start++); + utfchar32_t trail_surrogate = utflib::internal::mask16(*start++); cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET; } - result = utf8::unchecked::append(cp, result); + result = utflib::unchecked::append(cp, result); } return result; } @@ -199,7 +200,7 @@ namespace utf8 u16bit_iterator utf8to16(octet_iterator start, octet_iterator end, u16bit_iterator result) { while (start < end) { - utfchar32_t cp = utf8::unchecked::next(start); + utfchar32_t cp = utflib::unchecked::next(start); if (cp > 0xffff) { //make a surrogate pair *result++ = static_cast((cp >> 10) + internal::LEAD_OFFSET); *result++ = static_cast((cp & 0x3ff) + internal::TRAIL_SURROGATE_MIN); @@ -214,7 +215,7 @@ namespace utf8 octet_iterator utf32to8(u32bit_iterator start, u32bit_iterator end, octet_iterator result) { while (start != end) - result = utf8::unchecked::append(*(start++), result); + result = utflib::unchecked::append(*(start++), result); return result; } @@ -223,7 +224,7 @@ namespace utf8 u32bit_iterator utf8to32(octet_iterator start, octet_iterator end, u32bit_iterator result) { while (start < end) - (*result++) = utf8::unchecked::next(start); + (*result++) = utflib::unchecked::next(start); return result; } @@ -245,7 +246,7 @@ namespace utf8 utfchar32_t operator * () const { octet_iterator temp = it; - return utf8::unchecked::next(temp); + return utflib::unchecked::next(temp); } bool operator == (const iterator& rhs) const { @@ -257,30 +258,30 @@ namespace utf8 } iterator& operator ++ () { - ::std::advance(it, utf8::internal::sequence_length(it)); + ::std::advance(it, utflib::internal::sequence_length(it)); return *this; } iterator operator ++ (int) { iterator temp = *this; - ::std::advance(it, utf8::internal::sequence_length(it)); + ::std::advance(it, utflib::internal::sequence_length(it)); return temp; } iterator& operator -- () { - utf8::unchecked::prior(it); + utflib::unchecked::prior(it); return *this; } iterator operator -- (int) { iterator temp = *this; - utf8::unchecked::prior(it); + utflib::unchecked::prior(it); return temp; } }; // class iterator - } // namespace utf8::unchecked -} // namespace utf8 + } // namespace unchecked +} // namespace iris::utflib #endif // header guard diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index f2bf1fc..55822a2 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,10 +1,4 @@ -cmake_minimum_required (VERSION 3.5) -project(utfcpptests LANGUAGES CXX) -enable_testing() - -add_library(${PROJECT_NAME} INTERFACE) - -include_directories("${PROJECT_SOURCE_DIR}/../source") +include(CTest) if (MSVC) # warning level 4 @@ -48,8 +42,14 @@ set_target_properties(cpp20 CXX_STANDARD_REQUIRED YES CXX_EXTENSIONS NO) +target_link_libraries(negative PRIVATE iris_utf8cpp) +target_link_libraries(cpp11 PRIVATE iris_utf8cpp) +target_link_libraries(cpp17 PRIVATE iris_utf8cpp) +target_link_libraries(cpp20 PRIVATE iris_utf8cpp) +target_link_libraries(apitests PRIVATE iris_utf8cpp) +target_link_libraries(noexceptionstests PRIVATE iris_utf8cpp) -add_test(negative_test negative ${PROJECT_SOURCE_DIR}/test_data/utf8_invalid.txt) +add_test(negative_test negative ${CMAKE_CURRENT_LIST_DIR}/test_data/utf8_invalid.txt) add_test(cpp11_test cpp11) add_test(cpp17_test cpp17) add_test(cpp20_test cpp20) diff --git a/tests/negative.cpp b/tests/negative.cpp index f1bcc99..d2b90ae 100644 --- a/tests/negative.cpp +++ b/tests/negative.cpp @@ -1,5 +1,5 @@ #include "utf8.h" -using namespace utf8; +using namespace iris::utflib; #include #include diff --git a/tests/test_checked_api.h b/tests/test_checked_api.h index 54e9cf8..d3484af 100644 --- a/tests/test_checked_api.h +++ b/tests/test_checked_api.h @@ -1,11 +1,11 @@ -#ifndef UTF8_FOR_CPP_TEST_CHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731 -#define UTF8_FOR_CPP_TEST_CHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731 +#ifndef IRIS_UTFLIB_TEST_CHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731 +#define IRIS_UTFLIB_TEST_CHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731 #include "utf8.h" #include #include -using namespace utf8; +using namespace iris::utflib; using namespace std; @@ -91,7 +91,7 @@ TEST(CheckedAPITests, test_next16) { const utfchar16_t u[3] = {0x65e5, 0xd800, 0xdf46}; const utfchar16_t* w = u; - utf8::utfchar32_t cp = next16(w, w + 3); + iris::utflib::utfchar32_t cp = next16(w, w + 3); EXPECT_EQ (cp, 0x65e5); EXPECT_EQ (w, u + 1); @@ -147,7 +147,7 @@ TEST(CheckedAPITests, test_advance) TEST(CheckedAPITests, test_distance) { const char* twochars = "\xe6\x97\xa5\xd1\x88"; - size_t dist = static_cast(utf8::distance(twochars, twochars + 5)); + size_t dist = static_cast(iris::utflib::distance(twochars, twochars + 5)); EXPECT_EQ (dist, 2); } diff --git a/tests/test_checked_iterator.h b/tests/test_checked_iterator.h index 2829a73..61ce11b 100644 --- a/tests/test_checked_iterator.h +++ b/tests/test_checked_iterator.h @@ -1,34 +1,34 @@ -#ifndef UTF8_FOR_CPP_TEST_CHECKED_ITERATOR_H_2675DCD0_9480_4c0c_B92A_CC14C027B731 -#define UTF8_FOR_CPP_TEST_CHECKED_ITERATOR_H_2675DCD0_9480_4c0c_B92A_CC14C027B731 +#ifndef IRIS_UTFLIB_TEST_CHECKED_ITERATOR_H_2675DCD0_9480_4c0c_B92A_CC14C027B731 +#define IRIS_UTFLIB_TEST_CHECKED_ITERATOR_H_2675DCD0_9480_4c0c_B92A_CC14C027B731 #include "utf8.h" -using namespace utf8; +using namespace iris::utflib; TEST(CheckedIteratrTests, test_increment) { const char* threechars = "\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88"; - utf8::iterator it(threechars, threechars, threechars + 9); - utf8::iterator it2 = it; + iris::utflib::iterator it(threechars, threechars, threechars + 9); + iris::utflib::iterator it2 = it; EXPECT_EQ (it2, it); EXPECT_EQ (*it, 0x10346); EXPECT_EQ (*(++it), 0x65e5); EXPECT_EQ ((*it++), 0x65e5); EXPECT_EQ (*it, 0x0448); EXPECT_NE (it, it2); - utf8::iterator endit (threechars + 9, threechars, threechars + 9); + iris::utflib::iterator endit (threechars + 9, threechars, threechars + 9); EXPECT_EQ (++it, endit); } TEST(CheckedIteratrTests, test_decrement) { const char* threechars = "\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88"; - utf8::iterator it(threechars+9, threechars, threechars + 9); + iris::utflib::iterator it(threechars+9, threechars, threechars + 9); EXPECT_EQ (*(--it), 0x0448); EXPECT_EQ ((*it--), 0x0448); EXPECT_EQ (*it, 0x65e5); - EXPECT_EQ (--it, utf8::iterator(threechars, threechars, threechars + 9)); + EXPECT_EQ (--it, iris::utflib::iterator(threechars, threechars, threechars + 9)); EXPECT_EQ (*it, 0x10346); } diff --git a/tests/test_cpp11.cpp b/tests/test_cpp11.cpp index 682aec0..6cbef5f 100644 --- a/tests/test_cpp11.cpp +++ b/tests/test_cpp11.cpp @@ -1,7 +1,7 @@ #include "ftest.h" #include "utf8.h" #include -using namespace utf8; +using namespace iris::utflib; using namespace std; #if __cplusplus >= 201103L // C++ 11 or later @@ -53,7 +53,7 @@ TEST(CPP11APITests, test_utf16to8) u16string h16 = u"h!"; string h8; - utf8::unchecked::utf16to8(h16.begin(), h16.end(), std::back_inserter(h8)); + iris::utflib::unchecked::utf16to8(h16.begin(), h16.end(), std::back_inserter(h8)); EXPECT_EQ (h8, "h!"); } diff --git a/tests/test_cpp17.cpp b/tests/test_cpp17.cpp index 88591e3..7e6863a 100644 --- a/tests/test_cpp17.cpp +++ b/tests/test_cpp17.cpp @@ -1,7 +1,7 @@ #include "ftest.h" #include "utf8.h" #include -using namespace utf8; +using namespace iris::utflib; using namespace std; #if __cplusplus >= 201703L // C++ 17 or later diff --git a/tests/test_cpp20.cpp b/tests/test_cpp20.cpp index f84f7c0..a3bcc7f 100644 --- a/tests/test_cpp20.cpp +++ b/tests/test_cpp20.cpp @@ -1,8 +1,8 @@ #include "ftest.h" -#define UTF_CPP_CPLUSPLUS 202002L +#define IRIS_UTFLIB_CPP_CPLUSPLUS 202002L #include "utf8.h" #include -using namespace utf8; +using namespace iris::utflib; using namespace std; TEST(CPP20APITests, test_utf16tou8) diff --git a/tests/test_unchecked_api.h b/tests/test_unchecked_api.h index 3d56178..cf0da56 100644 --- a/tests/test_unchecked_api.h +++ b/tests/test_unchecked_api.h @@ -1,5 +1,5 @@ -#ifndef UTF8_FOR_CPP_TEST_UNCHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731 -#define UTF8_FOR_CPP_TEST_UNCHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731 +#ifndef IRIS_UTFLIB_TEST_UNCHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731 +#define IRIS_UTFLIB_TEST_UNCHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731 #include "utf8/unchecked.h" @@ -11,28 +11,28 @@ using namespace std; TEST(UnCheckedAPITests, test_append) { unsigned char u[5] = {0,0,0,0,0}; - utf8::unchecked::append(0x0448, u); + iris::utflib::unchecked::append(0x0448, u); EXPECT_EQ (u[0], 0xd1); EXPECT_EQ (u[1], 0x88); EXPECT_EQ (u[2], 0); EXPECT_EQ (u[3], 0); EXPECT_EQ (u[4], 0); - utf8::unchecked::append(0x65e5, u); + iris::utflib::unchecked::append(0x65e5, u); EXPECT_EQ (u[0], 0xe6); EXPECT_EQ (u[1], 0x97); EXPECT_EQ (u[2], 0xa5); EXPECT_EQ (u[3], 0); EXPECT_EQ (u[4], 0); - utf8::unchecked::append(0x3044, u); + iris::utflib::unchecked::append(0x3044, u); EXPECT_EQ (u[0], 0xe3); EXPECT_EQ (u[1], 0x81); EXPECT_EQ (u[2], 0x84); EXPECT_EQ (u[3], 0); EXPECT_EQ (u[4], 0); - utf8::unchecked::append(0x10346, u); + iris::utflib::unchecked::append(0x10346, u); EXPECT_EQ (u[0], 0xf0); EXPECT_EQ (u[1], 0x90); EXPECT_EQ (u[2], 0x8d); @@ -43,15 +43,15 @@ TEST(UnCheckedAPITests, test_append) TEST(UnCheckedAPITests, test_append16) { unsigned short u[5] = {0,0}; - utf8::unchecked::append16(0x0448, u); + iris::utflib::unchecked::append16(0x0448, u); EXPECT_EQ (u[0], 0x0448); EXPECT_EQ (u[1], 0x0000); - utf8::unchecked::append16(0x65e5, u); + iris::utflib::unchecked::append16(0x65e5, u); EXPECT_EQ (u[0], 0x65e5); EXPECT_EQ (u[1], 0x0000); - utf8::unchecked::append16(0x10346, u); + iris::utflib::unchecked::append16(0x10346, u); EXPECT_EQ (u[0], 0xd800); EXPECT_EQ (u[1], 0xdf46); } @@ -60,35 +60,35 @@ TEST(UnCheckedAPITests, test_next) { const char* twochars = "\xe6\x97\xa5\xd1\x88"; const char* w = twochars; - unsigned int cp = utf8::unchecked::next(w); + unsigned int cp = iris::utflib::unchecked::next(w); EXPECT_EQ (cp, 0x65e5); EXPECT_EQ (w, twochars + 3); const char* threechars = "\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88"; w = threechars; - cp = utf8::unchecked::next(w); + cp = iris::utflib::unchecked::next(w); EXPECT_EQ (cp, 0x10346); EXPECT_EQ (w, threechars + 4); - cp = utf8::unchecked::next(w); + cp = iris::utflib::unchecked::next(w); EXPECT_EQ (cp, 0x65e5); EXPECT_EQ (w, threechars + 7); - cp = utf8::unchecked::next(w); + cp = iris::utflib::unchecked::next(w); EXPECT_EQ (cp, 0x0448); EXPECT_EQ (w, threechars + 9); } TEST(UnCheckedAPITests, test_next16) { - const utf8::utfchar16_t u[3] = {0x65e5, 0xd800, 0xdf46}; - const utf8::utfchar16_t* w = u; - utf8::utfchar32_t cp = utf8::unchecked::next16(w); + const iris::utflib::utfchar16_t u[3] = {0x65e5, 0xd800, 0xdf46}; + const iris::utflib::utfchar16_t* w = u; + iris::utflib::utfchar32_t cp = iris::utflib::unchecked::next16(w); EXPECT_EQ (cp, 0x65e5); EXPECT_EQ (w, u + 1); - cp = utf8::unchecked::next16(w); + cp = iris::utflib::unchecked::next16(w); EXPECT_EQ (cp, 0x10346); EXPECT_EQ (w, u + 3); } @@ -96,7 +96,7 @@ TEST(UnCheckedAPITests, test_next16) TEST(UnCheckedAPITests, test_peek_next) { const char* const cw = "\xe6\x97\xa5\xd1\x88"; - unsigned int cp = utf8::unchecked::peek_next(cw); + unsigned int cp = iris::utflib::unchecked::peek_next(cw); EXPECT_EQ (cp, 0x65e5); } @@ -104,19 +104,19 @@ TEST(UnCheckedAPITests, test_prior) { const char* twochars = "\xe6\x97\xa5\xd1\x88"; const char* w = twochars + 3; - unsigned int cp = utf8::unchecked::prior (w); + unsigned int cp = iris::utflib::unchecked::prior (w); EXPECT_EQ (cp, 0x65e5); EXPECT_EQ (w, twochars); const char* threechars = "\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88"; w = threechars + 9; - cp = utf8::unchecked::prior(w); + cp = iris::utflib::unchecked::prior(w); EXPECT_EQ (cp, 0x0448); EXPECT_EQ (w, threechars + 7); - cp = utf8::unchecked::prior(w); + cp = iris::utflib::unchecked::prior(w); EXPECT_EQ (cp, 0x65e5); EXPECT_EQ (w, threechars + 4); - cp = utf8::unchecked::prior(w); + cp = iris::utflib::unchecked::prior(w); EXPECT_EQ (cp, 0x10346); EXPECT_EQ (w, threechars); } @@ -125,22 +125,22 @@ TEST(UnCheckedAPITests, test_advance) { const char* threechars = "\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88"; const char* w = threechars; - utf8::unchecked::advance(w, 2); + iris::utflib::unchecked::advance(w, 2); EXPECT_EQ(w, threechars + 7); - utf8::unchecked::advance(w, -2); + iris::utflib::unchecked::advance(w, -2); EXPECT_EQ(w, threechars); - utf8::unchecked::advance(w, 3); + iris::utflib::unchecked::advance(w, 3); EXPECT_EQ(w, threechars + 9); - utf8::unchecked::advance(w, -2); + iris::utflib::unchecked::advance(w, -2); EXPECT_EQ(w, threechars + 4); - utf8::unchecked::advance(w, -1); + iris::utflib::unchecked::advance(w, -1); EXPECT_EQ(w, threechars); } TEST(UnCheckedAPITests, test_distance) { const char* twochars = "\xe6\x97\xa5\xd1\x88"; - size_t dist = static_cast(utf8::unchecked::distance(twochars, twochars + 5)); + size_t dist = static_cast(iris::utflib::unchecked::distance(twochars, twochars + 5)); EXPECT_EQ (dist, 2); } @@ -148,7 +148,7 @@ TEST(UnCheckedAPITests, test_utf32to8) { unsigned int utf32string[] = {0x448, 0x65E5, 0x10346, 0}; string utf8result; - utf8::unchecked::utf32to8(utf32string, utf32string + 3, back_inserter(utf8result)); + iris::utflib::unchecked::utf32to8(utf32string, utf32string + 3, back_inserter(utf8result)); EXPECT_EQ (utf8result.size(), 9); } @@ -156,7 +156,7 @@ TEST(UnCheckedAPITests, test_utf8to32) { const char* twochars = "\xe6\x97\xa5\xd1\x88"; vector utf32result; - utf8::unchecked::utf8to32(twochars, twochars + 5, back_inserter(utf32result)); + iris::utflib::unchecked::utf8to32(twochars, twochars + 5, back_inserter(utf32result)); EXPECT_EQ (utf32result.size(), 2); } @@ -164,12 +164,12 @@ TEST(UnCheckedAPITests, test_utf16to8) { unsigned short utf16string[] = {0x41, 0x0448, 0x65e5, 0xd834, 0xdd1e}; string utf8result; - utf8::unchecked::utf16to8(utf16string, utf16string + 5, back_inserter(utf8result)); + iris::utflib::unchecked::utf16to8(utf16string, utf16string + 5, back_inserter(utf8result)); EXPECT_EQ (utf8result.size(), 10); utf8result.clear(); unsigned short highsurrogateonly[] = {0xd800}; - utf8::unchecked::utf16to8(highsurrogateonly, highsurrogateonly + 1, back_inserter(utf8result)); + iris::utflib::unchecked::utf16to8(highsurrogateonly, highsurrogateonly + 1, back_inserter(utf8result)); EXPECT_TRUE(true); // we didn't crash } @@ -177,7 +177,7 @@ TEST(UnCheckedAPITests, test_utf8to16) { char utf8_with_surrogates[] = "\xe6\x97\xa5\xd1\x88\xf0\x9d\x84\x9e"; vector utf16result; - utf8::unchecked::utf8to16(utf8_with_surrogates, utf8_with_surrogates + 9, back_inserter(utf16result)); + iris::utflib::unchecked::utf8to16(utf8_with_surrogates, utf8_with_surrogates + 9, back_inserter(utf16result)); EXPECT_EQ (utf16result.size(), 4); EXPECT_EQ (utf16result[2], 0xd834); EXPECT_EQ (utf16result[3], 0xdd1e); @@ -187,19 +187,19 @@ TEST(UnCheckedAPITests, test_replace_invalid) { char invalid_sequence[] = "a\x80\xe0\xa0\xc0\xAF\xED\xa0\x80z"; vector replace_invalid_result; - utf8::unchecked::replace_invalid (invalid_sequence, invalid_sequence + sizeof(invalid_sequence), std::back_inserter(replace_invalid_result), '?'); - bool bvalid = utf8::is_valid(replace_invalid_result.begin(), replace_invalid_result.end()); + iris::utflib::unchecked::replace_invalid (invalid_sequence, invalid_sequence + sizeof(invalid_sequence), std::back_inserter(replace_invalid_result), '?'); + bool bvalid = iris::utflib::is_valid(replace_invalid_result.begin(), replace_invalid_result.end()); EXPECT_TRUE (bvalid); const char fixed_invalid_sequence[] = "a????z"; EXPECT_EQ (sizeof(fixed_invalid_sequence), replace_invalid_result.size()); EXPECT_TRUE (std::equal(replace_invalid_result.begin(), replace_invalid_result.begin() + sizeof(fixed_invalid_sequence), fixed_invalid_sequence)); char invalid_sequence2[] = "\xD9\x85\xD8\x80\xE0\xA0\xC0\xAF\xED\xA0\x80z"; - bvalid = utf8::is_valid(invalid_sequence2); + bvalid = iris::utflib::is_valid(invalid_sequence2); EXPECT_FALSE (bvalid); replace_invalid_result.clear(); - utf8::unchecked::replace_invalid (invalid_sequence2, invalid_sequence2 + sizeof(invalid_sequence2), std::back_inserter(replace_invalid_result), '?'); - bvalid = utf8::is_valid(replace_invalid_result.begin(), replace_invalid_result.end()); + iris::utflib::unchecked::replace_invalid (invalid_sequence2, invalid_sequence2 + sizeof(invalid_sequence2), std::back_inserter(replace_invalid_result), '?'); + bvalid = iris::utflib::is_valid(replace_invalid_result.begin(), replace_invalid_result.end()); EXPECT_TRUE (bvalid); const char fixed_invalid_sequence2[] = "\xD9\x85\xD8\x80???z"; EXPECT_EQ (sizeof(fixed_invalid_sequence2), replace_invalid_result.size()); diff --git a/tests/test_unchecked_iterator.h b/tests/test_unchecked_iterator.h index 4294232..dcc9806 100644 --- a/tests/test_unchecked_iterator.h +++ b/tests/test_unchecked_iterator.h @@ -1,34 +1,34 @@ -#ifndef UTF8_FOR_CPP_TEST_UNCHECKED_ITERATOR_H_2675DCD0_9480_4c0c_B92A_CC14C027B731 -#define UTF8_FOR_CPP_TEST_UNCHECKED_ITERATOR_H_2675DCD0_9480_4c0c_B92A_CC14C027B731 +#ifndef IRIS_UTFLIB_TEST_UNCHECKED_ITERATOR_H_2675DCD0_9480_4c0c_B92A_CC14C027B731 +#define IRIS_UTFLIB_TEST_UNCHECKED_ITERATOR_H_2675DCD0_9480_4c0c_B92A_CC14C027B731 #include "utf8/unchecked.h" -using namespace utf8::unchecked; +using namespace iris::utflib::unchecked; TEST(UnCheckedIteratrTests, test_increment) { const char* threechars = "\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88"; - utf8::unchecked::iterator it(threechars); - utf8::unchecked::iterator it2 = it; + iris::utflib::unchecked::iterator it(threechars); + iris::utflib::unchecked::iterator it2 = it; EXPECT_EQ (it2, it); EXPECT_EQ (*it, 0x10346); EXPECT_EQ (*(++it), 0x65e5); EXPECT_EQ ((*it++), 0x65e5); EXPECT_EQ (*it, 0x0448); EXPECT_NE (it, it2); - utf8::unchecked::iterator endit (threechars + 9); + iris::utflib::unchecked::iterator endit (threechars + 9); EXPECT_EQ (++it, endit); } TEST(UnCheckedIteratrTests, test_decrement) { const char* threechars = "\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88"; - utf8::unchecked::iterator it(threechars+9); + iris::utflib::unchecked::iterator it(threechars+9); EXPECT_EQ (*(--it), 0x0448); EXPECT_EQ ((*it--), 0x0448); EXPECT_EQ (*it, 0x65e5); - EXPECT_EQ (--it, utf8::unchecked::iterator(threechars)); + EXPECT_EQ (--it, iris::utflib::unchecked::iterator(threechars)); EXPECT_EQ (*it, 0x10346); } diff --git a/utf8cppConfig.cmake.in b/utf8cppConfig.cmake.in deleted file mode 100644 index 4bdb9c4..0000000 --- a/utf8cppConfig.cmake.in +++ /dev/null @@ -1,8 +0,0 @@ -@PACKAGE_INIT@ - -include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake") -check_required_components("@PROJECT_NAME@") - -if(NOT TARGET utf8::cpp) - add_library(utf8::cpp ALIAS utf8cpp::utf8cpp) -endif() From e7546f046c36bb9f4a421e620e3ad7cc358d9b69 Mon Sep 17 00:00:00 2001 From: yaito3014 Date: Tue, 17 Feb 2026 13:58:58 +0900 Subject: [PATCH 02/28] Merge into single header and refine tests --- CMakeLists.txt | 7 +- source/utf8.h | 801 +++++++++++++++++++++++++++++++- source/utf8/checked.h | 360 -------------- source/utf8/core.h | 501 -------------------- source/utf8/cpp11.h | 71 --- source/utf8/cpp17.h | 97 ---- source/utf8/cpp20.h | 125 ----- source/utf8/unchecked.h | 287 ------------ tests/CMakeLists.txt | 32 +- tests/apitests.cpp | 257 +++++++++- tests/negative.cpp | 2 + tests/noexceptionstests.cpp | 4 - tests/test_checked_api.h | 233 ---------- tests/test_checked_iterator.h | 35 -- tests/test_cpp11.cpp | 11 +- tests/test_cpp17.cpp | 10 +- tests/test_cpp20.cpp | 4 +- tests/test_unchecked_api.h | 209 --------- tests/test_unchecked_iterator.h | 36 -- 19 files changed, 1059 insertions(+), 2023 deletions(-) delete mode 100644 source/utf8/checked.h delete mode 100644 source/utf8/core.h delete mode 100644 source/utf8/cpp11.h delete mode 100644 source/utf8/cpp17.h delete mode 100644 source/utf8/cpp20.h delete mode 100644 source/utf8/unchecked.h delete mode 100644 tests/noexceptionstests.cpp delete mode 100644 tests/test_checked_api.h delete mode 100644 tests/test_checked_iterator.h delete mode 100644 tests/test_unchecked_api.h delete mode 100644 tests/test_unchecked_iterator.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 345467a..4e3985f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,8 @@ project( add_library(${PROJECT_NAME} INTERFACE) +target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_23) + target_sources( ${PROJECT_NAME} INTERFACE @@ -16,11 +18,6 @@ target_sources( TYPE HEADERS BASE_DIRS source FILES - source/utf8/core.hpp - source/utf8/cpp11.hpp - source/utf8/cpp17.hpp - source/utf8/cpp20.hpp - source/utf8/checked.hpp source/utf8.hpp ) diff --git a/source/utf8.h b/source/utf8.h index 3ecc6da..2aea1ea 100644 --- a/source/utf8.h +++ b/source/utf8.h @@ -29,19 +29,798 @@ DEALINGS IN THE SOFTWARE. #ifndef IRIS_UTFLIB_UTF8_H #define IRIS_UTFLIB_UTF8_H -/* -To control the C++ language version used by the library, you can define IRIS_UTFLIB_CPP_CPLUSPLUS macro -and set it to one of the values used by the __cplusplus predefined macro. +#include +#include -For instance, - #define IRIS_UTFLIB_CPP_CPLUSPLUS 199711L -will cause the UTF-8 CPP library to use only types and language features available in the C++ 98 standard. -Some library features will be disabled. +#include -If you leave IRIS_UTFLIB_CPP_CPLUSPLUS undefined, it will be internally assigned to __cplusplus. -*/ +namespace iris::utflib +{ + using utfchar8_t = char8_t; + using utfchar16_t = char16_t; + using utfchar32_t = char32_t; + + // Helper code - not intended to be directly called by the library users. May be changed at any time + namespace internal + { + // Unicode constants + // Leading (high) surrogates: 0xd800 - 0xdbff + // Trailing (low) surrogates: 0xdc00 - 0xdfff + const utfchar16_t LEAD_SURROGATE_MIN = 0xd800u; + const utfchar16_t LEAD_SURROGATE_MAX = 0xdbffu; + const utfchar16_t TRAIL_SURROGATE_MIN = 0xdc00u; + const utfchar16_t TRAIL_SURROGATE_MAX = 0xdfffu; + const utfchar16_t LEAD_OFFSET = 0xd7c0u; // LEAD_SURROGATE_MIN - (0x10000 >> 10) + const utfchar32_t SURROGATE_OFFSET = 0xfca02400u; // 0x10000u - (LEAD_SURROGATE_MIN << 10) - TRAIL_SURROGATE_MIN + + // Maximum valid value for a Unicode code point + const utfchar32_t CODE_POINT_MAX = 0x0010ffffu; + + template + inline utfchar8_t mask8(octet_type oc) + { + return static_cast(0xff & oc); + } + + template + inline utfchar16_t mask16(u16_type oc) + { + return static_cast(0xffff & oc); + } + + template + inline bool is_trail(octet_type oc) + { + return ((utflib::internal::mask8(oc) >> 6) == 0x2); + } + + inline bool is_lead_surrogate(utfchar32_t cp) + { + return (cp >= static_cast(LEAD_SURROGATE_MIN) && cp <= static_cast(LEAD_SURROGATE_MAX)); + } + + inline bool is_trail_surrogate(utfchar32_t cp) + { + return (cp >= static_cast(TRAIL_SURROGATE_MIN) && cp <= static_cast(TRAIL_SURROGATE_MAX)); + } + + inline bool is_surrogate(utfchar32_t cp) + { + return (cp >= static_cast(LEAD_SURROGATE_MIN) && cp <= static_cast(TRAIL_SURROGATE_MAX)); + } + + inline bool is_code_point_valid(utfchar32_t cp) + { + return (cp <= CODE_POINT_MAX && !utflib::internal::is_surrogate(cp)); + } + + inline bool is_in_bmp(utfchar32_t cp) + { + return cp < utfchar32_t(0x10000); + } + + template + int sequence_length(octet_iterator lead_it) + { + const utfchar8_t lead = utflib::internal::mask8(*lead_it); + if (lead < 0x80) + return 1; + else if ((lead >> 5) == 0x6) + return 2; + else if ((lead >> 4) == 0xe) + return 3; + else if ((lead >> 3) == 0x1e) + return 4; + else + return 0; + } + + inline bool is_overlong_sequence(utfchar32_t cp, int length) + { + if (cp < 0x80) { + if (length != 1) + return true; + } + else if (cp < 0x800) { + if (length != 2) + return true; + } + else if (cp < 0x10000) { + if (length != 3) + return true; + } + return false; + } + + enum utf_error {UTF8_OK, NOT_ENOUGH_ROOM, INVALID_LEAD, INCOMPLETE_SEQUENCE, OVERLONG_SEQUENCE, INVALID_CODE_POINT}; + + /// Helper for get_sequence_x + template + utf_error increase_safely(octet_iterator& it, const octet_iterator end) + { + if (++it == end) + return NOT_ENOUGH_ROOM; + + if (!utflib::internal::is_trail(*it)) + return INCOMPLETE_SEQUENCE; + + return UTF8_OK; + } + + #define IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(IT, END) {utf_error ret = increase_safely(IT, END); if (ret != UTF8_OK) return ret;} + + /// get_sequence_x functions decode utf-8 sequences of the length x + template + utf_error get_sequence_1(octet_iterator& it, octet_iterator end, utfchar32_t& code_point) + { + if (it == end) + return NOT_ENOUGH_ROOM; + + code_point = static_cast(utflib::internal::mask8(*it)); + + return UTF8_OK; + } + + template + utf_error get_sequence_2(octet_iterator& it, octet_iterator end, utfchar32_t& code_point) + { + if (it == end) + return NOT_ENOUGH_ROOM; + + code_point = static_cast(utflib::internal::mask8(*it)); + + IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end) + + code_point = ((code_point << 6) & 0x7ff) + ((*it) & 0x3f); + + return UTF8_OK; + } + + template + utf_error get_sequence_3(octet_iterator& it, octet_iterator end, utfchar32_t& code_point) + { + if (it == end) + return NOT_ENOUGH_ROOM; + + code_point = static_cast(utflib::internal::mask8(*it)); + + IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end) + + code_point = ((code_point << 12) & 0xffff) + ((utflib::internal::mask8(*it) << 6) & 0xfff); + + IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end) + + code_point = static_cast(code_point + ((*it) & 0x3f)); + + return UTF8_OK; + } + + template + utf_error get_sequence_4(octet_iterator& it, octet_iterator end, utfchar32_t& code_point) + { + if (it == end) + return NOT_ENOUGH_ROOM; + + code_point = static_cast(utflib::internal::mask8(*it)); + + IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end) + + code_point = ((code_point << 18) & 0x1fffff) + ((utflib::internal::mask8(*it) << 12) & 0x3ffff); + + IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end) + + code_point = static_cast(code_point + ((utflib::internal::mask8(*it) << 6) & 0xfff)); + + IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end) + + code_point = static_cast(code_point + ((*it) & 0x3f)); + + return UTF8_OK; + } + + #undef IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR + + template + utf_error validate_next(octet_iterator& it, octet_iterator end, utfchar32_t& code_point) + { + if (it == end) + return NOT_ENOUGH_ROOM; + + // Save the original value of it so we can go back in case of failure + // Of course, it does not make much sense with i.e. stream iterators + octet_iterator original_it = it; + + utfchar32_t cp = 0; + // Determine the sequence length based on the lead octet + const int length = utflib::internal::sequence_length(it); + + // Get trail octets and calculate the code point + utf_error err = UTF8_OK; + switch (length) { + case 0: + return INVALID_LEAD; + case 1: + err = utflib::internal::get_sequence_1(it, end, cp); + break; + case 2: + err = utflib::internal::get_sequence_2(it, end, cp); + break; + case 3: + err = utflib::internal::get_sequence_3(it, end, cp); + break; + case 4: + err = utflib::internal::get_sequence_4(it, end, cp); + break; + } + + if (err == UTF8_OK) { + // Decoding succeeded. Now, security checks... + if (utflib::internal::is_code_point_valid(cp)) { + if (!utflib::internal::is_overlong_sequence(cp, length)){ + // Passed! Return here. + code_point = cp; + ++it; + return UTF8_OK; + } + else + err = OVERLONG_SEQUENCE; + } + else + err = INVALID_CODE_POINT; + } + + // Failure branch - restore the original value of the iterator + it = original_it; + return err; + } + + template + inline utf_error validate_next(octet_iterator& it, octet_iterator end) { + utfchar32_t ignored; + return utflib::internal::validate_next(it, end, ignored); + } + + template + utf_error validate_next16(word_iterator& it, word_iterator end, utfchar32_t& code_point) + { + // Make sure the iterator dereferences a large enough type + typedef typename std::iterator_traits::value_type word_type; + static_assert(sizeof(word_type) >= sizeof(utfchar16_t)); + // Check the edge case: + if (it == end) + return NOT_ENOUGH_ROOM; + // Save the original value of it so we can go back in case of failure + // Of course, it does not make much sense with i.e. stream iterators + word_iterator original_it = it; + + utf_error err = UTF8_OK; + + const utfchar16_t first_word = *it++; + if (!is_surrogate(first_word)) { + code_point = first_word; + return UTF8_OK; + } + else { + if (it == end) + err = NOT_ENOUGH_ROOM; + else if (is_lead_surrogate(first_word)) { + const utfchar16_t second_word = *it++; + if (is_trail_surrogate(static_cast(second_word))) { + code_point = static_cast(first_word << 10) + static_cast(second_word) + SURROGATE_OFFSET; + return UTF8_OK; + } else + err = INCOMPLETE_SEQUENCE; + + } else { + err = INVALID_LEAD; + } + } + // error branch + it = original_it; + return err; + } + + template ::value_type> + octet_iterator append(utfchar32_t cp, octet_iterator result) { + if (cp < 0x80) // one octet + *(result++) = static_cast(cp); + else if (cp < 0x800) { // two octets + *(result++) = static_cast((cp >> 6) | 0xc0); + *(result++) = static_cast((cp & 0x3f) | 0x80); + } + else if (cp < 0x10000) { // three octets + *(result++) = static_cast((cp >> 12) | 0xe0); + *(result++) = static_cast(((cp >> 6) & 0x3f) | 0x80); + *(result++) = static_cast((cp & 0x3f) | 0x80); + } + else { // four octets + *(result++) = static_cast((cp >> 18) | 0xf0); + *(result++) = static_cast(((cp >> 12) & 0x3f)| 0x80); + *(result++) = static_cast(((cp >> 6) & 0x3f) | 0x80); + *(result++) = static_cast((cp & 0x3f) | 0x80); + } + return result; + } + + template + std::back_insert_iterator append(utfchar32_t cp, std::back_insert_iterator result) { + return append, typename container_type::value_type>(cp, result); + } + + template ::value_type> + word_iterator append16(utfchar32_t cp, word_iterator result) { + static_assert(sizeof(word_type) >= sizeof(utfchar16_t)); + if (is_in_bmp(cp)) + *(result++) = static_cast(cp); + else { + // Code points from the supplementary planes are encoded via surrogate pairs + *(result++) = static_cast(LEAD_OFFSET + (cp >> 10)); + *(result++) = static_cast(TRAIL_SURROGATE_MIN + (cp & 0x3FF)); + } + return result; + } + + template + std::back_insert_iterator append16(utfchar32_t cp, std::back_insert_iterator result) { + return append16, typename container_type::value_type>(cp, result); + } + } // namespace internal + + // Base for the exceptions that may be thrown from the library + class exception : public ::std::exception { + }; + + // Exceptions that may be thrown from the library functions. + class invalid_code_point : public exception { + utfchar32_t cp; + public: + invalid_code_point(utfchar32_t codepoint) : cp(codepoint) {} + virtual const char* what() const noexcept override { return "Invalid code point"; } + utfchar32_t code_point() const {return cp;} + }; + + class invalid_utf8 : public exception { + utfchar8_t u8; + public: + invalid_utf8 (utfchar8_t u) : u8(u) {} + invalid_utf8 (char c) : u8(static_cast(c)) {} + virtual const char* what() const noexcept override { return "Invalid UTF-8"; } + utfchar8_t utf8_octet() const {return u8;} + }; + + class invalid_utf16 : public exception { + utfchar16_t u16; + public: + invalid_utf16 (utfchar16_t u) : u16(u) {} + virtual const char* what() const noexcept override { return "Invalid UTF-16"; } + utfchar16_t utf16_word() const {return u16;} + }; + + class not_enough_room : public exception { + public: + virtual const char* what() const noexcept override { return "Not enough space"; } + }; + + /// The library API - functions intended to be called by the users + + // Byte order mark + const utfchar8_t bom[] = {0xef, 0xbb, 0xbf}; + + template + octet_iterator find_invalid(octet_iterator start, octet_iterator end) + { + octet_iterator result = start; + while (result != end) { + utflib::internal::utf_error err_code = utflib::internal::validate_next(result, end); + if (err_code != internal::UTF8_OK) + return result; + } + return result; + } + + inline std::size_t find_invalid(std::string_view s) + { + std::string_view::const_iterator invalid = find_invalid(s.begin(), s.end()); + return (invalid == s.end()) ? std::string_view::npos : static_cast(invalid - s.begin()); + } + + inline std::size_t find_invalid(std::u8string_view s) + { + std::u8string_view::const_iterator invalid = find_invalid(s.begin(), s.end()); + return (invalid == s.end()) ? std::u8string_view::npos : static_cast(invalid - s.begin()); + } + + template + inline bool is_valid(octet_iterator start, octet_iterator end) + { + return (utflib::find_invalid(start, end) == end); + } + + inline bool is_valid(std::string_view s) + { + return is_valid(s.begin(), s.end()); + } + + inline bool is_valid(std::u8string_view s) + { + return is_valid(s.begin(), s.end()); + } + + template + inline bool starts_with_bom (octet_iterator it, octet_iterator end) + { + return ( + ((it != end) && (utflib::internal::mask8(*it++)) == bom[0]) && + ((it != end) && (utflib::internal::mask8(*it++)) == bom[1]) && + ((it != end) && (utflib::internal::mask8(*it)) == bom[2]) + ); + } + + inline bool starts_with_bom(std::string_view s) + { + return starts_with_bom(s.begin(), s.end()); + } + + inline bool starts_with_bom(std::u8string_view s) + { + return starts_with_bom(s.begin(), s.end()); + } + + template + octet_iterator append(utfchar32_t cp, octet_iterator result) + { + if (!utflib::internal::is_code_point_valid(cp)) + throw invalid_code_point(cp); + + return internal::append(cp, result); + } + + inline void append(utfchar32_t cp, std::string& s) + { + append(cp, std::back_inserter(s)); + } + + inline void append(utfchar32_t cp, std::u8string& s) + { + append(cp, std::back_inserter(s)); + } + + template + word_iterator append16(utfchar32_t cp, word_iterator result) + { + if (!utflib::internal::is_code_point_valid(cp)) + throw invalid_code_point(cp); + + return internal::append16(cp, result); + } + + inline void append16(utfchar32_t cp, std::u16string& s) + { + append16(cp, std::back_inserter(s)); + } + + template + output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out, utfchar32_t replacement) + { + while (start != end) { + octet_iterator sequence_start = start; + internal::utf_error err_code = utflib::internal::validate_next(start, end); + switch (err_code) { + case internal::UTF8_OK : + for (octet_iterator it = sequence_start; it != start; ++it) + *out++ = *it; + break; + case internal::NOT_ENOUGH_ROOM: + out = utflib::append (replacement, out); + start = end; + break; + case internal::INVALID_LEAD: + out = utflib::append (replacement, out); + ++start; + break; + case internal::INCOMPLETE_SEQUENCE: + case internal::OVERLONG_SEQUENCE: + case internal::INVALID_CODE_POINT: + out = utflib::append (replacement, out); + ++start; + // just one replacement mark for the sequence + while (start != end && utflib::internal::is_trail(*start)) + ++start; + break; + } + } + return out; + } + + template + inline output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out) + { + static const utfchar32_t replacement_marker = static_cast(utflib::internal::mask16(0xfffd)); + return utflib::replace_invalid(start, end, out, replacement_marker); + } + + inline std::string replace_invalid(std::string_view s, utfchar32_t replacement) + { + std::string result; + replace_invalid(s.begin(), s.end(), std::back_inserter(result), replacement); + return result; + } + + inline std::u8string replace_invalid(std::u8string_view s, char32_t replacement) + { + std::u8string result; + replace_invalid(s.begin(), s.end(), std::back_inserter(result), replacement); + return result; + } + + inline std::string replace_invalid(std::string_view s) + { + std::string result; + replace_invalid(s.begin(), s.end(), std::back_inserter(result)); + return result; + } + + inline std::u8string replace_invalid(std::u8string_view s) + { + std::u8string result; + replace_invalid(s.begin(), s.end(), std::back_inserter(result)); + return result; + } + + template + utfchar32_t next(octet_iterator& it, octet_iterator end) + { + utfchar32_t cp = 0; + internal::utf_error err_code = utflib::internal::validate_next(it, end, cp); + switch (err_code) { + case internal::UTF8_OK : + break; + case internal::NOT_ENOUGH_ROOM : + throw not_enough_room(); + case internal::INVALID_LEAD : + case internal::INCOMPLETE_SEQUENCE : + case internal::OVERLONG_SEQUENCE : + throw invalid_utf8(static_cast(*it)); + case internal::INVALID_CODE_POINT : + throw invalid_code_point(cp); + } + return cp; + } + + template + utfchar32_t next16(word_iterator& it, word_iterator end) + { + utfchar32_t cp = 0; + internal::utf_error err_code = utflib::internal::validate_next16(it, end, cp); + if (err_code == internal::NOT_ENOUGH_ROOM) + throw not_enough_room(); + return cp; + } + + template + utfchar32_t peek_next(octet_iterator it, octet_iterator end) + { + return utflib::next(it, end); + } + + template + utfchar32_t prior(octet_iterator& it, octet_iterator start) + { + // can't do much if it == start + if (it == start) + throw not_enough_room(); + + octet_iterator end = it; + // Go back until we hit either a lead octet or start + while (utflib::internal::is_trail(*(--it))) + if (it == start) + throw invalid_utf8(*it); // error - no lead byte in the sequence + return utflib::peek_next(it, end); + } + + template + void advance (octet_iterator& it, distance_type n, octet_iterator end) + { + const distance_type zero(0); + if (n < zero) { + // backward + for (distance_type i = n; i < zero; ++i) + utflib::prior(it, end); + } else { + // forward + for (distance_type i = zero; i < n; ++i) + utflib::next(it, end); + } + } + + template + typename std::iterator_traits::difference_type + distance (octet_iterator first, octet_iterator last) + { + typename std::iterator_traits::difference_type dist; + for (dist = 0; first < last; ++dist) + utflib::next(first, last); + return dist; + } + + template + octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result) + { + while (start != end) { + utfchar32_t cp = static_cast(utflib::internal::mask16(*start++)); + // Take care of surrogate pairs first + if (utflib::internal::is_lead_surrogate(cp)) { + if (start != end) { + const utfchar32_t trail_surrogate = static_cast(utflib::internal::mask16(*start++)); + if (utflib::internal::is_trail_surrogate(trail_surrogate)) + cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET; + else + throw invalid_utf16(static_cast(trail_surrogate)); + } + else + throw invalid_utf16(static_cast(cp)); + + } + // Lone trail surrogate + else if (utflib::internal::is_trail_surrogate(cp)) + throw invalid_utf16(static_cast(cp)); + + result = utflib::append(cp, result); + } + return result; + } + + inline std::string utf16to8(std::u16string_view s) + { + std::string result; + utf16to8(s.begin(), s.end(), std::back_inserter(result)); + return result; + } + + inline std::u8string utf16tou8(std::u16string_view s) + { + std::u8string result; + utf16to8(s.begin(), s.end(), std::back_inserter(result)); + return result; + } + + template + u16bit_iterator utf8to16 (octet_iterator start, octet_iterator end, u16bit_iterator result) + { + while (start < end) { + const utfchar32_t cp = utflib::next(start, end); + if (cp > 0xffff) { //make a surrogate pair + *result++ = static_cast((cp >> 10) + internal::LEAD_OFFSET); + *result++ = static_cast((cp & 0x3ff) + internal::TRAIL_SURROGATE_MIN); + } + else + *result++ = static_cast(cp); + } + return result; + } + + inline std::u16string utf8to16(std::string_view s) + { + std::u16string result; + utf8to16(s.begin(), s.end(), std::back_inserter(result)); + return result; + } + + inline std::u16string utf8to16(std::u8string_view s) + { + std::u16string result; + utf8to16(s.begin(), s.end(), std::back_inserter(result)); + return result; + } + + template + octet_iterator utf32to8 (u32bit_iterator start, u32bit_iterator end, octet_iterator result) + { + while (start != end) + result = utflib::append(*(start++), result); + + return result; + } + + inline std::string utf32to8(std::u32string_view s) + { + std::string result; + utf32to8(s.begin(), s.end(), std::back_inserter(result)); + return result; + } + + inline std::u8string utf32tou8(std::u32string_view s) + { + std::u8string result; + utf32to8(s.begin(), s.end(), std::back_inserter(result)); + return result; + } + + template + u32bit_iterator utf8to32 (octet_iterator start, octet_iterator end, u32bit_iterator result) + { + while (start < end) + (*result++) = utflib::next(start, end); + + return result; + } + + inline std::u32string utf8to32(std::string_view s) + { + std::u32string result; + utf8to32(s.begin(), s.end(), std::back_inserter(result)); + return result; + } + + inline std::u32string utf8to32(std::u8string_view s) + { + std::u32string result; + utf8to32(s.begin(), s.end(), std::back_inserter(result)); + return result; + } + + // The iterator class + template + class iterator { + octet_iterator it; + octet_iterator range_start; + octet_iterator range_end; + public: + typedef utfchar32_t value_type; + typedef utfchar32_t* pointer; + typedef utfchar32_t& reference; + typedef std::ptrdiff_t difference_type; + typedef std::bidirectional_iterator_tag iterator_category; + iterator () {} + explicit iterator (const octet_iterator& octet_it, + const octet_iterator& rangestart, + const octet_iterator& rangeend) : + it(octet_it), range_start(rangestart), range_end(rangeend) + { + if (it < range_start || it > range_end) + throw std::out_of_range("Invalid utf-8 iterator position"); + } + // the default "big three" are OK + octet_iterator base () const { return it; } + utfchar32_t operator * () const + { + octet_iterator temp = it; + return utflib::next(temp, range_end); + } + bool operator == (const iterator& rhs) const + { + if (range_start != rhs.range_start || range_end != rhs.range_end) + throw std::logic_error("Comparing utf-8 iterators defined with different ranges"); + return (it == rhs.it); + } + bool operator != (const iterator& rhs) const + { + return !(operator == (rhs)); + } + iterator& operator ++ () + { + utflib::next(it, range_end); + return *this; + } + iterator operator ++ (int) + { + iterator temp = *this; + utflib::next(it, range_end); + return temp; + } + iterator& operator -- () + { + utflib::prior(it, range_start); + return *this; + } + iterator operator -- (int) + { + iterator temp = *this; + utflib::prior(it, range_start); + return temp; + } + }; // class iterator -#include "utf8/checked.h" -#include "utf8/unchecked.h" +} // namespace iris::utflib #endif // header guard diff --git a/source/utf8/checked.h b/source/utf8/checked.h deleted file mode 100644 index 616b8e6..0000000 --- a/source/utf8/checked.h +++ /dev/null @@ -1,360 +0,0 @@ -// Copyright 2006-2016 Nemanja Trifunovic -// Copyright 2026 The Iris Project Contributors - -/* -Permission is hereby granted, free of charge, to any person or organization -obtaining a copy of the software and accompanying documentation covered by -this license (the "Software") to use, reproduce, display, distribute, -execute, and transmit the Software, and to prepare derivative works of the -Software, and to permit third-parties to whom the Software is furnished to -do so, all subject to the following: - -The copyright notices in the Software and this entire statement, including -the above license grant, this restriction and the following disclaimer, -must be included in all copies of the Software, in whole or in part, and -all derivative works of the Software, unless such copies or derivative -works are solely in the form of machine-executable object code generated by -a source language processor. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT -SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE -FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, -ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -DEALINGS IN THE SOFTWARE. -*/ - - -#ifndef IRIS_UTFLIB_UTF8_CHECKED_H -#define IRIS_UTFLIB_UTF8_CHECKED_H - -#include "core.h" -#include - -namespace iris::utflib -{ - // Base for the exceptions that may be thrown from the library - class exception : public ::std::exception { - }; - - // Exceptions that may be thrown from the library functions. - class invalid_code_point : public exception { - utfchar32_t cp; - public: - invalid_code_point(utfchar32_t codepoint) : cp(codepoint) {} - virtual const char* what() const IRIS_UTFLIB_CPP_NOEXCEPT IRIS_UTFLIB_CPP_OVERRIDE { return "Invalid code point"; } - utfchar32_t code_point() const {return cp;} - }; - - class invalid_utf8 : public exception { - utfchar8_t u8; - public: - invalid_utf8 (utfchar8_t u) : u8(u) {} - invalid_utf8 (char c) : u8(static_cast(c)) {} - virtual const char* what() const IRIS_UTFLIB_CPP_NOEXCEPT IRIS_UTFLIB_CPP_OVERRIDE { return "Invalid UTF-8"; } - utfchar8_t utf8_octet() const {return u8;} - }; - - class invalid_utf16 : public exception { - utfchar16_t u16; - public: - invalid_utf16 (utfchar16_t u) : u16(u) {} - virtual const char* what() const IRIS_UTFLIB_CPP_NOEXCEPT IRIS_UTFLIB_CPP_OVERRIDE { return "Invalid UTF-16"; } - utfchar16_t utf16_word() const {return u16;} - }; - - class not_enough_room : public exception { - public: - virtual const char* what() const IRIS_UTFLIB_CPP_NOEXCEPT IRIS_UTFLIB_CPP_OVERRIDE { return "Not enough space"; } - }; - - /// The library API - functions intended to be called by the users - - template - octet_iterator append(utfchar32_t cp, octet_iterator result) - { - if (!utflib::internal::is_code_point_valid(cp)) - throw invalid_code_point(cp); - - return internal::append(cp, result); - } - - inline void append(utfchar32_t cp, std::string& s) - { - append(cp, std::back_inserter(s)); - } - - template - word_iterator append16(utfchar32_t cp, word_iterator result) - { - if (!utflib::internal::is_code_point_valid(cp)) - throw invalid_code_point(cp); - - return internal::append16(cp, result); - } - - template - output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out, utfchar32_t replacement) - { - while (start != end) { - octet_iterator sequence_start = start; - internal::utf_error err_code = utflib::internal::validate_next(start, end); - switch (err_code) { - case internal::UTF8_OK : - for (octet_iterator it = sequence_start; it != start; ++it) - *out++ = *it; - break; - case internal::NOT_ENOUGH_ROOM: - out = utflib::append (replacement, out); - start = end; - break; - case internal::INVALID_LEAD: - out = utflib::append (replacement, out); - ++start; - break; - case internal::INCOMPLETE_SEQUENCE: - case internal::OVERLONG_SEQUENCE: - case internal::INVALID_CODE_POINT: - out = utflib::append (replacement, out); - ++start; - // just one replacement mark for the sequence - while (start != end && utflib::internal::is_trail(*start)) - ++start; - break; - } - } - return out; - } - - template - inline output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out) - { - static const utfchar32_t replacement_marker = static_cast(utflib::internal::mask16(0xfffd)); - return utflib::replace_invalid(start, end, out, replacement_marker); - } - - inline std::string replace_invalid(const std::string& s, utfchar32_t replacement) - { - std::string result; - replace_invalid(s.begin(), s.end(), std::back_inserter(result), replacement); - return result; - } - - inline std::string replace_invalid(const std::string& s) - { - std::string result; - replace_invalid(s.begin(), s.end(), std::back_inserter(result)); - return result; - } - - template - utfchar32_t next(octet_iterator& it, octet_iterator end) - { - utfchar32_t cp = 0; - internal::utf_error err_code = utflib::internal::validate_next(it, end, cp); - switch (err_code) { - case internal::UTF8_OK : - break; - case internal::NOT_ENOUGH_ROOM : - throw not_enough_room(); - case internal::INVALID_LEAD : - case internal::INCOMPLETE_SEQUENCE : - case internal::OVERLONG_SEQUENCE : - throw invalid_utf8(static_cast(*it)); - case internal::INVALID_CODE_POINT : - throw invalid_code_point(cp); - } - return cp; - } - - template - utfchar32_t next16(word_iterator& it, word_iterator end) - { - utfchar32_t cp = 0; - internal::utf_error err_code = utflib::internal::validate_next16(it, end, cp); - if (err_code == internal::NOT_ENOUGH_ROOM) - throw not_enough_room(); - return cp; - } - - template - utfchar32_t peek_next(octet_iterator it, octet_iterator end) - { - return utflib::next(it, end); - } - - template - utfchar32_t prior(octet_iterator& it, octet_iterator start) - { - // can't do much if it == start - if (it == start) - throw not_enough_room(); - - octet_iterator end = it; - // Go back until we hit either a lead octet or start - while (utflib::internal::is_trail(*(--it))) - if (it == start) - throw invalid_utf8(*it); // error - no lead byte in the sequence - return utflib::peek_next(it, end); - } - - template - void advance (octet_iterator& it, distance_type n, octet_iterator end) - { - const distance_type zero(0); - if (n < zero) { - // backward - for (distance_type i = n; i < zero; ++i) - utflib::prior(it, end); - } else { - // forward - for (distance_type i = zero; i < n; ++i) - utflib::next(it, end); - } - } - - template - typename std::iterator_traits::difference_type - distance (octet_iterator first, octet_iterator last) - { - typename std::iterator_traits::difference_type dist; - for (dist = 0; first < last; ++dist) - utflib::next(first, last); - return dist; - } - - template - octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result) - { - while (start != end) { - utfchar32_t cp = static_cast(utflib::internal::mask16(*start++)); - // Take care of surrogate pairs first - if (utflib::internal::is_lead_surrogate(cp)) { - if (start != end) { - const utfchar32_t trail_surrogate = static_cast(utflib::internal::mask16(*start++)); - if (utflib::internal::is_trail_surrogate(trail_surrogate)) - cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET; - else - throw invalid_utf16(static_cast(trail_surrogate)); - } - else - throw invalid_utf16(static_cast(cp)); - - } - // Lone trail surrogate - else if (utflib::internal::is_trail_surrogate(cp)) - throw invalid_utf16(static_cast(cp)); - - result = utflib::append(cp, result); - } - return result; - } - - template - u16bit_iterator utf8to16 (octet_iterator start, octet_iterator end, u16bit_iterator result) - { - while (start < end) { - const utfchar32_t cp = utflib::next(start, end); - if (cp > 0xffff) { //make a surrogate pair - *result++ = static_cast((cp >> 10) + internal::LEAD_OFFSET); - *result++ = static_cast((cp & 0x3ff) + internal::TRAIL_SURROGATE_MIN); - } - else - *result++ = static_cast(cp); - } - return result; - } - - template - octet_iterator utf32to8 (u32bit_iterator start, u32bit_iterator end, octet_iterator result) - { - while (start != end) - result = utflib::append(*(start++), result); - - return result; - } - - template - u32bit_iterator utf8to32 (octet_iterator start, octet_iterator end, u32bit_iterator result) - { - while (start < end) - (*result++) = utflib::next(start, end); - - return result; - } - - // The iterator class - template - class iterator { - octet_iterator it; - octet_iterator range_start; - octet_iterator range_end; - public: - typedef utfchar32_t value_type; - typedef utfchar32_t* pointer; - typedef utfchar32_t& reference; - typedef std::ptrdiff_t difference_type; - typedef std::bidirectional_iterator_tag iterator_category; - iterator () {} - explicit iterator (const octet_iterator& octet_it, - const octet_iterator& rangestart, - const octet_iterator& rangeend) : - it(octet_it), range_start(rangestart), range_end(rangeend) - { - if (it < range_start || it > range_end) - throw std::out_of_range("Invalid utf-8 iterator position"); - } - // the default "big three" are OK - octet_iterator base () const { return it; } - utfchar32_t operator * () const - { - octet_iterator temp = it; - return utflib::next(temp, range_end); - } - bool operator == (const iterator& rhs) const - { - if (range_start != rhs.range_start || range_end != rhs.range_end) - throw std::logic_error("Comparing utf-8 iterators defined with different ranges"); - return (it == rhs.it); - } - bool operator != (const iterator& rhs) const - { - return !(operator == (rhs)); - } - iterator& operator ++ () - { - utflib::next(it, range_end); - return *this; - } - iterator operator ++ (int) - { - iterator temp = *this; - utflib::next(it, range_end); - return temp; - } - iterator& operator -- () - { - utflib::prior(it, range_start); - return *this; - } - iterator operator -- (int) - { - iterator temp = *this; - utflib::prior(it, range_start); - return temp; - } - }; // class iterator - -} // namespace iris::utflib - -#if IRIS_UTFLIB_CPP_CPLUSPLUS >= 202002L // C++ 20 or later -#include "cpp20.h" -#elif IRIS_UTFLIB_CPP_CPLUSPLUS >= 201703L // C++ 17 or later -#include "cpp17.h" -#elif IRIS_UTFLIB_CPP_CPLUSPLUS >= 201103L // C++ 11 or later -#include "cpp11.h" -#endif // C++ 11 or later - -#endif //header guard - diff --git a/source/utf8/core.h b/source/utf8/core.h deleted file mode 100644 index f75b57c..0000000 --- a/source/utf8/core.h +++ /dev/null @@ -1,501 +0,0 @@ -// Copyright 2006 Nemanja Trifunovic -// Copyright 2026 The Iris Project Contributors - -/* -Permission is hereby granted, free of charge, to any person or organization -obtaining a copy of the software and accompanying documentation covered by -this license (the "Software") to use, reproduce, display, distribute, -execute, and transmit the Software, and to prepare derivative works of the -Software, and to permit third-parties to whom the Software is furnished to -do so, all subject to the following: - -The copyright notices in the Software and this entire statement, including -the above license grant, this restriction and the following disclaimer, -must be included in all copies of the Software, in whole or in part, and -all derivative works of the Software, unless such copies or derivative -works are solely in the form of machine-executable object code generated by -a source language processor. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT -SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE -FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, -ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -DEALINGS IN THE SOFTWARE. -*/ - - -#ifndef IRIS_UTFLIB_UTF8_CORE_H -#define IRIS_UTFLIB_UTF8_CORE_H - -#include -#include -#include - -// Determine the C++ standard version. -// If the user defines IRIS_UTFLIB_CPP_CPLUSPLUS, use that. -// Otherwise, trust the unreliable predefined macro __cplusplus - -#if !defined IRIS_UTFLIB_CPP_CPLUSPLUS - #define IRIS_UTFLIB_CPP_CPLUSPLUS __cplusplus -#endif - -#if IRIS_UTFLIB_CPP_CPLUSPLUS >= 201103L // C++ 11 or later - #define IRIS_UTFLIB_CPP_OVERRIDE override - #define IRIS_UTFLIB_CPP_NOEXCEPT noexcept - #define IRIS_UTFLIB_CPP_STATIC_ASSERT(condition) static_assert(condition, "UTFCPP static assert"); -#else // C++ 98/03 - #define IRIS_UTFLIB_CPP_OVERRIDE - #define IRIS_UTFLIB_CPP_NOEXCEPT throw() - // Not worth simulating static_assert: - #define IRIS_UTFLIB_CPP_STATIC_ASSERT(condition) (void)(condition); -#endif // C++ 11 or later - - -namespace iris::utflib -{ -// The typedefs for 8-bit, 16-bit and 32-bit code units -#if IRIS_UTFLIB_CPP_CPLUSPLUS >= 201103L // C++ 11 or later - #if IRIS_UTFLIB_CPP_CPLUSPLUS >= 202002L // C++ 20 or later - typedef char8_t utfchar8_t; - #else // C++ 11/14/17 - typedef unsigned char utfchar8_t; - #endif - typedef char16_t utfchar16_t; - typedef char32_t utfchar32_t; -#else // C++ 98/03 - typedef unsigned char utfchar8_t; - typedef unsigned short utfchar16_t; - typedef unsigned int utfchar32_t; -#endif // C++ 11 or later - -// Helper code - not intended to be directly called by the library users. May be changed at any time -namespace internal -{ - // Unicode constants - // Leading (high) surrogates: 0xd800 - 0xdbff - // Trailing (low) surrogates: 0xdc00 - 0xdfff - const utfchar16_t LEAD_SURROGATE_MIN = 0xd800u; - const utfchar16_t LEAD_SURROGATE_MAX = 0xdbffu; - const utfchar16_t TRAIL_SURROGATE_MIN = 0xdc00u; - const utfchar16_t TRAIL_SURROGATE_MAX = 0xdfffu; - const utfchar16_t LEAD_OFFSET = 0xd7c0u; // LEAD_SURROGATE_MIN - (0x10000 >> 10) - const utfchar32_t SURROGATE_OFFSET = 0xfca02400u; // 0x10000u - (LEAD_SURROGATE_MIN << 10) - TRAIL_SURROGATE_MIN - - // Maximum valid value for a Unicode code point - const utfchar32_t CODE_POINT_MAX = 0x0010ffffu; - - template - inline utfchar8_t mask8(octet_type oc) - { - return static_cast(0xff & oc); - } - - template - inline utfchar16_t mask16(u16_type oc) - { - return static_cast(0xffff & oc); - } - - template - inline bool is_trail(octet_type oc) - { - return ((utflib::internal::mask8(oc) >> 6) == 0x2); - } - - inline bool is_lead_surrogate(utfchar32_t cp) - { - return (cp >= static_cast(LEAD_SURROGATE_MIN) && cp <= static_cast(LEAD_SURROGATE_MAX)); - } - - inline bool is_trail_surrogate(utfchar32_t cp) - { - return (cp >= static_cast(TRAIL_SURROGATE_MIN) && cp <= static_cast(TRAIL_SURROGATE_MAX)); - } - - inline bool is_surrogate(utfchar32_t cp) - { - return (cp >= static_cast(LEAD_SURROGATE_MIN) && cp <= static_cast(TRAIL_SURROGATE_MAX)); - } - - inline bool is_code_point_valid(utfchar32_t cp) - { - return (cp <= CODE_POINT_MAX && !utflib::internal::is_surrogate(cp)); - } - - inline bool is_in_bmp(utfchar32_t cp) - { - return cp < utfchar32_t(0x10000); - } - - template - int sequence_length(octet_iterator lead_it) - { - const utfchar8_t lead = utflib::internal::mask8(*lead_it); - if (lead < 0x80) - return 1; - else if ((lead >> 5) == 0x6) - return 2; - else if ((lead >> 4) == 0xe) - return 3; - else if ((lead >> 3) == 0x1e) - return 4; - else - return 0; - } - - inline bool is_overlong_sequence(utfchar32_t cp, int length) - { - if (cp < 0x80) { - if (length != 1) - return true; - } - else if (cp < 0x800) { - if (length != 2) - return true; - } - else if (cp < 0x10000) { - if (length != 3) - return true; - } - return false; - } - - enum utf_error {UTF8_OK, NOT_ENOUGH_ROOM, INVALID_LEAD, INCOMPLETE_SEQUENCE, OVERLONG_SEQUENCE, INVALID_CODE_POINT}; - - /// Helper for get_sequence_x - template - utf_error increase_safely(octet_iterator& it, const octet_iterator end) - { - if (++it == end) - return NOT_ENOUGH_ROOM; - - if (!utflib::internal::is_trail(*it)) - return INCOMPLETE_SEQUENCE; - - return UTF8_OK; - } - - #define IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(IT, END) {utf_error ret = increase_safely(IT, END); if (ret != UTF8_OK) return ret;} - - /// get_sequence_x functions decode utf-8 sequences of the length x - template - utf_error get_sequence_1(octet_iterator& it, octet_iterator end, utfchar32_t& code_point) - { - if (it == end) - return NOT_ENOUGH_ROOM; - - code_point = static_cast(utflib::internal::mask8(*it)); - - return UTF8_OK; - } - - template - utf_error get_sequence_2(octet_iterator& it, octet_iterator end, utfchar32_t& code_point) - { - if (it == end) - return NOT_ENOUGH_ROOM; - - code_point = static_cast(utflib::internal::mask8(*it)); - - IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end) - - code_point = ((code_point << 6) & 0x7ff) + ((*it) & 0x3f); - - return UTF8_OK; - } - - template - utf_error get_sequence_3(octet_iterator& it, octet_iterator end, utfchar32_t& code_point) - { - if (it == end) - return NOT_ENOUGH_ROOM; - - code_point = static_cast(utflib::internal::mask8(*it)); - - IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end) - - code_point = ((code_point << 12) & 0xffff) + ((utflib::internal::mask8(*it) << 6) & 0xfff); - - IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end) - - code_point = static_cast(code_point + ((*it) & 0x3f)); - - return UTF8_OK; - } - - template - utf_error get_sequence_4(octet_iterator& it, octet_iterator end, utfchar32_t& code_point) - { - if (it == end) - return NOT_ENOUGH_ROOM; - - code_point = static_cast(utflib::internal::mask8(*it)); - - IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end) - - code_point = ((code_point << 18) & 0x1fffff) + ((utflib::internal::mask8(*it) << 12) & 0x3ffff); - - IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end) - - code_point = static_cast(code_point + ((utflib::internal::mask8(*it) << 6) & 0xfff)); - - IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end) - - code_point = static_cast(code_point + ((*it) & 0x3f)); - - return UTF8_OK; - } - - #undef IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR - - template - utf_error validate_next(octet_iterator& it, octet_iterator end, utfchar32_t& code_point) - { - if (it == end) - return NOT_ENOUGH_ROOM; - - // Save the original value of it so we can go back in case of failure - // Of course, it does not make much sense with i.e. stream iterators - octet_iterator original_it = it; - - utfchar32_t cp = 0; - // Determine the sequence length based on the lead octet - const int length = utflib::internal::sequence_length(it); - - // Get trail octets and calculate the code point - utf_error err = UTF8_OK; - switch (length) { - case 0: - return INVALID_LEAD; - case 1: - err = utflib::internal::get_sequence_1(it, end, cp); - break; - case 2: - err = utflib::internal::get_sequence_2(it, end, cp); - break; - case 3: - err = utflib::internal::get_sequence_3(it, end, cp); - break; - case 4: - err = utflib::internal::get_sequence_4(it, end, cp); - break; - } - - if (err == UTF8_OK) { - // Decoding succeeded. Now, security checks... - if (utflib::internal::is_code_point_valid(cp)) { - if (!utflib::internal::is_overlong_sequence(cp, length)){ - // Passed! Return here. - code_point = cp; - ++it; - return UTF8_OK; - } - else - err = OVERLONG_SEQUENCE; - } - else - err = INVALID_CODE_POINT; - } - - // Failure branch - restore the original value of the iterator - it = original_it; - return err; - } - - template - inline utf_error validate_next(octet_iterator& it, octet_iterator end) { - utfchar32_t ignored; - return utflib::internal::validate_next(it, end, ignored); - } - - template - utf_error validate_next16(word_iterator& it, word_iterator end, utfchar32_t& code_point) - { - // Make sure the iterator dereferences a large enough type - typedef typename std::iterator_traits::value_type word_type; - IRIS_UTFLIB_CPP_STATIC_ASSERT(sizeof(word_type) >= sizeof(utfchar16_t)); - // Check the edge case: - if (it == end) - return NOT_ENOUGH_ROOM; - // Save the original value of it so we can go back in case of failure - // Of course, it does not make much sense with i.e. stream iterators - word_iterator original_it = it; - - utf_error err = UTF8_OK; - - const utfchar16_t first_word = *it++; - if (!is_surrogate(first_word)) { - code_point = first_word; - return UTF8_OK; - } - else { - if (it == end) - err = NOT_ENOUGH_ROOM; - else if (is_lead_surrogate(first_word)) { - const utfchar16_t second_word = *it++; - if (is_trail_surrogate(static_cast(second_word))) { - code_point = static_cast(first_word << 10) + static_cast(second_word) + SURROGATE_OFFSET; - return UTF8_OK; - } else - err = INCOMPLETE_SEQUENCE; - - } else { - err = INVALID_LEAD; - } - } - // error branch - it = original_it; - return err; - } - - // Internal implementation of both checked and unchecked append() function - // This function will be invoked by the overloads below, as they will know - // the octet_type. - template - octet_iterator append(utfchar32_t cp, octet_iterator result) { - if (cp < 0x80) // one octet - *(result++) = static_cast(cp); - else if (cp < 0x800) { // two octets - *(result++) = static_cast((cp >> 6) | 0xc0); - *(result++) = static_cast((cp & 0x3f) | 0x80); - } - else if (cp < 0x10000) { // three octets - *(result++) = static_cast((cp >> 12) | 0xe0); - *(result++) = static_cast(((cp >> 6) & 0x3f) | 0x80); - *(result++) = static_cast((cp & 0x3f) | 0x80); - } - else { // four octets - *(result++) = static_cast((cp >> 18) | 0xf0); - *(result++) = static_cast(((cp >> 12) & 0x3f)| 0x80); - *(result++) = static_cast(((cp >> 6) & 0x3f) | 0x80); - *(result++) = static_cast((cp & 0x3f) | 0x80); - } - return result; - } - - // One of the following overloads will be invoked from the API calls - - // A simple (but dangerous) case: the caller appends byte(s) to a char array - inline char* append(utfchar32_t cp, char* result) { - return append(cp, result); - } - - // Hopefully, most common case: the caller uses back_inserter - // i.e. append(cp, std::back_inserter(str)); - template - std::back_insert_iterator append - (utfchar32_t cp, std::back_insert_iterator result) { - return append, - typename container_type::value_type>(cp, result); - } - - // The caller uses some other kind of output operator - not covered above - // Note that in this case we are not able to determine octet_type - // so we assume it's utfchar8_t; that can cause a conversion warning if we are wrong. - template - octet_iterator append(utfchar32_t cp, octet_iterator result) { - return append(cp, result); - } - - // Internal implementation of both checked and unchecked append16() function - // This function will be invoked by the overloads below, as they will know - // the word_type. - template - word_iterator append16(utfchar32_t cp, word_iterator result) { - IRIS_UTFLIB_CPP_STATIC_ASSERT(sizeof(word_type) >= sizeof(utfchar16_t)); - if (is_in_bmp(cp)) - *(result++) = static_cast(cp); - else { - // Code points from the supplementary planes are encoded via surrogate pairs - *(result++) = static_cast(LEAD_OFFSET + (cp >> 10)); - *(result++) = static_cast(TRAIL_SURROGATE_MIN + (cp & 0x3FF)); - } - return result; - } - - // Hopefully, most common case: the caller uses back_inserter - // i.e. append16(cp, std::back_inserter(str)); - template - std::back_insert_iterator append16 - (utfchar32_t cp, std::back_insert_iterator result) { - return append16, - typename container_type::value_type>(cp, result); - } - - // The caller uses some other kind of output operator - not covered above - // Note that in this case we are not able to determine word_type - // so we assume it's utfchar16_t; that can cause a conversion warning if we are wrong. - template - word_iterator append16(utfchar32_t cp, word_iterator result) { - return append16(cp, result); - } - -} // namespace internal - - /// The library API - functions intended to be called by the users - - // Byte order mark - const utfchar8_t bom[] = {0xef, 0xbb, 0xbf}; - - template - octet_iterator find_invalid(octet_iterator start, octet_iterator end) - { - octet_iterator result = start; - while (result != end) { - utflib::internal::utf_error err_code = utflib::internal::validate_next(result, end); - if (err_code != internal::UTF8_OK) - return result; - } - return result; - } - - inline const char* find_invalid(const char* str) - { - const char* end = str + std::strlen(str); - return find_invalid(str, end); - } - - inline std::size_t find_invalid(const std::string& s) - { - std::string::const_iterator invalid = find_invalid(s.begin(), s.end()); - return (invalid == s.end()) ? std::string::npos : static_cast(invalid - s.begin()); - } - - template - inline bool is_valid(octet_iterator start, octet_iterator end) - { - return (utflib::find_invalid(start, end) == end); - } - - inline bool is_valid(const char* str) - { - return (*(utflib::find_invalid(str)) == '\0'); - } - - inline bool is_valid(const std::string& s) - { - return is_valid(s.begin(), s.end()); - } - - - - template - inline bool starts_with_bom (octet_iterator it, octet_iterator end) - { - return ( - ((it != end) && (utflib::internal::mask8(*it++)) == bom[0]) && - ((it != end) && (utflib::internal::mask8(*it++)) == bom[1]) && - ((it != end) && (utflib::internal::mask8(*it)) == bom[2]) - ); - } - - inline bool starts_with_bom(const std::string& s) - { - return starts_with_bom(s.begin(), s.end()); - } -} // namespace iris::utflib - -#endif // header guard - diff --git a/source/utf8/cpp11.h b/source/utf8/cpp11.h deleted file mode 100644 index 538d818..0000000 --- a/source/utf8/cpp11.h +++ /dev/null @@ -1,71 +0,0 @@ -// Copyright 2018 Nemanja Trifunovic -// Copyright 2026 The Iris Project Contributors - -/* -Permission is hereby granted, free of charge, to any person or organization -obtaining a copy of the software and accompanying documentation covered by -this license (the "Software") to use, reproduce, display, distribute, -execute, and transmit the Software, and to prepare derivative works of the -Software, and to permit third-parties to whom the Software is furnished to -do so, all subject to the following: - -The copyright notices in the Software and this entire statement, including -the above license grant, this restriction and the following disclaimer, -must be included in all copies of the Software, in whole or in part, and -all derivative works of the Software, unless such copies or derivative -works are solely in the form of machine-executable object code generated by -a source language processor. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT -SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE -FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, -ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -DEALINGS IN THE SOFTWARE. -*/ - - -#ifndef IRIS_UTFLIB_UTF8_CPP11_H -#define IRIS_UTFLIB_UTF8_CPP11_H - -#include "checked.h" - -namespace iris::utflib -{ - inline void append16(utfchar32_t cp, std::u16string& s) - { - append16(cp, std::back_inserter(s)); - } - - inline std::string utf16to8(const std::u16string& s) - { - std::string result; - utf16to8(s.begin(), s.end(), std::back_inserter(result)); - return result; - } - - inline std::u16string utf8to16(const std::string& s) - { - std::u16string result; - utf8to16(s.begin(), s.end(), std::back_inserter(result)); - return result; - } - - inline std::string utf32to8(const std::u32string& s) - { - std::string result; - utf32to8(s.begin(), s.end(), std::back_inserter(result)); - return result; - } - - inline std::u32string utf8to32(const std::string& s) - { - std::u32string result; - utf8to32(s.begin(), s.end(), std::back_inserter(result)); - return result; - } -} // namespace iris::utflib - -#endif // header guard - diff --git a/source/utf8/cpp17.h b/source/utf8/cpp17.h deleted file mode 100644 index f357d02..0000000 --- a/source/utf8/cpp17.h +++ /dev/null @@ -1,97 +0,0 @@ -// Copyright 2018 Nemanja Trifunovic -// Copyright 2026 The Iris Project Contributors - -/* -Permission is hereby granted, free of charge, to any person or organization -obtaining a copy of the software and accompanying documentation covered by -this license (the "Software") to use, reproduce, display, distribute, -execute, and transmit the Software, and to prepare derivative works of the -Software, and to permit third-parties to whom the Software is furnished to -do so, all subject to the following: - -The copyright notices in the Software and this entire statement, including -the above license grant, this restriction and the following disclaimer, -must be included in all copies of the Software, in whole or in part, and -all derivative works of the Software, unless such copies or derivative -works are solely in the form of machine-executable object code generated by -a source language processor. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT -SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE -FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, -ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -DEALINGS IN THE SOFTWARE. -*/ - - -#ifndef IRIS_UTFLIB_UTF8_CPP17_H -#define IRIS_UTFLIB_UTF8_CPP17_H - -#include "cpp11.h" - -namespace iris::utflib -{ - inline std::string utf16to8(std::u16string_view s) - { - std::string result; - utf16to8(s.begin(), s.end(), std::back_inserter(result)); - return result; - } - - inline std::u16string utf8to16(std::string_view s) - { - std::u16string result; - utf8to16(s.begin(), s.end(), std::back_inserter(result)); - return result; - } - - inline std::string utf32to8(std::u32string_view s) - { - std::string result; - utf32to8(s.begin(), s.end(), std::back_inserter(result)); - return result; - } - - inline std::u32string utf8to32(std::string_view s) - { - std::u32string result; - utf8to32(s.begin(), s.end(), std::back_inserter(result)); - return result; - } - - inline std::size_t find_invalid(std::string_view s) - { - std::string_view::const_iterator invalid = find_invalid(s.begin(), s.end()); - return (invalid == s.end()) ? std::string_view::npos : static_cast(invalid - s.begin()); - } - - inline bool is_valid(std::string_view s) - { - return is_valid(s.begin(), s.end()); - } - - inline std::string replace_invalid(std::string_view s, char32_t replacement) - { - std::string result; - replace_invalid(s.begin(), s.end(), std::back_inserter(result), replacement); - return result; - } - - inline std::string replace_invalid(std::string_view s) - { - std::string result; - replace_invalid(s.begin(), s.end(), std::back_inserter(result)); - return result; - } - - inline bool starts_with_bom(std::string_view s) - { - return starts_with_bom(s.begin(), s.end()); - } - -} // namespace iris::utflib - -#endif // header guard - diff --git a/source/utf8/cpp20.h b/source/utf8/cpp20.h deleted file mode 100644 index c45f46a..0000000 --- a/source/utf8/cpp20.h +++ /dev/null @@ -1,125 +0,0 @@ -// Copyright 2022 Nemanja Trifunovic -// Copyright 2026 The Iris Project Contributors - -/* -Permission is hereby granted, free of charge, to any person or organization -obtaining a copy of the software and accompanying documentation covered by -this license (the "Software") to use, reproduce, display, distribute, -execute, and transmit the Software, and to prepare derivative works of the -Software, and to permit third-parties to whom the Software is furnished to -do so, all subject to the following: - -The copyright notices in the Software and this entire statement, including -the above license grant, this restriction and the following disclaimer, -must be included in all copies of the Software, in whole or in part, and -all derivative works of the Software, unless such copies or derivative -works are solely in the form of machine-executable object code generated by -a source language processor. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT -SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE -FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, -ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -DEALINGS IN THE SOFTWARE. -*/ - - -#ifndef IRIS_UTFLIB_UTF8_CPP20_H -#define IRIS_UTFLIB_UTF8_CPP20_H - -#include "cpp17.h" - -namespace iris::utflib -{ - inline std::u8string utf16tou8(const std::u16string& s) - { - std::u8string result; - utf16to8(s.begin(), s.end(), std::back_inserter(result)); - return result; - } - - inline std::u8string utf16tou8(std::u16string_view s) - { - std::u8string result; - utf16to8(s.begin(), s.end(), std::back_inserter(result)); - return result; - } - - inline std::u16string utf8to16(const std::u8string& s) - { - std::u16string result; - utf8to16(s.begin(), s.end(), std::back_inserter(result)); - return result; - } - - inline std::u16string utf8to16(const std::u8string_view& s) - { - std::u16string result; - utf8to16(s.begin(), s.end(), std::back_inserter(result)); - return result; - } - - inline std::u8string utf32tou8(const std::u32string& s) - { - std::u8string result; - utf32to8(s.begin(), s.end(), std::back_inserter(result)); - return result; - } - - inline std::u8string utf32tou8(const std::u32string_view& s) - { - std::u8string result; - utf32to8(s.begin(), s.end(), std::back_inserter(result)); - return result; - } - - inline std::u32string utf8to32(const std::u8string& s) - { - std::u32string result; - utf8to32(s.begin(), s.end(), std::back_inserter(result)); - return result; - } - - inline std::u32string utf8to32(const std::u8string_view& s) - { - std::u32string result; - utf8to32(s.begin(), s.end(), std::back_inserter(result)); - return result; - } - - inline std::size_t find_invalid(const std::u8string& s) - { - std::u8string::const_iterator invalid = find_invalid(s.begin(), s.end()); - return (invalid == s.end()) ? std::string_view::npos : static_cast(invalid - s.begin()); - } - - inline bool is_valid(const std::u8string& s) - { - return is_valid(s.begin(), s.end()); - } - - inline std::u8string replace_invalid(const std::u8string& s, char32_t replacement) - { - std::u8string result; - replace_invalid(s.begin(), s.end(), std::back_inserter(result), replacement); - return result; - } - - inline std::u8string replace_invalid(const std::u8string& s) - { - std::u8string result; - replace_invalid(s.begin(), s.end(), std::back_inserter(result)); - return result; - } - - inline bool starts_with_bom(const std::u8string& s) - { - return starts_with_bom(s.begin(), s.end()); - } - -} // namespace iris::utflib - -#endif // header guard - diff --git a/source/utf8/unchecked.h b/source/utf8/unchecked.h deleted file mode 100644 index 1955acb..0000000 --- a/source/utf8/unchecked.h +++ /dev/null @@ -1,287 +0,0 @@ -// Copyright 2006 Nemanja Trifunovic -// Copyright 2026 The Iris Project Contributors - -/* -Permission is hereby granted, free of charge, to any person or organization -obtaining a copy of the software and accompanying documentation covered by -this license (the "Software") to use, reproduce, display, distribute, -execute, and transmit the Software, and to prepare derivative works of the -Software, and to permit third-parties to whom the Software is furnished to -do so, all subject to the following: - -The copyright notices in the Software and this entire statement, including -the above license grant, this restriction and the following disclaimer, -must be included in all copies of the Software, in whole or in part, and -all derivative works of the Software, unless such copies or derivative -works are solely in the form of machine-executable object code generated by -a source language processor. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT -SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE -FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, -ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -DEALINGS IN THE SOFTWARE. -*/ - - -#ifndef IRIS_UTFLIB_UTF8_UNCHECKED_H -#define IRIS_UTFLIB_UTF8_UNCHECKED_H - -#include "core.h" - -namespace iris::utflib -{ - namespace unchecked - { - template - octet_iterator append(utfchar32_t cp, octet_iterator result) - { - return internal::append(cp, result); - } - - template - word_iterator append16(utfchar32_t cp, word_iterator result) - { - return internal::append16(cp, result); - } - - template - output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out, utfchar32_t replacement) - { - while (start != end) { - octet_iterator sequence_start = start; - internal::utf_error err_code = utflib::internal::validate_next(start, end); - switch (err_code) { - case internal::UTF8_OK : - for (octet_iterator it = sequence_start; it != start; ++it) - *out++ = *it; - break; - case internal::NOT_ENOUGH_ROOM: - out = utflib::unchecked::append(replacement, out); - start = end; - break; - case internal::INVALID_LEAD: - out = utflib::unchecked::append(replacement, out); - ++start; - break; - case internal::INCOMPLETE_SEQUENCE: - case internal::OVERLONG_SEQUENCE: - case internal::INVALID_CODE_POINT: - out = utflib::unchecked::append(replacement, out); - ++start; - // just one replacement mark for the sequence - while (start != end && utflib::internal::is_trail(*start)) - ++start; - break; - } - } - return out; - } - - template - inline output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out) - { - static const utfchar32_t replacement_marker = static_cast(utflib::internal::mask16(0xfffd)); - return utflib::unchecked::replace_invalid(start, end, out, replacement_marker); - } - - inline std::string replace_invalid(const std::string& s, utfchar32_t replacement) - { - std::string result; - replace_invalid(s.begin(), s.end(), std::back_inserter(result), replacement); - return result; - } - - inline std::string replace_invalid(const std::string& s) - { - std::string result; - replace_invalid(s.begin(), s.end(), std::back_inserter(result)); - return result; - } - - template - utfchar32_t next(octet_iterator& it) - { - utfchar32_t cp = utflib::internal::mask8(*it); - switch (utflib::internal::sequence_length(it)) { - case 1: - break; - case 2: - ++it; - cp = ((cp << 6) & 0x7ff) + ((*it) & 0x3f); - break; - case 3: - ++it; - cp = ((cp << 12) & 0xffff) + ((utflib::internal::mask8(*it) << 6) & 0xfff); - ++it; - cp = static_cast(cp + ((*it) & 0x3f)); - break; - case 4: - ++it; - cp = ((cp << 18) & 0x1fffff) + ((utflib::internal::mask8(*it) << 12) & 0x3ffff); - ++it; - cp = static_cast(cp + ((utflib::internal::mask8(*it) << 6) & 0xfff)); - ++it; - cp = static_cast(cp + ((*it) & 0x3f)); - break; - } - ++it; - return cp; - } - - template - utfchar32_t peek_next(octet_iterator it) - { - return utflib::unchecked::next(it); - } - - template - utfchar32_t next16(word_iterator& it) - { - utfchar32_t cp = utflib::internal::mask16(*it++); - if (utflib::internal::is_lead_surrogate(cp)) - return (cp << 10) + *it++ + utflib::internal::SURROGATE_OFFSET; - return cp; - } - - template - utfchar32_t prior(octet_iterator& it) - { - while (utflib::internal::is_trail(*(--it))) ; - octet_iterator temp = it; - return utflib::unchecked::next(temp); - } - - template - void advance(octet_iterator& it, distance_type n) - { - const distance_type zero(0); - if (n < zero) { - // backward - for (distance_type i = n; i < zero; ++i) - utflib::unchecked::prior(it); - } else { - // forward - for (distance_type i = zero; i < n; ++i) - utflib::unchecked::next(it); - } - } - - template - typename std::iterator_traits::difference_type - distance(octet_iterator first, octet_iterator last) - { - typename std::iterator_traits::difference_type dist; - for (dist = 0; first < last; ++dist) - utflib::unchecked::next(first); - return dist; - } - - template - octet_iterator utf16to8(u16bit_iterator start, u16bit_iterator end, octet_iterator result) - { - while (start != end) { - utfchar32_t cp = utflib::internal::mask16(*start++); - // Take care of surrogate pairs first - if (utflib::internal::is_lead_surrogate(cp)) { - if (start == end) - return result; - utfchar32_t trail_surrogate = utflib::internal::mask16(*start++); - cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET; - } - result = utflib::unchecked::append(cp, result); - } - return result; - } - - template - u16bit_iterator utf8to16(octet_iterator start, octet_iterator end, u16bit_iterator result) - { - while (start < end) { - utfchar32_t cp = utflib::unchecked::next(start); - if (cp > 0xffff) { //make a surrogate pair - *result++ = static_cast((cp >> 10) + internal::LEAD_OFFSET); - *result++ = static_cast((cp & 0x3ff) + internal::TRAIL_SURROGATE_MIN); - } - else - *result++ = static_cast(cp); - } - return result; - } - - template - octet_iterator utf32to8(u32bit_iterator start, u32bit_iterator end, octet_iterator result) - { - while (start != end) - result = utflib::unchecked::append(*(start++), result); - - return result; - } - - template - u32bit_iterator utf8to32(octet_iterator start, octet_iterator end, u32bit_iterator result) - { - while (start < end) - (*result++) = utflib::unchecked::next(start); - - return result; - } - - // The iterator class - template - class iterator { - octet_iterator it; - public: - typedef utfchar32_t value_type; - typedef utfchar32_t* pointer; - typedef utfchar32_t& reference; - typedef std::ptrdiff_t difference_type; - typedef std::bidirectional_iterator_tag iterator_category; - iterator () {} - explicit iterator (const octet_iterator& octet_it): it(octet_it) {} - // the default "big three" are OK - octet_iterator base () const { return it; } - utfchar32_t operator * () const - { - octet_iterator temp = it; - return utflib::unchecked::next(temp); - } - bool operator == (const iterator& rhs) const - { - return (it == rhs.it); - } - bool operator != (const iterator& rhs) const - { - return !(operator == (rhs)); - } - iterator& operator ++ () - { - ::std::advance(it, utflib::internal::sequence_length(it)); - return *this; - } - iterator operator ++ (int) - { - iterator temp = *this; - ::std::advance(it, utflib::internal::sequence_length(it)); - return temp; - } - iterator& operator -- () - { - utflib::unchecked::prior(it); - return *this; - } - iterator operator -- (int) - { - iterator temp = *this; - utflib::unchecked::prior(it); - return temp; - } - }; // class iterator - - } // namespace unchecked -} // namespace iris::utflib - -#endif // header guard - diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 55822a2..a84047d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,5 +1,7 @@ include(CTest) +enable_testing() + if (MSVC) # warning level 4 add_compile_options(/W4) @@ -14,45 +16,15 @@ add_executable(cpp17 test_cpp17.cpp) add_executable(cpp20 test_cpp20.cpp) add_executable(apitests apitests.cpp) -add_executable(noexceptionstests noexceptionstests.cpp) - -target_compile_options(noexceptionstests PUBLIC -fno-exceptions) - -set_target_properties(negative apitests noexceptionstests - PROPERTIES - CXX_STANDARD 98 - CXX_STANDARD_REQUIRED YES - CXX_EXTENSIONS NO) - -set_target_properties(cpp11 - PROPERTIES - CXX_STANDARD 11 - CXX_STANDARD_REQUIRED YES - CXX_EXTENSIONS NO) - -set_target_properties(cpp17 - PROPERTIES - CXX_STANDARD 17 - CXX_STANDARD_REQUIRED YES - CXX_EXTENSIONS NO) - -set_target_properties(cpp20 - PROPERTIES - CXX_STANDARD 20 - CXX_STANDARD_REQUIRED YES - CXX_EXTENSIONS NO) - target_link_libraries(negative PRIVATE iris_utf8cpp) target_link_libraries(cpp11 PRIVATE iris_utf8cpp) target_link_libraries(cpp17 PRIVATE iris_utf8cpp) target_link_libraries(cpp20 PRIVATE iris_utf8cpp) target_link_libraries(apitests PRIVATE iris_utf8cpp) -target_link_libraries(noexceptionstests PRIVATE iris_utf8cpp) add_test(negative_test negative ${CMAKE_CURRENT_LIST_DIR}/test_data/utf8_invalid.txt) add_test(cpp11_test cpp11) add_test(cpp17_test cpp17) add_test(cpp20_test cpp20) add_test(api_test apitests) -add_test(noexceptions_test noexceptionstests) diff --git a/tests/apitests.cpp b/tests/apitests.cpp index adc4776..fbc7b13 100644 --- a/tests/apitests.cpp +++ b/tests/apitests.cpp @@ -1,8 +1,257 @@ #include "ftest.h" +#include "utf8.h" + +#include + #include -#include "test_checked_api.h" -#include "test_checked_iterator.h" -#include "test_unchecked_api.h" -#include "test_unchecked_iterator.h" +using namespace iris::utflib; +using namespace std; + +TEST(CheckedAPITests, test_append) +{ + unsigned char u[5] = {0,0,0,0,0}; + append(0x0448, u); + EXPECT_EQ (u[0], 0xd1); + EXPECT_EQ (u[1], 0x88); + EXPECT_EQ (u[2], 0); + EXPECT_EQ (u[3], 0); + EXPECT_EQ (u[4], 0); + + append(0x65e5, u); + EXPECT_EQ (u[0], 0xe6); + EXPECT_EQ (u[1], 0x97); + EXPECT_EQ (u[2], 0xa5); + EXPECT_EQ (u[3], 0); + EXPECT_EQ (u[4], 0); + + append(0x3044, u); + EXPECT_EQ (u[0], 0xe3); + EXPECT_EQ (u[1], 0x81); + EXPECT_EQ (u[2], 0x84); + EXPECT_EQ (u[3], 0); + EXPECT_EQ (u[4], 0); + + append(0x10346, u); + EXPECT_EQ (u[0], 0xf0); + EXPECT_EQ (u[1], 0x90); + EXPECT_EQ (u[2], 0x8d); + EXPECT_EQ (u[3], 0x86); + EXPECT_EQ (u[4], 0); + + // Ensure no warnings with plain char + char c[2] = {0,0}; + append('a', c); + EXPECT_EQ (c[0], 'a'); + EXPECT_EQ (c[1], 0); +} + +TEST(CheckedAPITests, test_append16) +{ + utfchar16_t u[5] = {0,0}; + append16(0x0448, u); + EXPECT_EQ (u[0], 0x0448); + EXPECT_EQ (u[1], 0x0000); + + append16(0x65e5, u); + EXPECT_EQ (u[0], 0x65e5); + EXPECT_EQ (u[1], 0x0000); + + append16(0x10346, u); + EXPECT_EQ (u[0], 0xd800); + EXPECT_EQ (u[1], 0xdf46); +} + +TEST(CheckedAPITests, test_next) +{ + const char* twochars = "\xe6\x97\xa5\xd1\x88"; + const char* w = twochars; + unsigned int cp = next(w, twochars + 6); + EXPECT_EQ (cp, 0x65e5); + EXPECT_EQ (w, twochars + 3); + + const char* threechars = "\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88"; + w = threechars; + + cp = next(w, threechars + 9); + EXPECT_EQ (cp, 0x10346); + EXPECT_EQ (w, threechars + 4); + + cp = next(w, threechars + 9); + EXPECT_EQ (cp, 0x65e5); + EXPECT_EQ (w, threechars + 7); + + cp = next(w, threechars + 9); + EXPECT_EQ (cp, 0x0448); + EXPECT_EQ (w, threechars + 9); +} + +TEST(CheckedAPITests, test_next16) +{ + const utfchar16_t u[3] = {0x65e5, 0xd800, 0xdf46}; + const utfchar16_t* w = u; + iris::utflib::utfchar32_t cp = next16(w, w + 3); + EXPECT_EQ (cp, 0x65e5); + EXPECT_EQ (w, u + 1); + + cp = next16(w, w + 2); + EXPECT_EQ (cp, 0x10346); + EXPECT_EQ (w, u + 3); +} + +TEST(CheckedAPITests, test_peek_next) +{ + const char* const cw = "\xe6\x97\xa5\xd1\x88"; + unsigned int cp = peek_next(cw, cw + 6); + EXPECT_EQ (cp, 0x65e5); +} + +TEST(CheckedAPITests, test_prior) +{ + const char* twochars = "\xe6\x97\xa5\xd1\x88"; + const char* w = twochars + 3; + unsigned int cp = prior (w, twochars); + EXPECT_EQ (cp, 0x65e5); + EXPECT_EQ (w, twochars); + + const char* threechars = "\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88"; + w = threechars + 9; + cp = prior(w, threechars); + EXPECT_EQ (cp, 0x0448); + EXPECT_EQ (w, threechars + 7); + cp = prior(w, threechars); + EXPECT_EQ (cp, 0x65e5); + EXPECT_EQ (w, threechars + 4); + cp = prior(w, threechars); + EXPECT_EQ (cp, 0x10346); + EXPECT_EQ (w, threechars); +} + +TEST(CheckedAPITests, test_advance) +{ + const char* threechars = "\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88"; + const char* w = threechars; + advance(w, 2, threechars + 9); + EXPECT_EQ(w, threechars + 7); + advance(w, -2, threechars); + EXPECT_EQ(w, threechars); + advance(w, 3, threechars + 9); + EXPECT_EQ(w, threechars + 9); + advance(w, -2, threechars); + EXPECT_EQ(w, threechars + 4); + advance(w, -1, threechars); + EXPECT_EQ(w, threechars); +} + +TEST(CheckedAPITests, test_distance) +{ + const char* twochars = "\xe6\x97\xa5\xd1\x88"; + size_t dist = static_cast(iris::utflib::distance(twochars, twochars + 5)); + EXPECT_EQ (dist, 2); +} + +TEST(CheckedAPITests, test_utf32to8) +{ + unsigned int utf32string[] = {0x448, 0x65E5, 0x10346, 0}; + string utf8result; + utf32to8(utf32string, utf32string + 3, back_inserter(utf8result)); + EXPECT_EQ (utf8result.size(), 9); +} + +TEST(CheckedAPITests, test_utf8to32) +{ + const char* twochars = "\xe6\x97\xa5\xd1\x88"; + vector utf32result; + utf8to32(twochars, twochars + 5, back_inserter(utf32result)); + EXPECT_EQ (utf32result.size(), 2); +} + +TEST(CheckedAPITests, test_utf16to8) +{ + unsigned short utf16string[] = {0x41, 0x0448, 0x65e5, 0xd834, 0xdd1e}; + string utf8result; + utf16to8(utf16string, utf16string + 5, back_inserter(utf8result)); + EXPECT_EQ (utf8result.size(), 10); +} + +TEST(CheckedAPITests, test_utf8to16) +{ + char utf8_with_surrogates[] = "\xe6\x97\xa5\xd1\x88\xf0\x9d\x84\x9e"; + vector utf16result; + utf8to16(utf8_with_surrogates, utf8_with_surrogates + 9, back_inserter(utf16result)); + EXPECT_EQ (utf16result.size(), 4); + EXPECT_EQ (utf16result[2], 0xd834); + EXPECT_EQ (utf16result[3], 0xdd1e); +} + +TEST(CheckedAPITests, test_replace_invalid) +{ + char invalid_sequence[] = "a\x80\xe0\xa0\xc0\xaf\xed\xa0\x80z"; + vector replace_invalid_result; + replace_invalid (invalid_sequence, invalid_sequence + sizeof(invalid_sequence), std::back_inserter(replace_invalid_result), '?'); + bool bvalid = is_valid(replace_invalid_result.begin(), replace_invalid_result.end()); + EXPECT_TRUE (bvalid); + const char fixed_invalid_sequence[] = "a????z"; + EXPECT_EQ (sizeof(fixed_invalid_sequence), replace_invalid_result.size()); + EXPECT_TRUE (std::equal(replace_invalid_result.begin(), replace_invalid_result.begin() + sizeof(fixed_invalid_sequence), fixed_invalid_sequence)); +} + +TEST(CheckedAPITests, test_find_invalid) +{ + char utf_invalid[] = "\xe6\x97\xa5\xd1\x88\xfa"; + const char* invalid = find_invalid(utf_invalid, utf_invalid + 6); + EXPECT_EQ (invalid, utf_invalid + 5); + invalid = utf_invalid + find_invalid(utf_invalid); + EXPECT_EQ (invalid, utf_invalid + 5); +} + +TEST(CheckedAPITests, test_is_valid) +{ + char utf_invalid[] = "\xe6\x97\xa5\xd1\x88\xfa"; + bool bvalid = is_valid(utf_invalid, utf_invalid + 6); + EXPECT_FALSE (bvalid); + bvalid = is_valid(utf_invalid); + EXPECT_FALSE (bvalid); + char utf8_with_surrogates[] = "\xe6\x97\xa5\xd1\x88\xf0\x9d\x84\x9e"; + bvalid = is_valid(utf8_with_surrogates, utf8_with_surrogates + 9); + EXPECT_TRUE (bvalid); + bvalid = is_valid(utf8_with_surrogates); + EXPECT_TRUE (bvalid); +} + +TEST(CheckedAPITests, test_starts_with_bom) +{ + unsigned char byte_order_mark[] = {0xef, 0xbb, 0xbf}; + bool bbom = starts_with_bom(byte_order_mark, byte_order_mark + sizeof(byte_order_mark)); + EXPECT_TRUE (bbom); + const char* threechars = "\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88"; + bool no_bbom = starts_with_bom(threechars, threechars + sizeof(threechars)); + EXPECT_FALSE (no_bbom); +} + +TEST(CheckedIteratrTests, test_increment) +{ + const char* threechars = "\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88"; + iris::utflib::iterator it(threechars, threechars, threechars + 9); + iris::utflib::iterator it2 = it; + EXPECT_EQ (it2, it); + EXPECT_EQ (*it, 0x10346); + EXPECT_EQ (*(++it), 0x65e5); + EXPECT_EQ ((*it++), 0x65e5); + EXPECT_EQ (*it, 0x0448); + EXPECT_NE (it, it2); + iris::utflib::iterator endit (threechars + 9, threechars, threechars + 9); + EXPECT_EQ (++it, endit); +} + +TEST(CheckedIteratrTests, test_decrement) +{ + const char* threechars = "\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88"; + iris::utflib::iterator it(threechars+9, threechars, threechars + 9); + EXPECT_EQ (*(--it), 0x0448); + EXPECT_EQ ((*it--), 0x0448); + EXPECT_EQ (*it, 0x65e5); + EXPECT_EQ (--it, iris::utflib::iterator(threechars, threechars, threechars + 9)); + EXPECT_EQ (*it, 0x10346); +} diff --git a/tests/negative.cpp b/tests/negative.cpp index d2b90ae..665585b 100644 --- a/tests/negative.cpp +++ b/tests/negative.cpp @@ -1,10 +1,12 @@ #include "utf8.h" + using namespace iris::utflib; #include #include #include #include + using namespace std; const unsigned INVALID_LINES[] = { 75, 76, 83, 84, 85, 93, 102, 103, 105, 106, 107, 108, 109, 110, 114, 115, 116, 117, 124, 125, 130, 135, 140, 145, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 169, 175, 176, 177, 207, 208, 209, 210, 211, 220, 221, 222, 223, 224, 232, 233, 234, 235, 236, 247, 248, 249, 250, 251, 252, 253, 257, 258, 259, 260, 261, 262, 263, 264}; diff --git a/tests/noexceptionstests.cpp b/tests/noexceptionstests.cpp deleted file mode 100644 index 1d83e55..0000000 --- a/tests/noexceptionstests.cpp +++ /dev/null @@ -1,4 +0,0 @@ -#include "ftest.h" - -#include "test_unchecked_api.h" -#include "test_unchecked_iterator.h" diff --git a/tests/test_checked_api.h b/tests/test_checked_api.h deleted file mode 100644 index d3484af..0000000 --- a/tests/test_checked_api.h +++ /dev/null @@ -1,233 +0,0 @@ -#ifndef IRIS_UTFLIB_TEST_CHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731 -#define IRIS_UTFLIB_TEST_CHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731 - -#include "utf8.h" - -#include -#include -using namespace iris::utflib; -using namespace std; - - -TEST(CheckedAPITests, test_append) -{ - unsigned char u[5] = {0,0,0,0,0}; - append(0x0448, u); - EXPECT_EQ (u[0], 0xd1); - EXPECT_EQ (u[1], 0x88); - EXPECT_EQ (u[2], 0); - EXPECT_EQ (u[3], 0); - EXPECT_EQ (u[4], 0); - - append(0x65e5, u); - EXPECT_EQ (u[0], 0xe6); - EXPECT_EQ (u[1], 0x97); - EXPECT_EQ (u[2], 0xa5); - EXPECT_EQ (u[3], 0); - EXPECT_EQ (u[4], 0); - - append(0x3044, u); - EXPECT_EQ (u[0], 0xe3); - EXPECT_EQ (u[1], 0x81); - EXPECT_EQ (u[2], 0x84); - EXPECT_EQ (u[3], 0); - EXPECT_EQ (u[4], 0); - - append(0x10346, u); - EXPECT_EQ (u[0], 0xf0); - EXPECT_EQ (u[1], 0x90); - EXPECT_EQ (u[2], 0x8d); - EXPECT_EQ (u[3], 0x86); - EXPECT_EQ (u[4], 0); - - // Ensure no warnings with plain char - char c[2] = {0,0}; - append('a', c); - EXPECT_EQ (c[0], 'a'); - EXPECT_EQ (c[1], 0); -} - -TEST(CheckedAPITests, test_append16) -{ - utfchar16_t u[5] = {0,0}; - append16(0x0448, u); - EXPECT_EQ (u[0], 0x0448); - EXPECT_EQ (u[1], 0x0000); - - append16(0x65e5, u); - EXPECT_EQ (u[0], 0x65e5); - EXPECT_EQ (u[1], 0x0000); - - append16(0x10346, u); - EXPECT_EQ (u[0], 0xd800); - EXPECT_EQ (u[1], 0xdf46); -} - -TEST(CheckedAPITests, test_next) -{ - const char* twochars = "\xe6\x97\xa5\xd1\x88"; - const char* w = twochars; - unsigned int cp = next(w, twochars + 6); - EXPECT_EQ (cp, 0x65e5); - EXPECT_EQ (w, twochars + 3); - - const char* threechars = "\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88"; - w = threechars; - - cp = next(w, threechars + 9); - EXPECT_EQ (cp, 0x10346); - EXPECT_EQ (w, threechars + 4); - - cp = next(w, threechars + 9); - EXPECT_EQ (cp, 0x65e5); - EXPECT_EQ (w, threechars + 7); - - cp = next(w, threechars + 9); - EXPECT_EQ (cp, 0x0448); - EXPECT_EQ (w, threechars + 9); -} - -TEST(CheckedAPITests, test_next16) -{ - const utfchar16_t u[3] = {0x65e5, 0xd800, 0xdf46}; - const utfchar16_t* w = u; - iris::utflib::utfchar32_t cp = next16(w, w + 3); - EXPECT_EQ (cp, 0x65e5); - EXPECT_EQ (w, u + 1); - - cp = next16(w, w + 2); - EXPECT_EQ (cp, 0x10346); - EXPECT_EQ (w, u + 3); -} - -TEST(CheckedAPITests, test_peek_next) -{ - const char* const cw = "\xe6\x97\xa5\xd1\x88"; - unsigned int cp = peek_next(cw, cw + 6); - EXPECT_EQ (cp, 0x65e5); -} - -TEST(CheckedAPITests, test_prior) -{ - const char* twochars = "\xe6\x97\xa5\xd1\x88"; - const char* w = twochars + 3; - unsigned int cp = prior (w, twochars); - EXPECT_EQ (cp, 0x65e5); - EXPECT_EQ (w, twochars); - - const char* threechars = "\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88"; - w = threechars + 9; - cp = prior(w, threechars); - EXPECT_EQ (cp, 0x0448); - EXPECT_EQ (w, threechars + 7); - cp = prior(w, threechars); - EXPECT_EQ (cp, 0x65e5); - EXPECT_EQ (w, threechars + 4); - cp = prior(w, threechars); - EXPECT_EQ (cp, 0x10346); - EXPECT_EQ (w, threechars); -} - -TEST(CheckedAPITests, test_advance) -{ - const char* threechars = "\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88"; - const char* w = threechars; - advance(w, 2, threechars + 9); - EXPECT_EQ(w, threechars + 7); - advance(w, -2, threechars); - EXPECT_EQ(w, threechars); - advance(w, 3, threechars + 9); - EXPECT_EQ(w, threechars + 9); - advance(w, -2, threechars); - EXPECT_EQ(w, threechars + 4); - advance(w, -1, threechars); - EXPECT_EQ(w, threechars); -} - -TEST(CheckedAPITests, test_distance) -{ - const char* twochars = "\xe6\x97\xa5\xd1\x88"; - size_t dist = static_cast(iris::utflib::distance(twochars, twochars + 5)); - EXPECT_EQ (dist, 2); -} - -TEST(CheckedAPITests, test_utf32to8) -{ - unsigned int utf32string[] = {0x448, 0x65E5, 0x10346, 0}; - string utf8result; - utf32to8(utf32string, utf32string + 3, back_inserter(utf8result)); - EXPECT_EQ (utf8result.size(), 9); -} - -TEST(CheckedAPITests, test_utf8to32) -{ - const char* twochars = "\xe6\x97\xa5\xd1\x88"; - vector utf32result; - utf8to32(twochars, twochars + 5, back_inserter(utf32result)); - EXPECT_EQ (utf32result.size(), 2); -} - -TEST(CheckedAPITests, test_utf16to8) -{ - unsigned short utf16string[] = {0x41, 0x0448, 0x65e5, 0xd834, 0xdd1e}; - string utf8result; - utf16to8(utf16string, utf16string + 5, back_inserter(utf8result)); - EXPECT_EQ (utf8result.size(), 10); -} - -TEST(CheckedAPITests, test_utf8to16) -{ - char utf8_with_surrogates[] = "\xe6\x97\xa5\xd1\x88\xf0\x9d\x84\x9e"; - vector utf16result; - utf8to16(utf8_with_surrogates, utf8_with_surrogates + 9, back_inserter(utf16result)); - EXPECT_EQ (utf16result.size(), 4); - EXPECT_EQ (utf16result[2], 0xd834); - EXPECT_EQ (utf16result[3], 0xdd1e); -} - -TEST(CheckedAPITests, test_replace_invalid) -{ - char invalid_sequence[] = "a\x80\xe0\xa0\xc0\xaf\xed\xa0\x80z"; - vector replace_invalid_result; - replace_invalid (invalid_sequence, invalid_sequence + sizeof(invalid_sequence), std::back_inserter(replace_invalid_result), '?'); - bool bvalid = is_valid(replace_invalid_result.begin(), replace_invalid_result.end()); - EXPECT_TRUE (bvalid); - const char fixed_invalid_sequence[] = "a????z"; - EXPECT_EQ (sizeof(fixed_invalid_sequence), replace_invalid_result.size()); - EXPECT_TRUE (std::equal(replace_invalid_result.begin(), replace_invalid_result.begin() + sizeof(fixed_invalid_sequence), fixed_invalid_sequence)); -} - -TEST(CheckedAPITests, test_find_invalid) -{ - char utf_invalid[] = "\xe6\x97\xa5\xd1\x88\xfa"; - const char* invalid = find_invalid(utf_invalid, utf_invalid + 6); - EXPECT_EQ (invalid, utf_invalid + 5); - invalid = find_invalid(utf_invalid); - EXPECT_EQ (invalid, utf_invalid + 5); -} - -TEST(CheckedAPITests, test_is_valid) -{ - char utf_invalid[] = "\xe6\x97\xa5\xd1\x88\xfa"; - bool bvalid = is_valid(utf_invalid, utf_invalid + 6); - EXPECT_FALSE (bvalid); - bvalid = is_valid(utf_invalid); - EXPECT_FALSE (bvalid); - char utf8_with_surrogates[] = "\xe6\x97\xa5\xd1\x88\xf0\x9d\x84\x9e"; - bvalid = is_valid(utf8_with_surrogates, utf8_with_surrogates + 9); - EXPECT_TRUE (bvalid); - bvalid = is_valid(utf8_with_surrogates); - EXPECT_TRUE (bvalid); -} - -TEST(CheckedAPITests, test_starts_with_bom) -{ - unsigned char byte_order_mark[] = {0xef, 0xbb, 0xbf}; - bool bbom = starts_with_bom(byte_order_mark, byte_order_mark + sizeof(byte_order_mark)); - EXPECT_TRUE (bbom); - const char* threechars = "\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88"; - bool no_bbom = starts_with_bom(threechars, threechars + sizeof(threechars)); - EXPECT_FALSE (no_bbom); -} - -#endif diff --git a/tests/test_checked_iterator.h b/tests/test_checked_iterator.h deleted file mode 100644 index 61ce11b..0000000 --- a/tests/test_checked_iterator.h +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef IRIS_UTFLIB_TEST_CHECKED_ITERATOR_H_2675DCD0_9480_4c0c_B92A_CC14C027B731 -#define IRIS_UTFLIB_TEST_CHECKED_ITERATOR_H_2675DCD0_9480_4c0c_B92A_CC14C027B731 - -#include "utf8.h" - -using namespace iris::utflib; - - -TEST(CheckedIteratrTests, test_increment) -{ - const char* threechars = "\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88"; - iris::utflib::iterator it(threechars, threechars, threechars + 9); - iris::utflib::iterator it2 = it; - EXPECT_EQ (it2, it); - EXPECT_EQ (*it, 0x10346); - EXPECT_EQ (*(++it), 0x65e5); - EXPECT_EQ ((*it++), 0x65e5); - EXPECT_EQ (*it, 0x0448); - EXPECT_NE (it, it2); - iris::utflib::iterator endit (threechars + 9, threechars, threechars + 9); - EXPECT_EQ (++it, endit); -} - -TEST(CheckedIteratrTests, test_decrement) -{ - const char* threechars = "\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88"; - iris::utflib::iterator it(threechars+9, threechars, threechars + 9); - EXPECT_EQ (*(--it), 0x0448); - EXPECT_EQ ((*it--), 0x0448); - EXPECT_EQ (*it, 0x65e5); - EXPECT_EQ (--it, iris::utflib::iterator(threechars, threechars, threechars + 9)); - EXPECT_EQ (*it, 0x10346); -} - -#endif diff --git a/tests/test_cpp11.cpp b/tests/test_cpp11.cpp index 6cbef5f..33e88ff 100644 --- a/tests/test_cpp11.cpp +++ b/tests/test_cpp11.cpp @@ -1,11 +1,12 @@ #include "ftest.h" + #include "utf8.h" + #include + using namespace iris::utflib; using namespace std; -#if __cplusplus >= 201103L // C++ 11 or later - TEST(CPP11APITests, test_append) { string u; @@ -50,11 +51,6 @@ TEST(CPP11APITests, test_utf16to8) u16string utf16string = {0x41, 0x0448, 0x65e5, 0xd834, 0xdd1e}; string u = utf16to8(utf16string); EXPECT_EQ (u.size(), 10); - - u16string h16 = u"h!"; - string h8; - iris::utflib::unchecked::utf16to8(h16.begin(), h16.end(), std::back_inserter(h8)); - EXPECT_EQ (h8, "h!"); } TEST(CPP11APITests, test_utf8to16) @@ -119,4 +115,3 @@ TEST(CPP11APITests, test_starts_with_bom) bool no_bbom = starts_with_bom(threechars); EXPECT_FALSE (no_bbom); } -#endif // C++ 11 or later diff --git a/tests/test_cpp17.cpp b/tests/test_cpp17.cpp index 7e6863a..2d3756c 100644 --- a/tests/test_cpp17.cpp +++ b/tests/test_cpp17.cpp @@ -1,12 +1,12 @@ #include "ftest.h" + #include "utf8.h" + #include + using namespace iris::utflib; using namespace std; -#if __cplusplus >= 201703L // C++ 17 or later - - TEST(CPP17APITests, test_utf16to8) { u16string utf16string = {0x41, 0x0448, 0x65e5, 0xd834, 0xdd1e}; @@ -79,10 +79,8 @@ TEST(CPP17APITests, test_starts_with_bom) TEST(CPP17APITests, string_class_and_literals) { - const char* twochars = u8"ab"; + const char* twochars = "ab"; EXPECT_TRUE (is_valid(twochars)); const string two_chars_string(twochars); EXPECT_TRUE (is_valid(two_chars_string)); } - -#endif // C++ 11 or later diff --git a/tests/test_cpp20.cpp b/tests/test_cpp20.cpp index a3bcc7f..330027d 100644 --- a/tests/test_cpp20.cpp +++ b/tests/test_cpp20.cpp @@ -1,7 +1,9 @@ #include "ftest.h" -#define IRIS_UTFLIB_CPP_CPLUSPLUS 202002L + #include "utf8.h" + #include + using namespace iris::utflib; using namespace std; diff --git a/tests/test_unchecked_api.h b/tests/test_unchecked_api.h deleted file mode 100644 index cf0da56..0000000 --- a/tests/test_unchecked_api.h +++ /dev/null @@ -1,209 +0,0 @@ -#ifndef IRIS_UTFLIB_TEST_UNCHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731 -#define IRIS_UTFLIB_TEST_UNCHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731 - -#include "utf8/unchecked.h" - -#include -#include - -using namespace std; - -TEST(UnCheckedAPITests, test_append) -{ - unsigned char u[5] = {0,0,0,0,0}; - iris::utflib::unchecked::append(0x0448, u); - EXPECT_EQ (u[0], 0xd1); - EXPECT_EQ (u[1], 0x88); - EXPECT_EQ (u[2], 0); - EXPECT_EQ (u[3], 0); - EXPECT_EQ (u[4], 0); - - iris::utflib::unchecked::append(0x65e5, u); - EXPECT_EQ (u[0], 0xe6); - EXPECT_EQ (u[1], 0x97); - EXPECT_EQ (u[2], 0xa5); - EXPECT_EQ (u[3], 0); - EXPECT_EQ (u[4], 0); - - iris::utflib::unchecked::append(0x3044, u); - EXPECT_EQ (u[0], 0xe3); - EXPECT_EQ (u[1], 0x81); - EXPECT_EQ (u[2], 0x84); - EXPECT_EQ (u[3], 0); - EXPECT_EQ (u[4], 0); - - iris::utflib::unchecked::append(0x10346, u); - EXPECT_EQ (u[0], 0xf0); - EXPECT_EQ (u[1], 0x90); - EXPECT_EQ (u[2], 0x8d); - EXPECT_EQ (u[3], 0x86); - EXPECT_EQ (u[4], 0); -} - -TEST(UnCheckedAPITests, test_append16) -{ - unsigned short u[5] = {0,0}; - iris::utflib::unchecked::append16(0x0448, u); - EXPECT_EQ (u[0], 0x0448); - EXPECT_EQ (u[1], 0x0000); - - iris::utflib::unchecked::append16(0x65e5, u); - EXPECT_EQ (u[0], 0x65e5); - EXPECT_EQ (u[1], 0x0000); - - iris::utflib::unchecked::append16(0x10346, u); - EXPECT_EQ (u[0], 0xd800); - EXPECT_EQ (u[1], 0xdf46); -} - -TEST(UnCheckedAPITests, test_next) -{ - const char* twochars = "\xe6\x97\xa5\xd1\x88"; - const char* w = twochars; - unsigned int cp = iris::utflib::unchecked::next(w); - EXPECT_EQ (cp, 0x65e5); - EXPECT_EQ (w, twochars + 3); - - const char* threechars = "\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88"; - w = threechars; - - cp = iris::utflib::unchecked::next(w); - EXPECT_EQ (cp, 0x10346); - EXPECT_EQ (w, threechars + 4); - - cp = iris::utflib::unchecked::next(w); - EXPECT_EQ (cp, 0x65e5); - EXPECT_EQ (w, threechars + 7); - - cp = iris::utflib::unchecked::next(w); - EXPECT_EQ (cp, 0x0448); - EXPECT_EQ (w, threechars + 9); -} - -TEST(UnCheckedAPITests, test_next16) -{ - const iris::utflib::utfchar16_t u[3] = {0x65e5, 0xd800, 0xdf46}; - const iris::utflib::utfchar16_t* w = u; - iris::utflib::utfchar32_t cp = iris::utflib::unchecked::next16(w); - EXPECT_EQ (cp, 0x65e5); - EXPECT_EQ (w, u + 1); - - cp = iris::utflib::unchecked::next16(w); - EXPECT_EQ (cp, 0x10346); - EXPECT_EQ (w, u + 3); -} - -TEST(UnCheckedAPITests, test_peek_next) -{ - const char* const cw = "\xe6\x97\xa5\xd1\x88"; - unsigned int cp = iris::utflib::unchecked::peek_next(cw); - EXPECT_EQ (cp, 0x65e5); -} - -TEST(UnCheckedAPITests, test_prior) -{ - const char* twochars = "\xe6\x97\xa5\xd1\x88"; - const char* w = twochars + 3; - unsigned int cp = iris::utflib::unchecked::prior (w); - EXPECT_EQ (cp, 0x65e5); - EXPECT_EQ (w, twochars); - - const char* threechars = "\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88"; - w = threechars + 9; - cp = iris::utflib::unchecked::prior(w); - EXPECT_EQ (cp, 0x0448); - EXPECT_EQ (w, threechars + 7); - cp = iris::utflib::unchecked::prior(w); - EXPECT_EQ (cp, 0x65e5); - EXPECT_EQ (w, threechars + 4); - cp = iris::utflib::unchecked::prior(w); - EXPECT_EQ (cp, 0x10346); - EXPECT_EQ (w, threechars); -} - -TEST(UnCheckedAPITests, test_advance) -{ - const char* threechars = "\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88"; - const char* w = threechars; - iris::utflib::unchecked::advance(w, 2); - EXPECT_EQ(w, threechars + 7); - iris::utflib::unchecked::advance(w, -2); - EXPECT_EQ(w, threechars); - iris::utflib::unchecked::advance(w, 3); - EXPECT_EQ(w, threechars + 9); - iris::utflib::unchecked::advance(w, -2); - EXPECT_EQ(w, threechars + 4); - iris::utflib::unchecked::advance(w, -1); - EXPECT_EQ(w, threechars); -} - -TEST(UnCheckedAPITests, test_distance) -{ - const char* twochars = "\xe6\x97\xa5\xd1\x88"; - size_t dist = static_cast(iris::utflib::unchecked::distance(twochars, twochars + 5)); - EXPECT_EQ (dist, 2); -} - -TEST(UnCheckedAPITests, test_utf32to8) -{ - unsigned int utf32string[] = {0x448, 0x65E5, 0x10346, 0}; - string utf8result; - iris::utflib::unchecked::utf32to8(utf32string, utf32string + 3, back_inserter(utf8result)); - EXPECT_EQ (utf8result.size(), 9); -} - -TEST(UnCheckedAPITests, test_utf8to32) -{ - const char* twochars = "\xe6\x97\xa5\xd1\x88"; - vector utf32result; - iris::utflib::unchecked::utf8to32(twochars, twochars + 5, back_inserter(utf32result)); - EXPECT_EQ (utf32result.size(), 2); -} - -TEST(UnCheckedAPITests, test_utf16to8) -{ - unsigned short utf16string[] = {0x41, 0x0448, 0x65e5, 0xd834, 0xdd1e}; - string utf8result; - iris::utflib::unchecked::utf16to8(utf16string, utf16string + 5, back_inserter(utf8result)); - EXPECT_EQ (utf8result.size(), 10); - - utf8result.clear(); - unsigned short highsurrogateonly[] = {0xd800}; - iris::utflib::unchecked::utf16to8(highsurrogateonly, highsurrogateonly + 1, back_inserter(utf8result)); - EXPECT_TRUE(true); // we didn't crash -} - -TEST(UnCheckedAPITests, test_utf8to16) -{ - char utf8_with_surrogates[] = "\xe6\x97\xa5\xd1\x88\xf0\x9d\x84\x9e"; - vector utf16result; - iris::utflib::unchecked::utf8to16(utf8_with_surrogates, utf8_with_surrogates + 9, back_inserter(utf16result)); - EXPECT_EQ (utf16result.size(), 4); - EXPECT_EQ (utf16result[2], 0xd834); - EXPECT_EQ (utf16result[3], 0xdd1e); -} - -TEST(UnCheckedAPITests, test_replace_invalid) -{ - char invalid_sequence[] = "a\x80\xe0\xa0\xc0\xAF\xED\xa0\x80z"; - vector replace_invalid_result; - iris::utflib::unchecked::replace_invalid (invalid_sequence, invalid_sequence + sizeof(invalid_sequence), std::back_inserter(replace_invalid_result), '?'); - bool bvalid = iris::utflib::is_valid(replace_invalid_result.begin(), replace_invalid_result.end()); - EXPECT_TRUE (bvalid); - const char fixed_invalid_sequence[] = "a????z"; - EXPECT_EQ (sizeof(fixed_invalid_sequence), replace_invalid_result.size()); - EXPECT_TRUE (std::equal(replace_invalid_result.begin(), replace_invalid_result.begin() + sizeof(fixed_invalid_sequence), fixed_invalid_sequence)); - - char invalid_sequence2[] = "\xD9\x85\xD8\x80\xE0\xA0\xC0\xAF\xED\xA0\x80z"; - bvalid = iris::utflib::is_valid(invalid_sequence2); - EXPECT_FALSE (bvalid); - replace_invalid_result.clear(); - iris::utflib::unchecked::replace_invalid (invalid_sequence2, invalid_sequence2 + sizeof(invalid_sequence2), std::back_inserter(replace_invalid_result), '?'); - bvalid = iris::utflib::is_valid(replace_invalid_result.begin(), replace_invalid_result.end()); - EXPECT_TRUE (bvalid); - const char fixed_invalid_sequence2[] = "\xD9\x85\xD8\x80???z"; - EXPECT_EQ (sizeof(fixed_invalid_sequence2), replace_invalid_result.size()); - EXPECT_TRUE (std::equal(replace_invalid_result.begin(), replace_invalid_result.begin() + sizeof(fixed_invalid_sequence2), fixed_invalid_sequence2)); -} - -#endif diff --git a/tests/test_unchecked_iterator.h b/tests/test_unchecked_iterator.h deleted file mode 100644 index dcc9806..0000000 --- a/tests/test_unchecked_iterator.h +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef IRIS_UTFLIB_TEST_UNCHECKED_ITERATOR_H_2675DCD0_9480_4c0c_B92A_CC14C027B731 -#define IRIS_UTFLIB_TEST_UNCHECKED_ITERATOR_H_2675DCD0_9480_4c0c_B92A_CC14C027B731 - -#include "utf8/unchecked.h" - -using namespace iris::utflib::unchecked; - - -TEST(UnCheckedIteratrTests, test_increment) -{ - const char* threechars = "\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88"; - iris::utflib::unchecked::iterator it(threechars); - iris::utflib::unchecked::iterator it2 = it; - EXPECT_EQ (it2, it); - EXPECT_EQ (*it, 0x10346); - EXPECT_EQ (*(++it), 0x65e5); - EXPECT_EQ ((*it++), 0x65e5); - EXPECT_EQ (*it, 0x0448); - EXPECT_NE (it, it2); - iris::utflib::unchecked::iterator endit (threechars + 9); - EXPECT_EQ (++it, endit); -} - -TEST(UnCheckedIteratrTests, test_decrement) -{ - const char* threechars = "\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88"; - iris::utflib::unchecked::iterator it(threechars+9); - EXPECT_EQ (*(--it), 0x0448); - EXPECT_EQ ((*it--), 0x0448); - EXPECT_EQ (*it, 0x65e5); - EXPECT_EQ (--it, iris::utflib::unchecked::iterator(threechars)); - EXPECT_EQ (*it, 0x10346); - -} - -#endif From 176a930b65c5fd1c7ab751aa4bb1113fc25e40db Mon Sep 17 00:00:00 2001 From: yaito3014 Date: Tue, 17 Feb 2026 14:03:14 +0900 Subject: [PATCH 03/28] Add missing include --- source/utf8.h | 1 + 1 file changed, 1 insertion(+) diff --git a/source/utf8.h b/source/utf8.h index 2aea1ea..b0e983a 100644 --- a/source/utf8.h +++ b/source/utf8.h @@ -31,6 +31,7 @@ DEALINGS IN THE SOFTWARE. #include #include +#include #include From fa439960e4f87d349def67b98170e080d3c9bc6a Mon Sep 17 00:00:00 2001 From: yaito3014 Date: Tue, 17 Feb 2026 14:04:47 +0900 Subject: [PATCH 04/28] Add missing include 2 --- source/utf8.h | 1 + 1 file changed, 1 insertion(+) diff --git a/source/utf8.h b/source/utf8.h index b0e983a..cdc1467 100644 --- a/source/utf8.h +++ b/source/utf8.h @@ -31,6 +31,7 @@ DEALINGS IN THE SOFTWARE. #include #include +#include #include #include From 84ae5359af741107887174a4f4c4665f619a9e8c Mon Sep 17 00:00:00 2001 From: yaito3014 Date: Tue, 17 Feb 2026 14:06:24 +0900 Subject: [PATCH 05/28] Add missing include 3 --- source/utf8.h | 1 + 1 file changed, 1 insertion(+) diff --git a/source/utf8.h b/source/utf8.h index cdc1467..127c541 100644 --- a/source/utf8.h +++ b/source/utf8.h @@ -31,6 +31,7 @@ DEALINGS IN THE SOFTWARE. #include #include +#include #include #include From 1353852d0ed09852f40c05f46361f12da14ad55a Mon Sep 17 00:00:00 2001 From: yaito3014 Date: Tue, 17 Feb 2026 14:09:36 +0900 Subject: [PATCH 06/28] Fix CI --- .github/workflows/ci-linux-clang.yml | 2 +- .github/workflows/ci-linux-gcc.yml | 2 +- .github/workflows/ci-windows-msvc.yml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci-linux-clang.yml b/.github/workflows/ci-linux-clang.yml index 7669a25..384daef 100644 --- a/.github/workflows/ci-linux-clang.yml +++ b/.github/workflows/ci-linux-clang.yml @@ -28,5 +28,5 @@ jobs: run: cmake --build build --config Release - name: Test - working-directory: build + working-directory: build/tests run: ctest -VV diff --git a/.github/workflows/ci-linux-gcc.yml b/.github/workflows/ci-linux-gcc.yml index 1e77938..0ed4244 100644 --- a/.github/workflows/ci-linux-gcc.yml +++ b/.github/workflows/ci-linux-gcc.yml @@ -28,5 +28,5 @@ jobs: run: cmake --build build --config Release - name: Test - working-directory: build + working-directory: build/tests run: ctest -VV diff --git a/.github/workflows/ci-windows-msvc.yml b/.github/workflows/ci-windows-msvc.yml index 45e7906..2d48f9a 100644 --- a/.github/workflows/ci-windows-msvc.yml +++ b/.github/workflows/ci-windows-msvc.yml @@ -19,5 +19,5 @@ jobs: run: cmake --build build --config Release - name: Test - working-directory: build + working-directory: build/tests run: ctest -VV --build-config Release From 367c2e03032c6dcdd076fd0811a88a2a7bc70b0c Mon Sep 17 00:00:00 2001 From: yaito3014 Date: Tue, 17 Feb 2026 14:12:32 +0900 Subject: [PATCH 07/28] Remove unused Dockerfile --- tests/docker/Dockerfile | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 tests/docker/Dockerfile diff --git a/tests/docker/Dockerfile b/tests/docker/Dockerfile deleted file mode 100644 index dcdd47d..0000000 --- a/tests/docker/Dockerfile +++ /dev/null @@ -1,5 +0,0 @@ -FROM gcc:12.2 - -RUN apt-get update \ - && apt-get install -y make g++ cmake git \ - && rm -rf /var/lib/apt/lists/* From 5708f9d60c1f885e60c817ec145c082491354e7d Mon Sep 17 00:00:00 2001 From: yaito3014 Date: Tue, 17 Feb 2026 14:53:41 +0900 Subject: [PATCH 08/28] Refactor qualification --- source/utf8.h | 72 +++++++++++++++++++++++++-------------------------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/source/utf8.h b/source/utf8.h index 127c541..e8d2786 100644 --- a/source/utf8.h +++ b/source/utf8.h @@ -74,7 +74,7 @@ namespace iris::utflib template inline bool is_trail(octet_type oc) { - return ((utflib::internal::mask8(oc) >> 6) == 0x2); + return ((internal::mask8(oc) >> 6) == 0x2); } inline bool is_lead_surrogate(utfchar32_t cp) @@ -94,7 +94,7 @@ namespace iris::utflib inline bool is_code_point_valid(utfchar32_t cp) { - return (cp <= CODE_POINT_MAX && !utflib::internal::is_surrogate(cp)); + return (cp <= CODE_POINT_MAX && !internal::is_surrogate(cp)); } inline bool is_in_bmp(utfchar32_t cp) @@ -105,7 +105,7 @@ namespace iris::utflib template int sequence_length(octet_iterator lead_it) { - const utfchar8_t lead = utflib::internal::mask8(*lead_it); + const utfchar8_t lead = internal::mask8(*lead_it); if (lead < 0x80) return 1; else if ((lead >> 5) == 0x6) @@ -144,7 +144,7 @@ namespace iris::utflib if (++it == end) return NOT_ENOUGH_ROOM; - if (!utflib::internal::is_trail(*it)) + if (!internal::is_trail(*it)) return INCOMPLETE_SEQUENCE; return UTF8_OK; @@ -159,7 +159,7 @@ namespace iris::utflib if (it == end) return NOT_ENOUGH_ROOM; - code_point = static_cast(utflib::internal::mask8(*it)); + code_point = static_cast(internal::mask8(*it)); return UTF8_OK; } @@ -170,7 +170,7 @@ namespace iris::utflib if (it == end) return NOT_ENOUGH_ROOM; - code_point = static_cast(utflib::internal::mask8(*it)); + code_point = static_cast(internal::mask8(*it)); IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end) @@ -185,11 +185,11 @@ namespace iris::utflib if (it == end) return NOT_ENOUGH_ROOM; - code_point = static_cast(utflib::internal::mask8(*it)); + code_point = static_cast(internal::mask8(*it)); IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end) - code_point = ((code_point << 12) & 0xffff) + ((utflib::internal::mask8(*it) << 6) & 0xfff); + code_point = ((code_point << 12) & 0xffff) + ((internal::mask8(*it) << 6) & 0xfff); IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end) @@ -204,15 +204,15 @@ namespace iris::utflib if (it == end) return NOT_ENOUGH_ROOM; - code_point = static_cast(utflib::internal::mask8(*it)); + code_point = static_cast(internal::mask8(*it)); IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end) - code_point = ((code_point << 18) & 0x1fffff) + ((utflib::internal::mask8(*it) << 12) & 0x3ffff); + code_point = ((code_point << 18) & 0x1fffff) + ((internal::mask8(*it) << 12) & 0x3ffff); IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end) - code_point = static_cast(code_point + ((utflib::internal::mask8(*it) << 6) & 0xfff)); + code_point = static_cast(code_point + ((internal::mask8(*it) << 6) & 0xfff)); IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end) @@ -235,7 +235,7 @@ namespace iris::utflib utfchar32_t cp = 0; // Determine the sequence length based on the lead octet - const int length = utflib::internal::sequence_length(it); + const int length = internal::sequence_length(it); // Get trail octets and calculate the code point utf_error err = UTF8_OK; @@ -243,23 +243,23 @@ namespace iris::utflib case 0: return INVALID_LEAD; case 1: - err = utflib::internal::get_sequence_1(it, end, cp); + err = internal::get_sequence_1(it, end, cp); break; case 2: - err = utflib::internal::get_sequence_2(it, end, cp); + err = internal::get_sequence_2(it, end, cp); break; case 3: - err = utflib::internal::get_sequence_3(it, end, cp); + err = internal::get_sequence_3(it, end, cp); break; case 4: - err = utflib::internal::get_sequence_4(it, end, cp); + err = internal::get_sequence_4(it, end, cp); break; } if (err == UTF8_OK) { // Decoding succeeded. Now, security checks... - if (utflib::internal::is_code_point_valid(cp)) { - if (!utflib::internal::is_overlong_sequence(cp, length)){ + if (internal::is_code_point_valid(cp)) { + if (!internal::is_overlong_sequence(cp, length)){ // Passed! Return here. code_point = cp; ++it; @@ -280,7 +280,7 @@ namespace iris::utflib template inline utf_error validate_next(octet_iterator& it, octet_iterator end) { utfchar32_t ignored; - return utflib::internal::validate_next(it, end, ignored); + return internal::validate_next(it, end, ignored); } template @@ -414,7 +414,7 @@ namespace iris::utflib { octet_iterator result = start; while (result != end) { - utflib::internal::utf_error err_code = utflib::internal::validate_next(result, end); + internal::utf_error err_code = internal::validate_next(result, end); if (err_code != internal::UTF8_OK) return result; } @@ -453,9 +453,9 @@ namespace iris::utflib inline bool starts_with_bom (octet_iterator it, octet_iterator end) { return ( - ((it != end) && (utflib::internal::mask8(*it++)) == bom[0]) && - ((it != end) && (utflib::internal::mask8(*it++)) == bom[1]) && - ((it != end) && (utflib::internal::mask8(*it)) == bom[2]) + ((it != end) && (internal::mask8(*it++)) == bom[0]) && + ((it != end) && (internal::mask8(*it++)) == bom[1]) && + ((it != end) && (internal::mask8(*it)) == bom[2]) ); } @@ -472,7 +472,7 @@ namespace iris::utflib template octet_iterator append(utfchar32_t cp, octet_iterator result) { - if (!utflib::internal::is_code_point_valid(cp)) + if (!internal::is_code_point_valid(cp)) throw invalid_code_point(cp); return internal::append(cp, result); @@ -491,7 +491,7 @@ namespace iris::utflib template word_iterator append16(utfchar32_t cp, word_iterator result) { - if (!utflib::internal::is_code_point_valid(cp)) + if (!internal::is_code_point_valid(cp)) throw invalid_code_point(cp); return internal::append16(cp, result); @@ -507,7 +507,7 @@ namespace iris::utflib { while (start != end) { octet_iterator sequence_start = start; - internal::utf_error err_code = utflib::internal::validate_next(start, end); + internal::utf_error err_code = internal::validate_next(start, end); switch (err_code) { case internal::UTF8_OK : for (octet_iterator it = sequence_start; it != start; ++it) @@ -527,7 +527,7 @@ namespace iris::utflib out = utflib::append (replacement, out); ++start; // just one replacement mark for the sequence - while (start != end && utflib::internal::is_trail(*start)) + while (start != end && internal::is_trail(*start)) ++start; break; } @@ -538,7 +538,7 @@ namespace iris::utflib template inline output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out) { - static const utfchar32_t replacement_marker = static_cast(utflib::internal::mask16(0xfffd)); + static const utfchar32_t replacement_marker = static_cast(internal::mask16(0xfffd)); return utflib::replace_invalid(start, end, out, replacement_marker); } @@ -574,7 +574,7 @@ namespace iris::utflib utfchar32_t next(octet_iterator& it, octet_iterator end) { utfchar32_t cp = 0; - internal::utf_error err_code = utflib::internal::validate_next(it, end, cp); + internal::utf_error err_code = internal::validate_next(it, end, cp); switch (err_code) { case internal::UTF8_OK : break; @@ -594,7 +594,7 @@ namespace iris::utflib utfchar32_t next16(word_iterator& it, word_iterator end) { utfchar32_t cp = 0; - internal::utf_error err_code = utflib::internal::validate_next16(it, end, cp); + internal::utf_error err_code = internal::validate_next16(it, end, cp); if (err_code == internal::NOT_ENOUGH_ROOM) throw not_enough_room(); return cp; @@ -615,7 +615,7 @@ namespace iris::utflib octet_iterator end = it; // Go back until we hit either a lead octet or start - while (utflib::internal::is_trail(*(--it))) + while (internal::is_trail(*(--it))) if (it == start) throw invalid_utf8(*it); // error - no lead byte in the sequence return utflib::peek_next(it, end); @@ -650,12 +650,12 @@ namespace iris::utflib octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result) { while (start != end) { - utfchar32_t cp = static_cast(utflib::internal::mask16(*start++)); + utfchar32_t cp = static_cast(internal::mask16(*start++)); // Take care of surrogate pairs first - if (utflib::internal::is_lead_surrogate(cp)) { + if (internal::is_lead_surrogate(cp)) { if (start != end) { - const utfchar32_t trail_surrogate = static_cast(utflib::internal::mask16(*start++)); - if (utflib::internal::is_trail_surrogate(trail_surrogate)) + const utfchar32_t trail_surrogate = static_cast(internal::mask16(*start++)); + if (internal::is_trail_surrogate(trail_surrogate)) cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET; else throw invalid_utf16(static_cast(trail_surrogate)); @@ -665,7 +665,7 @@ namespace iris::utflib } // Lone trail surrogate - else if (utflib::internal::is_trail_surrogate(cp)) + else if (internal::is_trail_surrogate(cp)) throw invalid_utf16(static_cast(cp)); result = utflib::append(cp, result); From 48fe4b46538369fce3deca93fd5674690514c677 Mon Sep 17 00:00:00 2001 From: yaito3014 Date: Tue, 17 Feb 2026 14:58:57 +0900 Subject: [PATCH 09/28] Formatting --- source/utf8.h | 315 ++++++++++++++++++++++++++------------------------ 1 file changed, 165 insertions(+), 150 deletions(-) diff --git a/source/utf8.h b/source/utf8.h index e8d2786..90b1499 100644 --- a/source/utf8.h +++ b/source/utf8.h @@ -39,7 +39,7 @@ DEALINGS IN THE SOFTWARE. namespace iris::utflib { - using utfchar8_t = char8_t; + using utfchar8_t = char8_t; using utfchar16_t = char16_t; using utfchar32_t = char32_t; @@ -53,25 +53,25 @@ namespace iris::utflib const utfchar16_t LEAD_SURROGATE_MAX = 0xdbffu; const utfchar16_t TRAIL_SURROGATE_MIN = 0xdc00u; const utfchar16_t TRAIL_SURROGATE_MAX = 0xdfffu; - const utfchar16_t LEAD_OFFSET = 0xd7c0u; // LEAD_SURROGATE_MIN - (0x10000 >> 10) - const utfchar32_t SURROGATE_OFFSET = 0xfca02400u; // 0x10000u - (LEAD_SURROGATE_MIN << 10) - TRAIL_SURROGATE_MIN + const utfchar16_t LEAD_OFFSET = 0xd7c0u; // LEAD_SURROGATE_MIN - (0x10000 >> 10) + const utfchar32_t SURROGATE_OFFSET = 0xfca02400u; // 0x10000u - (LEAD_SURROGATE_MIN << 10) - TRAIL_SURROGATE_MIN // Maximum valid value for a Unicode code point - const utfchar32_t CODE_POINT_MAX = 0x0010ffffu; + const utfchar32_t CODE_POINT_MAX = 0x0010ffffu; - template + template inline utfchar8_t mask8(octet_type oc) { return static_cast(0xff & oc); } - template + template inline utfchar16_t mask16(u16_type oc) { return static_cast(0xffff & oc); } - template + template inline bool is_trail(octet_type oc) { return ((internal::mask8(oc) >> 6) == 0x2); @@ -123,19 +123,17 @@ namespace iris::utflib if (cp < 0x80) { if (length != 1) return true; - } - else if (cp < 0x800) { + } else if (cp < 0x800) { if (length != 2) return true; - } - else if (cp < 0x10000) { + } else if (cp < 0x10000) { if (length != 3) return true; } return false; } - enum utf_error {UTF8_OK, NOT_ENOUGH_ROOM, INVALID_LEAD, INCOMPLETE_SEQUENCE, OVERLONG_SEQUENCE, INVALID_CODE_POINT}; + enum utf_error { UTF8_OK, NOT_ENOUGH_ROOM, INVALID_LEAD, INCOMPLETE_SEQUENCE, OVERLONG_SEQUENCE, INVALID_CODE_POINT }; /// Helper for get_sequence_x template @@ -150,7 +148,12 @@ namespace iris::utflib return UTF8_OK; } - #define IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(IT, END) {utf_error ret = increase_safely(IT, END); if (ret != UTF8_OK) return ret;} +#define IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(IT, END) \ + { \ + utf_error ret = increase_safely(IT, END); \ + if (ret != UTF8_OK) \ + return ret; \ + } /// get_sequence_x functions decode utf-8 sequences of the length x template @@ -202,7 +205,7 @@ namespace iris::utflib utf_error get_sequence_4(octet_iterator& it, octet_iterator end, utfchar32_t& code_point) { if (it == end) - return NOT_ENOUGH_ROOM; + return NOT_ENOUGH_ROOM; code_point = static_cast(internal::mask8(*it)); @@ -221,7 +224,7 @@ namespace iris::utflib return UTF8_OK; } - #undef IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR +#undef IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR template utf_error validate_next(octet_iterator& it, octet_iterator end, utfchar32_t& code_point) @@ -247,28 +250,26 @@ namespace iris::utflib break; case 2: err = internal::get_sequence_2(it, end, cp); - break; + break; case 3: err = internal::get_sequence_3(it, end, cp); - break; + break; case 4: err = internal::get_sequence_4(it, end, cp); - break; + break; } if (err == UTF8_OK) { // Decoding succeeded. Now, security checks... if (internal::is_code_point_valid(cp)) { - if (!internal::is_overlong_sequence(cp, length)){ + if (!internal::is_overlong_sequence(cp, length)) { // Passed! Return here. code_point = cp; ++it; return UTF8_OK; - } - else + } else err = OVERLONG_SEQUENCE; - } - else + } else err = INVALID_CODE_POINT; } @@ -278,7 +279,8 @@ namespace iris::utflib } template - inline utf_error validate_next(octet_iterator& it, octet_iterator end) { + inline utf_error validate_next(octet_iterator& it, octet_iterator end) + { utfchar32_t ignored; return internal::validate_next(it, end, ignored); } @@ -302,14 +304,13 @@ namespace iris::utflib if (!is_surrogate(first_word)) { code_point = first_word; return UTF8_OK; - } - else { + } else { if (it == end) err = NOT_ENOUGH_ROOM; else if (is_lead_surrogate(first_word)) { const utfchar16_t second_word = *it++; if (is_trail_surrogate(static_cast(second_word))) { - code_point = static_cast(first_word << 10) + static_cast(second_word) + SURROGATE_OFFSET; + code_point = static_cast(first_word << 10) + static_cast(second_word) + SURROGATE_OFFSET; return UTF8_OK; } else err = INCOMPLETE_SEQUENCE; @@ -324,34 +325,35 @@ namespace iris::utflib } template ::value_type> - octet_iterator append(utfchar32_t cp, octet_iterator result) { - if (cp < 0x80) // one octet + octet_iterator append(utfchar32_t cp, octet_iterator result) + { + if (cp < 0x80) // one octet *(result++) = static_cast(cp); - else if (cp < 0x800) { // two octets - *(result++) = static_cast((cp >> 6) | 0xc0); - *(result++) = static_cast((cp & 0x3f) | 0x80); - } - else if (cp < 0x10000) { // three octets - *(result++) = static_cast((cp >> 12) | 0xe0); + else if (cp < 0x800) { // two octets + *(result++) = static_cast((cp >> 6) | 0xc0); + *(result++) = static_cast((cp & 0x3f) | 0x80); + } else if (cp < 0x10000) { // three octets + *(result++) = static_cast((cp >> 12) | 0xe0); *(result++) = static_cast(((cp >> 6) & 0x3f) | 0x80); - *(result++) = static_cast((cp & 0x3f) | 0x80); - } - else { // four octets - *(result++) = static_cast((cp >> 18) | 0xf0); - *(result++) = static_cast(((cp >> 12) & 0x3f)| 0x80); + *(result++) = static_cast((cp & 0x3f) | 0x80); + } else { // four octets + *(result++) = static_cast((cp >> 18) | 0xf0); + *(result++) = static_cast(((cp >> 12) & 0x3f) | 0x80); *(result++) = static_cast(((cp >> 6) & 0x3f) | 0x80); - *(result++) = static_cast((cp & 0x3f) | 0x80); + *(result++) = static_cast((cp & 0x3f) | 0x80); } return result; } - template - std::back_insert_iterator append(utfchar32_t cp, std::back_insert_iterator result) { + template + std::back_insert_iterator append(utfchar32_t cp, std::back_insert_iterator result) + { return append, typename container_type::value_type>(cp, result); } template ::value_type> - word_iterator append16(utfchar32_t cp, word_iterator result) { + word_iterator append16(utfchar32_t cp, word_iterator result) + { static_assert(sizeof(word_type) >= sizeof(utfchar16_t)); if (is_in_bmp(cp)) *(result++) = static_cast(cp); @@ -363,43 +365,64 @@ namespace iris::utflib return result; } - template - std::back_insert_iterator append16(utfchar32_t cp, std::back_insert_iterator result) { + template + std::back_insert_iterator append16(utfchar32_t cp, std::back_insert_iterator result) + { return append16, typename container_type::value_type>(cp, result); } } // namespace internal // Base for the exceptions that may be thrown from the library - class exception : public ::std::exception { + class exception : public ::std::exception + { }; // Exceptions that may be thrown from the library functions. - class invalid_code_point : public exception { + class invalid_code_point : public exception + { utfchar32_t cp; + public: - invalid_code_point(utfchar32_t codepoint) : cp(codepoint) {} + invalid_code_point(utfchar32_t codepoint) + : cp(codepoint) + { + } virtual const char* what() const noexcept override { return "Invalid code point"; } - utfchar32_t code_point() const {return cp;} + utfchar32_t code_point() const { return cp; } }; - class invalid_utf8 : public exception { + class invalid_utf8 : public exception + { utfchar8_t u8; + public: - invalid_utf8 (utfchar8_t u) : u8(u) {} - invalid_utf8 (char c) : u8(static_cast(c)) {} + invalid_utf8(utfchar8_t u) + : u8(u) + { + } + invalid_utf8(char c) + : u8(static_cast(c)) + { + } virtual const char* what() const noexcept override { return "Invalid UTF-8"; } - utfchar8_t utf8_octet() const {return u8;} + utfchar8_t utf8_octet() const { return u8; } }; - class invalid_utf16 : public exception { + class invalid_utf16 : public exception + { utfchar16_t u16; + public: - invalid_utf16 (utfchar16_t u) : u16(u) {} + invalid_utf16(utfchar16_t u) + : u16(u) + { + } virtual const char* what() const noexcept override { return "Invalid UTF-16"; } - utfchar16_t utf16_word() const {return u16;} + utfchar16_t utf16_word() const { return u16; } }; - class not_enough_room : public exception { + class not_enough_room : public exception + { public: virtual const char* what() const noexcept override { return "Not enough space"; } }; @@ -426,7 +449,7 @@ namespace iris::utflib std::string_view::const_iterator invalid = find_invalid(s.begin(), s.end()); return (invalid == s.end()) ? std::string_view::npos : static_cast(invalid - s.begin()); } - + inline std::size_t find_invalid(std::u8string_view s) { std::u8string_view::const_iterator invalid = find_invalid(s.begin(), s.end()); @@ -443,20 +466,16 @@ namespace iris::utflib { return is_valid(s.begin(), s.end()); } - + inline bool is_valid(std::u8string_view s) { return is_valid(s.begin(), s.end()); } template - inline bool starts_with_bom (octet_iterator it, octet_iterator end) + inline bool starts_with_bom(octet_iterator it, octet_iterator end) { - return ( - ((it != end) && (internal::mask8(*it++)) == bom[0]) && - ((it != end) && (internal::mask8(*it++)) == bom[1]) && - ((it != end) && (internal::mask8(*it)) == bom[2]) - ); + return (((it != end) && (internal::mask8(*it++)) == bom[0]) && ((it != end) && (internal::mask8(*it++)) == bom[1]) && ((it != end) && (internal::mask8(*it)) == bom[2])); } inline bool starts_with_bom(std::string_view s) @@ -507,24 +526,24 @@ namespace iris::utflib { while (start != end) { octet_iterator sequence_start = start; - internal::utf_error err_code = internal::validate_next(start, end); + internal::utf_error err_code = internal::validate_next(start, end); switch (err_code) { - case internal::UTF8_OK : + case internal::UTF8_OK: for (octet_iterator it = sequence_start; it != start; ++it) *out++ = *it; break; case internal::NOT_ENOUGH_ROOM: - out = utflib::append (replacement, out); + out = utflib::append(replacement, out); start = end; break; case internal::INVALID_LEAD: - out = utflib::append (replacement, out); + out = utflib::append(replacement, out); ++start; break; case internal::INCOMPLETE_SEQUENCE: case internal::OVERLONG_SEQUENCE: case internal::INVALID_CODE_POINT: - out = utflib::append (replacement, out); + out = utflib::append(replacement, out); ++start; // just one replacement mark for the sequence while (start != end && internal::is_trail(*start)) @@ -573,18 +592,18 @@ namespace iris::utflib template utfchar32_t next(octet_iterator& it, octet_iterator end) { - utfchar32_t cp = 0; + utfchar32_t cp = 0; internal::utf_error err_code = internal::validate_next(it, end, cp); switch (err_code) { - case internal::UTF8_OK : + case internal::UTF8_OK: break; - case internal::NOT_ENOUGH_ROOM : + case internal::NOT_ENOUGH_ROOM: throw not_enough_room(); - case internal::INVALID_LEAD : - case internal::INCOMPLETE_SEQUENCE : - case internal::OVERLONG_SEQUENCE : + case internal::INVALID_LEAD: + case internal::INCOMPLETE_SEQUENCE: + case internal::OVERLONG_SEQUENCE: throw invalid_utf8(static_cast(*it)); - case internal::INVALID_CODE_POINT : + case internal::INVALID_CODE_POINT: throw invalid_code_point(cp); } return cp; @@ -593,7 +612,7 @@ namespace iris::utflib template utfchar32_t next16(word_iterator& it, word_iterator end) { - utfchar32_t cp = 0; + utfchar32_t cp = 0; internal::utf_error err_code = internal::validate_next16(it, end, cp); if (err_code == internal::NOT_ENOUGH_ROOM) throw not_enough_room(); @@ -622,7 +641,7 @@ namespace iris::utflib } template - void advance (octet_iterator& it, distance_type n, octet_iterator end) + void advance(octet_iterator& it, distance_type n, octet_iterator end) { const distance_type zero(0); if (n < zero) { @@ -637,8 +656,7 @@ namespace iris::utflib } template - typename std::iterator_traits::difference_type - distance (octet_iterator first, octet_iterator last) + typename std::iterator_traits::difference_type distance(octet_iterator first, octet_iterator last) { typename std::iterator_traits::difference_type dist; for (dist = 0; first < last; ++dist) @@ -647,7 +665,7 @@ namespace iris::utflib } template - octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result) + octet_iterator utf16to8(u16bit_iterator start, u16bit_iterator end, octet_iterator result) { while (start != end) { utfchar32_t cp = static_cast(internal::mask16(*start++)); @@ -659,8 +677,7 @@ namespace iris::utflib cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET; else throw invalid_utf16(static_cast(trail_surrogate)); - } - else + } else throw invalid_utf16(static_cast(cp)); } @@ -688,15 +705,14 @@ namespace iris::utflib } template - u16bit_iterator utf8to16 (octet_iterator start, octet_iterator end, u16bit_iterator result) + u16bit_iterator utf8to16(octet_iterator start, octet_iterator end, u16bit_iterator result) { while (start < end) { const utfchar32_t cp = utflib::next(start, end); - if (cp > 0xffff) { //make a surrogate pair - *result++ = static_cast((cp >> 10) + internal::LEAD_OFFSET); + if (cp > 0xffff) { // make a surrogate pair + *result++ = static_cast((cp >> 10) + internal::LEAD_OFFSET); *result++ = static_cast((cp & 0x3ff) + internal::TRAIL_SURROGATE_MIN); - } - else + } else *result++ = static_cast(cp); } return result; @@ -717,7 +733,7 @@ namespace iris::utflib } template - octet_iterator utf32to8 (u32bit_iterator start, u32bit_iterator end, octet_iterator result) + octet_iterator utf32to8(u32bit_iterator start, u32bit_iterator end, octet_iterator result) { while (start != end) result = utflib::append(*(start++), result); @@ -740,7 +756,7 @@ namespace iris::utflib } template - u32bit_iterator utf8to32 (octet_iterator start, octet_iterator end, u32bit_iterator result) + u32bit_iterator utf8to32(octet_iterator start, octet_iterator end, u32bit_iterator result) { while (start < end) (*result++) = utflib::next(start, end); @@ -764,64 +780,63 @@ namespace iris::utflib // The iterator class template - class iterator { - octet_iterator it; - octet_iterator range_start; - octet_iterator range_end; - public: - typedef utfchar32_t value_type; - typedef utfchar32_t* pointer; - typedef utfchar32_t& reference; - typedef std::ptrdiff_t difference_type; - typedef std::bidirectional_iterator_tag iterator_category; - iterator () {} - explicit iterator (const octet_iterator& octet_it, - const octet_iterator& rangestart, - const octet_iterator& rangeend) : - it(octet_it), range_start(rangestart), range_end(rangeend) - { - if (it < range_start || it > range_end) - throw std::out_of_range("Invalid utf-8 iterator position"); - } - // the default "big three" are OK - octet_iterator base () const { return it; } - utfchar32_t operator * () const - { - octet_iterator temp = it; - return utflib::next(temp, range_end); - } - bool operator == (const iterator& rhs) const - { - if (range_start != rhs.range_start || range_end != rhs.range_end) - throw std::logic_error("Comparing utf-8 iterators defined with different ranges"); - return (it == rhs.it); - } - bool operator != (const iterator& rhs) const - { - return !(operator == (rhs)); - } - iterator& operator ++ () - { - utflib::next(it, range_end); - return *this; - } - iterator operator ++ (int) - { - iterator temp = *this; - utflib::next(it, range_end); - return temp; - } - iterator& operator -- () - { - utflib::prior(it, range_start); - return *this; - } - iterator operator -- (int) - { - iterator temp = *this; - utflib::prior(it, range_start); - return temp; - } + class iterator + { + octet_iterator it; + octet_iterator range_start; + octet_iterator range_end; + + public: + typedef utfchar32_t value_type; + typedef utfchar32_t* pointer; + typedef utfchar32_t& reference; + typedef std::ptrdiff_t difference_type; + typedef std::bidirectional_iterator_tag iterator_category; + iterator() {} + explicit iterator(const octet_iterator& octet_it, const octet_iterator& rangestart, const octet_iterator& rangeend) + : it(octet_it) + , range_start(rangestart) + , range_end(rangeend) + { + if (it < range_start || it > range_end) + throw std::out_of_range("Invalid utf-8 iterator position"); + } + // the default "big three" are OK + octet_iterator base() const { return it; } + utfchar32_t operator*() const + { + octet_iterator temp = it; + return utflib::next(temp, range_end); + } + bool operator==(const iterator& rhs) const + { + if (range_start != rhs.range_start || range_end != rhs.range_end) + throw std::logic_error("Comparing utf-8 iterators defined with different ranges"); + return (it == rhs.it); + } + bool operator!=(const iterator& rhs) const { return !(operator==(rhs)); } + iterator& operator++() + { + utflib::next(it, range_end); + return *this; + } + iterator operator++(int) + { + iterator temp = *this; + utflib::next(it, range_end); + return temp; + } + iterator& operator--() + { + utflib::prior(it, range_start); + return *this; + } + iterator operator--(int) + { + iterator temp = *this; + utflib::prior(it, range_start); + return temp; + } }; // class iterator } // namespace iris::utflib From 119e5933c2dbec94eef14100bd1e8f14073d0540 Mon Sep 17 00:00:00 2001 From: yaito3014 Date: Tue, 17 Feb 2026 14:59:40 +0900 Subject: [PATCH 10/28] Use do-while in macro --- source/utf8.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/source/utf8.h b/source/utf8.h index 90b1499..3f52aeb 100644 --- a/source/utf8.h +++ b/source/utf8.h @@ -149,11 +149,11 @@ namespace iris::utflib } #define IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(IT, END) \ - { \ + do { \ utf_error ret = increase_safely(IT, END); \ if (ret != UTF8_OK) \ return ret; \ - } + } while (false) /// get_sequence_x functions decode utf-8 sequences of the length x template @@ -175,7 +175,7 @@ namespace iris::utflib code_point = static_cast(internal::mask8(*it)); - IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end) + IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end); code_point = ((code_point << 6) & 0x7ff) + ((*it) & 0x3f); @@ -190,11 +190,11 @@ namespace iris::utflib code_point = static_cast(internal::mask8(*it)); - IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end) + IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end); code_point = ((code_point << 12) & 0xffff) + ((internal::mask8(*it) << 6) & 0xfff); - IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end) + IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end); code_point = static_cast(code_point + ((*it) & 0x3f)); @@ -209,15 +209,15 @@ namespace iris::utflib code_point = static_cast(internal::mask8(*it)); - IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end) + IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end); code_point = ((code_point << 18) & 0x1fffff) + ((internal::mask8(*it) << 12) & 0x3ffff); - IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end) + IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end); code_point = static_cast(code_point + ((internal::mask8(*it) << 6) & 0xfff)); - IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end) + IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end); code_point = static_cast(code_point + ((*it) & 0x3f)); From 9e9fd93729337da02d76597fbebe44a541735f7a Mon Sep 17 00:00:00 2001 From: yaito3014 Date: Tue, 17 Feb 2026 16:41:08 +0900 Subject: [PATCH 11/28] Add constexpr to almost every function --- source/utf8.h | 156 +++++++++++++++++++++++++------------------------- 1 file changed, 78 insertions(+), 78 deletions(-) diff --git a/source/utf8.h b/source/utf8.h index 3f52aeb..327e462 100644 --- a/source/utf8.h +++ b/source/utf8.h @@ -60,50 +60,50 @@ namespace iris::utflib const utfchar32_t CODE_POINT_MAX = 0x0010ffffu; template - inline utfchar8_t mask8(octet_type oc) + constexpr utfchar8_t mask8(octet_type oc) { return static_cast(0xff & oc); } template - inline utfchar16_t mask16(u16_type oc) + constexpr utfchar16_t mask16(u16_type oc) { return static_cast(0xffff & oc); } template - inline bool is_trail(octet_type oc) + constexpr bool is_trail(octet_type oc) { return ((internal::mask8(oc) >> 6) == 0x2); } - inline bool is_lead_surrogate(utfchar32_t cp) + constexpr bool is_lead_surrogate(utfchar32_t cp) { return (cp >= static_cast(LEAD_SURROGATE_MIN) && cp <= static_cast(LEAD_SURROGATE_MAX)); } - inline bool is_trail_surrogate(utfchar32_t cp) + constexpr bool is_trail_surrogate(utfchar32_t cp) { return (cp >= static_cast(TRAIL_SURROGATE_MIN) && cp <= static_cast(TRAIL_SURROGATE_MAX)); } - inline bool is_surrogate(utfchar32_t cp) + constexpr bool is_surrogate(utfchar32_t cp) { return (cp >= static_cast(LEAD_SURROGATE_MIN) && cp <= static_cast(TRAIL_SURROGATE_MAX)); } - inline bool is_code_point_valid(utfchar32_t cp) + constexpr bool is_code_point_valid(utfchar32_t cp) { return (cp <= CODE_POINT_MAX && !internal::is_surrogate(cp)); } - inline bool is_in_bmp(utfchar32_t cp) + constexpr bool is_in_bmp(utfchar32_t cp) { return cp < utfchar32_t(0x10000); } template - int sequence_length(octet_iterator lead_it) + constexpr int sequence_length(octet_iterator lead_it) { const utfchar8_t lead = internal::mask8(*lead_it); if (lead < 0x80) @@ -118,7 +118,7 @@ namespace iris::utflib return 0; } - inline bool is_overlong_sequence(utfchar32_t cp, int length) + constexpr bool is_overlong_sequence(utfchar32_t cp, int length) { if (cp < 0x80) { if (length != 1) @@ -137,7 +137,7 @@ namespace iris::utflib /// Helper for get_sequence_x template - utf_error increase_safely(octet_iterator& it, const octet_iterator end) + constexpr utf_error increase_safely(octet_iterator& it, const octet_iterator end) { if (++it == end) return NOT_ENOUGH_ROOM; @@ -157,7 +157,7 @@ namespace iris::utflib /// get_sequence_x functions decode utf-8 sequences of the length x template - utf_error get_sequence_1(octet_iterator& it, octet_iterator end, utfchar32_t& code_point) + constexpr utf_error get_sequence_1(octet_iterator& it, octet_iterator end, utfchar32_t& code_point) { if (it == end) return NOT_ENOUGH_ROOM; @@ -168,7 +168,7 @@ namespace iris::utflib } template - utf_error get_sequence_2(octet_iterator& it, octet_iterator end, utfchar32_t& code_point) + constexpr utf_error get_sequence_2(octet_iterator& it, octet_iterator end, utfchar32_t& code_point) { if (it == end) return NOT_ENOUGH_ROOM; @@ -183,7 +183,7 @@ namespace iris::utflib } template - utf_error get_sequence_3(octet_iterator& it, octet_iterator end, utfchar32_t& code_point) + constexpr utf_error get_sequence_3(octet_iterator& it, octet_iterator end, utfchar32_t& code_point) { if (it == end) return NOT_ENOUGH_ROOM; @@ -202,7 +202,7 @@ namespace iris::utflib } template - utf_error get_sequence_4(octet_iterator& it, octet_iterator end, utfchar32_t& code_point) + constexpr utf_error get_sequence_4(octet_iterator& it, octet_iterator end, utfchar32_t& code_point) { if (it == end) return NOT_ENOUGH_ROOM; @@ -227,7 +227,7 @@ namespace iris::utflib #undef IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR template - utf_error validate_next(octet_iterator& it, octet_iterator end, utfchar32_t& code_point) + constexpr utf_error validate_next(octet_iterator& it, octet_iterator end, utfchar32_t& code_point) { if (it == end) return NOT_ENOUGH_ROOM; @@ -279,14 +279,14 @@ namespace iris::utflib } template - inline utf_error validate_next(octet_iterator& it, octet_iterator end) + constexpr utf_error validate_next(octet_iterator& it, octet_iterator end) { utfchar32_t ignored; return internal::validate_next(it, end, ignored); } template - utf_error validate_next16(word_iterator& it, word_iterator end, utfchar32_t& code_point) + constexpr utf_error validate_next16(word_iterator& it, word_iterator end, utfchar32_t& code_point) { // Make sure the iterator dereferences a large enough type typedef typename std::iterator_traits::value_type word_type; @@ -325,7 +325,7 @@ namespace iris::utflib } template ::value_type> - octet_iterator append(utfchar32_t cp, octet_iterator result) + constexpr octet_iterator append(utfchar32_t cp, octet_iterator result) { if (cp < 0x80) // one octet *(result++) = static_cast(cp); @@ -346,13 +346,13 @@ namespace iris::utflib } template - std::back_insert_iterator append(utfchar32_t cp, std::back_insert_iterator result) + constexpr std::back_insert_iterator append(utfchar32_t cp, std::back_insert_iterator result) { return append, typename container_type::value_type>(cp, result); } template ::value_type> - word_iterator append16(utfchar32_t cp, word_iterator result) + constexpr word_iterator append16(utfchar32_t cp, word_iterator result) { static_assert(sizeof(word_type) >= sizeof(utfchar16_t)); if (is_in_bmp(cp)) @@ -366,7 +366,7 @@ namespace iris::utflib } template - std::back_insert_iterator append16(utfchar32_t cp, std::back_insert_iterator result) + constexpr std::back_insert_iterator append16(utfchar32_t cp, std::back_insert_iterator result) { return append16, typename container_type::value_type>(cp, result); } @@ -383,12 +383,12 @@ namespace iris::utflib utfchar32_t cp; public: - invalid_code_point(utfchar32_t codepoint) + constexpr invalid_code_point(utfchar32_t codepoint) : cp(codepoint) { } virtual const char* what() const noexcept override { return "Invalid code point"; } - utfchar32_t code_point() const { return cp; } + constexpr utfchar32_t code_point() const { return cp; } }; class invalid_utf8 : public exception @@ -396,16 +396,16 @@ namespace iris::utflib utfchar8_t u8; public: - invalid_utf8(utfchar8_t u) + constexpr invalid_utf8(utfchar8_t u) : u8(u) { } - invalid_utf8(char c) + constexpr invalid_utf8(char c) : u8(static_cast(c)) { } virtual const char* what() const noexcept override { return "Invalid UTF-8"; } - utfchar8_t utf8_octet() const { return u8; } + constexpr utfchar8_t utf8_octet() const { return u8; } }; class invalid_utf16 : public exception @@ -413,12 +413,12 @@ namespace iris::utflib utfchar16_t u16; public: - invalid_utf16(utfchar16_t u) + constexpr invalid_utf16(utfchar16_t u) : u16(u) { } virtual const char* what() const noexcept override { return "Invalid UTF-16"; } - utfchar16_t utf16_word() const { return u16; } + constexpr utfchar16_t utf16_word() const { return u16; } }; class not_enough_room : public exception @@ -433,7 +433,7 @@ namespace iris::utflib const utfchar8_t bom[] = {0xef, 0xbb, 0xbf}; template - octet_iterator find_invalid(octet_iterator start, octet_iterator end) + constexpr octet_iterator find_invalid(octet_iterator start, octet_iterator end) { octet_iterator result = start; while (result != end) { @@ -444,52 +444,52 @@ namespace iris::utflib return result; } - inline std::size_t find_invalid(std::string_view s) + constexpr std::size_t find_invalid(std::string_view s) { std::string_view::const_iterator invalid = find_invalid(s.begin(), s.end()); return (invalid == s.end()) ? std::string_view::npos : static_cast(invalid - s.begin()); } - inline std::size_t find_invalid(std::u8string_view s) + constexpr std::size_t find_invalid(std::u8string_view s) { std::u8string_view::const_iterator invalid = find_invalid(s.begin(), s.end()); return (invalid == s.end()) ? std::u8string_view::npos : static_cast(invalid - s.begin()); } template - inline bool is_valid(octet_iterator start, octet_iterator end) + constexpr bool is_valid(octet_iterator start, octet_iterator end) { return (utflib::find_invalid(start, end) == end); } - inline bool is_valid(std::string_view s) + constexpr bool is_valid(std::string_view s) { return is_valid(s.begin(), s.end()); } - inline bool is_valid(std::u8string_view s) + constexpr bool is_valid(std::u8string_view s) { return is_valid(s.begin(), s.end()); } template - inline bool starts_with_bom(octet_iterator it, octet_iterator end) + constexpr bool starts_with_bom(octet_iterator it, octet_iterator end) { return (((it != end) && (internal::mask8(*it++)) == bom[0]) && ((it != end) && (internal::mask8(*it++)) == bom[1]) && ((it != end) && (internal::mask8(*it)) == bom[2])); } - inline bool starts_with_bom(std::string_view s) + constexpr bool starts_with_bom(std::string_view s) { return starts_with_bom(s.begin(), s.end()); } - inline bool starts_with_bom(std::u8string_view s) + constexpr bool starts_with_bom(std::u8string_view s) { return starts_with_bom(s.begin(), s.end()); } template - octet_iterator append(utfchar32_t cp, octet_iterator result) + constexpr octet_iterator append(utfchar32_t cp, octet_iterator result) { if (!internal::is_code_point_valid(cp)) throw invalid_code_point(cp); @@ -497,18 +497,18 @@ namespace iris::utflib return internal::append(cp, result); } - inline void append(utfchar32_t cp, std::string& s) + constexpr void append(utfchar32_t cp, std::string& s) { append(cp, std::back_inserter(s)); } - inline void append(utfchar32_t cp, std::u8string& s) + constexpr void append(utfchar32_t cp, std::u8string& s) { append(cp, std::back_inserter(s)); } template - word_iterator append16(utfchar32_t cp, word_iterator result) + constexpr word_iterator append16(utfchar32_t cp, word_iterator result) { if (!internal::is_code_point_valid(cp)) throw invalid_code_point(cp); @@ -516,13 +516,13 @@ namespace iris::utflib return internal::append16(cp, result); } - inline void append16(utfchar32_t cp, std::u16string& s) + constexpr void append16(utfchar32_t cp, std::u16string& s) { append16(cp, std::back_inserter(s)); } template - output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out, utfchar32_t replacement) + constexpr output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out, utfchar32_t replacement) { while (start != end) { octet_iterator sequence_start = start; @@ -555,34 +555,34 @@ namespace iris::utflib } template - inline output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out) + constexpr output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out) { - static const utfchar32_t replacement_marker = static_cast(internal::mask16(0xfffd)); + constexpr utfchar32_t replacement_marker = static_cast(internal::mask16(0xfffd)); return utflib::replace_invalid(start, end, out, replacement_marker); } - inline std::string replace_invalid(std::string_view s, utfchar32_t replacement) + constexpr std::string replace_invalid(std::string_view s, utfchar32_t replacement) { std::string result; replace_invalid(s.begin(), s.end(), std::back_inserter(result), replacement); return result; } - inline std::u8string replace_invalid(std::u8string_view s, char32_t replacement) + constexpr std::u8string replace_invalid(std::u8string_view s, char32_t replacement) { std::u8string result; replace_invalid(s.begin(), s.end(), std::back_inserter(result), replacement); return result; } - inline std::string replace_invalid(std::string_view s) + constexpr std::string replace_invalid(std::string_view s) { std::string result; replace_invalid(s.begin(), s.end(), std::back_inserter(result)); return result; } - inline std::u8string replace_invalid(std::u8string_view s) + constexpr std::u8string replace_invalid(std::u8string_view s) { std::u8string result; replace_invalid(s.begin(), s.end(), std::back_inserter(result)); @@ -590,7 +590,7 @@ namespace iris::utflib } template - utfchar32_t next(octet_iterator& it, octet_iterator end) + constexpr utfchar32_t next(octet_iterator& it, octet_iterator end) { utfchar32_t cp = 0; internal::utf_error err_code = internal::validate_next(it, end, cp); @@ -610,7 +610,7 @@ namespace iris::utflib } template - utfchar32_t next16(word_iterator& it, word_iterator end) + constexpr utfchar32_t next16(word_iterator& it, word_iterator end) { utfchar32_t cp = 0; internal::utf_error err_code = internal::validate_next16(it, end, cp); @@ -620,13 +620,13 @@ namespace iris::utflib } template - utfchar32_t peek_next(octet_iterator it, octet_iterator end) + constexpr utfchar32_t peek_next(octet_iterator it, octet_iterator end) { return utflib::next(it, end); } template - utfchar32_t prior(octet_iterator& it, octet_iterator start) + constexpr utfchar32_t prior(octet_iterator& it, octet_iterator start) { // can't do much if it == start if (it == start) @@ -641,7 +641,7 @@ namespace iris::utflib } template - void advance(octet_iterator& it, distance_type n, octet_iterator end) + constexpr void advance(octet_iterator& it, distance_type n, octet_iterator end) { const distance_type zero(0); if (n < zero) { @@ -656,7 +656,7 @@ namespace iris::utflib } template - typename std::iterator_traits::difference_type distance(octet_iterator first, octet_iterator last) + constexpr typename std::iterator_traits::difference_type distance(octet_iterator first, octet_iterator last) { typename std::iterator_traits::difference_type dist; for (dist = 0; first < last; ++dist) @@ -665,7 +665,7 @@ namespace iris::utflib } template - octet_iterator utf16to8(u16bit_iterator start, u16bit_iterator end, octet_iterator result) + constexpr octet_iterator utf16to8(u16bit_iterator start, u16bit_iterator end, octet_iterator result) { while (start != end) { utfchar32_t cp = static_cast(internal::mask16(*start++)); @@ -690,14 +690,14 @@ namespace iris::utflib return result; } - inline std::string utf16to8(std::u16string_view s) + constexpr std::string utf16to8(std::u16string_view s) { std::string result; utf16to8(s.begin(), s.end(), std::back_inserter(result)); return result; } - inline std::u8string utf16tou8(std::u16string_view s) + constexpr std::u8string utf16tou8(std::u16string_view s) { std::u8string result; utf16to8(s.begin(), s.end(), std::back_inserter(result)); @@ -705,7 +705,7 @@ namespace iris::utflib } template - u16bit_iterator utf8to16(octet_iterator start, octet_iterator end, u16bit_iterator result) + constexpr u16bit_iterator utf8to16(octet_iterator start, octet_iterator end, u16bit_iterator result) { while (start < end) { const utfchar32_t cp = utflib::next(start, end); @@ -718,14 +718,14 @@ namespace iris::utflib return result; } - inline std::u16string utf8to16(std::string_view s) + constexpr std::u16string utf8to16(std::string_view s) { std::u16string result; utf8to16(s.begin(), s.end(), std::back_inserter(result)); return result; } - inline std::u16string utf8to16(std::u8string_view s) + constexpr std::u16string utf8to16(std::u8string_view s) { std::u16string result; utf8to16(s.begin(), s.end(), std::back_inserter(result)); @@ -733,7 +733,7 @@ namespace iris::utflib } template - octet_iterator utf32to8(u32bit_iterator start, u32bit_iterator end, octet_iterator result) + constexpr octet_iterator utf32to8(u32bit_iterator start, u32bit_iterator end, octet_iterator result) { while (start != end) result = utflib::append(*(start++), result); @@ -741,14 +741,14 @@ namespace iris::utflib return result; } - inline std::string utf32to8(std::u32string_view s) + constexpr std::string utf32to8(std::u32string_view s) { std::string result; utf32to8(s.begin(), s.end(), std::back_inserter(result)); return result; } - inline std::u8string utf32tou8(std::u32string_view s) + constexpr std::u8string utf32tou8(std::u32string_view s) { std::u8string result; utf32to8(s.begin(), s.end(), std::back_inserter(result)); @@ -756,7 +756,7 @@ namespace iris::utflib } template - u32bit_iterator utf8to32(octet_iterator start, octet_iterator end, u32bit_iterator result) + constexpr u32bit_iterator utf8to32(octet_iterator start, octet_iterator end, u32bit_iterator result) { while (start < end) (*result++) = utflib::next(start, end); @@ -764,14 +764,14 @@ namespace iris::utflib return result; } - inline std::u32string utf8to32(std::string_view s) + constexpr std::u32string utf8to32(std::string_view s) { std::u32string result; utf8to32(s.begin(), s.end(), std::back_inserter(result)); return result; } - inline std::u32string utf8to32(std::u8string_view s) + constexpr std::u32string utf8to32(std::u8string_view s) { std::u32string result; utf8to32(s.begin(), s.end(), std::back_inserter(result)); @@ -792,8 +792,8 @@ namespace iris::utflib typedef utfchar32_t& reference; typedef std::ptrdiff_t difference_type; typedef std::bidirectional_iterator_tag iterator_category; - iterator() {} - explicit iterator(const octet_iterator& octet_it, const octet_iterator& rangestart, const octet_iterator& rangeend) + constexpr iterator() {} + constexpr explicit iterator(const octet_iterator& octet_it, const octet_iterator& rangestart, const octet_iterator& rangeend) : it(octet_it) , range_start(rangestart) , range_end(rangeend) @@ -802,36 +802,36 @@ namespace iris::utflib throw std::out_of_range("Invalid utf-8 iterator position"); } // the default "big three" are OK - octet_iterator base() const { return it; } - utfchar32_t operator*() const + constexpr octet_iterator base() const { return it; } + constexpr utfchar32_t operator*() const { octet_iterator temp = it; return utflib::next(temp, range_end); } - bool operator==(const iterator& rhs) const + constexpr bool operator==(const iterator& rhs) const { if (range_start != rhs.range_start || range_end != rhs.range_end) throw std::logic_error("Comparing utf-8 iterators defined with different ranges"); return (it == rhs.it); } - bool operator!=(const iterator& rhs) const { return !(operator==(rhs)); } - iterator& operator++() + constexpr bool operator!=(const iterator& rhs) const { return !(operator==(rhs)); } + constexpr iterator& operator++() { utflib::next(it, range_end); return *this; } - iterator operator++(int) + constexpr iterator operator++(int) { iterator temp = *this; utflib::next(it, range_end); return temp; } - iterator& operator--() + constexpr iterator& operator--() { utflib::prior(it, range_start); return *this; } - iterator operator--(int) + constexpr iterator operator--(int) { iterator temp = *this; utflib::prior(it, range_start); From f202798ebb66aaee8684a39e628b9860db460817 Mon Sep 17 00:00:00 2001 From: yaito3014 Date: Tue, 17 Feb 2026 16:43:12 +0900 Subject: [PATCH 12/28] Remove constexpr from exception types --- source/utf8.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/source/utf8.h b/source/utf8.h index 327e462..e121eac 100644 --- a/source/utf8.h +++ b/source/utf8.h @@ -383,12 +383,12 @@ namespace iris::utflib utfchar32_t cp; public: - constexpr invalid_code_point(utfchar32_t codepoint) + invalid_code_point(utfchar32_t codepoint) : cp(codepoint) { } virtual const char* what() const noexcept override { return "Invalid code point"; } - constexpr utfchar32_t code_point() const { return cp; } + utfchar32_t code_point() const { return cp; } }; class invalid_utf8 : public exception @@ -396,16 +396,16 @@ namespace iris::utflib utfchar8_t u8; public: - constexpr invalid_utf8(utfchar8_t u) + invalid_utf8(utfchar8_t u) : u8(u) { } - constexpr invalid_utf8(char c) + invalid_utf8(char c) : u8(static_cast(c)) { } virtual const char* what() const noexcept override { return "Invalid UTF-8"; } - constexpr utfchar8_t utf8_octet() const { return u8; } + utfchar8_t utf8_octet() const { return u8; } }; class invalid_utf16 : public exception @@ -413,12 +413,12 @@ namespace iris::utflib utfchar16_t u16; public: - constexpr invalid_utf16(utfchar16_t u) + invalid_utf16(utfchar16_t u) : u16(u) { } virtual const char* what() const noexcept override { return "Invalid UTF-16"; } - constexpr utfchar16_t utf16_word() const { return u16; } + utfchar16_t utf16_word() const { return u16; } }; class not_enough_room : public exception From 57501fb87ad8fa9115470e5033404b8486574897 Mon Sep 17 00:00:00 2001 From: yaito3014 Date: Tue, 17 Feb 2026 16:50:23 +0900 Subject: [PATCH 13/28] Replace some constants' const to constexpr --- source/utf8.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/source/utf8.h b/source/utf8.h index e121eac..e4c157b 100644 --- a/source/utf8.h +++ b/source/utf8.h @@ -49,15 +49,15 @@ namespace iris::utflib // Unicode constants // Leading (high) surrogates: 0xd800 - 0xdbff // Trailing (low) surrogates: 0xdc00 - 0xdfff - const utfchar16_t LEAD_SURROGATE_MIN = 0xd800u; - const utfchar16_t LEAD_SURROGATE_MAX = 0xdbffu; - const utfchar16_t TRAIL_SURROGATE_MIN = 0xdc00u; - const utfchar16_t TRAIL_SURROGATE_MAX = 0xdfffu; - const utfchar16_t LEAD_OFFSET = 0xd7c0u; // LEAD_SURROGATE_MIN - (0x10000 >> 10) - const utfchar32_t SURROGATE_OFFSET = 0xfca02400u; // 0x10000u - (LEAD_SURROGATE_MIN << 10) - TRAIL_SURROGATE_MIN + constexpr utfchar16_t LEAD_SURROGATE_MIN = 0xd800u; + constexpr utfchar16_t LEAD_SURROGATE_MAX = 0xdbffu; + constexpr utfchar16_t TRAIL_SURROGATE_MIN = 0xdc00u; + constexpr utfchar16_t TRAIL_SURROGATE_MAX = 0xdfffu; + constexpr utfchar16_t LEAD_OFFSET = 0xd7c0u; // LEAD_SURROGATE_MIN - (0x10000 >> 10) + constexpr utfchar32_t SURROGATE_OFFSET = 0xfca02400u; // 0x10000u - (LEAD_SURROGATE_MIN << 10) - TRAIL_SURROGATE_MIN // Maximum valid value for a Unicode code point - const utfchar32_t CODE_POINT_MAX = 0x0010ffffu; + constexpr utfchar32_t CODE_POINT_MAX = 0x0010ffffu; template constexpr utfchar8_t mask8(octet_type oc) @@ -430,7 +430,7 @@ namespace iris::utflib /// The library API - functions intended to be called by the users // Byte order mark - const utfchar8_t bom[] = {0xef, 0xbb, 0xbf}; + constexpr utfchar8_t bom[] = {0xef, 0xbb, 0xbf}; template constexpr octet_iterator find_invalid(octet_iterator start, octet_iterator end) From df50d2f056e4f857b6db09c7fe1c042794d06532 Mon Sep 17 00:00:00 2001 From: yaito3014 Date: Tue, 17 Feb 2026 16:54:08 +0900 Subject: [PATCH 14/28] Make unscoped enum to scoped enum --- source/utf8.h | 82 +++++++++++++++++++++++++-------------------------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/source/utf8.h b/source/utf8.h index e4c157b..d923580 100644 --- a/source/utf8.h +++ b/source/utf8.h @@ -133,25 +133,25 @@ namespace iris::utflib return false; } - enum utf_error { UTF8_OK, NOT_ENOUGH_ROOM, INVALID_LEAD, INCOMPLETE_SEQUENCE, OVERLONG_SEQUENCE, INVALID_CODE_POINT }; + enum class utf_error { UTF8_OK, NOT_ENOUGH_ROOM, INVALID_LEAD, INCOMPLETE_SEQUENCE, OVERLONG_SEQUENCE, INVALID_CODE_POINT }; /// Helper for get_sequence_x template constexpr utf_error increase_safely(octet_iterator& it, const octet_iterator end) { if (++it == end) - return NOT_ENOUGH_ROOM; + return utf_error::NOT_ENOUGH_ROOM; if (!internal::is_trail(*it)) - return INCOMPLETE_SEQUENCE; + return utf_error::INCOMPLETE_SEQUENCE; - return UTF8_OK; + return utf_error::UTF8_OK; } #define IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(IT, END) \ do { \ utf_error ret = increase_safely(IT, END); \ - if (ret != UTF8_OK) \ + if (ret != utf_error::UTF8_OK) \ return ret; \ } while (false) @@ -160,18 +160,18 @@ namespace iris::utflib constexpr utf_error get_sequence_1(octet_iterator& it, octet_iterator end, utfchar32_t& code_point) { if (it == end) - return NOT_ENOUGH_ROOM; + return utf_error::NOT_ENOUGH_ROOM; code_point = static_cast(internal::mask8(*it)); - return UTF8_OK; + return utf_error::UTF8_OK; } template constexpr utf_error get_sequence_2(octet_iterator& it, octet_iterator end, utfchar32_t& code_point) { if (it == end) - return NOT_ENOUGH_ROOM; + return utf_error::NOT_ENOUGH_ROOM; code_point = static_cast(internal::mask8(*it)); @@ -179,14 +179,14 @@ namespace iris::utflib code_point = ((code_point << 6) & 0x7ff) + ((*it) & 0x3f); - return UTF8_OK; + return utf_error::UTF8_OK; } template constexpr utf_error get_sequence_3(octet_iterator& it, octet_iterator end, utfchar32_t& code_point) { if (it == end) - return NOT_ENOUGH_ROOM; + return utf_error::NOT_ENOUGH_ROOM; code_point = static_cast(internal::mask8(*it)); @@ -198,14 +198,14 @@ namespace iris::utflib code_point = static_cast(code_point + ((*it) & 0x3f)); - return UTF8_OK; + return utf_error::UTF8_OK; } template constexpr utf_error get_sequence_4(octet_iterator& it, octet_iterator end, utfchar32_t& code_point) { if (it == end) - return NOT_ENOUGH_ROOM; + return utf_error::NOT_ENOUGH_ROOM; code_point = static_cast(internal::mask8(*it)); @@ -221,7 +221,7 @@ namespace iris::utflib code_point = static_cast(code_point + ((*it) & 0x3f)); - return UTF8_OK; + return utf_error::UTF8_OK; } #undef IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR @@ -230,7 +230,7 @@ namespace iris::utflib constexpr utf_error validate_next(octet_iterator& it, octet_iterator end, utfchar32_t& code_point) { if (it == end) - return NOT_ENOUGH_ROOM; + return utf_error::NOT_ENOUGH_ROOM; // Save the original value of it so we can go back in case of failure // Of course, it does not make much sense with i.e. stream iterators @@ -241,10 +241,10 @@ namespace iris::utflib const int length = internal::sequence_length(it); // Get trail octets and calculate the code point - utf_error err = UTF8_OK; + utf_error err = utf_error::UTF8_OK; switch (length) { case 0: - return INVALID_LEAD; + return utf_error::INVALID_LEAD; case 1: err = internal::get_sequence_1(it, end, cp); break; @@ -259,18 +259,18 @@ namespace iris::utflib break; } - if (err == UTF8_OK) { + if (err == utf_error::UTF8_OK) { // Decoding succeeded. Now, security checks... if (internal::is_code_point_valid(cp)) { if (!internal::is_overlong_sequence(cp, length)) { // Passed! Return here. code_point = cp; ++it; - return UTF8_OK; + return utf_error::UTF8_OK; } else - err = OVERLONG_SEQUENCE; + err = utf_error::OVERLONG_SEQUENCE; } else - err = INVALID_CODE_POINT; + err = utf_error::INVALID_CODE_POINT; } // Failure branch - restore the original value of the iterator @@ -293,30 +293,30 @@ namespace iris::utflib static_assert(sizeof(word_type) >= sizeof(utfchar16_t)); // Check the edge case: if (it == end) - return NOT_ENOUGH_ROOM; + return utf_error::NOT_ENOUGH_ROOM; // Save the original value of it so we can go back in case of failure // Of course, it does not make much sense with i.e. stream iterators word_iterator original_it = it; - utf_error err = UTF8_OK; + utf_error err = utf_error::UTF8_OK; const utfchar16_t first_word = *it++; if (!is_surrogate(first_word)) { code_point = first_word; - return UTF8_OK; + return utf_error::UTF8_OK; } else { if (it == end) - err = NOT_ENOUGH_ROOM; + err = utf_error::NOT_ENOUGH_ROOM; else if (is_lead_surrogate(first_word)) { const utfchar16_t second_word = *it++; if (is_trail_surrogate(static_cast(second_word))) { code_point = static_cast(first_word << 10) + static_cast(second_word) + SURROGATE_OFFSET; - return UTF8_OK; + return utf_error::UTF8_OK; } else - err = INCOMPLETE_SEQUENCE; + err = utf_error::INCOMPLETE_SEQUENCE; } else { - err = INVALID_LEAD; + err = utf_error::INVALID_LEAD; } } // error branch @@ -438,7 +438,7 @@ namespace iris::utflib octet_iterator result = start; while (result != end) { internal::utf_error err_code = internal::validate_next(result, end); - if (err_code != internal::UTF8_OK) + if (err_code != internal::utf_error::UTF8_OK) return result; } return result; @@ -528,21 +528,21 @@ namespace iris::utflib octet_iterator sequence_start = start; internal::utf_error err_code = internal::validate_next(start, end); switch (err_code) { - case internal::UTF8_OK: + case internal::utf_error::UTF8_OK: for (octet_iterator it = sequence_start; it != start; ++it) *out++ = *it; break; - case internal::NOT_ENOUGH_ROOM: + case internal::utf_error::NOT_ENOUGH_ROOM: out = utflib::append(replacement, out); start = end; break; - case internal::INVALID_LEAD: + case internal::utf_error::INVALID_LEAD: out = utflib::append(replacement, out); ++start; break; - case internal::INCOMPLETE_SEQUENCE: - case internal::OVERLONG_SEQUENCE: - case internal::INVALID_CODE_POINT: + case internal::utf_error::INCOMPLETE_SEQUENCE: + case internal::utf_error::OVERLONG_SEQUENCE: + case internal::utf_error::INVALID_CODE_POINT: out = utflib::append(replacement, out); ++start; // just one replacement mark for the sequence @@ -595,15 +595,15 @@ namespace iris::utflib utfchar32_t cp = 0; internal::utf_error err_code = internal::validate_next(it, end, cp); switch (err_code) { - case internal::UTF8_OK: + case internal::utf_error::UTF8_OK: break; - case internal::NOT_ENOUGH_ROOM: + case internal::utf_error::NOT_ENOUGH_ROOM: throw not_enough_room(); - case internal::INVALID_LEAD: - case internal::INCOMPLETE_SEQUENCE: - case internal::OVERLONG_SEQUENCE: + case internal::utf_error::INVALID_LEAD: + case internal::utf_error::INCOMPLETE_SEQUENCE: + case internal::utf_error::OVERLONG_SEQUENCE: throw invalid_utf8(static_cast(*it)); - case internal::INVALID_CODE_POINT: + case internal::utf_error::INVALID_CODE_POINT: throw invalid_code_point(cp); } return cp; @@ -614,7 +614,7 @@ namespace iris::utflib { utfchar32_t cp = 0; internal::utf_error err_code = internal::validate_next16(it, end, cp); - if (err_code == internal::NOT_ENOUGH_ROOM) + if (err_code == internal::utf_error::NOT_ENOUGH_ROOM) throw not_enough_room(); return cp; } From a874bc9150ecad6f6ca0b8684b6bc1249d66cea8 Mon Sep 17 00:00:00 2001 From: yaito3014 Date: Wed, 18 Feb 2026 07:34:06 +0900 Subject: [PATCH 15/28] Add nodiscard and noexcept --- source/utf8.h | 119 +++++++++++++++++++++++++------------------------- 1 file changed, 59 insertions(+), 60 deletions(-) diff --git a/source/utf8.h b/source/utf8.h index d923580..a1646e7 100644 --- a/source/utf8.h +++ b/source/utf8.h @@ -59,66 +59,52 @@ namespace iris::utflib // Maximum valid value for a Unicode code point constexpr utfchar32_t CODE_POINT_MAX = 0x0010ffffu; + enum class utf_error { UTF8_OK, NOT_ENOUGH_ROOM, INVALID_LEAD, INCOMPLETE_SEQUENCE, OVERLONG_SEQUENCE, INVALID_CODE_POINT }; + template - constexpr utfchar8_t mask8(octet_type oc) + [[nodiscard]] constexpr utfchar8_t mask8(octet_type oc) noexcept { return static_cast(0xff & oc); } template - constexpr utfchar16_t mask16(u16_type oc) + [[nodiscard]] constexpr utfchar16_t mask16(u16_type oc) noexcept { return static_cast(0xffff & oc); } template - constexpr bool is_trail(octet_type oc) + [[nodiscard]] constexpr bool is_trail(octet_type oc) noexcept { return ((internal::mask8(oc) >> 6) == 0x2); } - constexpr bool is_lead_surrogate(utfchar32_t cp) + [[nodiscard]] constexpr bool is_lead_surrogate(utfchar32_t cp) noexcept { return (cp >= static_cast(LEAD_SURROGATE_MIN) && cp <= static_cast(LEAD_SURROGATE_MAX)); } - constexpr bool is_trail_surrogate(utfchar32_t cp) + [[nodiscard]] constexpr bool is_trail_surrogate(utfchar32_t cp) noexcept { return (cp >= static_cast(TRAIL_SURROGATE_MIN) && cp <= static_cast(TRAIL_SURROGATE_MAX)); } - constexpr bool is_surrogate(utfchar32_t cp) + [[nodiscard]] constexpr bool is_surrogate(utfchar32_t cp) noexcept { return (cp >= static_cast(LEAD_SURROGATE_MIN) && cp <= static_cast(TRAIL_SURROGATE_MAX)); } - constexpr bool is_code_point_valid(utfchar32_t cp) + [[nodiscard]] constexpr bool is_code_point_valid(utfchar32_t cp) noexcept { return (cp <= CODE_POINT_MAX && !internal::is_surrogate(cp)); } - constexpr bool is_in_bmp(utfchar32_t cp) + [[nodiscard]] constexpr bool is_in_bmp(utfchar32_t cp) noexcept { return cp < utfchar32_t(0x10000); } - template - constexpr int sequence_length(octet_iterator lead_it) - { - const utfchar8_t lead = internal::mask8(*lead_it); - if (lead < 0x80) - return 1; - else if ((lead >> 5) == 0x6) - return 2; - else if ((lead >> 4) == 0xe) - return 3; - else if ((lead >> 3) == 0x1e) - return 4; - else - return 0; - } - - constexpr bool is_overlong_sequence(utfchar32_t cp, int length) + [[nodiscard]] constexpr bool is_overlong_sequence(utfchar32_t cp, int length) noexcept { if (cp < 0x80) { if (length != 1) @@ -133,7 +119,21 @@ namespace iris::utflib return false; } - enum class utf_error { UTF8_OK, NOT_ENOUGH_ROOM, INVALID_LEAD, INCOMPLETE_SEQUENCE, OVERLONG_SEQUENCE, INVALID_CODE_POINT }; + template + [[nodiscard]] constexpr int sequence_length(octet_iterator lead_it) + { + const utfchar8_t lead = internal::mask8(*lead_it); + if (lead < 0x80) + return 1; + else if ((lead >> 5) == 0x6) + return 2; + else if ((lead >> 4) == 0xe) + return 3; + else if ((lead >> 3) == 0x1e) + return 4; + else + return 0; + } /// Helper for get_sequence_x template @@ -388,7 +388,7 @@ namespace iris::utflib { } virtual const char* what() const noexcept override { return "Invalid code point"; } - utfchar32_t code_point() const { return cp; } + [[nodiscard]] utfchar32_t code_point() const { return cp; } }; class invalid_utf8 : public exception @@ -405,7 +405,7 @@ namespace iris::utflib { } virtual const char* what() const noexcept override { return "Invalid UTF-8"; } - utfchar8_t utf8_octet() const { return u8; } + [[nodiscard]] utfchar8_t utf8_octet() const { return u8; } }; class invalid_utf16 : public exception @@ -418,7 +418,7 @@ namespace iris::utflib { } virtual const char* what() const noexcept override { return "Invalid UTF-16"; } - utfchar16_t utf16_word() const { return u16; } + [[nodiscard]] utfchar16_t utf16_word() const { return u16; } }; class not_enough_room : public exception @@ -433,7 +433,7 @@ namespace iris::utflib constexpr utfchar8_t bom[] = {0xef, 0xbb, 0xbf}; template - constexpr octet_iterator find_invalid(octet_iterator start, octet_iterator end) + [[nodiscard]] constexpr octet_iterator find_invalid(octet_iterator start, octet_iterator end) { octet_iterator result = start; while (result != end) { @@ -444,46 +444,46 @@ namespace iris::utflib return result; } - constexpr std::size_t find_invalid(std::string_view s) + [[nodiscard]] constexpr std::size_t find_invalid(std::string_view s) { std::string_view::const_iterator invalid = find_invalid(s.begin(), s.end()); return (invalid == s.end()) ? std::string_view::npos : static_cast(invalid - s.begin()); } - constexpr std::size_t find_invalid(std::u8string_view s) + [[nodiscard]] constexpr std::size_t find_invalid(std::u8string_view s) { std::u8string_view::const_iterator invalid = find_invalid(s.begin(), s.end()); return (invalid == s.end()) ? std::u8string_view::npos : static_cast(invalid - s.begin()); } template - constexpr bool is_valid(octet_iterator start, octet_iterator end) + [[nodiscard]] constexpr bool is_valid(octet_iterator start, octet_iterator end) { return (utflib::find_invalid(start, end) == end); } - constexpr bool is_valid(std::string_view s) + [[nodiscard]] constexpr bool is_valid(std::string_view s) { return is_valid(s.begin(), s.end()); } - constexpr bool is_valid(std::u8string_view s) + [[nodiscard]] constexpr bool is_valid(std::u8string_view s) { return is_valid(s.begin(), s.end()); } template - constexpr bool starts_with_bom(octet_iterator it, octet_iterator end) + [[nodiscard]] constexpr bool starts_with_bom(octet_iterator it, octet_iterator end) { return (((it != end) && (internal::mask8(*it++)) == bom[0]) && ((it != end) && (internal::mask8(*it++)) == bom[1]) && ((it != end) && (internal::mask8(*it)) == bom[2])); } - constexpr bool starts_with_bom(std::string_view s) + [[nodiscard]] constexpr bool starts_with_bom(std::string_view s) { return starts_with_bom(s.begin(), s.end()); } - constexpr bool starts_with_bom(std::u8string_view s) + [[nodiscard]] constexpr bool starts_with_bom(std::u8string_view s) { return starts_with_bom(s.begin(), s.end()); } @@ -561,28 +561,28 @@ namespace iris::utflib return utflib::replace_invalid(start, end, out, replacement_marker); } - constexpr std::string replace_invalid(std::string_view s, utfchar32_t replacement) + [[nodiscard]] constexpr std::string replace_invalid(std::string_view s, utfchar32_t replacement) { std::string result; replace_invalid(s.begin(), s.end(), std::back_inserter(result), replacement); return result; } - constexpr std::u8string replace_invalid(std::u8string_view s, char32_t replacement) + [[nodiscard]] constexpr std::u8string replace_invalid(std::u8string_view s, char32_t replacement) { std::u8string result; replace_invalid(s.begin(), s.end(), std::back_inserter(result), replacement); return result; } - constexpr std::string replace_invalid(std::string_view s) + [[nodiscard]] constexpr std::string replace_invalid(std::string_view s) { std::string result; replace_invalid(s.begin(), s.end(), std::back_inserter(result)); return result; } - constexpr std::u8string replace_invalid(std::u8string_view s) + [[nodiscard]] constexpr std::u8string replace_invalid(std::u8string_view s) { std::u8string result; replace_invalid(s.begin(), s.end(), std::back_inserter(result)); @@ -590,7 +590,7 @@ namespace iris::utflib } template - constexpr utfchar32_t next(octet_iterator& it, octet_iterator end) + [[nodiscard]] constexpr utfchar32_t next(octet_iterator& it, octet_iterator end) { utfchar32_t cp = 0; internal::utf_error err_code = internal::validate_next(it, end, cp); @@ -610,7 +610,7 @@ namespace iris::utflib } template - constexpr utfchar32_t next16(word_iterator& it, word_iterator end) + [[nodiscard]] constexpr utfchar32_t next16(word_iterator& it, word_iterator end) { utfchar32_t cp = 0; internal::utf_error err_code = internal::validate_next16(it, end, cp); @@ -620,7 +620,7 @@ namespace iris::utflib } template - constexpr utfchar32_t peek_next(octet_iterator it, octet_iterator end) + [[nodiscard]] constexpr utfchar32_t peek_next(octet_iterator it, octet_iterator end) { return utflib::next(it, end); } @@ -656,7 +656,7 @@ namespace iris::utflib } template - constexpr typename std::iterator_traits::difference_type distance(octet_iterator first, octet_iterator last) + [[nodiscard]] constexpr typename std::iterator_traits::difference_type distance(octet_iterator first, octet_iterator last) { typename std::iterator_traits::difference_type dist; for (dist = 0; first < last; ++dist) @@ -690,14 +690,14 @@ namespace iris::utflib return result; } - constexpr std::string utf16to8(std::u16string_view s) + [[nodiscard]] constexpr std::string utf16to8(std::u16string_view s) { std::string result; utf16to8(s.begin(), s.end(), std::back_inserter(result)); return result; } - constexpr std::u8string utf16tou8(std::u16string_view s) + [[nodiscard]] constexpr std::u8string utf16tou8(std::u16string_view s) { std::u8string result; utf16to8(s.begin(), s.end(), std::back_inserter(result)); @@ -718,14 +718,14 @@ namespace iris::utflib return result; } - constexpr std::u16string utf8to16(std::string_view s) + [[nodiscard]] constexpr std::u16string utf8to16(std::string_view s) { std::u16string result; utf8to16(s.begin(), s.end(), std::back_inserter(result)); return result; } - constexpr std::u16string utf8to16(std::u8string_view s) + [[nodiscard]] constexpr std::u16string utf8to16(std::u8string_view s) { std::u16string result; utf8to16(s.begin(), s.end(), std::back_inserter(result)); @@ -741,14 +741,14 @@ namespace iris::utflib return result; } - constexpr std::string utf32to8(std::u32string_view s) + [[nodiscard]] constexpr std::string utf32to8(std::u32string_view s) { std::string result; utf32to8(s.begin(), s.end(), std::back_inserter(result)); return result; } - constexpr std::u8string utf32tou8(std::u32string_view s) + [[nodiscard]] constexpr std::u8string utf32tou8(std::u32string_view s) { std::u8string result; utf32to8(s.begin(), s.end(), std::back_inserter(result)); @@ -764,14 +764,14 @@ namespace iris::utflib return result; } - constexpr std::u32string utf8to32(std::string_view s) + [[nodiscard]] constexpr std::u32string utf8to32(std::string_view s) { std::u32string result; utf8to32(s.begin(), s.end(), std::back_inserter(result)); return result; } - constexpr std::u32string utf8to32(std::u8string_view s) + [[nodiscard]] constexpr std::u32string utf8to32(std::u8string_view s) { std::u32string result; utf8to32(s.begin(), s.end(), std::back_inserter(result)); @@ -792,8 +792,8 @@ namespace iris::utflib typedef utfchar32_t& reference; typedef std::ptrdiff_t difference_type; typedef std::bidirectional_iterator_tag iterator_category; - constexpr iterator() {} - constexpr explicit iterator(const octet_iterator& octet_it, const octet_iterator& rangestart, const octet_iterator& rangeend) + [[nodiscard]] constexpr iterator() {} + [[nodiscard]] constexpr explicit iterator(const octet_iterator& octet_it, const octet_iterator& rangestart, const octet_iterator& rangeend) : it(octet_it) , range_start(rangestart) , range_end(rangeend) @@ -802,19 +802,18 @@ namespace iris::utflib throw std::out_of_range("Invalid utf-8 iterator position"); } // the default "big three" are OK - constexpr octet_iterator base() const { return it; } - constexpr utfchar32_t operator*() const + [[nodiscard]] constexpr octet_iterator base() const { return it; } + [[nodiscard]] constexpr utfchar32_t operator*() const { octet_iterator temp = it; return utflib::next(temp, range_end); } - constexpr bool operator==(const iterator& rhs) const + [[nodiscard]] constexpr bool operator==(const iterator& rhs) const { if (range_start != rhs.range_start || range_end != rhs.range_end) throw std::logic_error("Comparing utf-8 iterators defined with different ranges"); return (it == rhs.it); } - constexpr bool operator!=(const iterator& rhs) const { return !(operator==(rhs)); } constexpr iterator& operator++() { utflib::next(it, range_end); From 65eaa3ff3e5ff44eb81f67fa95d634d031fc3d26 Mon Sep 17 00:00:00 2001 From: yaito3014 Date: Wed, 18 Feb 2026 07:40:35 +0900 Subject: [PATCH 16/28] Use `using` instead of `typedef` --- source/utf8.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/source/utf8.h b/source/utf8.h index a1646e7..2fb3a0b 100644 --- a/source/utf8.h +++ b/source/utf8.h @@ -289,7 +289,7 @@ namespace iris::utflib constexpr utf_error validate_next16(word_iterator& it, word_iterator end, utfchar32_t& code_point) { // Make sure the iterator dereferences a large enough type - typedef typename std::iterator_traits::value_type word_type; + using word_type = std::iterator_traits::value_type; static_assert(sizeof(word_type) >= sizeof(utfchar16_t)); // Check the edge case: if (it == end) @@ -787,11 +787,11 @@ namespace iris::utflib octet_iterator range_end; public: - typedef utfchar32_t value_type; - typedef utfchar32_t* pointer; - typedef utfchar32_t& reference; - typedef std::ptrdiff_t difference_type; - typedef std::bidirectional_iterator_tag iterator_category; + using value_type = utfchar32_t; + using pointer = utfchar32_t*; + using reference = utfchar32_t&; + using difference_type = std::ptrdiff_t; + using iterator_category = std::bidirectional_iterator_tag; [[nodiscard]] constexpr iterator() {} [[nodiscard]] constexpr explicit iterator(const octet_iterator& octet_it, const octet_iterator& rangestart, const octet_iterator& rangeend) : it(octet_it) From 854369f4665bdd1923dd2150888cca16c0947fcb Mon Sep 17 00:00:00 2001 From: yaito3014 Date: Wed, 18 Feb 2026 08:25:42 +0900 Subject: [PATCH 17/28] Refactor --- source/utf8.h | 191 +++++++++++++++++++++++-------------------- tests/apitests.cpp | 14 ++-- tests/test_cpp11.cpp | 4 +- 3 files changed, 112 insertions(+), 97 deletions(-) diff --git a/source/utf8.h b/source/utf8.h index 2fb3a0b..8ce9af6 100644 --- a/source/utf8.h +++ b/source/utf8.h @@ -29,6 +29,7 @@ DEALINGS IN THE SOFTWARE. #ifndef IRIS_UTFLIB_UTF8_H #define IRIS_UTFLIB_UTF8_H +#include #include #include #include @@ -43,6 +44,18 @@ namespace iris::utflib using utfchar16_t = char16_t; using utfchar32_t = char32_t; + template + concept octet_iterator = std::input_iterator && std::integral> && sizeof(std::iter_value_t) == 1; + + template + concept utf8_iterator = octet_iterator && (std::same_as, char> || std::same_as, char8_t>); + + template + concept utf16_iterator = std::input_iterator && std::same_as, char16_t>; + + template + concept utf32_iterator = std::input_iterator && std::same_as, char32_t>; + // Helper code - not intended to be directly called by the library users. May be changed at any time namespace internal { @@ -119,8 +132,8 @@ namespace iris::utflib return false; } - template - [[nodiscard]] constexpr int sequence_length(octet_iterator lead_it) + template + [[nodiscard]] constexpr int sequence_length(It lead_it) { const utfchar8_t lead = internal::mask8(*lead_it); if (lead < 0x80) @@ -136,8 +149,8 @@ namespace iris::utflib } /// Helper for get_sequence_x - template - constexpr utf_error increase_safely(octet_iterator& it, const octet_iterator end) + template + constexpr utf_error increase_safely(It& it, It end) { if (++it == end) return utf_error::NOT_ENOUGH_ROOM; @@ -156,8 +169,8 @@ namespace iris::utflib } while (false) /// get_sequence_x functions decode utf-8 sequences of the length x - template - constexpr utf_error get_sequence_1(octet_iterator& it, octet_iterator end, utfchar32_t& code_point) + template + constexpr utf_error get_sequence_1(It& it, It end, utfchar32_t& code_point) { if (it == end) return utf_error::NOT_ENOUGH_ROOM; @@ -167,8 +180,8 @@ namespace iris::utflib return utf_error::UTF8_OK; } - template - constexpr utf_error get_sequence_2(octet_iterator& it, octet_iterator end, utfchar32_t& code_point) + template + constexpr utf_error get_sequence_2(It& it, It end, utfchar32_t& code_point) { if (it == end) return utf_error::NOT_ENOUGH_ROOM; @@ -182,8 +195,8 @@ namespace iris::utflib return utf_error::UTF8_OK; } - template - constexpr utf_error get_sequence_3(octet_iterator& it, octet_iterator end, utfchar32_t& code_point) + template + constexpr utf_error get_sequence_3(It& it, It end, utfchar32_t& code_point) { if (it == end) return utf_error::NOT_ENOUGH_ROOM; @@ -201,8 +214,8 @@ namespace iris::utflib return utf_error::UTF8_OK; } - template - constexpr utf_error get_sequence_4(octet_iterator& it, octet_iterator end, utfchar32_t& code_point) + template + constexpr utf_error get_sequence_4(It& it, It end, utfchar32_t& code_point) { if (it == end) return utf_error::NOT_ENOUGH_ROOM; @@ -226,15 +239,15 @@ namespace iris::utflib #undef IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR - template - constexpr utf_error validate_next(octet_iterator& it, octet_iterator end, utfchar32_t& code_point) + template + constexpr utf_error validate_next(It& it, It end, utfchar32_t& code_point) { if (it == end) return utf_error::NOT_ENOUGH_ROOM; // Save the original value of it so we can go back in case of failure // Of course, it does not make much sense with i.e. stream iterators - octet_iterator original_it = it; + It original_it = it; utfchar32_t cp = 0; // Determine the sequence length based on the lead octet @@ -278,25 +291,22 @@ namespace iris::utflib return err; } - template - constexpr utf_error validate_next(octet_iterator& it, octet_iterator end) + template + constexpr utf_error validate_next(It& it, It end) { utfchar32_t ignored; return internal::validate_next(it, end, ignored); } - template - constexpr utf_error validate_next16(word_iterator& it, word_iterator end, utfchar32_t& code_point) + template + constexpr utf_error validate_next16(It& it, It end, utfchar32_t& code_point) { - // Make sure the iterator dereferences a large enough type - using word_type = std::iterator_traits::value_type; - static_assert(sizeof(word_type) >= sizeof(utfchar16_t)); // Check the edge case: if (it == end) return utf_error::NOT_ENOUGH_ROOM; // Save the original value of it so we can go back in case of failure // Of course, it does not make much sense with i.e. stream iterators - word_iterator original_it = it; + It original_it = it; utf_error err = utf_error::UTF8_OK; @@ -324,8 +334,9 @@ namespace iris::utflib return err; } - template ::value_type> - constexpr octet_iterator append(utfchar32_t cp, octet_iterator result) + template > + requires std::output_iterator + constexpr It append(utfchar32_t cp, It result) { if (cp < 0x80) // one octet *(result++) = static_cast(cp); @@ -351,8 +362,9 @@ namespace iris::utflib return append, typename container_type::value_type>(cp, result); } - template ::value_type> - constexpr word_iterator append16(utfchar32_t cp, word_iterator result) + template > + requires std::output_iterator + constexpr It append16(utfchar32_t cp, It result) { static_assert(sizeof(word_type) >= sizeof(utfchar16_t)); if (is_in_bmp(cp)) @@ -432,10 +444,10 @@ namespace iris::utflib // Byte order mark constexpr utfchar8_t bom[] = {0xef, 0xbb, 0xbf}; - template - [[nodiscard]] constexpr octet_iterator find_invalid(octet_iterator start, octet_iterator end) + template + [[nodiscard]] constexpr It find_invalid(It start, It end) { - octet_iterator result = start; + It result = start; while (result != end) { internal::utf_error err_code = internal::validate_next(result, end); if (err_code != internal::utf_error::UTF8_OK) @@ -456,8 +468,8 @@ namespace iris::utflib return (invalid == s.end()) ? std::u8string_view::npos : static_cast(invalid - s.begin()); } - template - [[nodiscard]] constexpr bool is_valid(octet_iterator start, octet_iterator end) + template + [[nodiscard]] constexpr bool is_valid(It start, It end) { return (utflib::find_invalid(start, end) == end); } @@ -472,8 +484,8 @@ namespace iris::utflib return is_valid(s.begin(), s.end()); } - template - [[nodiscard]] constexpr bool starts_with_bom(octet_iterator it, octet_iterator end) + template + [[nodiscard]] constexpr bool starts_with_bom(It it, It end) { return (((it != end) && (internal::mask8(*it++)) == bom[0]) && ((it != end) && (internal::mask8(*it++)) == bom[1]) && ((it != end) && (internal::mask8(*it)) == bom[2])); } @@ -488,8 +500,8 @@ namespace iris::utflib return starts_with_bom(s.begin(), s.end()); } - template - constexpr octet_iterator append(utfchar32_t cp, octet_iterator result) + template // TODO: add constraints + constexpr It append(utfchar32_t cp, It result) { if (!internal::is_code_point_valid(cp)) throw invalid_code_point(cp); @@ -507,8 +519,8 @@ namespace iris::utflib append(cp, std::back_inserter(s)); } - template - constexpr word_iterator append16(utfchar32_t cp, word_iterator result) + template // TODO: add constraints + constexpr It append16(utfchar32_t cp, It result) { if (!internal::is_code_point_valid(cp)) throw invalid_code_point(cp); @@ -521,15 +533,15 @@ namespace iris::utflib append16(cp, std::back_inserter(s)); } - template - constexpr output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out, utfchar32_t replacement) + template // TODO: add constraints + constexpr Out replace_invalid(It start, It end, Out out, utfchar32_t replacement) { while (start != end) { - octet_iterator sequence_start = start; + It sequence_start = start; internal::utf_error err_code = internal::validate_next(start, end); switch (err_code) { case internal::utf_error::UTF8_OK: - for (octet_iterator it = sequence_start; it != start; ++it) + for (It it = sequence_start; it != start; ++it) *out++ = *it; break; case internal::utf_error::NOT_ENOUGH_ROOM: @@ -554,8 +566,8 @@ namespace iris::utflib return out; } - template - constexpr output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out) + template // TODO: add constraints + constexpr Out replace_invalid(It start, It end, Out out) { constexpr utfchar32_t replacement_marker = static_cast(internal::mask16(0xfffd)); return utflib::replace_invalid(start, end, out, replacement_marker); @@ -589,8 +601,8 @@ namespace iris::utflib return result; } - template - [[nodiscard]] constexpr utfchar32_t next(octet_iterator& it, octet_iterator end) + template + [[nodiscard]] constexpr utfchar32_t next(It& it, It end) { utfchar32_t cp = 0; internal::utf_error err_code = internal::validate_next(it, end, cp); @@ -609,8 +621,8 @@ namespace iris::utflib return cp; } - template - [[nodiscard]] constexpr utfchar32_t next16(word_iterator& it, word_iterator end) + template + [[nodiscard]] constexpr utfchar32_t next16(It& it, It end) { utfchar32_t cp = 0; internal::utf_error err_code = internal::validate_next16(it, end, cp); @@ -619,20 +631,20 @@ namespace iris::utflib return cp; } - template - [[nodiscard]] constexpr utfchar32_t peek_next(octet_iterator it, octet_iterator end) + template + [[nodiscard]] constexpr utfchar32_t peek_next(It it, It end) { return utflib::next(it, end); } - template - constexpr utfchar32_t prior(octet_iterator& it, octet_iterator start) + template + [[nodiscard]] constexpr utfchar32_t prior(It& it, It start) { // can't do much if it == start if (it == start) throw not_enough_room(); - octet_iterator end = it; + It end = it; // Go back until we hit either a lead octet or start while (internal::is_trail(*(--it))) if (it == start) @@ -640,32 +652,33 @@ namespace iris::utflib return utflib::peek_next(it, end); } - template - constexpr void advance(octet_iterator& it, distance_type n, octet_iterator end) + template + constexpr void advance(It& it, distance_type n, It end) { const distance_type zero(0); if (n < zero) { // backward for (distance_type i = n; i < zero; ++i) - utflib::prior(it, end); + (void)utflib::prior(it, end); } else { // forward for (distance_type i = zero; i < n; ++i) - utflib::next(it, end); + (void)utflib::next(it, end); } } - template - [[nodiscard]] constexpr typename std::iterator_traits::difference_type distance(octet_iterator first, octet_iterator last) + template + requires std::random_access_iterator + [[nodiscard]] constexpr typename std::iterator_traits::difference_type distance(It first, It last) { - typename std::iterator_traits::difference_type dist; - for (dist = 0; first < last; ++dist) - utflib::next(first, last); + typename std::iterator_traits::difference_type dist; + for (dist = 0; first < last; ++dist) // TODO: investigate whether this operator< is needed + (void)utflib::next(first, last); return dist; } - template - constexpr octet_iterator utf16to8(u16bit_iterator start, u16bit_iterator end, octet_iterator result) + template // TODO: add constraints + constexpr OutIt utf16to8(It start, It end, OutIt result) { while (start != end) { utfchar32_t cp = static_cast(internal::mask16(*start++)); @@ -704,10 +717,10 @@ namespace iris::utflib return result; } - template - constexpr u16bit_iterator utf8to16(octet_iterator start, octet_iterator end, u16bit_iterator result) + template // TODO: add constraints + constexpr OutIt utf8to16(It start, It end, OutIt result) { - while (start < end) { + while (start < end) { // TODO: replace < with != const utfchar32_t cp = utflib::next(start, end); if (cp > 0xffff) { // make a surrogate pair *result++ = static_cast((cp >> 10) + internal::LEAD_OFFSET); @@ -732,8 +745,8 @@ namespace iris::utflib return result; } - template - constexpr octet_iterator utf32to8(u32bit_iterator start, u32bit_iterator end, octet_iterator result) + template // TODO: add constraints + constexpr OutIt utf32to8(It start, It end, OutIt result) { while (start != end) result = utflib::append(*(start++), result); @@ -755,10 +768,10 @@ namespace iris::utflib return result; } - template - constexpr u32bit_iterator utf8to32(octet_iterator start, octet_iterator end, u32bit_iterator result) + template + constexpr OutIt utf8to32(It start, It end, OutIt result) { - while (start < end) + while (start < end) // TODO: replace < with != (*result++) = utflib::next(start, end); return result; @@ -767,24 +780,24 @@ namespace iris::utflib [[nodiscard]] constexpr std::u32string utf8to32(std::string_view s) { std::u32string result; - utf8to32(s.begin(), s.end(), std::back_inserter(result)); + utflib::utf8to32(s.begin(), s.end(), std::back_inserter(result)); return result; } [[nodiscard]] constexpr std::u32string utf8to32(std::u8string_view s) { std::u32string result; - utf8to32(s.begin(), s.end(), std::back_inserter(result)); + utflib::utf8to32(s.begin(), s.end(), std::back_inserter(result)); return result; } // The iterator class - template + template class iterator { - octet_iterator it; - octet_iterator range_start; - octet_iterator range_end; + It it; + It range_start; + It range_end; public: using value_type = utfchar32_t; @@ -792,20 +805,22 @@ namespace iris::utflib using reference = utfchar32_t&; using difference_type = std::ptrdiff_t; using iterator_category = std::bidirectional_iterator_tag; - [[nodiscard]] constexpr iterator() {} - [[nodiscard]] constexpr explicit iterator(const octet_iterator& octet_it, const octet_iterator& rangestart, const octet_iterator& rangeend) + constexpr iterator() {} + constexpr explicit iterator(It octet_it, It rangestart, It rangeend) : it(octet_it) , range_start(rangestart) , range_end(rangeend) { - if (it < range_start || it > range_end) - throw std::out_of_range("Invalid utf-8 iterator position"); + if constexpr (std::random_access_iterator) { + if (it < range_start || it > range_end) + throw std::out_of_range("Invalid utf-8 iterator position"); + } } // the default "big three" are OK - [[nodiscard]] constexpr octet_iterator base() const { return it; } + [[nodiscard]] constexpr It base() const { return it; } [[nodiscard]] constexpr utfchar32_t operator*() const { - octet_iterator temp = it; + It temp = it; return utflib::next(temp, range_end); } [[nodiscard]] constexpr bool operator==(const iterator& rhs) const @@ -816,24 +831,24 @@ namespace iris::utflib } constexpr iterator& operator++() { - utflib::next(it, range_end); + (void)utflib::next(it, range_end); return *this; } constexpr iterator operator++(int) { iterator temp = *this; - utflib::next(it, range_end); + (void)utflib::next(it, range_end); return temp; } constexpr iterator& operator--() { - utflib::prior(it, range_start); + (void)utflib::prior(it, range_start); return *this; } constexpr iterator operator--(int) { iterator temp = *this; - utflib::prior(it, range_start); + (void)utflib::prior(it, range_start); return temp; } }; // class iterator diff --git a/tests/apitests.cpp b/tests/apitests.cpp index fbc7b13..2c80daf 100644 --- a/tests/apitests.cpp +++ b/tests/apitests.cpp @@ -153,9 +153,9 @@ TEST(CheckedAPITests, test_distance) TEST(CheckedAPITests, test_utf32to8) { - unsigned int utf32string[] = {0x448, 0x65E5, 0x10346, 0}; + char32_t utf32string[] = {0x448, 0x65E5, 0x10346, 0}; string utf8result; - utf32to8(utf32string, utf32string + 3, back_inserter(utf8result)); + iris::utflib::utf32to8(utf32string, utf32string + 3, back_inserter(utf8result)); EXPECT_EQ (utf8result.size(), 9); } @@ -163,23 +163,23 @@ TEST(CheckedAPITests, test_utf8to32) { const char* twochars = "\xe6\x97\xa5\xd1\x88"; vector utf32result; - utf8to32(twochars, twochars + 5, back_inserter(utf32result)); + iris::utflib::utf8to32(twochars, twochars + 5, back_inserter(utf32result)); EXPECT_EQ (utf32result.size(), 2); } TEST(CheckedAPITests, test_utf16to8) { - unsigned short utf16string[] = {0x41, 0x0448, 0x65e5, 0xd834, 0xdd1e}; + char16_t utf16string[] = {0x41, 0x0448, 0x65e5, 0xd834, 0xdd1e}; string utf8result; - utf16to8(utf16string, utf16string + 5, back_inserter(utf8result)); + iris::utflib::utf16to8(utf16string, utf16string + 5, back_inserter(utf8result)); EXPECT_EQ (utf8result.size(), 10); } TEST(CheckedAPITests, test_utf8to16) { char utf8_with_surrogates[] = "\xe6\x97\xa5\xd1\x88\xf0\x9d\x84\x9e"; - vector utf16result; - utf8to16(utf8_with_surrogates, utf8_with_surrogates + 9, back_inserter(utf16result)); + vector utf16result; + iris::utflib::utf8to16(utf8_with_surrogates, utf8_with_surrogates + 9, back_inserter(utf16result)); EXPECT_EQ (utf16result.size(), 4); EXPECT_EQ (utf16result[2], 0xd834); EXPECT_EQ (utf16result[3], 0xdd1e); diff --git a/tests/test_cpp11.cpp b/tests/test_cpp11.cpp index 33e88ff..9de19be 100644 --- a/tests/test_cpp11.cpp +++ b/tests/test_cpp11.cpp @@ -61,8 +61,8 @@ TEST(CPP11APITests, test_utf8to16) EXPECT_EQ (utf16result[2], 0xd834); EXPECT_EQ (utf16result[3], 0xdd1e); // Just to make sure it compiles with string literals - utf8to16(u8"simple"); - utf8to16("simple"); + EXPECT_EQ(utf8to16(u8"simple"), u"simple"); + EXPECT_EQ(utf8to16("simple"), u"simple"); } TEST(CPP11APITests, test_utf32to8) From 4e907c29484f9357a7cdcdc11f87b9f6d36f0be4 Mon Sep 17 00:00:00 2001 From: yaito3014 Date: Wed, 18 Feb 2026 08:49:59 +0900 Subject: [PATCH 18/28] Accept sentinel that differs in type --- source/utf8.h | 92 +++++++++++++++++++++++++-------------------------- 1 file changed, 46 insertions(+), 46 deletions(-) diff --git a/source/utf8.h b/source/utf8.h index 8ce9af6..4aae338 100644 --- a/source/utf8.h +++ b/source/utf8.h @@ -149,8 +149,8 @@ namespace iris::utflib } /// Helper for get_sequence_x - template - constexpr utf_error increase_safely(It& it, It end) + template Se> + constexpr utf_error increase_safely(It& it, Se end) { if (++it == end) return utf_error::NOT_ENOUGH_ROOM; @@ -169,8 +169,8 @@ namespace iris::utflib } while (false) /// get_sequence_x functions decode utf-8 sequences of the length x - template - constexpr utf_error get_sequence_1(It& it, It end, utfchar32_t& code_point) + template Se> + constexpr utf_error get_sequence_1(It& it, Se end, utfchar32_t& code_point) { if (it == end) return utf_error::NOT_ENOUGH_ROOM; @@ -180,8 +180,8 @@ namespace iris::utflib return utf_error::UTF8_OK; } - template - constexpr utf_error get_sequence_2(It& it, It end, utfchar32_t& code_point) + template Se> + constexpr utf_error get_sequence_2(It& it, Se end, utfchar32_t& code_point) { if (it == end) return utf_error::NOT_ENOUGH_ROOM; @@ -195,8 +195,8 @@ namespace iris::utflib return utf_error::UTF8_OK; } - template - constexpr utf_error get_sequence_3(It& it, It end, utfchar32_t& code_point) + template Se> + constexpr utf_error get_sequence_3(It& it, Se end, utfchar32_t& code_point) { if (it == end) return utf_error::NOT_ENOUGH_ROOM; @@ -214,8 +214,8 @@ namespace iris::utflib return utf_error::UTF8_OK; } - template - constexpr utf_error get_sequence_4(It& it, It end, utfchar32_t& code_point) + template Se> + constexpr utf_error get_sequence_4(It& it, Se end, utfchar32_t& code_point) { if (it == end) return utf_error::NOT_ENOUGH_ROOM; @@ -239,8 +239,8 @@ namespace iris::utflib #undef IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR - template - constexpr utf_error validate_next(It& it, It end, utfchar32_t& code_point) + template Se> + constexpr utf_error validate_next(It& it, Se end, utfchar32_t& code_point) { if (it == end) return utf_error::NOT_ENOUGH_ROOM; @@ -291,15 +291,15 @@ namespace iris::utflib return err; } - template - constexpr utf_error validate_next(It& it, It end) + template Se> + constexpr utf_error validate_next(It& it, Se end) { utfchar32_t ignored; return internal::validate_next(it, end, ignored); } - template - constexpr utf_error validate_next16(It& it, It end, utfchar32_t& code_point) + template Se> + constexpr utf_error validate_next16(It& it, Se end, utfchar32_t& code_point) { // Check the edge case: if (it == end) @@ -444,8 +444,8 @@ namespace iris::utflib // Byte order mark constexpr utfchar8_t bom[] = {0xef, 0xbb, 0xbf}; - template - [[nodiscard]] constexpr It find_invalid(It start, It end) + template Se> + [[nodiscard]] constexpr It find_invalid(It start, Se end) { It result = start; while (result != end) { @@ -468,8 +468,8 @@ namespace iris::utflib return (invalid == s.end()) ? std::u8string_view::npos : static_cast(invalid - s.begin()); } - template - [[nodiscard]] constexpr bool is_valid(It start, It end) + template Se> + [[nodiscard]] constexpr bool is_valid(It start, Se end) { return (utflib::find_invalid(start, end) == end); } @@ -484,8 +484,8 @@ namespace iris::utflib return is_valid(s.begin(), s.end()); } - template - [[nodiscard]] constexpr bool starts_with_bom(It it, It end) + template Se> + [[nodiscard]] constexpr bool starts_with_bom(It it, Se end) { return (((it != end) && (internal::mask8(*it++)) == bom[0]) && ((it != end) && (internal::mask8(*it++)) == bom[1]) && ((it != end) && (internal::mask8(*it)) == bom[2])); } @@ -533,8 +533,8 @@ namespace iris::utflib append16(cp, std::back_inserter(s)); } - template // TODO: add constraints - constexpr Out replace_invalid(It start, It end, Out out, utfchar32_t replacement) + template Se, typename Out> // TODO: add constraints + constexpr Out replace_invalid(It start, Se end, Out out, utfchar32_t replacement) { while (start != end) { It sequence_start = start; @@ -566,8 +566,8 @@ namespace iris::utflib return out; } - template // TODO: add constraints - constexpr Out replace_invalid(It start, It end, Out out) + template Se, typename Out> // TODO: add constraints + constexpr Out replace_invalid(It start, Se end, Out out) { constexpr utfchar32_t replacement_marker = static_cast(internal::mask16(0xfffd)); return utflib::replace_invalid(start, end, out, replacement_marker); @@ -601,8 +601,8 @@ namespace iris::utflib return result; } - template - [[nodiscard]] constexpr utfchar32_t next(It& it, It end) + template Se> + [[nodiscard]] constexpr utfchar32_t next(It& it, Se end) { utfchar32_t cp = 0; internal::utf_error err_code = internal::validate_next(it, end, cp); @@ -621,8 +621,8 @@ namespace iris::utflib return cp; } - template - [[nodiscard]] constexpr utfchar32_t next16(It& it, It end) + template Se> + [[nodiscard]] constexpr utfchar32_t next16(It& it, Se end) { utfchar32_t cp = 0; internal::utf_error err_code = internal::validate_next16(it, end, cp); @@ -631,14 +631,14 @@ namespace iris::utflib return cp; } - template - [[nodiscard]] constexpr utfchar32_t peek_next(It it, It end) + template Se> + [[nodiscard]] constexpr utfchar32_t peek_next(It it, Se end) { return utflib::next(it, end); } - template - [[nodiscard]] constexpr utfchar32_t prior(It& it, It start) + template Se> + [[nodiscard]] constexpr utfchar32_t prior(It& it, Se start) { // can't do much if it == start if (it == start) @@ -652,8 +652,8 @@ namespace iris::utflib return utflib::peek_next(it, end); } - template - constexpr void advance(It& it, distance_type n, It end) + template Se, typename distance_type> + constexpr void advance(It& it, distance_type n, Se end) { const distance_type zero(0); if (n < zero) { @@ -667,9 +667,9 @@ namespace iris::utflib } } - template + template Se> requires std::random_access_iterator - [[nodiscard]] constexpr typename std::iterator_traits::difference_type distance(It first, It last) + [[nodiscard]] constexpr typename std::iterator_traits::difference_type distance(It first, Se last) { typename std::iterator_traits::difference_type dist; for (dist = 0; first < last; ++dist) // TODO: investigate whether this operator< is needed @@ -677,8 +677,8 @@ namespace iris::utflib return dist; } - template // TODO: add constraints - constexpr OutIt utf16to8(It start, It end, OutIt result) + template Se, typename OutIt> // TODO: add constraints + constexpr OutIt utf16to8(It start, Se end, OutIt result) { while (start != end) { utfchar32_t cp = static_cast(internal::mask16(*start++)); @@ -717,8 +717,8 @@ namespace iris::utflib return result; } - template // TODO: add constraints - constexpr OutIt utf8to16(It start, It end, OutIt result) + template Se, typename OutIt> // TODO: add constraints + constexpr OutIt utf8to16(It start, Se end, OutIt result) { while (start < end) { // TODO: replace < with != const utfchar32_t cp = utflib::next(start, end); @@ -745,8 +745,8 @@ namespace iris::utflib return result; } - template // TODO: add constraints - constexpr OutIt utf32to8(It start, It end, OutIt result) + template Se, typename OutIt> // TODO: add constraints + constexpr OutIt utf32to8(It start, Se end, OutIt result) { while (start != end) result = utflib::append(*(start++), result); @@ -768,8 +768,8 @@ namespace iris::utflib return result; } - template - constexpr OutIt utf8to32(It start, It end, OutIt result) + template Se, typename OutIt> + constexpr OutIt utf8to32(It start, Se end, OutIt result) { while (start < end) // TODO: replace < with != (*result++) = utflib::next(start, end); From 1fb3492cecbed1bb8bc88801d8c7af4191a3697a Mon Sep 17 00:00:00 2001 From: yaito3014 Date: Wed, 18 Feb 2026 08:50:09 +0900 Subject: [PATCH 19/28] Fix CMakeLists.txt --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4e3985f..03e0ce8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,7 +18,7 @@ target_sources( TYPE HEADERS BASE_DIRS source FILES - source/utf8.hpp + source/utf8.h ) option(UTF8CPP_ENABLE_TESTS "Build and enable tests" ${PROJECT_IS_TOP_LEVEL}) From cfda0050bce104ae766fd5aa376925990ba906fd Mon Sep 17 00:00:00 2001 From: yaito3014 Date: Wed, 18 Feb 2026 08:50:17 +0900 Subject: [PATCH 20/28] Remove README --- README.md | 2131 ----------------------------------------------------- 1 file changed, 2131 deletions(-) delete mode 100644 README.md diff --git a/README.md b/README.md deleted file mode 100644 index 2a2516d..0000000 --- a/README.md +++ /dev/null @@ -1,2131 +0,0 @@ - - -# UTF8-CPP: UTF-8 with C++ in a Portable Way - - - -## Introduction - -C++ developers still miss an easy and portable way of handling Unicode encoded strings. The original C++ standard (known as C++98 or C++03) is Unicode agnostic. Some progress has been made in the later editions of the standard, but it is still hard to work with Unicode using only the standard facilities. - -I came up with a small, C++98 compatible generic library in order to handle UTF-8 encoded strings. For anybody used to work with STL algorithms and iterators, it should be easy and natural to use. The code is freely available for any purpose - check out the [license](./LICENSE). The library has been used a lot since the first release in 2006 both in commercial and open-source projects and proved to be stable and useful. - -## Table of Contents - -- [UTF8-CPP: UTF-8 with C++ in a Portable Way](#utf8-cpp-utf-8-with-c-in-a-portable-way) - * [Introduction](#introduction) - * [Installation](#installation) - * [Examples of use](#examples-of-use) - + [Introductory Sample](#introductory-sample) - + [Checking if a file contains valid UTF-8 text](#checking-if-a-file-contains-valid-utf-8-text) - + [Ensure that a string contains valid UTF-8 text](#ensure-that-a-string-contains-valid-utf-8-text) - * [Points of interest](#points-of-interest) - - [Design goals and decisions](#design-goals-and-decisions) - - [Alternatives](#alternatives) - * [Reference](#reference) - + [Functions From utf8 Namespace](#functions-from-utf8-namespace) - - [utflib::append](#utf8append) - * [octet_iterator append(utfchar32_t cp, octet_iterator result)](#octet_iterator-appendutfchar32_t-cp-octet_iterator-result) - * [void append(utfchar32_t cp, std::string& s);](#void-appendutfchar32_t-cp-stdstring-s) - - [utflib::append16](#utf8append16) - * [word_iterator append16(utfchar32_t cp, word_iterator result)](#word_iterator-append16utfchar32_t-cp-word_iterator-result) - * [void append(utfchar32_t cp, std::u16string& s)](#void-appendutfchar32_t-cp-stdu16string-s) - - [utflib::next](#utf8next) - - [utflib::next16](#utf8next16) - - [utflib::peek_next](#utf8peek_next) - - [utflib::prior](#utf8prior) - - [utflib::advance](#utf8advance) - - [utflib::distance](#utf8distance) - - [utflib::utf16to8](#utf8utf16to8) - * [octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result)](#octet_iterator-utf16to8-u16bit_iterator-start-u16bit_iterator-end-octet_iterator-result) - * [std::string utf16to8(const std::u16string& s)](#stdstring-utf16to8const-stdu16string-s) - * [std::string utf16to8(std::u16string_view s)](#stdstring-utf16to8stdu16string_view-s) - - [utflib::utf16tou8](#utf8utf16tou8) - * [std::u8string utf16tou8(const std::u16string& s)](#stdu8string-utf16tou8const-stdu16string-s) - * [std::u8string utf16tou8(const std::u16string_view& s)](#stdu8string-utf16tou8const-stdu16string_view-s) - - [utflib::utf8to16](#utf8utf8to16) - * [u16bit_iterator utf8to16 (octet_iterator start, octet_iterator end, u16bit_iterator result)](#u16bit_iterator-utf8to16-octet_iterator-start-octet_iterator-end-u16bit_iterator-result) - * [std::u16string utf8to16(const std::string& s)](#stdu16string-utf8to16const-stdstring-s) - * [std::u16string utf8to16(std::string_view s)](#stdu16string-utf8to16stdstring_view-s) - * [std::u16string utf8to16(std::u8string& s)](#stdu16string-utf8to16stdu8string-s) - * [std::u16string utf8to16(std::u8string_view& s)](#stdu16string-utf8to16stdu8string_view-s) - - [utflib::utf32to8](#utf8utf32to8) - * [octet_iterator utf32to8 (u32bit_iterator start, u32bit_iterator end, octet_iterator result)](#octet_iterator-utf32to8-u32bit_iterator-start-u32bit_iterator-end-octet_iterator-result) - * [std::string utf32to8(const std::u32string& s)](#stdstring-utf32to8const-stdu32string-s) - * [std::u8string utf32to8(const std::u32string& s)](#stdu8string-utf32to8const-stdu32string-s) - * [std::u8string utf32to8(const std::u32string_view& s)](#stdu8string-utf32to8const-stdu32string_view-s) - * [std::string utf32to8(const std::u32string& s)](#stdstring-utf32to8const-stdu32string-s-1) - * [std::string utf32to8(std::u32string_view s)](#stdstring-utf32to8stdu32string_view-s) - - [utflib::utf8to32](#utf8utf8to32) - * [u32bit_iterator utf8to32 (octet_iterator start, octet_iterator end, u32bit_iterator result)](#u32bit_iterator-utf8to32-octet_iterator-start-octet_iterator-end-u32bit_iterator-result) - * [std::u32string utf8to32(const std::u8string& s)](#stdu32string-utf8to32const-stdu8string-s) - * [std::u32string utf8to32(const std::u8string_view& s)](#stdu32string-utf8to32const-stdu8string_view-s) - * [std::u32string utf8to32(const std::string& s)](#stdu32string-utf8to32const-stdstring-s) - * [std::u32string utf8to32(std::string_view s)](#stdu32string-utf8to32stdstring_view-s) - - [utflib::find_invalid](#utf8find_invalid) - * [octet_iterator find_invalid(octet_iterator start, octet_iterator end)](#octet_iterator-find_invalidoctet_iterator-start-octet_iterator-end) - * [const char* find_invalid(const char* str)](#const-char-find_invalidconst-char-str) - * [std::size_t find_invalid(const std::string& s)](#stdsize_t-find_invalidconst-stdstring-s) - * [std::size_t find_invalid(std::string_view s)](#stdsize_t-find_invalidstdstring_view-s) - - [utflib::is_valid](#utf8is_valid) - * [bool is_valid(octet_iterator start, octet_iterator end)](#bool-is_validoctet_iterator-start-octet_iterator-end) - * [bool is_valid(const char* str)](#bool-is_validconst-char-str) - * [bool is_valid(const std::string& s)](#bool-is_validconst-stdstring-s) - * [bool is_valid(std::string_view s)](#bool-is_validstdstring_view-s) - - [utflib::replace_invalid](#utf8replace_invalid) - * [output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out, utfchar32_t replacement)](#output_iterator-replace_invalidoctet_iterator-start-octet_iterator-end-output_iterator-out-utfchar32_t-replacement) - * [std::string replace_invalid(const std::string& s, utfchar32_t replacement)](#stdstring-replace_invalidconst-stdstring-s-utfchar32_t-replacement) - * [std::string replace_invalid(std::string_view s, char32_t replacement)](#stdstring-replace_invalidstdstring_view-s-char32_t-replacement) - - [utflib::starts_with_bom](#utf8starts_with_bom) - * [bool starts_with_bom (octet_iterator it, octet_iterator end)](#bool-starts_with_bom-octet_iterator-it-octet_iterator-end) - * [bool starts_with_bom(const std::string& s)](#bool-starts_with_bomconst-stdstring-s) - * [bool starts_with_bom(std::string_view s)](#bool-starts_with_bomstdstring_view-s) - + [Types From utf8 Namespace](#types-from-utf8-namespace) - - [utflib::exception](#utf8exception) - - [utflib::invalid_code_point](#utf8invalid_code_point) - - [utflib::invalid_utf8](#utf8invalid_utf8) - - [utflib::invalid_utf16](#utf8invalid_utf16) - - [utflib::not_enough_room](#utf8not_enough_room) - - [utflib::iterator](#utf8iterator) - * [Member functions](#member-functions) - + [Functions From utflib::unchecked Namespace](#functions-from-utf8unchecked-namespace) - - [utflib::unchecked::append](#utf8uncheckedappend) - - [utflib::unchecked::append16](#utf8uncheckedappend16) - - [utflib::unchecked::next](#utf8uncheckednext) - - [utflib::next16](#utf8next16-1) - - [utflib::unchecked::peek_next](#utf8uncheckedpeek_next) - - [utflib::unchecked::prior](#utf8uncheckedprior) - - [utflib::unchecked::advance](#utf8uncheckedadvance) - - [utflib::unchecked::distance](#utf8uncheckeddistance) - - [utflib::unchecked::utf16to8](#utf8uncheckedutf16to8) - - [utflib::unchecked::utf8to16](#utf8uncheckedutf8to16) - - [utflib::unchecked::utf32to8](#utf8uncheckedutf32to8) - - [utflib::unchecked::utf8to32](#utf8uncheckedutf8to32) - - [utflib::unchecked::replace_invalid](#utf8uncheckedreplace_invalid) - + [Types From utflib::unchecked Namespace](#types-from-utf8unchecked-namespace) - - [utflib::iterator](#utf8iterator-1) - * [Member functions](#member-functions-1) - - - - - -## Installation - -This is a header-only library and the supported way of deploying it is: -- Download a release from https://github.com/nemtrif/utfcpp/releases into a temporary directory -- Unzip the release -- Copy the content of utfcpp/source file into the directory where you keep include files for your project - - -The CMakeList.txt file was originally made for testing purposes only, but unfortunately over time I accepted contributions that added install target. *This is not a supported way of installing the utfcpp library* and I am considering removing the CMakeList.txt in a future release. - - -## Examples of use - - -### Introductory Sample - -To illustrate the use of the library, let's start with a small but complete program that opens a file containing UTF-8 encoded text, reads it line by line, checks each line for invalid UTF-8 byte sequences, and converts it to UTF-16 encoding and back to UTF-8: - -```cpp -#include -#include -#include -#include -#include "utf8.h" -using namespace std; -int main(int argc, char** argv) -{ - if (argc != 2) { - cout << "\nUsage: docsample filename\n"; - return 0; - } - const char* test_file_path = argv[1]; - // Open the test file (must be UTF-8 encoded) - ifstream fs8(test_file_path); - if (!fs8.is_open()) { - cout << "Could not open " << test_file_path << endl; - return 0; - } - - unsigned line_count = 1; - string line; - // Play with all the lines in the file - while (getline(fs8, line)) { - // check for invalid utf-8 (for a simple yes/no check, there is also utflib::is_valid function) -#if __cplusplus >= 201103L // C++ 11 or later - auto end_it = utflib::find_invalid(line.begin(), line.end()); -#else - string::iterator end_it = utflib::find_invalid(line.begin(), line.end()); -#endif // C++ 11 - if (end_it != line.end()) { - cout << "Invalid UTF-8 encoding detected at line " << line_count << "\n"; - cout << "This part is fine: " << string(line.begin(), end_it) << "\n"; - } - // Get the line length (at least for the valid part) - int length = utflib::distance(line.begin(), end_it); - cout << "Length of line " << line_count << " is " << length << "\n"; - - // Convert it to utf-16 -#if __cplusplus >= 201103L // C++ 11 or later - u16string utf16line = utflib::utf8to16(line); -#else - vector utf16line; - utflib::utf8to16(line.begin(), end_it, back_inserter(utf16line)); -#endif // C++ 11 - // And back to utf-8; -#if __cplusplus >= 201103L // C++ 11 or later - string utf8line = utflib::utf16to8(utf16line); -#else - string utf8line; - utflib::utf16to8(utf16line.begin(), utf16line.end(), back_inserter(utf8line)); -#endif // C++ 11 - // Confirm that the conversion went OK: - if (utf8line != string(line.begin(), end_it)) - cout << "Error in UTF-16 conversion at line: " << line_count << "\n"; - - line_count++; - } - - return 0; -} -``` - -In the previous code sample, for each line we performed a detection of invalid UTF-8 sequences with `find_invalid`; the number of characters (more precisely - the number of Unicode code points, including the end of line and even BOM if there is one) in each line was determined with a use of `utflib::distance`; finally, we have converted each line to UTF-16 encoding with `utf8to16` and back to UTF-8 with `utf16to8`. - -Note a different pattern of usage for old compilers. For instance, this is how we convert -a UTF-8 encoded string to a UTF-16 encoded one with a pre - C++11 compiler: -```cpp - vector utf16line; - utflib::utf8to16(line.begin(), end_it, back_inserter(utf16line)); -``` - -With a more modern compiler, the same operation would look like: -```cpp - u16string utf16line = utflib::utf8to16(line); -``` -If `__cplusplus` macro points to a C++ 11 or later, the library exposes API that takes into -account C++ standard Unicode strings and move semantics. With an older compiler, it is still -possible to use the same functionality, just in a little less convenient way - -In case you do not trust the `__cplusplus` macro or, for instance, do not want to include -the C++ 11 helper functions even with a modern compiler, define `IRIS_UTFLIB_CPP_CPLUSPLUS` macro -before including `utf8.h` and assign it a value for the standard you want to use - the values are the same as for the `__cplusplus` macro. This can be also useful with compilers that are conservative in setting the `__cplusplus` macro even if they have a good support for a recent standard edition - Microsoft's Visual C++ is one example. - - -### Checking if a file contains valid UTF-8 text - -Here is a function that checks whether the content of a file is valid UTF-8 encoded text without reading the content into the memory: - -```cpp -bool valid_utf8_file(const char* file_name) -{ - ifstream ifs(file_name); - if (!ifs) - return false; // even better, throw here - - istreambuf_iterator it(ifs.rdbuf()); - istreambuf_iterator eos; - - return utflib::is_valid(it, eos); -} -``` - -Because the function `utflib::is_valid()` works with input iterators, we were able to pass an `istreambuf_iterator` to `it` and read the content of the file directly without loading it to the memory first. - -Note that other functions that take input iterator arguments can be used in a similar way. For instance, to read the content of a UTF-8 encoded text file and convert the text to UTF-16, just do something like: - -```cpp - utflib::utf8to16(it, eos, back_inserter(u16string)); -``` - - -### Ensure that a string contains valid UTF-8 text - -If we have some text that "probably" contains UTF-8 encoded text and we want to replace any invalid UTF-8 sequence with a replacement character, something like the following function may be used: - -```cpp -void fix_utf8_string(std::string& str) -{ - std::string temp; - utflib::replace_invalid(str.begin(), str.end(), back_inserter(temp)); - str = temp; -} -``` - -The function will replace any invalid UTF-8 sequence with a Unicode replacement character. There is an overloaded function that enables the caller to supply their own replacement character. - - - -## Points of interest - - -#### Design goals and decisions - -The library was designed to be: - -1. Generic: for better or worse, there are many C++ string classes out there, and the library should work with as many of them as possible. -2. Portable: the library should be portable both across different platforms and compilers. The only non-portable code is a small section that declares unsigned integers of different sizes: three typedefs. They can be changed by the users of the library if they don't match their platform. The default setting should work for Windows (both 32 and 64 bit), and most 32 bit and 64 bit Unix derivatives. Support for post C++03 language features is included for modern compilers at API level only, so the library should work even with pretty old compilers. -3. Lightweight: follow the "pay only for what you use" guideline. -4. Unintrusive: avoid forcing any particular design or even programming style on the user. This is a library, not a framework. - - -#### Alternatives - -For alternatives and comparisons, I recommend the following article: [The Wonderfully Terrible World of C and C++ Encoding APIs (with Some Rust)](https://thephd.dev/the-c-c++-rust-string-text-encoding-api-landscape), by JeanHeyd Meneide. In the article, this library is compared with: - -- [simdutf](https://github.com/simdutf/simdutf) -- [iconv](https://www.gnu.org/software/libiconv/) -- [boost.text](https://github.com/tzlaine/text) -- [ICU](https://unicode-org.github.io/icu/userguide/conversion/converters.html) -- [encoding_rs](https://github.com/hsivonen/encoding_rs) -- [Windows API functions for converting text between encodings](https://learn.microsoft.com/en-us/windows/win32/api/stringapiset/) -- [ztd.text](https://github.com/soasis/text/) - -The article presents author's view of the quality of the API design, but also some speed benchmarks. - - -## Reference - - -### Functions From utf8 Namespace - - -#### utflib::append - - -##### octet_iterator append(utfchar32_t cp, octet_iterator result) - -Available in version 1.0 and later. - -Encodes a 32 bit code point as a UTF-8 sequence of octets and appends the sequence to a UTF-8 string. - -```cpp -template -octet_iterator append(utfchar32_t cp, octet_iterator result); -``` - -`octet_iterator`: an output iterator. -`cp`: a 32 bit integer representing a code point to append to the sequence. -`result`: an output iterator to the place in the sequence where to append the code point. -Return value: an iterator pointing to the place after the newly appended sequence. - -Example of use: - -```cpp -unsigned char u[5] = {0,0,0,0,0}; -unsigned char* end = append(0x0448, u); -assert (u[0] == 0xd1 && u[1] == 0x88 && u[2] == 0 && u[3] == 0 && u[4] == 0); -``` - -Note that `append` does not allocate any memory - it is the burden of the caller to make sure there is enough memory allocated for the operation. To make things more interesting, `append` can add anywhere between 1 and 4 octets to the sequence. In practice, you would most often want to use `std::back_inserter` to ensure that the necessary memory is allocated. - -In case of an invalid code point, a `utflib::invalid_code_point` exception is thrown. - - - -##### void append(utfchar32_t cp, std::string& s); - -Available in version 3.0 and later. Prior to 4.0 it required a C++ 11 compiler; the requirement is lifted with 4.0. - -Encodes a 32 bit code point as a UTF-8 sequence of octets and appends the sequence to a UTF-8 string. - -```cpp -void append(utfchar32_t cp, std::string& s); -``` - -`cp`: a code point to append to the string. -`s`: a utf-8 encoded string to append the code point to. - -Example of use: - -```cpp -std::string u; -append(0x0448, u); -assert (u[0] == char(0xd1) && u[1] == char(0x88) && u.length() == 2); -``` - -In case of an invalid code point, a `utflib::invalid_code_point` exception is thrown. - - -#### utflib::append16 - -##### word_iterator append16(utfchar32_t cp, word_iterator result) - -Available in version 4.0 and later. - -Encodes a 32 bit code point as a UTF-16 sequence of words and appends the sequence to a UTF-16 string. - -```cpp -template -word_iterator append16(utfchar32_t cp, word_iterator result); -``` - -`word_iterator`: an output iterator. -`cp`: a 32 bit integer representing a code point to append to the sequence. -`result`: an output iterator to the place in the sequence where to append the code point. -Return value: an iterator pointing to the place after the newly appended sequence. - -Example of use: - -```cpp -unsigned short u[2] = {0,0}; -unsigned short* end = append16(0x0448, u); -assert (u[0] == 0x0448 && u[1] == 0); -``` - -Note that `append16` does not allocate any memory - it is the burden of the caller to make sure there is enough memory allocated for the operation. To make things more interesting, `append16` can add either one or two words to the sequence. In practice, you would most often want to use `std::back_inserter` to ensure that the necessary memory is allocated. - -In case of an invalid code point, a `utflib::invalid_code_point` exception is thrown. - - - -##### void append(utfchar32_t cp, std::u16string& s) - -Available in version 4.0 and later. Requires a C++11 compliant compiler. - -Encodes a 32 bit code point as a UTF-16 sequence of words and appends the sequence to a UTF-16 string. - -```cpp -void append(utfchar32_t cp, std::u16string& s); -``` - -`cp`: a code point to append to the string. -`s`: a utf-16 encoded string to append the code point to. - -Example of use: - -```cpp -std::u16string u; -append(0x0448, u); -assert (u[0] == 0x0448 && u.length() == 1); -``` - -In case of an invalid code point, a `utflib::invalid_code_point` exception is thrown. - - - -#### utflib::next - -Available in version 1.0 and later. - -Given the iterator to the beginning of the UTF-8 sequence, it returns the code point and moves the iterator to the next position. - -```cpp -template -utfchar32_t next(octet_iterator& it, octet_iterator end); -``` - -`octet_iterator`: an input iterator. -`it`: a reference to an iterator pointing to the beginning of an UTF-8 encoded code point. After the function returns, it is incremented to point to the beginning of the next code point. -`end`: end of the UTF-8 sequence to be processed. If `it` gets equal to `end` during the extraction of a code point, an `utflib::not_enough_room` exception is thrown. -Return value: the 32 bit representation of the processed UTF-8 code point. - -Example of use: - -```cpp -char* twochars = "\xe6\x97\xa5\xd1\x88"; -char* w = twochars; -int cp = next(w, twochars + 6); -assert (cp == 0x65e5); -assert (w == twochars + 3); -``` - -This function is typically used to iterate through a UTF-8 encoded string. - -In case of an invalid UTF-8 sequence, a `utflib::invalid_utf8` exception is thrown. - - -#### utflib::next16 - -Available in version 4.0 and later. - -Given the iterator to the beginning of the UTF-16 sequence, it returns the code point and moves the iterator to the next position. - -```cpp -template -utfchar32_t next16(word_iterator& it, word_iterator end); -``` - -`word_iterator`: an input iterator. -`it`: a reference to an iterator pointing to the beginning of an UTF-16 encoded code point. After the function returns, it is incremented to point to the beginning of the next code point. -`end`: end of the UTF-16 sequence to be processed. If `it` gets equal to `end` during the extraction of a code point, an `utflib::not_enough_room` exception is thrown. -Return value: the 32 bit representation of the processed UTF-16 code point. - -Example of use: - -```cpp -const unsigned short u[3] = {0x65e5, 0xd800, 0xdf46}; -const unsigned short* w = u; -int cp = next16(w, w + 3); -assert (cp, 0x65e5); -assert (w, u + 1); -``` - -This function is typically used to iterate through a UTF-16 encoded string. - -In case of an invalid UTF-16 sequence, a `utflib::invalid_utf8` exception is thrown. - - - -#### utflib::peek_next - -Available in version 2.1 and later. - -Given the iterator to the beginning of the UTF-8 sequence, it returns the code point for the following sequence without changing the value of the iterator. - -```cpp -template -utfchar32_t peek_next(octet_iterator it, octet_iterator end); -``` - - -`octet_iterator`: an input iterator. -`it`: an iterator pointing to the beginning of an UTF-8 encoded code point. -`end`: end of the UTF-8 sequence to be processed. If `it` gets equal to `end` during the extraction of a code point, an `utflib::not_enough_room` exception is thrown. -Return value: the 32 bit representation of the processed UTF-8 code point. - -Example of use: - -```cpp -char* twochars = "\xe6\x97\xa5\xd1\x88"; -char* w = twochars; -int cp = peek_next(w, twochars + 6); -assert (cp == 0x65e5); -assert (w == twochars); -``` - -In case of an invalid UTF-8 sequence, a `utflib::invalid_utf8` exception is thrown. - - -#### utflib::prior - -Available in version 1.02 and later. - -Given a reference to an iterator pointing to an octet in a UTF-8 sequence, it decreases the iterator until it hits the beginning of the previous UTF-8 encoded code point and returns the 32 bits representation of the code point. - -```cpp -template -utfchar32_t prior(octet_iterator& it, octet_iterator start); -``` - -`octet_iterator`: a bidirectional iterator. -`it`: a reference pointing to an octet within a UTF-8 encoded string. After the function returns, it is decremented to point to the beginning of the previous code point. -`start`: an iterator to the beginning of the sequence where the search for the beginning of a code point is performed. It is a safety measure to prevent passing the beginning of the string in the search for a UTF-8 lead octet. - Return value: the 32 bit representation of the previous code point. - -Example of use: - -```cpp -char* twochars = "\xe6\x97\xa5\xd1\x88"; -unsigned char* w = twochars + 3; -int cp = prior (w, twochars); -assert (cp == 0x65e5); -assert (w == twochars); -``` - -This function has two purposes: one is two iterate backwards through a UTF-8 encoded string. Note that it is usually a better idea to iterate forward instead, since `utflib::next` is faster. The second purpose is to find a beginning of a UTF-8 sequence if we have a random position within a string. Note that in that case `utflib::prior` may not detect an invalid UTF-8 sequence in some scenarios: for instance if there are superfluous trail octets, it will just skip them. - -`it` will typically point to the beginning of a code point, and `start` will point to the beginning of the string to ensure we don't go backwards too far. `it` is decreased until it points to a lead UTF-8 octet, and then the UTF-8 sequence beginning with that octet is decoded to a 32 bit representation and returned. - -In case `start` is reached before a UTF-8 lead octet is hit, or if an invalid UTF-8 sequence is started by the lead octet, an `invalid_utf8` exception is thrown. - -In case `start` equals `it`, a `not_enough_room` exception is thrown. - - -#### utflib::advance -Available in version 1.0 and later. - -Advances an iterator by the specified number of code points within an UTF-8 sequence. - -```cpp -template -void advance (octet_iterator& it, distance_type n, octet_iterator end); -``` - -`octet_iterator`: an input iterator. -`distance_type`: an integral type convertible to `octet_iterator`'s difference type. -`it`: a reference to an iterator pointing to the beginning of an UTF-8 encoded code point. After the function returns, it is incremented to point to the nth following code point. -`n`: number of code points `it` should be advanced. A negative value means decrement. -`end`: limit of the UTF-8 sequence to be processed. If `n` is positive and `it` gets equal to `end` during the extraction of a code point, an `utflib::not_enough_room` exception is thrown. If `n` is negative and `it` reaches `end` while `it` points t a trail byte of a UTF-8 sequence, a `utflib::invalid_code_point` exception is thrown. - -Example of use: - -```cpp -char* twochars = "\xe6\x97\xa5\xd1\x88"; -unsigned char* w = twochars; -advance (w, 2, twochars + 6); -assert (w == twochars + 5); -advance (w, -2, twochars); -assert (w == twochars); -``` - -In case of an invalid code point, a `utflib::invalid_code_point` exception is thrown. - - -#### utflib::distance - -Available in version 1.0 and later. - -Given the iterators to two UTF-8 encoded code points in a sequence, returns the number of code points between them. - -```cpp -template -typename std::iterator_traits::difference_type distance (octet_iterator first, octet_iterator last); -``` - -`octet_iterator`: an input iterator. -`first`: an iterator to a beginning of a UTF-8 encoded code point. -`last`: an iterator to a "post-end" of the last UTF-8 encoded code point in the sequence we are trying to determine the length. It can be the beginning of a new code point, or not. - Return value the distance between the iterators, in code points. - -Example of use: - -```cpp -char* twochars = "\xe6\x97\xa5\xd1\x88"; -size_t dist = utflib::distance(twochars, twochars + 5); -assert (dist == 2); -``` - -This function is used to find the length (in code points) of a UTF-8 encoded string. The reason it is called _distance_, rather than, say, _length_ is mainly because developers are used that _length_ is an O(1) function. Computing the length of an UTF-8 string is a linear operation, and it looked better to model it after `std::distance` algorithm. - -In case of an invalid UTF-8 sequence, a `utflib::invalid_utf8` exception is thrown. If `last` does not point to the past-of-end of a UTF-8 sequence, a `utflib::not_enough_room` exception is thrown. - - -#### utflib::utf16to8 - -##### octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result) - -Available in version 1.0 and later. - -Converts a UTF-16 encoded string to UTF-8. - -```cpp -template -octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result); -``` - -`u16bit_iterator`: an input iterator. -`octet_iterator`: an output iterator. -`start`: an iterator pointing to the beginning of the UTF-16 encoded string to convert. -`end`: an iterator pointing to pass-the-end of the UTF-16 encoded string to convert. -`result`: an output iterator to the place in the UTF-8 string where to append the result of conversion. -Return value: An iterator pointing to the place after the appended UTF-8 string. - -Example of use: - -```cpp -unsigned short utf16string[] = {0x41, 0x0448, 0x65e5, 0xd834, 0xdd1e}; -vector utf8result; -utf16to8(utf16string, utf16string + 5, back_inserter(utf8result)); -assert (utf8result.size() == 10); -``` - -In case of invalid UTF-16 sequence, a `utflib::invalid_utf16` exception is thrown. - - - -##### std::string utf16to8(const std::u16string& s) - -Available in version 3.0 and later. Requires a C++ 11 compliant compiler. - -Converts a UTF-16 encoded string to UTF-8. - -```cpp -std::string utf16to8(const std::u16string& s); -``` - -`s`: a UTF-16 encoded string. -Return value: A UTF-8 encoded string. - -Example of use: - -```cpp - u16string utf16string = {0x41, 0x0448, 0x65e5, 0xd834, 0xdd1e}; - string u = utf16to8(utf16string); - assert (u.size() == 10); -``` - -In case of invalid UTF-16 sequence, a `utflib::invalid_utf16` exception is thrown. - - -##### std::string utf16to8(std::u16string_view s) - -Available in version 3.2 and later. Requires a C++ 17 compliant compiler. - -Converts a UTF-16 encoded string to UTF-8. - -```cpp -std::string utf16to8(std::u16string_view s); -``` - -`s`: a UTF-16 encoded string. -Return value: A UTF-8 encoded string. - -Example of use: - -```cpp - u16string utf16string = {0x41, 0x0448, 0x65e5, 0xd834, 0xdd1e}; - u16string_view utf16stringview(u16string); - string u = utf16to8(utf16string); - assert (u.size() == 10); -``` - -In case of invalid UTF-16 sequence, a `utflib::invalid_utf16` exception is thrown. - - -#### utflib::utf16tou8 - -##### std::u8string utf16tou8(const std::u16string& s) - -Available in version 4.0 and later. Requires a C++ 20 compliant compiler. - -Converts a UTF-16 encoded string to UTF-8. - -```cpp -std::u8string utf16tou8(const std::u16string& s); -``` - -`s`: a UTF-16 encoded string. -Return value: A UTF-8 encoded string. - -Example of use: - -```cpp - u16string utf16string = {0x41, 0x0448, 0x65e5, 0xd834, 0xdd1e}; - u8string u = utf16tou8(utf16string); - assert (u.size() == 10); -``` - -In case of invalid UTF-16 sequence, a `utflib::invalid_utf16` exception is thrown. - - -##### std::u8string utf16tou8(const std::u16string_view& s) - -Available in version 4.0 and later. Requires a C++ 20 compliant compiler. - -Converts a UTF-16 encoded string to UTF-8. - -```cpp -std::u8string utf16tou8(const std::u16string_view& s); -``` - -`s`: a UTF-16 encoded string. -Return value: A UTF-8 encoded string. - -Example of use: - -```cpp - u16string utf16string = {0x41, 0x0448, 0x65e5, 0xd834, 0xdd1e}; - u16string_view utf16stringview(u16string); - u8string u = utf16tou8(utf16string); - assert (u.size() == 10); -``` - -In case of invalid UTF-16 sequence, a `utflib::invalid_utf16` exception is thrown. - - -#### utflib::utf8to16 - -##### u16bit_iterator utf8to16 (octet_iterator start, octet_iterator end, u16bit_iterator result) - -Available in version 1.0 and later. - -Converts an UTF-8 encoded string to UTF-16 - -```cpp -template -u16bit_iterator utf8to16 (octet_iterator start, octet_iterator end, u16bit_iterator result); -``` - -`octet_iterator`: an input iterator. -`u16bit_iterator`: an output iterator. -`start`: an iterator pointing to the beginning of the UTF-8 encoded string to convert. -`end`: an iterator pointing to pass-the-end of the UTF-8 encoded string to convert. -`result`: an output iterator to the place in the UTF-16 string where to append the result of conversion. -Return value: An iterator pointing to the place after the appended UTF-16 string. - -Example of use: - -```cpp -char utf8_with_surrogates[] = "\xe6\x97\xa5\xd1\x88\xf0\x9d\x84\x9e"; -vector utf16result; -utf8to16(utf8_with_surrogates, utf8_with_surrogates + 9, back_inserter(utf16result)); -assert (utf16result.size() == 4); -assert (utf16result[2] == 0xd834); -assert (utf16result[3] == 0xdd1e); -``` - -In case of an invalid UTF-8 sequence, a `utflib::invalid_utf8` exception is thrown. If `end` does not point to the past-of-end of a UTF-8 sequence, a `utflib::not_enough_room` exception is thrown. - - - - -##### std::u16string utf8to16(const std::string& s) - -Available in version 3.0 and later. Requires a C++ 11 compliant compiler. - -Converts an UTF-8 encoded string to UTF-16. - -```cpp -std::u16string utf8to16(const std::string& s); -``` - -`s`: an UTF-8 encoded string to convert. -Return value: A UTF-16 encoded string - -Example of use: - -```cpp -string utf8_with_surrogates = "\xe6\x97\xa5\xd1\x88\xf0\x9d\x84\x9e"; -u16string utf16result = utf8to16(utf8_with_surrogates); -assert (utf16result.length() == 4); -assert (utf16result[2] == 0xd834); -assert (utf16result[3] == 0xdd1e); -``` - -In case of an invalid UTF-8 sequence, a `utflib::invalid_utf8` exception is thrown. - - - -##### std::u16string utf8to16(std::string_view s) - -Available in version 3.2 and later. Requires a C++ 17 compliant compiler. - -Converts an UTF-8 encoded string to UTF-16. - -```cpp -std::u16string utf8to16(std::string_view s); -``` - -`s`: an UTF-8 encoded string to convert. -Return value: A UTF-16 encoded string - -Example of use: - -```cpp -string_view utf8_with_surrogates = "\xe6\x97\xa5\xd1\x88\xf0\x9d\x84\x9e"; -u16string utf16result = utf8to16(utf8_with_surrogates); -assert (utf16result.length() == 4); -assert (utf16result[2] == 0xd834); -assert (utf16result[3] == 0xdd1e); -``` - -In case of an invalid UTF-8 sequence, a `utflib::invalid_utf8` exception is thrown. - - - -##### std::u16string utf8to16(std::u8string& s) - -Available in version 4.0 and later. Requires a C++ 20 compliant compiler. - -Converts an UTF-8 encoded string to UTF-16. - -```cpp -std::u16string utf8to16(std::u8string& s); -``` - -`s`: an UTF-8 encoded string to convert. -Return value: A UTF-16 encoded string - -Example of use: - -```cpp -std::u8string utf8_with_surrogates = "\xe6\x97\xa5\xd1\x88\xf0\x9d\x84\x9e"; -std::u16string utf16result = utf8to16(utf8_with_surrogates); -assert (utf16result.length() == 4); -assert (utf16result[2] == 0xd834); -assert (utf16result[3] == 0xdd1e); -``` - -In case of an invalid UTF-8 sequence, a `utflib::invalid_utf8` exception is thrown. - - - -##### std::u16string utf8to16(std::u8string_view& s) - -Available in version 4.0 and later. Requires a C++ 20 compliant compiler. - -Converts an UTF-8 encoded string to UTF-16. - -```cpp -std::u16string utf8to16(std::u8string_view& s); -``` - -`s`: an UTF-8 encoded string to convert. -Return value: A UTF-16 encoded string - -Example of use: - -```cpp -std::u8string utf8_with_surrogates = "\xe6\x97\xa5\xd1\x88\xf0\x9d\x84\x9e"; -std::u8string_view utf8stringview {utf8_with_surrogates} -std::u16string utf16result = utf8to16(utf8stringview); -assert (utf16result.length() == 4); -assert (utf16result[2] == 0xd834); -assert (utf16result[3] == 0xdd1e); -``` - -In case of an invalid UTF-8 sequence, a `utflib::invalid_utf8` exception is thrown. - - -#### utflib::utf32to8 - -##### octet_iterator utf32to8 (u32bit_iterator start, u32bit_iterator end, octet_iterator result) - -Available in version 1.0 and later. - -Converts a UTF-32 encoded string to UTF-8. - -```cpp -template -octet_iterator utf32to8 (u32bit_iterator start, u32bit_iterator end, octet_iterator result); -``` - -`octet_iterator`: an output iterator. -`u32bit_iterator`: an input iterator. -`start`: an iterator pointing to the beginning of the UTF-32 encoded string to convert. -`end`: an iterator pointing to pass-the-end of the UTF-32 encoded string to convert. -`result`: an output iterator to the place in the UTF-8 string where to append the result of conversion. -Return value: An iterator pointing to the place after the appended UTF-8 string. - -Example of use: - -```cpp -int utf32string[] = {0x448, 0x65E5, 0x10346, 0}; -vector utf8result; -utf32to8(utf32string, utf32string + 3, back_inserter(utf8result)); -assert (utf8result.size() == 9); -``` - -In case of invalid UTF-32 string, a `utflib::invalid_code_point` exception is thrown. - - - -##### std::string utf32to8(const std::u32string& s) - -Available in version 3.0 and later. Requires a C++ 11 compliant compiler. - -Converts a UTF-32 encoded string to UTF-8. - -```cpp -std::string utf32to8(const std::u32string& s); -``` - -`s`: a UTF-32 encoded string. -Return value: a UTF-8 encoded string. - -Example of use: - -```cpp -u32string utf32string = {0x448, 0x65E5, 0x10346}; -string utf8result = utf32to8(utf32string); -assert (utf8result.size() == 9); -``` - -In case of invalid UTF-32 string, a `utflib::invalid_code_point` exception is thrown. - - -##### std::u8string utf32to8(const std::u32string& s) - -Available in version 4.0 and later. Requires a C++ 20 compliant compiler. - -Converts a UTF-32 encoded string to UTF-8. - -```cpp -std::u8string utf32to8(const std::u32string& s); -``` - -`s`: a UTF-32 encoded string. -Return value: a UTF-8 encoded string. - -Example of use: - -```cpp -u32string utf32string = {0x448, 0x65E5, 0x10346}; -u8string utf8result = utf32to8(utf32string); -assert (utf8result.size() == 9); -``` - -In case of invalid UTF-32 string, a `utflib::invalid_code_point` exception is thrown. - - - -##### std::u8string utf32to8(const std::u32string_view& s) - -Available in version 4.0 and later. Requires a C++ 20 compliant compiler. - -Converts a UTF-32 encoded string to UTF-8. - -```cpp -std::u8string utf32to8(const std::u32string_view& s); -``` - -`s`: a UTF-32 encoded string. -Return value: a UTF-8 encoded string. - -Example of use: - -```cpp -u32string utf32string = {0x448, 0x65E5, 0x10346}; -u32string_view utf32stringview(utf32string); -u8string utf8result = utf32to8(utf32stringview); -assert (utf8result.size() == 9); -``` - -In case of invalid UTF-32 string, a `utflib::invalid_code_point` exception is thrown. - - - -##### std::string utf32to8(const std::u32string& s) - -Available in version 3.0 and later. Requires a C++ 11 compliant compiler. - -Converts a UTF-32 encoded string to UTF-8. - -```cpp -std::string utf32to8(const std::u32string& s); -``` - -`s`: a UTF-32 encoded string. -Return value: a UTF-8 encoded string. - -Example of use: - -```cpp -u32string utf32string = {0x448, 0x65E5, 0x10346}; -string utf8result = utf32to8(utf32string); -assert (utf8result.size() == 9); -``` - -In case of invalid UTF-32 string, a `utflib::invalid_code_point` exception is thrown. - - -##### std::string utf32to8(std::u32string_view s) - -Available in version 3.2 and later. Requires a C++ 17 compliant compiler. - -Converts a UTF-32 encoded string to UTF-8. - -```cpp -std::string utf32to8(std::u32string_view s); -``` - -`s`: a UTF-32 encoded string. -Return value: a UTF-8 encoded string. - -Example of use: - -```cpp -u32string utf32string = {0x448, 0x65E5, 0x10346}; -u32string_view utf32stringview(utf32string); -string utf8result = utf32to8(utf32stringview); -assert (utf8result.size() == 9); -``` - -In case of invalid UTF-32 string, a `utflib::invalid_code_point` exception is thrown. - - - -#### utflib::utf8to32 - -##### u32bit_iterator utf8to32 (octet_iterator start, octet_iterator end, u32bit_iterator result) - -Available in version 1.0 and later. - -Converts a UTF-8 encoded string to UTF-32. - -```cpp -template -u32bit_iterator utf8to32 (octet_iterator start, octet_iterator end, u32bit_iterator result); -``` - -`octet_iterator`: an input iterator. -`u32bit_iterator`: an output iterator. -`start`: an iterator pointing to the beginning of the UTF-8 encoded string to convert. -`end`: an iterator pointing to pass-the-end of the UTF-8 encoded string to convert. -`result`: an output iterator to the place in the UTF-32 string where to append the result of conversion. -Return value: An iterator pointing to the place after the appended UTF-32 string. - -Example of use: - -```cpp -char* twochars = "\xe6\x97\xa5\xd1\x88"; -vector utf32result; -utf8to32(twochars, twochars + 5, back_inserter(utf32result)); -assert (utf32result.size() == 2); -``` - -In case of an invalid UTF-8 sequence, a `utflib::invalid_utf8` exception is thrown. If `end` does not point to the past-of-end of a UTF-8 sequence, a `utflib::not_enough_room` exception is thrown. - - - - -##### std::u32string utf8to32(const std::u8string& s) - -Available in version 4.0 and later. Requires a C++ 20 compliant compiler. - -Converts a UTF-8 encoded string to UTF-32. - -```cpp -std::u32string utf8to32(const std::u8string& s); -``` - -`s`: a UTF-8 encoded string. -Return value: a UTF-32 encoded string. - -Example of use: - -```cpp -const std::u8string* twochars = u8"\xe6\x97\xa5\xd1\x88"; -u32string utf32result = utf8to32(twochars); -assert (utf32result.size() == 2); -``` - -In case of an invalid UTF-8 sequence, a `utflib::invalid_utf8` exception is thrown. - - - -##### std::u32string utf8to32(const std::u8string_view& s) - -Available in version 4.0 and later. Requires a C++ 20 compliant compiler. - -Converts a UTF-8 encoded string to UTF-32. - -```cpp -std::u32string utf8to32(const std::u8string_view& s); -``` - -`s`: a UTF-8 encoded string. -Return value: a UTF-32 encoded string. - -Example of use: - -```cpp -const u8string* twochars = u8"\xe6\x97\xa5\xd1\x88"; -const u8string_view stringview{twochars}; -u32string utf32result = utf8to32(stringview); -assert (utf32result.size() == 2); -``` - -In case of an invalid UTF-8 sequence, a `utflib::invalid_utf8` exception is thrown. - - - -##### std::u32string utf8to32(const std::string& s) - -Available in version 3.0 and later. Requires a C++ 11 compliant compiler. - -Converts a UTF-8 encoded string to UTF-32. - -```cpp -std::u32string utf8to32(const std::string& s); -``` - -`s`: a UTF-8 encoded string. -Return value: a UTF-32 encoded string. - -Example of use: - -```cpp -const char* twochars = "\xe6\x97\xa5\xd1\x88"; -u32string utf32result = utf8to32(twochars); -assert (utf32result.size() == 2); -``` - -In case of an invalid UTF-8 sequence, a `utflib::invalid_utf8` exception is thrown. - - -##### std::u32string utf8to32(std::string_view s) - -Available in version 3.2 and later. Requires a C++ 17 compliant compiler. - -Converts a UTF-8 encoded string to UTF-32. - -```cpp -std::u32string utf8to32(std::string_view s); -``` - -`s`: a UTF-8 encoded string. -Return value: a UTF-32 encoded string. - -Example of use: - -```cpp -string_view twochars = "\xe6\x97\xa5\xd1\x88"; -u32string utf32result = utf8to32(twochars); -assert (utf32result.size() == 2); -``` - -In case of an invalid UTF-8 sequence, a `utflib::invalid_utf8` exception is thrown. - - -#### utflib::find_invalid - -##### octet_iterator find_invalid(octet_iterator start, octet_iterator end) - -Available in version 1.0 and later. - -Detects an invalid sequence within a UTF-8 string. - -```cpp -template -octet_iterator find_invalid(octet_iterator start, octet_iterator end); -``` - -`octet_iterator`: an input iterator. -`start`: an iterator pointing to the beginning of the UTF-8 string to test for validity. -`end`: an iterator pointing to pass-the-end of the UTF-8 string to test for validity. -Return value: an iterator pointing to the first invalid octet in the UTF-8 string. In case none were found, equals `end`. - -Example of use: - -```cpp -char utf_invalid[] = "\xe6\x97\xa5\xd1\x88\xfa"; -char* invalid = find_invalid(utf_invalid, utf_invalid + 6); -assert (invalid == utf_invalid + 5); -``` - -This function is typically used to make sure a UTF-8 string is valid before processing it with other functions. It is especially important to call it if before doing any of the _unchecked_ operations on it. - - - -##### const char* find_invalid(const char* str) - -Available in version 4.0 and later. - -Detects an invalid sequence within a C-style UTF-8 string. - -```cpp -const char* find_invalid(const char* str); -``` - -`str`: a UTF-8 encoded string. -Return value: a pointer to the first invalid octet in the UTF-8 string. In case none were found, points to the trailing zero byte. - -Example of use: - -```cpp -const char* utf_invalid = "\xe6\x97\xa5\xd1\x88\xfa"; -const char* invalid = find_invalid(utf_invalid); -assert ((invalid - utf_invalid) == 5); -``` - -This function is typically used to make sure a UTF-8 string is valid before processing it with other functions. It is especially important to call it if before doing any of the _unchecked_ operations on it. - - -##### std::size_t find_invalid(const std::string& s) - -Available in version 3.0 and later. Prior to 4.0 it required a C++ 11 compiler; the requirement is lifted with 4.0 - -Detects an invalid sequence within a UTF-8 string. - -```cpp -std::size_t find_invalid(const std::string& s); -``` - -`s`: a UTF-8 encoded string. -Return value: the index of the first invalid octet in the UTF-8 string. In case none were found, equals `std::string::npos`. - -Example of use: - -```cpp -string utf_invalid = "\xe6\x97\xa5\xd1\x88\xfa"; -auto invalid = find_invalid(utf_invalid); -assert (invalid == 5); -``` - -This function is typically used to make sure a UTF-8 string is valid before processing it with other functions. It is especially important to call it if before doing any of the _unchecked_ operations on it. - - -##### std::size_t find_invalid(std::string_view s) - -Available in version 3.2 and later. Requires a C++ 17 compliant compiler. - -Detects an invalid sequence within a UTF-8 string. - -```cpp -std::size_t find_invalid(std::string_view s); -``` - -`s`: a UTF-8 encoded string. -Return value: the index of the first invalid octet in the UTF-8 string. In case none were found, equals `std::string_view::npos`. - -Example of use: - -```cpp -string_view utf_invalid = "\xe6\x97\xa5\xd1\x88\xfa"; -auto invalid = find_invalid(utf_invalid); -assert (invalid == 5); -``` - -This function is typically used to make sure a UTF-8 string is valid before processing it with other functions. It is especially important to call it if before doing any of the _unchecked_ operations on it. - - -#### utflib::is_valid - -##### bool is_valid(octet_iterator start, octet_iterator end) - -Available in version 1.0 and later. - -Checks whether a sequence of octets is a valid UTF-8 string. - -```cpp -template -bool is_valid(octet_iterator start, octet_iterator end); -``` - -`octet_iterator`: an input iterator. -`start`: an iterator pointing to the beginning of the UTF-8 string to test for validity. -`end`: an iterator pointing to pass-the-end of the UTF-8 string to test for validity. -Return value: `true` if the sequence is a valid UTF-8 string; `false` if not. - -Example of use: - -```cpp -char utf_invalid[] = "\xe6\x97\xa5\xd1\x88\xfa"; -bool bvalid = is_valid(utf_invalid, utf_invalid + 6); -assert (bvalid == false); -``` - -`is_valid` is a shorthand for `find_invalid(start, end) == end;`. You may want to use it to make sure that a byte sequence is a valid UTF-8 string without the need to know where it fails if it is not valid. - - - -##### bool is_valid(const char* str) - -Available in version 4.0 and later. - -Checks whether a C-style string contains valid UTF-8 encoded text. - -```cpp -bool is_valid(const char* str); -``` - -`str`: a UTF-8 encoded string. -Return value: `true` if the string contains valid UTF-8 encoded text; `false` if not. - -Example of use: - -```cpp -char utf_invalid[] = "\xe6\x97\xa5\xd1\x88\xfa"; -bool bvalid = is_valid(utf_invalid); -assert (bvalid == false); -``` - -You may want to use `is_valid` to make sure that a string contains valid UTF-8 text without the need to know where it fails if it is not valid. - - - -##### bool is_valid(const std::string& s) - -Available in version 3.0 and later. Prior to 4.0 it required a C++ 11 compiler; the requirement is lifted with 4.0 - -Checks whether a string object contains valid UTF-8 encoded text. - -```cpp -bool is_valid(const std::string& s); -``` - -`s`: a UTF-8 encoded string. -Return value: `true` if the string contains valid UTF-8 encoded text; `false` if not. - -Example of use: - -```cpp -char utf_invalid[] = "\xe6\x97\xa5\xd1\x88\xfa"; -bool bvalid = is_valid(utf_invalid); -assert (bvalid == false); -``` - -You may want to use `is_valid` to make sure that a string contains valid UTF-8 text without the need to know where it fails if it is not valid. - - -##### bool is_valid(std::string_view s) - -Available in version 3.2 and later. Requires a C++ 17 compliant compiler. - -Checks whether a string object contains valid UTF-8 encoded text. - -```cpp -bool is_valid(std::string_view s); -``` - -`s`: a UTF-8 encoded string. -Return value: `true` if the string contains valid UTF-8 encoded text; `false` if not. - -Example of use: - -```cpp -string_view utf_invalid = "\xe6\x97\xa5\xd1\x88\xfa"; -bool bvalid = is_valid(utf_invalid); -assert (bvalid == false); -``` - -You may want to use `is_valid` to make sure that a string contains valid UTF-8 text without the need to know where it fails if it is not valid. - - -#### utflib::replace_invalid - -##### output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out, utfchar32_t replacement) - -Available in version 2.0 and later. - -Replaces all invalid UTF-8 sequences within a string with a replacement marker. - -```cpp -template -output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out, utfchar32_t replacement); -template -output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out); -``` - -`octet_iterator`: an input iterator. -`output_iterator`: an output iterator. -`start`: an iterator pointing to the beginning of the UTF-8 string to look for invalid UTF-8 sequences. -`end`: an iterator pointing to pass-the-end of the UTF-8 string to look for invalid UTF-8 sequences. -`out`: An output iterator to the range where the result of replacement is stored. -`replacement`: A Unicode code point for the replacement marker. The version without this parameter assumes the value `0xfffd` -Return value: An iterator pointing to the place after the UTF-8 string with replaced invalid sequences. - -Example of use: - -```cpp -char invalid_sequence[] = "a\x80\xe0\xa0\xc0\xaf\xed\xa0\x80z"; -vector replace_invalid_result; -replace_invalid (invalid_sequence, invalid_sequence + sizeof(invalid_sequence), back_inserter(replace_invalid_result), '?'); -bvalid = is_valid(replace_invalid_result.begin(), replace_invalid_result.end()); -assert (bvalid); -char* fixed_invalid_sequence = "a????z"; -assert (std::equal(replace_invalid_result.begin(), replace_invalid_result.end(), fixed_invalid_sequence)); -``` - -`replace_invalid` does not perform in-place replacement of invalid sequences. Rather, it produces a copy of the original string with the invalid sequences replaced with a replacement marker. Therefore, `out` must not be in the `[start, end]` range. - - - -##### std::string replace_invalid(const std::string& s, utfchar32_t replacement) - -Available in version 3.0 and later. Prior to 4.0 it required a C++ 11 compiler; the requirement is lifted with 4.0 - -Replaces all invalid UTF-8 sequences within a string with a replacement marker. - -```cpp -std::string replace_invalid(const std::string& s, utfchar32_t replacement); -std::string replace_invalid(const std::string& s); -``` - -`s`: a UTF-8 encoded string. -`replacement`: A Unicode code point for the replacement marker. The version without this parameter assumes the value `0xfffd` -Return value: A UTF-8 encoded string with replaced invalid sequences. - -Example of use: - -```cpp -string invalid_sequence = "a\x80\xe0\xa0\xc0\xaf\xed\xa0\x80z"; -string replace_invalid_result = replace_invalid(invalid_sequence, '?'); -bvalid = is_valid(replace_invalid_result); -assert (bvalid); -const string fixed_invalid_sequence = "a????z"; -assert (fixed_invalid_sequence == replace_invalid_result); -``` - - -##### std::string replace_invalid(std::string_view s, char32_t replacement) - -Available in version 3.2 and later. Requires a C++ 17 compliant compiler. - -Replaces all invalid UTF-8 sequences within a string with a replacement marker. - -```cpp -std::string replace_invalid(std::string_view s, char32_t replacement); -std::string replace_invalid(std::string_view s); -``` - -`s`: a UTF-8 encoded string. -`replacement`: A Unicode code point for the replacement marker. The version without this parameter assumes the value `0xfffd` -Return value: A UTF-8 encoded string with replaced invalid sequences. - -Example of use: - -```cpp -string_view invalid_sequence = "a\x80\xe0\xa0\xc0\xaf\xed\xa0\x80z"; -string replace_invalid_result = replace_invalid(invalid_sequence, '?'); -bool bvalid = is_valid(replace_invalid_result); -assert (bvalid); -const string fixed_invalid_sequence = "a????z"; -assert(fixed_invalid_sequence, replace_invalid_result); -``` - - -#### utflib::starts_with_bom - -##### bool starts_with_bom (octet_iterator it, octet_iterator end) - -Available in version 2.3 and later. - -Checks whether an octet sequence starts with a UTF-8 byte order mark (BOM) - -```cpp -template -bool starts_with_bom (octet_iterator it, octet_iterator end); -``` - -`octet_iterator`: an input iterator. -`it`: beginning of the octet sequence to check -`end`: pass-end of the sequence to check -Return value: `true` if the sequence starts with a UTF-8 byte order mark; `false` if not. - -Example of use: - -```cpp -unsigned char byte_order_mark[] = {0xef, 0xbb, 0xbf}; -bool bbom = starts_with_bom(byte_order_mark, byte_order_mark + sizeof(byte_order_mark)); -assert (bbom == true); -``` - -The typical use of this function is to check the first three bytes of a file. If they form the UTF-8 BOM, we want to skip them before processing the actual UTF-8 encoded text. - - - -##### bool starts_with_bom(const std::string& s) - -Available in version 3.0 and later. Prior to 4.0 it required a C++ 11 compiler; the requirement is lifted with 4.0 - -Checks whether a string starts with a UTF-8 byte order mark (BOM) - -```cpp -bool starts_with_bom(const std::string& s); -``` - -`s`: a UTF-8 encoded string. -Return value: `true` if the string starts with a UTF-8 byte order mark; `false` if not. - -Example of use: - -```cpp -string byte_order_mark = {char(0xef), char(0xbb), char(0xbf)}; -bool bbom = starts_with_bom(byte_order_mark); -assert (bbom == true); -string threechars = "\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88"; -bool no_bbom = starts_with_bom(threechars); -assert (no_bbom == false); - ``` - -The typical use of this function is to check the first three bytes of a file. If they form the UTF-8 BOM, we want to skip them before processing the actual UTF-8 encoded text. - - - -##### bool starts_with_bom(std::string_view s) - -Available in version 3.2 and later. Requires a C++ 17 compliant compiler. - -Checks whether a string starts with a UTF-8 byte order mark (BOM) - -```cpp -bool starts_with_bom(std::string_view s); -``` - -`s`: a UTF-8 encoded string. -Return value: `true` if the string starts with a UTF-8 byte order mark; `false` if not. - -Example of use: - -```cpp -string byte_order_mark = {char(0xef), char(0xbb), char(0xbf)}; -string_view byte_order_mark_view(byte_order_mark); -bool bbom = starts_with_bom(byte_order_mark_view); -assert (bbom); -string_view threechars = "\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88"; -bool no_bbom = starts_with_bom(threechars); -assert (!no_bbom); - ``` - -The typical use of this function is to check the first three bytes of a file. If they form the UTF-8 BOM, we want to skip them before processing the actual UTF-8 encoded text. - - - -### Types From utf8 Namespace - - -#### utflib::exception - -Available in version 2.3 and later. - -Base class for the exceptions thrown by UTF CPP library functions. - -```cpp -class exception : public std::exception {}; -``` - -Example of use: - -```cpp -try { - code_that_uses_utf_cpp_library(); -} -catch(const utflib::exception& utfcpp_ex) { - cerr << utfcpp_ex.what(); -} -``` - - -#### utflib::invalid_code_point - -Available in version 1.0 and later. - -Thrown by UTF8 CPP functions such as `advance` and `next` if an UTF-8 sequence represents and invalid code point. - -```cpp -class invalid_code_point : public exception { -public: - utfchar32_t code_point() const; -}; -``` - -Member function `code_point()` can be used to determine the invalid code point that caused the exception to be thrown. - - -#### utflib::invalid_utf8 - -Available in version 1.0 and later. - -Thrown by UTF8 CPP functions such as `next` and `prior` if an invalid UTF-8 sequence is detected during decoding. - -```cpp -class invalid_utf8 : public exception { -public: - utfchar8_t utf8_octet() const; -}; -``` - -Member function `utf8_octet()` can be used to determine the beginning of the byte sequence that caused the exception to be thrown. - - -#### utflib::invalid_utf16 - -Available in version 1.0 and later. - -Thrown by UTF8 CPP function `utf16to8` if an invalid UTF-16 sequence is detected during decoding. - -```cpp -class invalid_utf16 : public exception { -public: - utfchar16_t utf16_word() const; -}; -``` - -Member function `utf16_word()` can be used to determine the UTF-16 code unit that caused the exception to be thrown. - - -#### utflib::not_enough_room - -Available in version 1.0 and later. - -Thrown by UTF8 CPP functions such as `next` if the end of the decoded UTF-8 sequence was reached before the code point was decoded. - -```cpp -class not_enough_room : public exception {}; -``` - - -#### utflib::iterator - -Available in version 2.0 and later. - -Adapts the underlying octet iterator to iterate over the sequence of code points, rather than raw octets. - -```cpp -template -class iterator; -``` - - -##### Member functions - -`iterator();` the default constructor; the underlying octet_iterator is constructed with its default constructor. - -`explicit iterator (const octet_iterator& octet_it, const octet_iterator& range_start, const octet_iterator& range_end);` a constructor that initializes the underlying octet_iterator with octet_it and sets the range in which the iterator is considered valid. - -`octet_iterator base () const;` returns the underlying octet_iterator. - -`utfchar32_t operator * () const;` decodes the utf-8 sequence the underlying octet_iterator is pointing to and returns the code point. - -`bool operator == (const iterator& rhs) const;` returns `true` if the two underlying iterators are equal. - -`bool operator != (const iterator& rhs) const;` returns `true` if the two underlying iterators are not equal. - -`iterator& operator ++ ();` the prefix increment - moves the iterator to the next UTF-8 encoded code point. - -`iterator operator ++ (int);` the postfix increment - moves the iterator to the next UTF-8 encoded code point and returns the current one. - -`iterator& operator -- ();` the prefix decrement - moves the iterator to the previous UTF-8 encoded code point. - -`iterator operator -- (int);` the postfix decrement - moves the iterator to the previous UTF-8 encoded code point and returns the current one. - -Example of use: - -```cpp -char* threechars = "\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88"; -utflib::iterator it(threechars, threechars, threechars + 9); -utflib::iterator it2 = it; -assert (it2 == it); -assert (*it == 0x10346); -assert (*(++it) == 0x65e5); -assert ((*it++) == 0x65e5); -assert (*it == 0x0448); -assert (it != it2); -utflib::iterator endit (threechars + 9, threechars, threechars + 9); -assert (++it == endit); -assert (*(--it) == 0x0448); -assert ((*it--) == 0x0448); -assert (*it == 0x65e5); -assert (--it == utflib::iterator(threechars, threechars, threechars + 9)); -assert (*it == 0x10346); -``` - -The purpose of `utflib::iterator` adapter is to enable easy iteration as well as the use of STL algorithms with UTF-8 encoded strings. Increment and decrement operators are implemented in terms of `utflib::next()` and `utflib::prior()` functions. - -Note that `utflib::iterator` adapter is a checked iterator. It operates on the range specified in the constructor; any attempt to go out of that range will result in an exception. Even the comparison operators require both iterator object to be constructed against the same range - otherwise an exception is thrown. Typically, the range will be determined by sequence container functions `begin` and `end`, i.e.: - -```cpp -std::string s = "example"; -utflib::iterator i (s.begin(), s.begin(), s.end()); -``` - - -### Functions From utflib::unchecked Namespace - - -#### utflib::unchecked::append - -Available in version 1.0 and later. - -Encodes a 32 bit code point as a UTF-8 sequence of octets and appends the sequence to a UTF-8 string. - -```cpp -template -octet_iterator append(utfchar32_t cp, octet_iterator result); -``` - -`cp`: A 32 bit integer representing a code point to append to the sequence. -`result`: An output iterator to the place in the sequence where to append the code point. -Return value: An iterator pointing to the place after the newly appended sequence. - -Example of use: - -```cpp -unsigned char u[5] = {0,0,0,0,0}; -unsigned char* end = unchecked::append(0x0448, u); -assert (u[0] == 0xd1 && u[1] == 0x88 && u[2] == 0 && u[3] == 0 && u[4] == 0); -``` - -This is a faster but less safe version of `utflib::append`. It does not check for validity of the supplied code point, and may produce an invalid UTF-8 sequence. - - -#### utflib::unchecked::append16 - -Available in version 4.0 and later. - -Encodes a 32 bit code point as a UTF-16 sequence of words and appends the sequence to a UTF-16 string. - -```cpp -template -word_iterator append16(utfchar32_t cp, word_iterator result) -``` - -`cp`: A 32 bit integer representing a code point to append to the sequence. -`result`: An output iterator to the place in the sequence where to append the code point. -Return value: An iterator pointing to the place after the newly appended sequence. - -Example of use: - -```cpp -unsigned short u[5] = {0,0}; -utflib::unchecked::append16(0x0448, u); -assert(u[0], 0x0448); -assert(u[1], 0x0000); -``` - -This is a faster but less safe version of `utflib::append`. It does not check for validity of the supplied code point, and may produce an invalid UTF-8 sequence. - - - -#### utflib::unchecked::next - -Available in version 1.0 and later. - -Given the iterator to the beginning of a UTF-8 sequence, it returns the code point and moves the iterator to the next position. - -```cpp -template -utfchar32_t next(octet_iterator& it); -``` - -`it`: a reference to an iterator pointing to the beginning of an UTF-8 encoded code point. After the function returns, it is incremented to point to the beginning of the next code point. - Return value: the 32 bit representation of the processed UTF-8 code point. - -Example of use: - -```cpp -char* twochars = "\xe6\x97\xa5\xd1\x88"; -char* w = twochars; -int cp = unchecked::next(w); -assert (cp == 0x65e5); -assert (w == twochars + 3); -``` - -This is a faster but less safe version of `utflib::next`. It does not check for validity of the supplied UTF-8 sequence. - - -#### utflib::next16 - -Available in version 4.0 and later. - -Given the iterator to the beginning of the UTF-16 sequence, it returns the code point and moves the iterator to the next position. - -```cpp -template -utfchar32_t next16(word_iterator& it); -``` - -`word_iterator`: an input iterator. -`it`: a reference to an iterator pointing to the beginning of an UTF-16 encoded code point. After the function returns, it is incremented to point to the beginning of the next code point. - -Return value: the 32 bit representation of the processed UTF-16 code point. - -Example of use: - -```cpp -const unsigned short u[3] = {0x65e5, 0xd800, 0xdf46}; -const unsigned short* w = u; -int cp = unchecked::next16(w); -assert (cp, 0x65e5); -assert (w, u + 1); -``` - -This function is typically used to iterate through a UTF-16 encoded string. - -This is a faster but less safe version of `utflib::next16`. It does not check for validity of the supplied UTF-8 sequence. - - - -#### utflib::unchecked::peek_next - -Available in version 2.1 and later. - -Given the iterator to the beginning of a UTF-8 sequence, it returns the code point. - -```cpp -template -utfchar32_t peek_next(octet_iterator it); -``` - -`it`: an iterator pointing to the beginning of an UTF-8 encoded code point. -Return value: the 32 bit representation of the processed UTF-8 code point. - -Example of use: - -```cpp -char* twochars = "\xe6\x97\xa5\xd1\x88"; -char* w = twochars; -int cp = unchecked::peek_next(w); -assert (cp == 0x65e5); -assert (w == twochars); -``` - -This is a faster but less safe version of `utflib::peek_next`. It does not check for validity of the supplied UTF-8 sequence. - - -#### utflib::unchecked::prior - -Available in version 1.02 and later. - -Given a reference to an iterator pointing to an octet in a UTF-8 sequence, it decreases the iterator until it hits the beginning of the previous UTF-8 encoded code point and returns the 32 bits representation of the code point. - -```cpp -template -utfchar32_t prior(octet_iterator& it); -``` - -`it`: a reference pointing to an octet within a UTF-8 encoded string. After the function returns, it is decremented to point to the beginning of the previous code point. - Return value: the 32 bit representation of the previous code point. - -Example of use: - -```cpp -char* twochars = "\xe6\x97\xa5\xd1\x88"; -char* w = twochars + 3; -int cp = unchecked::prior (w); -assert (cp == 0x65e5); -assert (w == twochars); -``` - -This is a faster but less safe version of `utflib::prior`. It does not check for validity of the supplied UTF-8 sequence and offers no boundary checking. - - -#### utflib::unchecked::advance - -Available in version 1.0 and later. - -Advances an iterator by the specified number of code points within an UTF-8 sequence. - -```cpp -template -void advance (octet_iterator& it, distance_type n); -``` - -`it`: a reference to an iterator pointing to the beginning of an UTF-8 encoded code point. After the function returns, it is incremented to point to the nth following code point. -`n`: number of code points `it` should be advanced. A negative value means decrement. - -Example of use: - -```cpp -char* twochars = "\xe6\x97\xa5\xd1\x88"; -char* w = twochars; -unchecked::advance (w, 2); -assert (w == twochars + 5); -``` - -This is a faster but less safe version of `utflib::advance`. It does not check for validity of the supplied UTF-8 sequence and offers no boundary checking. - - -#### utflib::unchecked::distance - -Available in version 1.0 and later. - -Given the iterators to two UTF-8 encoded code points in a sequence, returns the number of code points between them. - -```cpp -template -typename std::iterator_traits::difference_type distance (octet_iterator first, octet_iterator last); -``` - -`first`: an iterator to a beginning of a UTF-8 encoded code point. -`last`: an iterator to a "post-end" of the last UTF-8 encoded code point in the sequence we are trying to determine the length. It can be the beginning of a new code point, or not. -Return value: the distance between the iterators, in code points. - -Example of use: - -```cpp -char* twochars = "\xe6\x97\xa5\xd1\x88"; -size_t dist = utflib::unchecked::distance(twochars, twochars + 5); -assert (dist == 2); -``` - -This is a faster but less safe version of `utflib::distance`. It does not check for validity of the supplied UTF-8 sequence. - - -#### utflib::unchecked::utf16to8 - -Available in version 1.0 and later. - -Converts a UTF-16 encoded string to UTF-8. - -```cpp -template -octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result); -``` - -`start`: an iterator pointing to the beginning of the UTF-16 encoded string to convert. -`end`: an iterator pointing to pass-the-end of the UTF-16 encoded string to convert. -`result`: an output iterator to the place in the UTF-8 string where to append the result of conversion. -Return value: An iterator pointing to the place after the appended UTF-8 string. - -Example of use: - -```cpp -unsigned short utf16string[] = {0x41, 0x0448, 0x65e5, 0xd834, 0xdd1e}; -vector utf8result; -unchecked::utf16to8(utf16string, utf16string + 5, back_inserter(utf8result)); -assert (utf8result.size() == 10); -``` - -This is a faster but less safe version of `utflib::utf16to8`. It does not check for validity of the supplied UTF-16 sequence. - - -#### utflib::unchecked::utf8to16 - -Available in version 1.0 and later. - -Converts an UTF-8 encoded string to UTF-16 - -```cpp -template -u16bit_iterator utf8to16 (octet_iterator start, octet_iterator end, u16bit_iterator result); -``` - -`start`: an iterator pointing to the beginning of the UTF-8 encoded string to convert. -`end`: an iterator pointing to pass-the-end of the UTF-8 encoded string to convert. -`result`: an output iterator to the place in the UTF-16 string where to append the result of conversion. -Return value: An iterator pointing to the place after the appended UTF-16 string. - -Example of use: - -```cpp -char utf8_with_surrogates[] = "\xe6\x97\xa5\xd1\x88\xf0\x9d\x84\x9e"; -vector utf16result; -unchecked::utf8to16(utf8_with_surrogates, utf8_with_surrogates + 9, back_inserter(utf16result)); -assert (utf16result.size() == 4); -assert (utf16result[2] == 0xd834); -assert (utf16result[3] == 0xdd1e); -``` - -This is a faster but less safe version of `utflib::utf8to16`. It does not check for validity of the supplied UTF-8 sequence. - - -#### utflib::unchecked::utf32to8 - -Available in version 1.0 and later. - -Converts a UTF-32 encoded string to UTF-8. - -```cpp -template -octet_iterator utf32to8 (u32bit_iterator start, u32bit_iterator end, octet_iterator result); -``` - -`start`: an iterator pointing to the beginning of the UTF-32 encoded string to convert. -`end`: an iterator pointing to pass-the-end of the UTF-32 encoded string to convert. -`result`: an output iterator to the place in the UTF-8 string where to append the result of conversion. -Return value: An iterator pointing to the place after the appended UTF-8 string. - -Example of use: - -```cpp -int utf32string[] = {0x448, 0x65e5, 0x10346, 0}; -vector utf8result; -utf32to8(utf32string, utf32string + 3, back_inserter(utf8result)); -assert (utf8result.size() == 9); -``` - -This is a faster but less safe version of `utflib::utf32to8`. It does not check for validity of the supplied UTF-32 sequence. - - -#### utflib::unchecked::utf8to32 - -Available in version 1.0 and later. - -Converts a UTF-8 encoded string to UTF-32. - -```cpp -template -u32bit_iterator utf8to32 (octet_iterator start, octet_iterator end, u32bit_iterator result); -``` - -`start`: an iterator pointing to the beginning of the UTF-8 encoded string to convert. -`end`: an iterator pointing to pass-the-end of the UTF-8 encoded string to convert. -`result`: an output iterator to the place in the UTF-32 string where to append the result of conversion. -Return value: An iterator pointing to the place after the appended UTF-32 string. - -Example of use: - -```cpp -char* twochars = "\xe6\x97\xa5\xd1\x88"; -vector utf32result; -unchecked::utf8to32(twochars, twochars + 5, back_inserter(utf32result)); -assert (utf32result.size() == 2); -``` - -This is a faster but less safe version of `utflib::utf8to32`. It does not check for validity of the supplied UTF-8 sequence. - - -#### utflib::unchecked::replace_invalid - -Available in version 3.1 and later. - -Replaces all invalid UTF-8 sequences within a string with a replacement marker. - -```cpp -template -output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out, utfchar32_t replacement); -template -output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out); -``` - -`octet_iterator`: an input iterator. -`output_iterator`: an output iterator. -`start`: an iterator pointing to the beginning of the UTF-8 string to look for invalid UTF-8 sequences. -`end`: an iterator pointing to pass-the-end of the UTF-8 string to look for invalid UTF-8 sequences. -`out`: An output iterator to the range where the result of replacement is stored. -`replacement`: A Unicode code point for the replacement marker. The version without this parameter assumes the value `0xfffd` -Return value: An iterator pointing to the place after the UTF-8 string with replaced invalid sequences. - -Example of use: - -```cpp -char invalid_sequence[] = "a\x80\xe0\xa0\xc0\xaf\xed\xa0\x80z"; -vector replace_invalid_result; -unchecked::replace_invalid (invalid_sequence, invalid_sequence + sizeof(invalid_sequence), back_inserter(replace_invalid_result), '?'); -bvalid = utflib::is_valid(replace_invalid_result.begin(), replace_invalid_result.end()); -assert (bvalid); -char* fixed_invalid_sequence = "a????z"; -assert (std::equal(replace_invalid_result.begin(), replace_invalid_result.end(), fixed_invalid_sequence)); -``` - -`replace_invalid` does not perform in-place replacement of invalid sequences. Rather, it produces a copy of the original string with the invalid sequences replaced with a replacement marker. Therefore, `out` must not be in the `[start, end]` range. - -Unlike `utflib::replace_invalid`, this function does not verify validity of the replacement marker. - - -### Types From utflib::unchecked Namespace - - -#### utflib::iterator - -Available in version 2.0 and later. - -Adapts the underlying octet iterator to iterate over the sequence of code points, rather than raw octets. - -```cpp -template -class iterator; -``` - - -##### Member functions - -`iterator();` the default constructor; the underlying octet_iterator is constructed with its default constructor. - -`explicit iterator (const octet_iterator& octet_it);` a constructor that initializes the underlying octet_iterator with `octet_it`. - -`octet_iterator base () const;` returns the underlying octet_iterator. - -`utfchar32_t operator * () const;` decodes the utf-8 sequence the underlying octet_iterator is pointing to and returns the code point. - -`bool operator == (const iterator& rhs) const;` returns `true` if the two underlying iterators are equal. - -`bool operator != (const iterator& rhs) const;` returns `true` if the two underlying iterators are not equal. - -`iterator& operator ++ ();` the prefix increment - moves the iterator to the next UTF-8 encoded code point. - -`iterator operator ++ (int);` the postfix increment - moves the iterator to the next UTF-8 encoded code point and returns the current one. - -`iterator& operator -- ();` the prefix decrement - moves the iterator to the previous UTF-8 encoded code point. - -`iterator operator -- (int);` the postfix decrement - moves the iterator to the previous UTF-8 encoded code point and returns the current one. - -Example of use: - -```cpp -char* threechars = "\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88"; -utflib::unchecked::iterator un_it(threechars); -utflib::unchecked::iterator un_it2 = un_it; -assert (un_it2 == un_it); -assert (*un_it == 0x10346); -assert (*(++un_it) == 0x65e5); -assert ((*un_it++) == 0x65e5); -assert (*un_it == 0x0448); -assert (un_it != un_it2); -utflib::::unchecked::iterator un_endit (threechars + 9); -assert (++un_it == un_endit); -assert (*(--un_it) == 0x0448); -assert ((*un_it--) == 0x0448); -assert (*un_it == 0x65e5); -assert (--un_it == utflib::unchecked::iterator(threechars)); -assert (*un_it == 0x10346); -``` - -This is an unchecked version of `utflib::iterator`. It is faster in many cases, but offers no validity or range checks. - From c57aec0e859dd2f76c474806662d6d0bc2469d89 Mon Sep 17 00:00:00 2001 From: yaito3014 Date: Wed, 18 Feb 2026 20:07:15 +0900 Subject: [PATCH 21/28] Relax required C++ version --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 03e0ce8..58e569b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,7 @@ project( add_library(${PROJECT_NAME} INTERFACE) -target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_23) +target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_20) target_sources( ${PROJECT_NAME} From 4deb0a6b0f7d10ce87b42f018372782ce2600b6f Mon Sep 17 00:00:00 2001 From: yaito3014 Date: Wed, 18 Feb 2026 21:26:31 +0900 Subject: [PATCH 22/28] Add noexcept to internal functions --- source/utf8.h | 313 +++++++++++++++++++++++++++++---------------- tests/apitests.cpp | 8 +- 2 files changed, 206 insertions(+), 115 deletions(-) diff --git a/source/utf8.h b/source/utf8.h index 4aae338..03e7a1a 100644 --- a/source/utf8.h +++ b/source/utf8.h @@ -40,21 +40,75 @@ DEALINGS IN THE SOFTWARE. namespace iris::utflib { - using utfchar8_t = char8_t; - using utfchar16_t = char16_t; - using utfchar32_t = char32_t; + template + concept octet = std::integral && sizeof(T) == 1; + + template + concept utf8char = octet && (std::same_as || std::same_as); + + template + concept utf16char = std::same_as; + + template + concept utf32char = std::same_as; template - concept octet_iterator = std::input_iterator && std::integral> && sizeof(std::iter_value_t) == 1; + concept octet_iterator = std::input_iterator && octet>; template - concept utf8_iterator = octet_iterator && (std::same_as, char> || std::same_as, char8_t>); + concept utf8_iterator = octet_iterator && utf8char>; template - concept utf16_iterator = std::input_iterator && std::same_as, char16_t>; + concept utf16_iterator = std::input_iterator && utf16char>; template - concept utf32_iterator = std::input_iterator && std::same_as, char32_t>; + concept utf32_iterator = std::input_iterator && utf32char>; + + namespace traits + { + template + struct is_nothrow_dereferenceable : std::false_type {}; + + template + struct is_nothrow_dereferenceable())>> : std::bool_constant())> {}; + + template + inline constexpr bool is_nothrow_dereferenceable_v = is_nothrow_dereferenceable::value; + + template + struct is_nothrow_prefix_incrementable : std::false_type {}; + + template + struct is_nothrow_prefix_incrementable())>> : std::bool_constant())> {}; + + template + inline constexpr bool is_nothrow_prefix_incrementable_v = is_nothrow_prefix_incrementable::value; + + template + struct is_nothrow_postfix_incrementable : std::false_type {}; + + template + struct is_nothrow_postfix_incrementable()++)>> : std::bool_constant()++)> {}; + + template + inline constexpr bool is_nothrow_postfix_incrementable_v = is_nothrow_postfix_incrementable::value; + + template + struct is_nothrow_sentinel : std::false_type {}; + + template + requires std::sentinel_for + struct is_nothrow_sentinel : std::bool_constant< + noexcept(std::declval() == std::declval()) && + noexcept(std::declval() != std::declval()) && + noexcept(std::declval() == std::declval()) && + noexcept(std::declval() != std::declval()) + > + {}; + + template + inline constexpr bool is_nothrow_sentinel_v = is_nothrow_sentinel::value; + } // namespace traits // Helper code - not intended to be directly called by the library users. May be changed at any time namespace internal @@ -62,62 +116,61 @@ namespace iris::utflib // Unicode constants // Leading (high) surrogates: 0xd800 - 0xdbff // Trailing (low) surrogates: 0xdc00 - 0xdfff - constexpr utfchar16_t LEAD_SURROGATE_MIN = 0xd800u; - constexpr utfchar16_t LEAD_SURROGATE_MAX = 0xdbffu; - constexpr utfchar16_t TRAIL_SURROGATE_MIN = 0xdc00u; - constexpr utfchar16_t TRAIL_SURROGATE_MAX = 0xdfffu; - constexpr utfchar16_t LEAD_OFFSET = 0xd7c0u; // LEAD_SURROGATE_MIN - (0x10000 >> 10) - constexpr utfchar32_t SURROGATE_OFFSET = 0xfca02400u; // 0x10000u - (LEAD_SURROGATE_MIN << 10) - TRAIL_SURROGATE_MIN + constexpr char16_t LEAD_SURROGATE_MIN = 0xd800u; + constexpr char16_t LEAD_SURROGATE_MAX = 0xdbffu; + constexpr char16_t TRAIL_SURROGATE_MIN = 0xdc00u; + constexpr char16_t TRAIL_SURROGATE_MAX = 0xdfffu; + constexpr char16_t LEAD_OFFSET = 0xd7c0u; // LEAD_SURROGATE_MIN - (0x10000 >> 10) + constexpr char32_t SURROGATE_OFFSET = 0xfca02400u; // 0x10000u - (LEAD_SURROGATE_MIN << 10) - TRAIL_SURROGATE_MIN // Maximum valid value for a Unicode code point - constexpr utfchar32_t CODE_POINT_MAX = 0x0010ffffu; + constexpr char32_t CODE_POINT_MAX = 0x0010ffffu; enum class utf_error { UTF8_OK, NOT_ENOUGH_ROOM, INVALID_LEAD, INCOMPLETE_SEQUENCE, OVERLONG_SEQUENCE, INVALID_CODE_POINT }; - template - [[nodiscard]] constexpr utfchar8_t mask8(octet_type oc) noexcept + template + [[nodiscard]] constexpr char8_t mask8(Octet oc) noexcept { - return static_cast(0xff & oc); + return static_cast(0xff & oc); } - template - [[nodiscard]] constexpr utfchar16_t mask16(u16_type oc) noexcept + [[nodiscard]] constexpr char16_t mask16(char16_t oc) noexcept { - return static_cast(0xffff & oc); + return static_cast(0xffff & oc); } - template - [[nodiscard]] constexpr bool is_trail(octet_type oc) noexcept + template + [[nodiscard]] constexpr bool is_trail(Octet oc) noexcept { return ((internal::mask8(oc) >> 6) == 0x2); } - [[nodiscard]] constexpr bool is_lead_surrogate(utfchar32_t cp) noexcept + [[nodiscard]] constexpr bool is_lead_surrogate(char32_t cp) noexcept { - return (cp >= static_cast(LEAD_SURROGATE_MIN) && cp <= static_cast(LEAD_SURROGATE_MAX)); + return (cp >= static_cast(LEAD_SURROGATE_MIN) && cp <= static_cast(LEAD_SURROGATE_MAX)); } - [[nodiscard]] constexpr bool is_trail_surrogate(utfchar32_t cp) noexcept + [[nodiscard]] constexpr bool is_trail_surrogate(char32_t cp) noexcept { - return (cp >= static_cast(TRAIL_SURROGATE_MIN) && cp <= static_cast(TRAIL_SURROGATE_MAX)); + return (cp >= static_cast(TRAIL_SURROGATE_MIN) && cp <= static_cast(TRAIL_SURROGATE_MAX)); } - [[nodiscard]] constexpr bool is_surrogate(utfchar32_t cp) noexcept + [[nodiscard]] constexpr bool is_surrogate(char32_t cp) noexcept { - return (cp >= static_cast(LEAD_SURROGATE_MIN) && cp <= static_cast(TRAIL_SURROGATE_MAX)); + return (cp >= static_cast(LEAD_SURROGATE_MIN) && cp <= static_cast(TRAIL_SURROGATE_MAX)); } - [[nodiscard]] constexpr bool is_code_point_valid(utfchar32_t cp) noexcept + [[nodiscard]] constexpr bool is_code_point_valid(char32_t cp) noexcept { return (cp <= CODE_POINT_MAX && !internal::is_surrogate(cp)); } - [[nodiscard]] constexpr bool is_in_bmp(utfchar32_t cp) noexcept + [[nodiscard]] constexpr bool is_in_bmp(char32_t cp) noexcept { - return cp < utfchar32_t(0x10000); + return cp < char32_t(0x10000); } - [[nodiscard]] constexpr bool is_overlong_sequence(utfchar32_t cp, int length) noexcept + [[nodiscard]] constexpr bool is_overlong_sequence(char32_t cp, int length) noexcept { if (cp < 0x80) { if (length != 1) @@ -134,8 +187,9 @@ namespace iris::utflib template [[nodiscard]] constexpr int sequence_length(It lead_it) + noexcept(traits::is_nothrow_dereferenceable_v) { - const utfchar8_t lead = internal::mask8(*lead_it); + const char8_t lead = internal::mask8(*lead_it); if (lead < 0x80) return 1; else if ((lead >> 5) == 0x6) @@ -151,6 +205,11 @@ namespace iris::utflib /// Helper for get_sequence_x template Se> constexpr utf_error increase_safely(It& it, Se end) + noexcept(std::conjunction_v< + traits::is_nothrow_dereferenceable, + traits::is_nothrow_prefix_incrementable, + traits::is_nothrow_sentinel + >) { if (++it == end) return utf_error::NOT_ENOUGH_ROOM; @@ -170,23 +229,33 @@ namespace iris::utflib /// get_sequence_x functions decode utf-8 sequences of the length x template Se> - constexpr utf_error get_sequence_1(It& it, Se end, utfchar32_t& code_point) + constexpr utf_error get_sequence_1(It& it, Se end, char32_t& code_point) + noexcept(std::conjunction_v< + traits::is_nothrow_dereferenceable, + traits::is_nothrow_prefix_incrementable, + traits::is_nothrow_sentinel + >) { if (it == end) return utf_error::NOT_ENOUGH_ROOM; - code_point = static_cast(internal::mask8(*it)); + code_point = static_cast(internal::mask8(*it)); return utf_error::UTF8_OK; } template Se> - constexpr utf_error get_sequence_2(It& it, Se end, utfchar32_t& code_point) + constexpr utf_error get_sequence_2(It& it, Se end, char32_t& code_point) + noexcept(std::conjunction_v< + traits::is_nothrow_dereferenceable, + traits::is_nothrow_prefix_incrementable, + traits::is_nothrow_sentinel + >) { if (it == end) return utf_error::NOT_ENOUGH_ROOM; - code_point = static_cast(internal::mask8(*it)); + code_point = static_cast(internal::mask8(*it)); IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end); @@ -196,12 +265,17 @@ namespace iris::utflib } template Se> - constexpr utf_error get_sequence_3(It& it, Se end, utfchar32_t& code_point) + constexpr utf_error get_sequence_3(It& it, Se end, char32_t& code_point) + noexcept(std::conjunction_v< + traits::is_nothrow_dereferenceable, + traits::is_nothrow_prefix_incrementable, + traits::is_nothrow_sentinel + >) { if (it == end) return utf_error::NOT_ENOUGH_ROOM; - code_point = static_cast(internal::mask8(*it)); + code_point = static_cast(internal::mask8(*it)); IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end); @@ -209,18 +283,23 @@ namespace iris::utflib IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end); - code_point = static_cast(code_point + ((*it) & 0x3f)); + code_point = static_cast(code_point + ((*it) & 0x3f)); return utf_error::UTF8_OK; } template Se> - constexpr utf_error get_sequence_4(It& it, Se end, utfchar32_t& code_point) + constexpr utf_error get_sequence_4(It& it, Se end, char32_t& code_point) + noexcept(std::conjunction_v< + traits::is_nothrow_dereferenceable, + traits::is_nothrow_prefix_incrementable, + traits::is_nothrow_sentinel + >) { if (it == end) return utf_error::NOT_ENOUGH_ROOM; - code_point = static_cast(internal::mask8(*it)); + code_point = static_cast(internal::mask8(*it)); IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end); @@ -228,11 +307,11 @@ namespace iris::utflib IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end); - code_point = static_cast(code_point + ((internal::mask8(*it) << 6) & 0xfff)); + code_point = static_cast(code_point + ((internal::mask8(*it) << 6) & 0xfff)); IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(it, end); - code_point = static_cast(code_point + ((*it) & 0x3f)); + code_point = static_cast(code_point + ((*it) & 0x3f)); return utf_error::UTF8_OK; } @@ -240,7 +319,14 @@ namespace iris::utflib #undef IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR template Se> - constexpr utf_error validate_next(It& it, Se end, utfchar32_t& code_point) + requires std::forward_iterator + constexpr utf_error validate_next(It& it, Se end, char32_t& code_point) + noexcept(std::conjunction_v< + traits::is_nothrow_dereferenceable, + traits::is_nothrow_prefix_incrementable, + traits::is_nothrow_sentinel, + std::is_nothrow_copy_constructible + >) { if (it == end) return utf_error::NOT_ENOUGH_ROOM; @@ -249,7 +335,7 @@ namespace iris::utflib // Of course, it does not make much sense with i.e. stream iterators It original_it = it; - utfchar32_t cp = 0; + char32_t cp = 0; // Determine the sequence length based on the lead octet const int length = internal::sequence_length(it); @@ -292,14 +378,24 @@ namespace iris::utflib } template Se> + requires std::forward_iterator constexpr utf_error validate_next(It& it, Se end) + noexcept(noexcept(internal::validate_next(it, end, std::declval()))) { - utfchar32_t ignored; + char32_t ignored; return internal::validate_next(it, end, ignored); } template Se> - constexpr utf_error validate_next16(It& it, Se end, utfchar32_t& code_point) + requires std::forward_iterator + constexpr utf_error validate_next16(It& it, Se end, char32_t& code_point) + noexcept(std::conjunction_v< + traits::is_nothrow_dereferenceable, + traits::is_nothrow_prefix_incrementable, + traits::is_nothrow_postfix_incrementable, + traits::is_nothrow_sentinel, + std::is_nothrow_copy_constructible + >) { // Check the edge case: if (it == end) @@ -310,17 +406,17 @@ namespace iris::utflib utf_error err = utf_error::UTF8_OK; - const utfchar16_t first_word = *it++; - if (!is_surrogate(first_word)) { + const char16_t first_word = *it++; + if (!internal::is_surrogate(first_word)) { code_point = first_word; return utf_error::UTF8_OK; } else { if (it == end) err = utf_error::NOT_ENOUGH_ROOM; - else if (is_lead_surrogate(first_word)) { - const utfchar16_t second_word = *it++; - if (is_trail_surrogate(static_cast(second_word))) { - code_point = static_cast(first_word << 10) + static_cast(second_word) + SURROGATE_OFFSET; + else if (internal::is_lead_surrogate(first_word)) { + const char16_t second_word = *it++; + if (internal::is_trail_surrogate(static_cast(second_word))) { + code_point = static_cast(first_word << 10) + static_cast(second_word) + SURROGATE_OFFSET; return utf_error::UTF8_OK; } else err = utf_error::INCOMPLETE_SEQUENCE; @@ -334,9 +430,10 @@ namespace iris::utflib return err; } - template > + template > requires std::output_iterator - constexpr It append(utfchar32_t cp, It result) + constexpr It append(char32_t cp, It result) + noexcept(noexcept(*result++ = std::declval())) { if (cp < 0x80) // one octet *(result++) = static_cast(cp); @@ -357,31 +454,25 @@ namespace iris::utflib } template - constexpr std::back_insert_iterator append(utfchar32_t cp, std::back_insert_iterator result) + constexpr std::back_insert_iterator append(char32_t cp, std::back_insert_iterator result) + noexcept(noexcept(internal::append, typename container_type::value_type>(cp, result))) { - return append, typename container_type::value_type>(cp, result); + return internal::append, typename container_type::value_type>(cp, result); } - template > - requires std::output_iterator - constexpr It append16(utfchar32_t cp, It result) + template It> + constexpr It append16(char32_t cp, It result) + noexcept(noexcept(*result++ = std::declval())) { - static_assert(sizeof(word_type) >= sizeof(utfchar16_t)); - if (is_in_bmp(cp)) - *(result++) = static_cast(cp); + if (internal::is_in_bmp(cp)) + *(result++) = static_cast(cp); else { // Code points from the supplementary planes are encoded via surrogate pairs - *(result++) = static_cast(LEAD_OFFSET + (cp >> 10)); - *(result++) = static_cast(TRAIL_SURROGATE_MIN + (cp & 0x3FF)); + *(result++) = static_cast(LEAD_OFFSET + (cp >> 10)); + *(result++) = static_cast(TRAIL_SURROGATE_MIN + (cp & 0x3FF)); } return result; } - - template - constexpr std::back_insert_iterator append16(utfchar32_t cp, std::back_insert_iterator result) - { - return append16, typename container_type::value_type>(cp, result); - } } // namespace internal // Base for the exceptions that may be thrown from the library @@ -392,45 +483,45 @@ namespace iris::utflib // Exceptions that may be thrown from the library functions. class invalid_code_point : public exception { - utfchar32_t cp; + char32_t cp; public: - invalid_code_point(utfchar32_t codepoint) + invalid_code_point(char32_t codepoint) : cp(codepoint) { } virtual const char* what() const noexcept override { return "Invalid code point"; } - [[nodiscard]] utfchar32_t code_point() const { return cp; } + [[nodiscard]] char32_t code_point() const { return cp; } }; class invalid_utf8 : public exception { - utfchar8_t u8; + char8_t u8; public: - invalid_utf8(utfchar8_t u) + invalid_utf8(char8_t u) : u8(u) { } invalid_utf8(char c) - : u8(static_cast(c)) + : u8(static_cast(c)) { } virtual const char* what() const noexcept override { return "Invalid UTF-8"; } - [[nodiscard]] utfchar8_t utf8_octet() const { return u8; } + [[nodiscard]] char8_t utf8_octet() const { return u8; } }; class invalid_utf16 : public exception { - utfchar16_t u16; + char16_t u16; public: - invalid_utf16(utfchar16_t u) + invalid_utf16(char16_t u) : u16(u) { } virtual const char* what() const noexcept override { return "Invalid UTF-16"; } - [[nodiscard]] utfchar16_t utf16_word() const { return u16; } + [[nodiscard]] char16_t utf16_word() const { return u16; } }; class not_enough_room : public exception @@ -442,7 +533,7 @@ namespace iris::utflib /// The library API - functions intended to be called by the users // Byte order mark - constexpr utfchar8_t bom[] = {0xef, 0xbb, 0xbf}; + constexpr char8_t bom[] = {0xef, 0xbb, 0xbf}; template Se> [[nodiscard]] constexpr It find_invalid(It start, Se end) @@ -501,7 +592,7 @@ namespace iris::utflib } template // TODO: add constraints - constexpr It append(utfchar32_t cp, It result) + constexpr It append(char32_t cp, It result) { if (!internal::is_code_point_valid(cp)) throw invalid_code_point(cp); @@ -509,18 +600,18 @@ namespace iris::utflib return internal::append(cp, result); } - constexpr void append(utfchar32_t cp, std::string& s) + constexpr void append(char32_t cp, std::string& s) { append(cp, std::back_inserter(s)); } - constexpr void append(utfchar32_t cp, std::u8string& s) + constexpr void append(char32_t cp, std::u8string& s) { append(cp, std::back_inserter(s)); } template // TODO: add constraints - constexpr It append16(utfchar32_t cp, It result) + constexpr It append16(char32_t cp, It result) { if (!internal::is_code_point_valid(cp)) throw invalid_code_point(cp); @@ -528,13 +619,13 @@ namespace iris::utflib return internal::append16(cp, result); } - constexpr void append16(utfchar32_t cp, std::u16string& s) + constexpr void append16(char32_t cp, std::u16string& s) { append16(cp, std::back_inserter(s)); } template Se, typename Out> // TODO: add constraints - constexpr Out replace_invalid(It start, Se end, Out out, utfchar32_t replacement) + constexpr Out replace_invalid(It start, Se end, Out out, char32_t replacement) { while (start != end) { It sequence_start = start; @@ -569,11 +660,11 @@ namespace iris::utflib template Se, typename Out> // TODO: add constraints constexpr Out replace_invalid(It start, Se end, Out out) { - constexpr utfchar32_t replacement_marker = static_cast(internal::mask16(0xfffd)); + constexpr char32_t replacement_marker = static_cast(internal::mask16(0xfffd)); return utflib::replace_invalid(start, end, out, replacement_marker); } - [[nodiscard]] constexpr std::string replace_invalid(std::string_view s, utfchar32_t replacement) + [[nodiscard]] constexpr std::string replace_invalid(std::string_view s, char32_t replacement) { std::string result; replace_invalid(s.begin(), s.end(), std::back_inserter(result), replacement); @@ -602,9 +693,9 @@ namespace iris::utflib } template Se> - [[nodiscard]] constexpr utfchar32_t next(It& it, Se end) + [[nodiscard]] constexpr char32_t next(It& it, Se end) { - utfchar32_t cp = 0; + char32_t cp = 0; internal::utf_error err_code = internal::validate_next(it, end, cp); switch (err_code) { case internal::utf_error::UTF8_OK: @@ -614,7 +705,7 @@ namespace iris::utflib case internal::utf_error::INVALID_LEAD: case internal::utf_error::INCOMPLETE_SEQUENCE: case internal::utf_error::OVERLONG_SEQUENCE: - throw invalid_utf8(static_cast(*it)); + throw invalid_utf8(static_cast(*it)); case internal::utf_error::INVALID_CODE_POINT: throw invalid_code_point(cp); } @@ -622,9 +713,9 @@ namespace iris::utflib } template Se> - [[nodiscard]] constexpr utfchar32_t next16(It& it, Se end) + [[nodiscard]] constexpr char32_t next16(It& it, Se end) { - utfchar32_t cp = 0; + char32_t cp = 0; internal::utf_error err_code = internal::validate_next16(it, end, cp); if (err_code == internal::utf_error::NOT_ENOUGH_ROOM) throw not_enough_room(); @@ -632,13 +723,13 @@ namespace iris::utflib } template Se> - [[nodiscard]] constexpr utfchar32_t peek_next(It it, Se end) + [[nodiscard]] constexpr char32_t peek_next(It it, Se end) { return utflib::next(it, end); } template Se> - [[nodiscard]] constexpr utfchar32_t prior(It& it, Se start) + [[nodiscard]] constexpr char32_t prior(It& it, Se start) { // can't do much if it == start if (it == start) @@ -681,22 +772,22 @@ namespace iris::utflib constexpr OutIt utf16to8(It start, Se end, OutIt result) { while (start != end) { - utfchar32_t cp = static_cast(internal::mask16(*start++)); + char32_t cp = static_cast(internal::mask16(*start++)); // Take care of surrogate pairs first if (internal::is_lead_surrogate(cp)) { if (start != end) { - const utfchar32_t trail_surrogate = static_cast(internal::mask16(*start++)); + const char32_t trail_surrogate = static_cast(internal::mask16(*start++)); if (internal::is_trail_surrogate(trail_surrogate)) cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET; else - throw invalid_utf16(static_cast(trail_surrogate)); + throw invalid_utf16(static_cast(trail_surrogate)); } else - throw invalid_utf16(static_cast(cp)); + throw invalid_utf16(static_cast(cp)); } // Lone trail surrogate else if (internal::is_trail_surrogate(cp)) - throw invalid_utf16(static_cast(cp)); + throw invalid_utf16(static_cast(cp)); result = utflib::append(cp, result); } @@ -721,12 +812,12 @@ namespace iris::utflib constexpr OutIt utf8to16(It start, Se end, OutIt result) { while (start < end) { // TODO: replace < with != - const utfchar32_t cp = utflib::next(start, end); + const char32_t cp = utflib::next(start, end); if (cp > 0xffff) { // make a surrogate pair - *result++ = static_cast((cp >> 10) + internal::LEAD_OFFSET); - *result++ = static_cast((cp & 0x3ff) + internal::TRAIL_SURROGATE_MIN); + *result++ = static_cast((cp >> 10) + internal::LEAD_OFFSET); + *result++ = static_cast((cp & 0x3ff) + internal::TRAIL_SURROGATE_MIN); } else - *result++ = static_cast(cp); + *result++ = static_cast(cp); } return result; } @@ -800,9 +891,9 @@ namespace iris::utflib It range_end; public: - using value_type = utfchar32_t; - using pointer = utfchar32_t*; - using reference = utfchar32_t&; + using value_type = char32_t; + using pointer = char32_t*; + using reference = char32_t&; using difference_type = std::ptrdiff_t; using iterator_category = std::bidirectional_iterator_tag; constexpr iterator() {} @@ -818,7 +909,7 @@ namespace iris::utflib } // the default "big three" are OK [[nodiscard]] constexpr It base() const { return it; } - [[nodiscard]] constexpr utfchar32_t operator*() const + [[nodiscard]] constexpr char32_t operator*() const { It temp = it; return utflib::next(temp, range_end); diff --git a/tests/apitests.cpp b/tests/apitests.cpp index 2c80daf..ba8fa90 100644 --- a/tests/apitests.cpp +++ b/tests/apitests.cpp @@ -49,7 +49,7 @@ TEST(CheckedAPITests, test_append) TEST(CheckedAPITests, test_append16) { - utfchar16_t u[5] = {0,0}; + char16_t u[5] = {0,0}; append16(0x0448, u); EXPECT_EQ (u[0], 0x0448); EXPECT_EQ (u[1], 0x0000); @@ -89,9 +89,9 @@ TEST(CheckedAPITests, test_next) TEST(CheckedAPITests, test_next16) { - const utfchar16_t u[3] = {0x65e5, 0xd800, 0xdf46}; - const utfchar16_t* w = u; - iris::utflib::utfchar32_t cp = next16(w, w + 3); + const char16_t u[3] = {0x65e5, 0xd800, 0xdf46}; + const char16_t* w = u; + char32_t cp = next16(w, w + 3); EXPECT_EQ (cp, 0x65e5); EXPECT_EQ (w, u + 1); From 7eedfc3baa5f0ffc31b4290ef0ac9fe2e6f79c45 Mon Sep 17 00:00:00 2001 From: yaito3014 Date: Wed, 18 Feb 2026 21:53:23 +0900 Subject: [PATCH 23/28] Minor refactor --- source/utf8.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/source/utf8.h b/source/utf8.h index 03e7a1a..ab12e64 100644 --- a/source/utf8.h +++ b/source/utf8.h @@ -896,11 +896,13 @@ namespace iris::utflib using reference = char32_t&; using difference_type = std::ptrdiff_t; using iterator_category = std::bidirectional_iterator_tag; - constexpr iterator() {} + constexpr iterator() + requires std::is_default_constructible_v + = default; constexpr explicit iterator(It octet_it, It rangestart, It rangeend) - : it(octet_it) - , range_start(rangestart) - , range_end(rangeend) + : it(std::move(octet_it)) + , range_start(std::move(rangestart)) + , range_end(std::move(rangeend)) { if constexpr (std::random_access_iterator) { if (it < range_start || it > range_end) From 48460a55b516d697e0936795284744337aa6854f Mon Sep 17 00:00:00 2001 From: yaito3014 Date: Wed, 18 Feb 2026 21:54:56 +0900 Subject: [PATCH 24/28] Rename UTF8_OK --- source/utf8.h | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/source/utf8.h b/source/utf8.h index ab12e64..cf4b197 100644 --- a/source/utf8.h +++ b/source/utf8.h @@ -126,7 +126,7 @@ namespace iris::utflib // Maximum valid value for a Unicode code point constexpr char32_t CODE_POINT_MAX = 0x0010ffffu; - enum class utf_error { UTF8_OK, NOT_ENOUGH_ROOM, INVALID_LEAD, INCOMPLETE_SEQUENCE, OVERLONG_SEQUENCE, INVALID_CODE_POINT }; + enum class utf_error { OK, NOT_ENOUGH_ROOM, INVALID_LEAD, INCOMPLETE_SEQUENCE, OVERLONG_SEQUENCE, INVALID_CODE_POINT }; template [[nodiscard]] constexpr char8_t mask8(Octet oc) noexcept @@ -217,13 +217,13 @@ namespace iris::utflib if (!internal::is_trail(*it)) return utf_error::INCOMPLETE_SEQUENCE; - return utf_error::UTF8_OK; + return utf_error::OK; } #define IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR(IT, END) \ do { \ utf_error ret = increase_safely(IT, END); \ - if (ret != utf_error::UTF8_OK) \ + if (ret != utf_error::OK) \ return ret; \ } while (false) @@ -241,7 +241,7 @@ namespace iris::utflib code_point = static_cast(internal::mask8(*it)); - return utf_error::UTF8_OK; + return utf_error::OK; } template Se> @@ -261,7 +261,7 @@ namespace iris::utflib code_point = ((code_point << 6) & 0x7ff) + ((*it) & 0x3f); - return utf_error::UTF8_OK; + return utf_error::OK; } template Se> @@ -285,7 +285,7 @@ namespace iris::utflib code_point = static_cast(code_point + ((*it) & 0x3f)); - return utf_error::UTF8_OK; + return utf_error::OK; } template Se> @@ -313,7 +313,7 @@ namespace iris::utflib code_point = static_cast(code_point + ((*it) & 0x3f)); - return utf_error::UTF8_OK; + return utf_error::OK; } #undef IRIS_UTFLIB_INCREASE_AND_RETURN_ON_ERROR @@ -340,7 +340,7 @@ namespace iris::utflib const int length = internal::sequence_length(it); // Get trail octets and calculate the code point - utf_error err = utf_error::UTF8_OK; + utf_error err = utf_error::OK; switch (length) { case 0: return utf_error::INVALID_LEAD; @@ -358,14 +358,14 @@ namespace iris::utflib break; } - if (err == utf_error::UTF8_OK) { + if (err == utf_error::OK) { // Decoding succeeded. Now, security checks... if (internal::is_code_point_valid(cp)) { if (!internal::is_overlong_sequence(cp, length)) { // Passed! Return here. code_point = cp; ++it; - return utf_error::UTF8_OK; + return utf_error::OK; } else err = utf_error::OVERLONG_SEQUENCE; } else @@ -404,12 +404,12 @@ namespace iris::utflib // Of course, it does not make much sense with i.e. stream iterators It original_it = it; - utf_error err = utf_error::UTF8_OK; + utf_error err = utf_error::OK; const char16_t first_word = *it++; if (!internal::is_surrogate(first_word)) { code_point = first_word; - return utf_error::UTF8_OK; + return utf_error::OK; } else { if (it == end) err = utf_error::NOT_ENOUGH_ROOM; @@ -417,7 +417,7 @@ namespace iris::utflib const char16_t second_word = *it++; if (internal::is_trail_surrogate(static_cast(second_word))) { code_point = static_cast(first_word << 10) + static_cast(second_word) + SURROGATE_OFFSET; - return utf_error::UTF8_OK; + return utf_error::OK; } else err = utf_error::INCOMPLETE_SEQUENCE; @@ -541,7 +541,7 @@ namespace iris::utflib It result = start; while (result != end) { internal::utf_error err_code = internal::validate_next(result, end); - if (err_code != internal::utf_error::UTF8_OK) + if (err_code != internal::utf_error::OK) return result; } return result; @@ -631,7 +631,7 @@ namespace iris::utflib It sequence_start = start; internal::utf_error err_code = internal::validate_next(start, end); switch (err_code) { - case internal::utf_error::UTF8_OK: + case internal::utf_error::OK: for (It it = sequence_start; it != start; ++it) *out++ = *it; break; @@ -698,7 +698,7 @@ namespace iris::utflib char32_t cp = 0; internal::utf_error err_code = internal::validate_next(it, end, cp); switch (err_code) { - case internal::utf_error::UTF8_OK: + case internal::utf_error::OK: break; case internal::utf_error::NOT_ENOUGH_ROOM: throw not_enough_room(); From 0ecf58d8a8a8a39c8ca8813b1ada66c4ee026f05 Mon Sep 17 00:00:00 2001 From: yaito3014 Date: Wed, 18 Feb 2026 21:56:46 +0900 Subject: [PATCH 25/28] Add explicit and noexcept to error classes --- source/utf8.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/source/utf8.h b/source/utf8.h index cf4b197..0eb600c 100644 --- a/source/utf8.h +++ b/source/utf8.h @@ -486,12 +486,12 @@ namespace iris::utflib char32_t cp; public: - invalid_code_point(char32_t codepoint) + explicit invalid_code_point(char32_t codepoint) : cp(codepoint) { } virtual const char* what() const noexcept override { return "Invalid code point"; } - [[nodiscard]] char32_t code_point() const { return cp; } + [[nodiscard]] char32_t code_point() const noexcept { return cp; } }; class invalid_utf8 : public exception @@ -499,16 +499,16 @@ namespace iris::utflib char8_t u8; public: - invalid_utf8(char8_t u) - : u8(u) + explicit invalid_utf8(char c) + : u8(static_cast(c)) { } - invalid_utf8(char c) - : u8(static_cast(c)) + explicit invalid_utf8(char8_t u) + : u8(u) { } virtual const char* what() const noexcept override { return "Invalid UTF-8"; } - [[nodiscard]] char8_t utf8_octet() const { return u8; } + [[nodiscard]] char8_t utf8_octet() const noexcept { return u8; } }; class invalid_utf16 : public exception @@ -516,12 +516,12 @@ namespace iris::utflib char16_t u16; public: - invalid_utf16(char16_t u) + explicit invalid_utf16(char16_t u) : u16(u) { } virtual const char* what() const noexcept override { return "Invalid UTF-16"; } - [[nodiscard]] char16_t utf16_word() const { return u16; } + [[nodiscard]] char16_t utf16_word() const noexcept { return u16; } }; class not_enough_room : public exception From edd36a86fe2da39a69de53821924daeea5be98f8 Mon Sep 17 00:00:00 2001 From: yaito3014 Date: Wed, 18 Feb 2026 22:11:15 +0900 Subject: [PATCH 26/28] Add noexcept, qualify functions --- source/utf8.h | 55 +++++++++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/source/utf8.h b/source/utf8.h index 0eb600c..a40868c 100644 --- a/source/utf8.h +++ b/source/utf8.h @@ -232,7 +232,6 @@ namespace iris::utflib constexpr utf_error get_sequence_1(It& it, Se end, char32_t& code_point) noexcept(std::conjunction_v< traits::is_nothrow_dereferenceable, - traits::is_nothrow_prefix_incrementable, traits::is_nothrow_sentinel >) { @@ -536,59 +535,67 @@ namespace iris::utflib constexpr char8_t bom[] = {0xef, 0xbb, 0xbf}; template Se> - [[nodiscard]] constexpr It find_invalid(It start, Se end) + [[nodiscard]] constexpr It find_invalid(It it, Se se) + noexcept(noexcept(internal::validate_next(it, se)) && std::is_nothrow_copy_constructible_v) { - It result = start; - while (result != end) { - internal::utf_error err_code = internal::validate_next(result, end); + while (it != se) { + internal::utf_error err_code = internal::validate_next(it, se); if (err_code != internal::utf_error::OK) - return result; + return it; } - return result; + return it; } [[nodiscard]] constexpr std::size_t find_invalid(std::string_view s) + noexcept(noexcept(utflib::find_invalid(s.begin(), s.end()))) { - std::string_view::const_iterator invalid = find_invalid(s.begin(), s.end()); + std::string_view::const_iterator invalid = utflib::find_invalid(s.begin(), s.end()); return (invalid == s.end()) ? std::string_view::npos : static_cast(invalid - s.begin()); } [[nodiscard]] constexpr std::size_t find_invalid(std::u8string_view s) + noexcept(noexcept(utflib::find_invalid(s.begin(), s.end()))) { - std::u8string_view::const_iterator invalid = find_invalid(s.begin(), s.end()); + std::u8string_view::const_iterator invalid = utflib::find_invalid(s.begin(), s.end()); return (invalid == s.end()) ? std::u8string_view::npos : static_cast(invalid - s.begin()); } template Se> - [[nodiscard]] constexpr bool is_valid(It start, Se end) + [[nodiscard]] constexpr bool is_valid(It it, Se se) + noexcept(noexcept(utflib::find_invalid(it, se)) && traits::is_nothrow_sentinel_v) { - return (utflib::find_invalid(start, end) == end); + return (utflib::find_invalid(it, se) == se); } [[nodiscard]] constexpr bool is_valid(std::string_view s) + noexcept(noexcept(utflib::is_valid(s.begin(), s.end()))) { - return is_valid(s.begin(), s.end()); + return utflib::is_valid(s.begin(), s.end()); } [[nodiscard]] constexpr bool is_valid(std::u8string_view s) + noexcept(noexcept(utflib::is_valid(s.begin(), s.end()))) { - return is_valid(s.begin(), s.end()); + return utflib::is_valid(s.begin(), s.end()); } template Se> [[nodiscard]] constexpr bool starts_with_bom(It it, Se end) + noexcept(noexcept(internal::mask8(*it++)) && traits::is_nothrow_sentinel_v) { return (((it != end) && (internal::mask8(*it++)) == bom[0]) && ((it != end) && (internal::mask8(*it++)) == bom[1]) && ((it != end) && (internal::mask8(*it)) == bom[2])); } [[nodiscard]] constexpr bool starts_with_bom(std::string_view s) + noexcept(noexcept(utflib::starts_with_bom(s.begin(), s.end()))) { - return starts_with_bom(s.begin(), s.end()); + return utflib::starts_with_bom(s.begin(), s.end()); } [[nodiscard]] constexpr bool starts_with_bom(std::u8string_view s) + noexcept(noexcept(utflib::starts_with_bom(s.begin(), s.end()))) { - return starts_with_bom(s.begin(), s.end()); + return utflib::starts_with_bom(s.begin(), s.end()); } template // TODO: add constraints @@ -602,12 +609,12 @@ namespace iris::utflib constexpr void append(char32_t cp, std::string& s) { - append(cp, std::back_inserter(s)); + utflib::append(cp, std::back_inserter(s)); } constexpr void append(char32_t cp, std::u8string& s) { - append(cp, std::back_inserter(s)); + utflib::append(cp, std::back_inserter(s)); } template // TODO: add constraints @@ -621,7 +628,7 @@ namespace iris::utflib constexpr void append16(char32_t cp, std::u16string& s) { - append16(cp, std::back_inserter(s)); + utflib::append16(cp, std::back_inserter(s)); } template Se, typename Out> // TODO: add constraints @@ -797,14 +804,14 @@ namespace iris::utflib [[nodiscard]] constexpr std::string utf16to8(std::u16string_view s) { std::string result; - utf16to8(s.begin(), s.end(), std::back_inserter(result)); + utflib::utf16to8(s.begin(), s.end(), std::back_inserter(result)); return result; } [[nodiscard]] constexpr std::u8string utf16tou8(std::u16string_view s) { std::u8string result; - utf16to8(s.begin(), s.end(), std::back_inserter(result)); + utflib::utf16to8(s.begin(), s.end(), std::back_inserter(result)); return result; } @@ -825,14 +832,14 @@ namespace iris::utflib [[nodiscard]] constexpr std::u16string utf8to16(std::string_view s) { std::u16string result; - utf8to16(s.begin(), s.end(), std::back_inserter(result)); + utflib::utf8to16(s.begin(), s.end(), std::back_inserter(result)); return result; } [[nodiscard]] constexpr std::u16string utf8to16(std::u8string_view s) { std::u16string result; - utf8to16(s.begin(), s.end(), std::back_inserter(result)); + utflib::utf8to16(s.begin(), s.end(), std::back_inserter(result)); return result; } @@ -848,14 +855,14 @@ namespace iris::utflib [[nodiscard]] constexpr std::string utf32to8(std::u32string_view s) { std::string result; - utf32to8(s.begin(), s.end(), std::back_inserter(result)); + utflib::utf32to8(s.begin(), s.end(), std::back_inserter(result)); return result; } [[nodiscard]] constexpr std::u8string utf32tou8(std::u32string_view s) { std::u8string result; - utf32to8(s.begin(), s.end(), std::back_inserter(result)); + utflib::utf32to8(s.begin(), s.end(), std::back_inserter(result)); return result; } From 1684caae201596d76ad2a080c6c05b55b5d71897 Mon Sep 17 00:00:00 2001 From: yaito3014 Date: Thu, 19 Feb 2026 02:19:05 +0900 Subject: [PATCH 27/28] Relax iterator requirement --- source/utf8.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/utf8.h b/source/utf8.h index a40868c..5042223 100644 --- a/source/utf8.h +++ b/source/utf8.h @@ -818,7 +818,7 @@ namespace iris::utflib template Se, typename OutIt> // TODO: add constraints constexpr OutIt utf8to16(It start, Se end, OutIt result) { - while (start < end) { // TODO: replace < with != + while (start != end) { const char32_t cp = utflib::next(start, end); if (cp > 0xffff) { // make a surrogate pair *result++ = static_cast((cp >> 10) + internal::LEAD_OFFSET); @@ -869,7 +869,7 @@ namespace iris::utflib template Se, typename OutIt> constexpr OutIt utf8to32(It start, Se end, OutIt result) { - while (start < end) // TODO: replace < with != + while (start != end) (*result++) = utflib::next(start, end); return result; From 6e2da694749caa42aa54eb6c728d223d4fd44ab7 Mon Sep 17 00:00:00 2001 From: yaito3014 Date: Thu, 19 Feb 2026 02:23:58 +0900 Subject: [PATCH 28/28] Relax iterator requirement 2 --- source/utf8.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/source/utf8.h b/source/utf8.h index 5042223..08d8d1f 100644 --- a/source/utf8.h +++ b/source/utf8.h @@ -766,11 +766,10 @@ namespace iris::utflib } template Se> - requires std::random_access_iterator [[nodiscard]] constexpr typename std::iterator_traits::difference_type distance(It first, Se last) { typename std::iterator_traits::difference_type dist; - for (dist = 0; first < last; ++dist) // TODO: investigate whether this operator< is needed + for (dist = 0; first != last; ++dist) (void)utflib::next(first, last); return dist; }