【发布时间】:2012-03-13 00:01:14
【问题描述】:
在给定字符串中查找第一个未转义字符的最佳方法是什么?
我就是这样做的,但我觉得它过于复杂。
/*
* Just like strchr, but find first -unescaped- occurrence of c in s.
*/
char *
strchr_unescaped(char *s, char c)
{
int i, escaped;
char *p;
/* Search for c until an unescaped occurrence is found or end of string is
reached. */
for (p=s; p=strchr(p, c); p++) {
escaped = -1;
/* We found a c. Backtrace from it's location to determine if it is
escaped. */
for (i=1; i<=p-s; i++) {
if (*(p-i) == '\\') {
/* Switch escaped flag every time a \ is found. */
escaped *= -1;
continue;
}
/* Stop backtracking when something other than a \ is found. */
break;
}
/* If an odd number of escapes were found, c is indeed escaped. Keep
looking. */
if (escaped == 1)
continue;
/* We found an unescaped c! */
return p;
}
return NULL;
}
【问题讨论】:
-
取决于最佳的定义,但您的解决方案似乎比必要的工作更多。而不是使用 strchr 和回溯(每个反斜杠查看两次),您可以向前读取并跟踪状态(转义/未转义),因此只查看每个字符一次。
-
我明白你的意思。另一方面,这允许我仅在我知道确实有必要时才测试转义。使用您的解决方案,无论是否有逃逸,都会为每个检查的角色支付跟踪逃逸的成本。我猜哪个更好取决于测试字符串的性质,即转义字符的比例。
-
取决于您所说的“成本”。 strchr() 正在查看您的代码避免检查是否为转义的所有这些字符,因此它不像它们没有被测试,尽管您必须检查每个字符的 c 和 \ (这不会看起来非常昂贵,但如果您使用查找表,您可以同时检查两者)。