Make contains take the string by const reference again, for performance reasons

This commit is contained in:
ridiculousfish 2014-03-10 09:43:40 +08:00
parent 689d0082d8
commit e47ad09130
2 changed files with 11 additions and 9 deletions

View file

@ -576,7 +576,7 @@ wcstring wsetlocale(int category, const wchar_t *locale)
return format_string(L"%s", res); return format_string(L"%s", res);
} }
bool contains_internal(const wchar_t *a, ...) bool contains_internal(const wchar_t *a, int vararg_handle, ...)
{ {
const wchar_t *arg; const wchar_t *arg;
va_list va; va_list va;
@ -584,7 +584,7 @@ bool contains_internal(const wchar_t *a, ...)
CHECK(a, 0); CHECK(a, 0);
va_start(va, a); va_start(va, vararg_handle);
while ((arg=va_arg(va, const wchar_t *))!= 0) while ((arg=va_arg(va, const wchar_t *))!= 0)
{ {
if (wcscmp(a,arg) == 0) if (wcscmp(a,arg) == 0)
@ -598,17 +598,19 @@ bool contains_internal(const wchar_t *a, ...)
return res; return res;
} }
/* wcstring variant of contains_internal. The first parameter is a wcstring, the rest are const wchar_t* */ /* wcstring variant of contains_internal. The first parameter is a wcstring, the rest are const wchar_t *. vararg_handle exists only to give us a POD-value to apss to va_start */
__sentinel bool contains_internal(const wcstring needle, ...) __sentinel bool contains_internal(const wcstring &needle, int vararg_handle, ...)
{ {
const wchar_t *arg; const wchar_t *arg;
va_list va; va_list va;
int res = 0; int res = 0;
va_start(va, needle); const wchar_t *needle_cstr = needle.c_str();
va_start(va, vararg_handle);
while ((arg=va_arg(va, const wchar_t *))!= 0) while ((arg=va_arg(va, const wchar_t *))!= 0)
{ {
if (needle == arg) /* libc++ has an unfortunate implementation of operator== that unconditonally wcslen's the wchar_t* parameter, so prefer wcscmp directly */
if (! wcscmp(needle_cstr, arg))
{ {
res=1; res=1;
break; break;

View file

@ -228,7 +228,7 @@ extern const wchar_t *program_name;
/** /**
Check if the specified string element is a part of the specified string list Check if the specified string element is a part of the specified string list
*/ */
#define contains( str,... ) contains_internal( str, __VA_ARGS__, NULL ) #define contains( str, ... ) contains_internal( str, 0, __VA_ARGS__, NULL )
/** /**
Print a stack trace to stderr Print a stack trace to stderr
@ -688,8 +688,8 @@ wcstring wsetlocale(int category, const wchar_t *locale);
\return zero if needle is not found, of if needle is null, non-zero otherwise \return zero if needle is not found, of if needle is null, non-zero otherwise
*/ */
__sentinel bool contains_internal(const wchar_t *needle, ...); __sentinel bool contains_internal(const wchar_t *needle, int vararg_handle, ...);
__sentinel bool contains_internal(const wcstring needle, ...); __sentinel bool contains_internal(const wcstring &needle, int vararg_handle, ...);
/** /**
Call read while blocking the SIGCHLD signal. Should only be called Call read while blocking the SIGCHLD signal. Should only be called