TopGit tracks andlabs/utf on GitHub. The project has 50 stars. [development paused; issues and PRs still welcome] Portable UTF-8 and UTF-16 routines in a single C source file.
Snapshot summary built from the project's own GitHub metadata — there's no written TopGit review yet. The page will update automatically when a full review is published.
WHY NO REVIEW YET
TopGit writes full reviews for the most-starred, most-requested repositories. This page is a snapshot until then — see the READ ME tab for the original README in full.
utf: a single-file portable standard C implementation of UTF-8 and UTF-16 utility functions
utf is a set of functions for dealing with UTF-8 and UTF-16 text.
utf is shipped as just one .c file and one .h file, so it can be integrated into any project with ease.
utf is written in standard C99, making it fully portable.
utf is intended to have fully defined and consistent behavior across platforms, including graceful handling of invalid input (so no error codes!).
On the flipside, this means utf might not perform optimally. It should, however, run fast enough for virtually every use. I've provided benchmarks for you to judge for yourself with; see below.
The design of utf is based on Go's unicode/utf8 and unicode/utf16 packages, however it does not use any of Go's code.
utf8EncodeRune() encodes the given rune as UTF-8 into encoded, returning the number of bytes encoded. encoded must be at least 4 bytes long. If the given rune cannot be encoded (for instance, if it is invalid or is a surrogate half), U+FFFD is encoded.
utf8DecodeRune() takes the UTF-8 sequence in s and decodes its first rune into rune. It returns a pointer to the start of the next rune.
nElem is the size of s; if nElem is 0, s is assumed to be large enough. Use this for C-style strings terminated with a '\0'.
If the first byte of s results in an invalid UTF-8 sequence, U+FFFD is stored in rune and the returned pointer is offset by one. So, for instance, if we pass in the invalid
EF BF 20
^
then the EF will be decoded as U+FFFD and a pointer to BF is returned:
EF BF 20
^
If you run utf8DecodeRune() again, the BF will also become U+FFFD. Keep this in mind.
utf16EncodeRune() encodes the given rune as UTF-16 into encoded, returning the number of uint16_ts encoded. encoded must be at least 2 elements long. If the given rune cannot be encoded (for instance, if it is invalid or is a surrogate half), U+FFFD is encoded.
utf16DecodeRune() takes the UTF-16 sequence in s and decodes its first rune into rune. It returns a pointer to the start of the next rune.
nElem is the size of s; if nElem is 0, s is assumed to be large enough. Use this for C-style strings terminated with a L'\0'.
If the first element of s results in an invalid UTF-16 sequence, U+FFFD is stored in rune and the returned pointer is offset by one. So, for instance, if we pass in the invalid
FDEF F987 0020
^
then the FDEF will be decoded as U+FFFD and a pointer to F987 is returned:
FDEF F987 0020
^
If you run utf16DecodeRune() again, the F987 will also become U+FFFD. Keep this in mind.
utf8UTF16Count() returns the number of elements (uint16_ts) needed to convert s from UTF-8 to UTF-16, following the same rules as utf8DecodeRune() and utf16EncodeRune(). This function runs in O(N) time.
If nElem is 0, utf8UTF16Count() stops at a '\0' (which is not included in the count); otherwise, it stops after nElem elements.
utf16UTF8Count() returns the number of bytes needed to convert s from UTF-16 to UTF-8, following the same rules as utf16DecodeRune() and utf8EncodeRune(). This function runs in O(N) time.
If nElem is 0, utf16UTF8Count() stops at a L'\0' (which is not included in the count); otherwise, it stops after nElem elements.
if you are using Microsoft's Visual Studio C++ compilers and
if you are using C++
These overloads transparently handle wchar_t * and uint16_t * being incompatible under all of the above conditions for you. There is no other difference. This extends to Windows API-specific types like WCHAR * that are aliases for wchar_t *. (The use of __wchar_t allows this to work even if wchar_t being a distinct type is turned off. This is fully documented in various places on MSDN.)
Benchmarks
The benchmark/ folder contains benchmarks you can use not only to evaluate utf's performance, but also to compare utf's performance against other libraries. At minimum, you'll need GNU make to build the benchmarks. See the comments at the top of GNUmakefile for details.
Contributing
Welcome.
TODOs
Add a utf8IsValid()/utf16IsValid()?
Add a utf8IsFull()/utf16IsFull()?
Add a utf8RuneEncodedLength()/utf16RuneEncodedLength()?
Add a utf16IsSurrogate()? utfValidRune()? named rune constants?
Fix remaining MSVC warnings
Write a real test suite sometime
Figure out the best way to make this eligible for https://github.com/nothings/single_file_libs#new-libraries-and-corrections-1 (can the license go at the bottom of the .c file? should it, for any other person ever? I've never dealt with file preambles before so I'm not sure what the subtleties are)
Background
This came about when I was planning the text event system of libui. Windows and OS X both use UTF-16 for its internal string data types; however, libui uses UTF-8 for all text strings. I got away with it so far because I either only needed to convert entire strings or I decided to use grapheme cluster boundaries instead of byte or codepoint offsets. However, this broke apart with the text handling system, since I have to allow attributed strings to be manipulated after they were made. Therefore, I needed to be able to build tables of mappings between UTF-8 byte offsets and UTF-16 array indices. Building such loops with OS-specific APIs introduces a number of pain points, such as what to do about API error codes and what to do about invalid byte sequences.
The most recent commit recorded on andlabs/utf was 9.7 years ago, based on the GitHub push timestamp. The repository has 5 forks — one of the better signals of community interest.
How many stars does andlabs/utf have?
andlabs/utf has 50 GitHub stars — refresh the page for the live number, or check github.com/andlabs/utf. TopGit mirrors GitHub's count but does not claim minute-by-minute accuracy.
Is andlabs/utf open source?
TopGit's metadata for andlabs/utf does not record a license. Most public repositories on GitHub ARE open source, but the exact terms vary — verify by opening the LICENSE file directly.
What is andlabs/utf?
andlabs/utf (andlabs/utf) is a C project on GitHub. From the project's own README: [development paused; issues and PRs still welcome] Portable UTF-8 and UTF-16 routines in a single C source file.
What language is andlabs/utf written in?
andlabs/utf is written primarily in C. GitHub's language field is based on the largest share of bytes in the default branch.
Where do I read more about andlabs/utf?
This TopGit page is a snapshot — the READ ME tab shows the project's own README content (links stripped, images preserved). The GitHub repository at github.com/andlabs/utf is the definitive source.
Read full README in the tab above.
Want a second opinion on utf?
Ask an AI that can read this page — one click and you get its take on utf.