将 UTF8 字符串中的变音符号从 C/Obj-C 转义为 javascript
首先,简要解释一下我这样做的原因:
我从 XML 加载字符串,并使用它们与现有的 javascript 函数进行交互。我需要转义它们,只是因为我正在使用 webview 的 stringByEvaluatingJavaScriptFromString 方法。
我正在使用这个转义函数:
- (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];
}
它工作正常,除了变音符号。例如,“é”显示为“é”
那么,我怎样才能摆脱变音符号呢?
First, a brief explanation of why I'm doing this:
I'm loading strings from XML, and using these to interact with existing javascript functions. I need to escape them, only because I'm using the webview's stringByEvaluatingJavaScriptFromString method.
I'm using this 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 for diacritics. For example, "é" shows up as "é"
So, how can I escape the diacritics?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要实现正确的 UTF-8 序列擒纵。像这样的事情:
You need to implement proper UTF-8 sequences escapement. Something like this: