objective c - Escaping diacritics in a UTF8 string from C/Obj-C to javascript -


first, brief explanation of why i'm doing this:

i'm loading strings xml, , using these interact existing javascript functions. need escape them, because i'm using webview's stringbyevaluatingjavascriptfromstring method.

i'm using escape function:

- (nsstring *) stringbyescapingmetacharacters {         const char *utf8input = [self utf8string];     char *utf8output = [[nsmutabledata datawithlength:strlen(utf8input) * 4 + 1  /* worst case */] mutablebytes];     char ch, *och = utf8output;      while ((ch = *utf8input++))         if (ch == '\'' || ch == '\'' || ch == '\\' || ch == '"')         {             *och++ = '\\';             *och++ = ch;         }          else if (isascii(ch))             och = vis(och, ch, vis_nl | vis_tab | vis_cstyle, *utf8input);         else             och+= sprintf(och, "\\%03hho", ch);     return [nsstring stringwithutf8string:utf8output]; } 

it works fine, except diacritics. example, "é" shows "é"

so, how can escape diacritics?

you need implement proper utf-8 sequences escapement. this:

if (ch == '\'' || ch == '\'' || ch == '\\' || ch == '"') {     *och++ = '\\';     *och++ = ch; }  else if (((unsigned char)ch & 0xe0) == 0xc0) // 2 byte utf8 sequence {     *och++ = ch;     *och++ = utf8input++; } else if (((unsigned char)ch & 0xf0) == 0xe0)  // 3 byte utf8 sequence {     *och++ = ch;     *och++ = utf8input++;     *och++ = utf8input++; } else if (isascii(ch))      och = vis(och, ch, vis_nl | vis_tab | vis_cstyle, *utf8input); 

Comments