mirror of
https://github.com/fltk/fltk.git
synced 2026-05-29 04:26:27 +08:00
STR 2822: function to count bytes in a UTF-8 string
This commit is contained in:
@@ -56,6 +56,9 @@ FL_EXPORT int fl_utf8len(char c);
|
|||||||
/* OD: returns the byte length of the first UTF-8 char sequence (returns +1 if not valid) */
|
/* OD: returns the byte length of the first UTF-8 char sequence (returns +1 if not valid) */
|
||||||
FL_EXPORT int fl_utf8len1(char c);
|
FL_EXPORT int fl_utf8len1(char c);
|
||||||
|
|
||||||
|
/* OD: returns the byte length of a UTF-8 text */
|
||||||
|
FL_EXPORT int fl_utf8strlen(const char *text, int len=-1);
|
||||||
|
|
||||||
/* OD: returns the number of Unicode chars in the UTF-8 string */
|
/* OD: returns the number of Unicode chars in the UTF-8 string */
|
||||||
FL_EXPORT int fl_utf_nb_char(const unsigned char *buf, int len);
|
FL_EXPORT int fl_utf_nb_char(const unsigned char *buf, int len);
|
||||||
|
|
||||||
|
|||||||
@@ -118,8 +118,30 @@ int fl_utf8len1(char c)
|
|||||||
} // fl_utf8len1
|
} // fl_utf8len1
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
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.
|
Returns the number of Unicode chars in the UTF-8 string.
|
||||||
|
\see fl_utf8strlen(const char *text, int len)
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
fl_utf_nb_char(
|
fl_utf_nb_char(
|
||||||
|
|||||||
Reference in New Issue
Block a user