Tiny simple library to convert between utf8 bytes and char32_t codepoints in C++.
- The default decoder function
Decode
is based on statemachine approach: https://bjoern.hoehrmann.de/utf-8/decoder/dfa. - I also implemented a naive approach decoder function:
DecodeNaive
, which seems to be a little faster. benchmark. - rfc3629 - UTF-8
No external dependencies except a compiler that supports at least C++17.
#include <iostream>
#include <string>
#include "simple_utf8.h"
int main() {
// Decoding via DFA
std::string s("你好世界, hello world! 😊");
std::u32string p(simple_utf8::CountCodes(s), 0);
auto n = simple_utf8::Decode(s, p); // returns number of codepoints.
for (auto codepoint : p) std::cout << codepoint << std::endl;
// Decoding via naive approach.
std::u32string p1(simple_utf8::CountCodes(s), 0);
auto n1 = simple_utf8::DecodeNaive(s, p1);
for (auto codepoint : p1) std::cout << codepoint << std::endl;
// Encoding
std::string s2(simple_utf8::CountBytes(p), 0);
auto k = simple_utf8::Encode(p, s2);
std::cout << s2 << std::endl;
return 0;
}
Install Catch2 via conan (optional, or cmake will download it automatically if missing):
make install-debug
Run tests:
make cmake-build-test
make run-tests
How to run benchmark:
rm -rf build
make install-release
make cmake-build-test-release-mode
make run-benchmark
Benchmark results on my computer for a 24KB random utf8 string UTF-8-random-1.txt, benchmark on action
benchmark name samples iterations est run time
mean low mean high mean
std dev low std dev high std dev
-------------------------------------------------------------------------------
Decode benchmark (random) 100 1 5.6547 ms
58.3573 us 57.9402 us 59.1934 us
2.89185 us 1.6351 us 4.63128 us
DecodeNaive benchmark (random) 100 2 3.5126 ms
16.0734 us 15.7466 us 16.9565 us
2.53024 us 1.14389 us 5.37552 us
中文博客: https://writings.sh/post/utf8
Copyright (c) 2023 Chao Wang <[email protected]> for this library.
Copyright (c) 2008-2009 Bjoern Hoehrmann <[email protected]> for the utf8 dfa decoder.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
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 AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.