text
stringlengths
0
357
if (s==NULL) {
return 0;
}
size_t i = 0;
size_t j = 0;
while (s[i]) {
if ((s[i]&0xC0) != 0x80)
j++;
i++;
}
return j;
}
static size_t dc_utf8_strnlen(const char* s, size_t n)
{
if (s==NULL) {
return 0;
}
size_t i = 0;
size_t j = 0;
while (i < n) {
if ((s[i]&0xC0) != 0x80)
j++;
i++;
}
return j;
}
void dc_truncate_n_unwrap_str(char* buf, int approx_characters, int do_unwrap)
{
/* Function unwraps the given string and removes unnecessary whitespace.
Function stops processing after approx_characters are processed.
(as we're using UTF-8, for simplicity, we cut the string only at whitespaces). */
const char* ellipse_utf8 = do_unwrap? " ..." : " " DC_EDITORIAL_ELLIPSE; /* a single line is truncated `...` instead of `[...]` (the former is typically also used by the UI to fit strings in a rectangle) */
int lastIsCharacter = 0;
unsigned char* p1 = (unsigned char*)buf; /* force unsigned - otherwise the `> ' '` comparison will fail */
while (*p1) {
if (*p1 > ' ') {
lastIsCharacter = 1;
}
else {
if (lastIsCharacter) {
size_t used_bytes = (size_t)((uintptr_t)p1 - (uintptr_t)buf);
if (dc_utf8_strnlen(buf, used_bytes) >= approx_characters) {
size_t buf_bytes = strlen(buf);
if (buf_bytes-used_bytes >= strlen(ellipse_utf8) /* check if we have room for the ellipse */) {
strcpy((char*)p1, ellipse_utf8);
}
break;
}
lastIsCharacter = 0;
if (do_unwrap) {
*p1 = ' ';
}
}
else {
if (do_unwrap) {
*p1 = '\r'; /* removed below */
}
}
}
p1++;
}
if (do_unwrap) {
dc_remove_cr_chars(buf);
}
}
void dc_truncate_str(char* buf, int approx_chars)
{
if (approx_chars > 0 && strlen(buf) > approx_chars+strlen(DC_EDITORIAL_ELLIPSE))
{
char* p = &buf[approx_chars]; /* null-terminate string at the desired length */
*p = 0;
if (strchr(buf, ' ')!=NULL) {
while (p[-1]!=' ' && p[-1]!='\n') { /* rewind to the previous space, avoid half utf-8 characters */
p--;
*p = 0;
}
}
strcat(p, DC_EDITORIAL_ELLIPSE);
}
}
carray* dc_split_into_lines(const char* buf_terminated)
{
carray* lines = carray_new(1024);
size_t line_chars = 0;
const char* p1 = buf_terminated;
const char* line_start = p1;