Skip to content
Snippets Groups Projects
Commit 1c9816d3 authored by Rob Swindell's avatar Rob Swindell :speech_balloon:
Browse files

Create utf8_strlcpy() which does a "safe" truncated-string copy

(doesn't leave a partial UTF-8 sequence at the end of the destination string).

This calls memcpy() for every char, which may not be performant, but I didn't
(yet) do any profiling. Hence, no premature optimization either.
parent a05acc7b
No related branches found
No related tags found
1 merge request!455Update branch with changes from master
......@@ -224,6 +224,28 @@ size_t utf8_str_count_width(const char* str, size_t min_width, size_t max_width)
return count;
}
// Like strlcpy(), but doesn't leave a partial UTF-8 sequence at the end of dst
size_t utf8_strlcpy(char* dst, const char* src, size_t size)
{
size_t i;
int len;
if (size < 1)
return 0;
for (i = 0; src[i] != '\0'; i += len) {
len = utf8_decode_firstbyte(src[i]);
if (len < 1)
break;
if(i + len < size) {
memcpy(dst, src + i, len);
dst += len;
}
}
*dst = '\0';
return i;
}
int cp437_to_utf8_str(const char* str, char* dest, size_t maxlen, unsigned char minval)
{
int retval = 0;
......
......@@ -44,6 +44,9 @@ size_t utf8_str_total_width(const char*);
// Return the count of chars within the specified width range in UTF-8 string (str)
size_t utf8_str_count_width(const char*, size_t min_width, size_t max_width);
// Like strlcpy(), but doesn't leave a partial UTF-8 sequence at the end of dst
size_t utf8_strlcpy(char* dst, const char* src, size_t size);
// Normalizes (to ASCII) chars in UTF-8 string 'str', in-place, resulting in string <= original in length
char* utf8_normalize_str(char* str);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment