UTF8-CPP Documentation

repository·master·Indexed 23 days ago

https://github.com/nemtrif/utfcpp

A lightweight, header-only, and portable C++ library for handling UTF-8 encoded strings. It provides tools for validation, character counting, and conversion between UTF-8, UTF-16, and UTF-32. The library supports various C++ standards, offering modern APIs for C++11 and later, as well as iterator-based functions for pre-C++11 environments.

Tokens
8.9K
Snippets
32
Records
45
Agent score
34%

What's inside UTF8-CPP

  1. How to use UTF8-CPP with different C++ standards

    master

    The library provides different API patterns depending on the C++ standard used by your compiler:

    C++11 and later

    The library exposes modern APIs that support standard Unicode strings (u16string) and move semantics.

    Pre-C++11

    You must use iterator-based functions and provide a container (like std::vector<unsigned short>) to receive the converted data.

    Manual Standard Selection

    If you want to override the automatic detection of the C++ standard (e.g., if your compiler's __cplusplus macro is unreliable, like in some versions of MSVC), define the UTF_CPP_CPLUSPLUS macro before including utf8.h. Assign it a value corresponding to the desired standard (matching the __cplusplus macro values).

  2. Use utf8::unchecked namespace for high-performance, unsafe UTF-8 operations

    master

    The utf8::unchecked namespace provides faster versions of the standard utf8 functions.

    WARNING: These functions are less safe. They do not check for the validity of the supplied UTF-8 sequences or code points and do not perform boundary checking. Using them on invalid data may produce invalid UTF-8 sequences or cause undefined behavior.

  3. Use utf8::iterator for STL-compatible UTF-8 iteration

    master

    The utf8::iterator adapter allows you to iterate over UTF-8 encoded strings as if they were sequences of code points. This enables the use of standard STL algorithms with UTF-8 data.

    Important Safety Note: utf8::iterator is a checked iterator. It operates within the specific range provided during construction. Any attempt to move the iterator outside this range, or to compare two iterators constructed from different ranges, will result in an exception.

    Commonly, you should initialize it using the begin() and end() methods of a sequence container like std::string.

    std::string s = "example";
    utf8::iterator i (s.begin(), s.begin(), s.end());
  4. Install UTF8-CPP

    master

    UTF8-CPP is a header-only library. To install it, follow these steps:

    1. Download a release from the GitHub releases page into a temporary directory.
    2. Unzip the release.
    3. Copy the contents of the utfcpp/source directory into your project's include directory.

    Note: Using the CMakeLists.txt file to install the library is not a supported deployment method.

  5. Calculate the number of UTF-8 code points with utf8::distance

    master

    The utf8::distance function returns the number of UTF-8 encoded code points between two iterators. This is a linear $O(N)$ operation used to find the length of a UTF-8 string in terms of code points.

    • first: Iterator to the beginning of the sequence.
    • last: Iterator to the "post-end" of the last code point (can be the start of a new code point or not).

    Exceptions:

    • utf8::invalid_utf8: Thrown if an invalid UTF-8 sequence is encountered.
    • utf8::not_enough_room: Thrown if last does not point to the past-of-end of a UTF-8 sequence.
    char* twochars = "\xe6\x97\xa5\xd1\x88";
    size_t dist = utf8::distance(twochars, twochars + 5);
    assert (dist == 2);
  6. Convert UTF-16 to UTF-8

    master

    There are several ways to convert UTF-16 to UTF-8 depending on your requirements and C++ standard:

    1. Using Iterators (Generic)

    Converts a range of UTF-16 iterators to an output iterator.

    • Signature: octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result)
    • Throws: utf8::invalid_utf16 if the sequence is invalid.

    2. Using std::u16string (C++11)

    • Signature: std::string utf16to8(const std::u16string& s)
    • Throws: utf8::invalid_utf16.

    3. Using std::u16string_view (C++17)

    • Signature: std::string utf16to8(std::u16string_view s)
    • Throws: utf8::invalid_utf16.

    4. Using std::u8string (C++20)

    Returns a std::u8string instead of std::string.

    • Signatures:
      • std::u8string utf16tou8(const std::u16string& s)
      • std::u8string utf16tou8(const std::u16string_view& s)
    • Throws: utf8::invalid_utf16.
    // Iterator version
    unsigned short utf16string[] = {0x41, 0x0448, 0x65e5, 0xd834, 0xdd1e};
    vector<unsigned char> utf8result;
    utf16to8(utf16string, utf16string + 5, back_inserter(utf8result));
    assert (utf8result.size() == 10);    
    
    // std::u16string version (C++11)
    u16string utf16string = {0x41, 0x0448, 0x65e5, 0xd834, 0xdd1e};
    string u = utf16to8(utf16string);
    assert (u.size() == 10);
  7. Convert UTF-8 to UTF-16

    master

    Convert UTF-8 encoded strings to UTF-16.

    1. Using Iterators (Generic)

    • Signature: u16bit_iterator utf8to16 (octet_iterator start, octet_iterator end, u16bit_iterator result)
    • Throws: utf8::invalid_utf8 if sequence is invalid; utf8::not_enough_room if end is not at a code point boundary.

    2. Using std::string (C++11)

    • Signature: std::u16string utf8to16(const std::string& s)
    • Throws: utf8::invalid_utf8.

    3. Using std::string_view (C++17)

    • Signature: std::u16string utf8to16(std::string_view s)
    • Throws: utf8::invalid_utf8.

    4. Using std::u8string (C++20)

    • Signatures:
      • std::u16string utf8to16(const std::u8string& s)
      • std::u16string utf8to16(std::u8string_view& s)
    • Throws: utf8::invalid_utf8.
    // Iterator version
    char utf8_with_surrogates[] = "\xe6\x97\xa5\xd1\x88\xf0\x9d\x84\x9e";
    vector <unsigned short> utf16result;
    utf8to16(utf8_with_surrogates, utf8_with_surrogates + 9, back_inserter(utf16result));
    assert (utf16result.size() == 4);
    
    // std::string version (C++11)
    string utf8_with_surrogates = "\xe6\x97\xa5\xd1\x88\xf0\x9d\x84\x9e";
    u16string utf16result = utf8to16(utf8_with_surrogates);
    assert (utf16result.length() == 4);
  8. Convert UTF-32 to UTF-8

    master

    Converts a UTF-32 encoded string to UTF-8 using iterators.

    • Signature: octet_iterator utf32to8 (u32bit_iterator start, u32bit_iterator end, octet_iterator result)
    • Parameters:
      • start: Iterator to the beginning of the UTF-32 string.
      • end: Iterator to the end of the UTF-32 string.
      • result: Output iterator where UTF-8 bytes are appended.
    • Return value: An iterator pointing to the position after the appended UTF-8 string.
    int utf32string[] = {0x448, 0x65E5, 0x10346, 0};
    vector<unsigned char> utf8result;
    utf32to8(utf32string, utf32string + 3, back_inserter(utf8result));
    assert (utf8result.size() == 9);
  9. Peek at the next UTF-8 code point with `utf8::peek_next`

    master

    Returns the 32-bit code point for the following sequence without advancing the iterator. This is useful for inspecting the next character without consuming it.

    Throws utf8::not_enough_room if the iterator reaches end during extraction, and utf8::invalid_utf8 if the sequence is invalid.

  10. Calculate Unicode code point distance

    master
    To count the number of Unicode code points (characters) in a UTF-8 encoded range, use utf8::distance(begin, end). This is more accurate than std::distance for UTF-8, as it counts actual characters rather than individual bytes.
  11. Append a code point to a UTF-16 string using `utf8::append16`

    master

    Encodes a 32-bit code point as a UTF-16 sequence and appends it to a UTF-16 string or an output iterator.

    Using an output iterator: Does not allocate memory. The caller must ensure sufficient space. It can add one or two words. Using std::back_inserter is recommended.

    Using std::u16string: Directly appends the encoded code point to a std::u16string object (requires C++11).

    Throws utf8::invalid_code_point if the code point is invalid.

  12. Append a code point to a UTF-8 string using `utf8::append`

    master

    Encodes a 32-bit code point as a UTF-8 sequence and appends it to a UTF-8 string or an output iterator.

    Using an output iterator: append does not allocate memory; the caller must ensure sufficient space. It can add between 1 and 4 octets. Using std::back_inserter is recommended to handle memory allocation automatically.

    Using std::string: Directly appends the encoded code point to a std::string object.

    Throws utf8::invalid_code_point if the code point is invalid.