diff options
| author | Matthias Melcher <github@matthiasm.com> | 2023-01-20 16:13:00 +0100 |
|---|---|---|
| committer | Matthias Melcher <github@matthiasm.com> | 2023-01-20 16:13:00 +0100 |
| commit | 2c595e8ddec759fb94bf1b06d21dada3a1b70978 (patch) | |
| tree | 35aaa548a6363327492796769b5b20cca729fa47 /src/fl_utf8.cxx | |
| parent | cd5301ac37cf05440ae3cff03dc3fdcea15af68d (diff) | |
STR 2822: function to count bytes in a UTF-8 string
Diffstat (limited to 'src/fl_utf8.cxx')
| -rw-r--r-- | src/fl_utf8.cxx | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/fl_utf8.cxx b/src/fl_utf8.cxx index 72589c8e7..2ca9d6b2f 100644 --- a/src/fl_utf8.cxx +++ b/src/fl_utf8.cxx @@ -119,7 +119,29 @@ int fl_utf8len1(char c) /** + Return the length in bytes of a UTF-8 string. + \param[in] text encoded in UTF-8 + \param[in] len number of Unicode characters, -1 to test until the end of text + \return number of bytes that make up the Unicode string + \see fl_utf_nb_char(const unsigned char *buf, int len) + */ +int fl_utf8strlen(const char *text, int len) +{ + if (len == -1) return (int)strlen(text); + int i, n = 0; + for (i=len; i>0; i--) { + if (*text == 0) return n; // end of string + int nc = fl_utf8len1(*text); + n += nc; + text += nc; + } + return n; +} + + +/** Returns the number of Unicode chars in the UTF-8 string. + \see fl_utf8strlen(const char *text, int len) */ int fl_utf_nb_char( |
