Skip to content

Commit 9a2cf21

Browse files
UTIL: sss_tc_utf8_str_tolower() optimization
In vast majority of cases strings are ascii and lowercase. In other cases overhead added should be negligible. Reviewed-by: Justin Stephenson <jstephen@redhat.com> Reviewed-by: Sumit Bose <sbose@redhat.com>
1 parent 09e283e commit 9a2cf21

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

src/util/sss_tc_utf8.c

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,29 @@
2121
#include "config.h"
2222

2323
#include <stdlib.h>
24+
#include <stdbool.h>
2425
#include <unistr.h>
2526
#include <unicase.h>
2627

2728
#include <talloc.h>
2829
#include "util/util.h"
2930

31+
static inline bool sss_is_ascii_lowercase(const char *s)
32+
{
33+
if (s == NULL) {
34+
return true;
35+
}
36+
37+
while (*s != 0) {
38+
if (((unsigned char)*s > 0x7F) || ((*s >= 'A') && (*s <= 'Z'))) {
39+
return false;
40+
}
41+
s++;
42+
}
43+
44+
return true;
45+
}
46+
3047
/* Expects and returns NULL-terminated string;
3148
* result must be freed with free()
3249
*/
@@ -41,10 +58,15 @@ char *sss_tc_utf8_str_tolower(TALLOC_CTX *mem_ctx, const char *s)
4158
char *lower;
4259
char *ret = NULL;
4360

44-
lower = sss_utf8_tolower(s);
45-
if (lower) {
46-
ret = talloc_strdup(mem_ctx, lower);
47-
free(lower);
61+
if (sss_is_ascii_lowercase(s)) {
62+
ret = talloc_strdup(mem_ctx, s);
63+
}
64+
else {
65+
lower = sss_utf8_tolower(s);
66+
if (lower) {
67+
ret = talloc_strdup(mem_ctx, lower);
68+
free(lower);
69+
}
4870
}
4971

5072
return ret;

0 commit comments

Comments
 (0)