[PATCH] scanner.c: prefer strchr() over for loop and toupper() in place
James Tirta Halim
tirtajames45 at gmail.com
Wed Jan 3 05:48:26 UTC 2024
uppercase_dup() was copying SRC to DST and converting DST to uppercase. Now it
converts to uppercase while copying.
find_enumeration() was calling strlen() on the whole string before checking for the '.'
character in a loop. Now it uses strchr().
---
src/scanner.c | 19 +++++++------------
1 file changed, 7 insertions(+), 12 deletions(-)
diff --git a/src/scanner.c b/src/scanner.c
index c512d23..1c67a4e 100644
--- a/src/scanner.c
+++ b/src/scanner.c
@@ -289,13 +289,10 @@ xstrdup(const char *s)
static char *
uppercase_dup(const char *src)
{
- char *u;
- int i;
+ char *u, *dst;
- u = xstrdup(src);
- for (i = 0; u[i]; i++)
- u[i] = toupper(u[i]);
- u[i] = '\0';
+ dst = u = fail_on_null(malloc(strlen(src) + 1));
+ while ((*dst++ = toupper(*src++)));
return u;
}
@@ -915,13 +912,11 @@ find_enumeration(struct protocol *protocol,
struct interface *i;
struct enumeration *e;
char *enum_name;
- uint32_t idx = 0, j;
+ uintptr_t idx;
- for (j = 0; j + 1 < strlen(enum_attribute); j++) {
- if (enum_attribute[j] == '.') {
- idx = j;
- }
- }
+ idx = (uintptr_t)strchr(enum_attribute, '.');
+ if (idx && *((char *)idx + 1) != '\0') {
+ idx = idx - (uintptr_t)enum_attribute;
-
- if (idx > 0) {
enum_name = enum_attribute + idx + 1;
--
2.43.0
More information about the wayland-devel
mailing list