Commit 2d20e3c2 authored by Olaf Bergmann's avatar Olaf Bergmann

dcaf_utf8.[hc]: add function to determine length of UTF8-encoded string

The function utf8_length() counts the total number of bytes required
to encode a given byte string.
parent 41b4e13b
......@@ -14,6 +14,18 @@
#include <stddef.h>
#include <stdint.h>
* Calculates the number of bytes the string @p src of length
* @p srclen would take when encoded as UTF8. This function
* considers only 8-bit values, i.e., code points between 0 and 255.
* @param src The source to be encoded.
* @param srclen The size of @p src in bytes.
* @return The number of bytes the UTF8-encoded string would take.
size_t utf8_length(const uint8_t *src, size_t srclen);
* UTF8-encodes @p srclen bytes from @p src into the buffer @p
* dst. @p *dstlen specifies the size of @p dst and is overwritten
......@@ -12,6 +12,16 @@
#define CHECK_PTR(Current, End, Count) \
if ((End) < ((Current) + (Count))) { goto finish; }
size_t utf8_length(const uint8_t *src, size_t srclen) {
size_t n = srclen;
while (srclen--) {
/* traverse string and add 1 for each character above 127 */
if (*src++ > 127)
return n;
bool uint8_to_utf8(char *dst, size_t *dstlen,
const uint8_t *src, size_t srclen) {
char *p = dst;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment