mirror of
https://github.com/neovim/neovim.git
synced 2025-10-04 08:56:29 +00:00
refactor: avoid quadratic behavior in backslash_halve() (#27827)
The original implementation has a worst-case of O(n^2). Every time rem_backslash() is true, it calculates the length of the rest of the string, and shift the rest of it to the left; backslash_halve_save() copies the original string before doing backslash_halve(). The new implementation is O(n). It will find the first character where rem_backslash() is true (it will do nothing if it's always false), and shift the characters in-place; backslash_halve_save() avoids copying the original string before doing backslash_halve().
This commit is contained in:
@@ -1457,11 +1457,21 @@ bool rem_backslash(const char *str)
|
||||
/// @param p
|
||||
void backslash_halve(char *p)
|
||||
{
|
||||
for (; *p; p++) {
|
||||
for (; *p && !rem_backslash(p); p++) {}
|
||||
if (*p != NUL) {
|
||||
char *dst = p;
|
||||
goto start;
|
||||
while (*p != NUL) {
|
||||
if (rem_backslash(p)) {
|
||||
STRMOVE(p, p + 1);
|
||||
start:
|
||||
*dst++ = *(p + 1);
|
||||
p += 2;
|
||||
} else {
|
||||
*dst++ = *p++;
|
||||
}
|
||||
}
|
||||
*dst = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
/// backslash_halve() plus save the result in allocated memory.
|
||||
@@ -1472,8 +1482,16 @@ void backslash_halve(char *p)
|
||||
char *backslash_halve_save(const char *p)
|
||||
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_RET
|
||||
{
|
||||
// TODO(philix): simplify and improve backslash_halve_save algorithm
|
||||
char *res = xstrdup(p);
|
||||
backslash_halve(res);
|
||||
char *res = xmalloc(strlen(p) + 1);
|
||||
char *dst = res;
|
||||
while (*p != NUL) {
|
||||
if (rem_backslash(p)) {
|
||||
*dst++ = *(p + 1);
|
||||
p += 2;
|
||||
} else {
|
||||
*dst++ = *p++;
|
||||
}
|
||||
}
|
||||
*dst = '\0';
|
||||
return res;
|
||||
}
|
||||
|
Reference in New Issue
Block a user