From 78a0d49bdb4dcabb53e381e841493e893d3095bf Mon Sep 17 00:00:00 2001 From: Frank Gevaerts Date: Sun, 11 Sep 2011 15:54:56 +0000 Subject: Second attempt at making %ss utf8-clean git-svn-id: svn://svn.rockbox.org/rockbox/trunk@30509 a1c6a512-1295-4272-9138-f99709370657 --- apps/gui/skin_engine/skin_tokens.c | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/apps/gui/skin_engine/skin_tokens.c b/apps/gui/skin_engine/skin_tokens.c index 51da8c1c18..fa697b18e9 100644 --- a/apps/gui/skin_engine/skin_tokens.c +++ b/apps/gui/skin_engine/skin_tokens.c @@ -874,24 +874,29 @@ const char *get_token_value(struct gui_wps *gwps, struct substring *ss = token->value.data; const char *token_val = get_token_value(gwps, ss->token, offset, buf, buf_size, intval); - int ret_len = ss->length; if (token_val) { - int len = utf8length(token_val); - if (len < ss->start) + int start_byte, end_byte, byte_len; + int utf8_len = utf8length(token_val); + + if (utf8_len < ss->start) return NULL; - int realstart = utf8seek(token_val, ss->start); - if (ret_len < 0) - ret_len = strlen(token_val) - realstart; + + start_byte = utf8seek(token_val, ss->start); + + if (ss->length < 0 || (ss->start + ss->length) > utf8_len) + end_byte = strlen(token_val); + else + end_byte = utf8seek(token_val, ss->start + ss->length); + + byte_len = end_byte - start_byte; + if (token_val != buf) - { - memcpy(buf, &token_val[realstart], ret_len); - } + memcpy(buf, &token_val[start_byte], byte_len); else - { - buf = &buf[realstart]; - } - buf[ret_len] = '\0'; + buf = &buf[start_byte]; + + buf[byte_len] = '\0'; return buf; } return NULL; -- cgit v1.2.3