Micro-optimise VarQuote by exploiting that meta characters are normally

rare and skipping over normal characters with strcspn is therefore
faster. Further simplify logic by always setting newline and precompute
the size of the string.

OK christos@
This commit is contained in:
joerg 2008-07-31 15:19:19 +00:00
parent 2ad045d932
commit f45c989c53
1 changed files with 16 additions and 9 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: var.c,v 1.132 2008/07/31 14:59:39 joerg Exp $ */
/* $NetBSD: var.c,v 1.133 2008/07/31 15:19:19 joerg Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1993
@ -69,14 +69,14 @@
*/
#ifndef MAKE_NATIVE
static char rcsid[] = "$NetBSD: var.c,v 1.132 2008/07/31 14:59:39 joerg Exp $";
static char rcsid[] = "$NetBSD: var.c,v 1.133 2008/07/31 15:19:19 joerg Exp $";
#else
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 3/19/94";
#else
__RCSID("$NetBSD: var.c,v 1.132 2008/07/31 14:59:39 joerg Exp $");
__RCSID("$NetBSD: var.c,v 1.133 2008/07/31 15:19:19 joerg Exp $");
#endif
#endif /* not lint */
#endif
@ -2055,17 +2055,24 @@ VarQuote(char *str)
/* This should cover most shells :-( */
static const char meta[] = "\n \t'`\";&<>()|*?{}[]\\$!#^~";
const char *newline;
size_t len, nlen;
newline = Shell_GetNewline();
if ((newline = Shell_GetNewline()) == NULL)
newline = "\\\n";
nlen = strlen(newline);
buf = Buf_Init(0);
for (; *str; str++) {
if (*str == '\n' && newline != NULL) {
Buf_AddBytes(buf, strlen(newline), newline);
while (*str != '\0') {
if ((len = strcspn(str, meta)) != 0) {
Buf_AddBytes(buf, len, str);
str += len;
} else if (*str == '\n') {
Buf_AddBytes(buf, nlen, newline);
++str;
} else {
if (strchr(meta, *str) != NULL)
Buf_AddByte(buf, (Byte)'\\');
Buf_AddByte(buf, (Byte)'\\');
Buf_AddByte(buf, (Byte)*str);
++str;
}
}
Buf_AddByte(buf, (Byte)'\0');