Replace strneq() with hardcoded lengths with strstartswith()

Slightly less efficient but easier to read and it's not possible to
accidentally provide the wrong length. Plus it handles null pointers
correctly so get to skip the checks (which weren't needed for strneq()
either, but still).

Part-of: <https://gitlab.freedesktop.org/libinput/libinput/-/merge_requests/1121>
This commit is contained in:
Peter Hutterer 2025-01-09 09:04:54 +10:00 committed by Marge Bot
parent 00bc910df7
commit 2c0c4a6516
12 changed files with 32 additions and 32 deletions

View file

@ -531,7 +531,7 @@ section_destroy(struct section *s)
static inline bool
parse_hex(const char *value, unsigned int *parsed)
{
return strneq(value, "0x", 2) &&
return strstartswith(value, "0x") &&
safe_atou_base(value, parsed, 16) &&
strspn(value, "0123456789xABCDEF") == strlen(value) &&
*parsed <= 0xFFFF;
@ -622,7 +622,7 @@ parse_match(struct quirks_context *ctx,
s->match.version = version;
} else if (streq(key, "MatchDMIModalias")) {
check_set_bit(s, M_DMI);
if (!strneq(value, "dmi:", 4)) {
if (!strstartswith(value, "dmi:")) {
qlog_parser(ctx,
"%s: MatchDMIModalias must start with 'dmi:'\n",
s->name);
@ -680,7 +680,7 @@ parse_model(struct quirks_context *ctx,
bool b;
enum quirk q = QUIRK_MODEL_ALPS_SERIAL_TOUCHPAD;
assert(strneq(key, "Model", 5));
assert(strstartswith(key, "Model"));
if (!parse_boolean_property(value, &b))
return false;
@ -918,11 +918,11 @@ parse_value_line(struct quirks_context *ctx, struct section *s, const char *line
if (value[0] == '"' || value[0] == '\'')
goto out;
if (strneq(key, "Match", 5))
if (strstartswith(key, "Match"))
rc = parse_match(ctx, s, key, value);
else if (strneq(key, "Model", 5))
else if (strstartswith(key, "Model"))
rc = parse_model(ctx, s, key, value);
else if (strneq(key, "Attr", 4))
else if (strstartswith(key, "Attr"))
rc = parse_attr(ctx, s, key, value);
else
qlog_error(ctx, "Unknown value prefix %s\n", line);
@ -1050,7 +1050,7 @@ parse_file(struct quirks_context *ctx, const char *path)
path, lineno, line);
goto out;
case STATE_MATCH:
if (!strneq(line, "Match", 5)) {
if (!strstartswith(line, "Match")) {
qlog_parser(ctx, "%s:%d: expected MatchFoo=bar, have %s\n",
path, lineno, line);
goto out;
@ -1058,11 +1058,11 @@ parse_file(struct quirks_context *ctx, const char *path)
state = STATE_MATCH_OR_VALUE;
break;
case STATE_MATCH_OR_VALUE:
if (!strneq(line, "Match", 5))
if (!strstartswith(line, "Match"))
state = STATE_VALUE_OR_SECTION;
break;
case STATE_VALUE_OR_SECTION:
if (strneq(line, "Match", 5)) {
if (strstartswith(line, "Match")) {
qlog_parser(ctx, "%s:%d: expected value or [Section], have %s\n",
path, lineno, line);
goto out;

View file

@ -183,7 +183,7 @@ udev_input_add_devices(struct udev_input *input, struct udev *udev)
continue;
sysname = udev_device_get_sysname(device);
if (!strneq("event", sysname, 5)) {
if (!strstartswith(sysname, "event")) {
udev_device_unref(device);
continue;
}
@ -227,7 +227,7 @@ evdev_udev_handler(void *data)
if (!action)
goto out;
if (!strneq("event", udev_device_get_sysname(udev_device), 5))
if (!strstartswith(udev_device_get_sysname(udev_device), "event"))
goto out;
if (streq(action, "add"))

View file

@ -305,7 +305,7 @@ parse_evcode_string(const char *s, int *type_out, int *code_out)
{
int type, code;
if (strneq(s, "EV_", 3)) {
if (strstartswith(s, "EV_")) {
type = libevdev_event_type_from_name(s);
if (type == -1)
return false;

View file

@ -57,7 +57,7 @@ all_codes_create(struct litest_device *d)
for (idx = 0, code = 0; code < KEY_MAX; code++) {
const char *name = libevdev_event_code_get_name(EV_KEY, code);
if (name && strneq(name, "BTN_", 4))
if (strstartswith(name, "BTN_"))
continue;
events[idx++] = EV_KEY;

View file

@ -1356,7 +1356,7 @@ litest_init_device_udev_rules(struct litest_test_device *dev, FILE *f)
kv = dev->udev_properties;
while (kv->key) {
fprintf(f, ", \\\n\tENV{%s}=\"%s\"", kv->key, kv->value);
if (strneq(kv->key, "EVDEV_ABS_", 10))
if (strstartswith(kv->key, "EVDEV_ABS_"))
need_keyboard_builtin = true;
kv++;
}
@ -1423,7 +1423,7 @@ open_restricted(const char *path, int flags, void *userdata)
if (fd < 0)
return -errno;
if (strneq(path, prefix, strlen(prefix))) {
if (strstartswith(path, prefix)) {
p = zalloc(sizeof *p);
p->path = safe_strdup(path);
p->fd = fd;
@ -2298,7 +2298,7 @@ udev_wait_for_device_event(struct udev_monitor *udev_monitor,
}
udev_syspath = udev_device_get_syspath(udev_device);
if (udev_syspath && strstartswith(udev_syspath, syspath))
if (strstartswith(udev_syspath, syspath))
break;
udev_device_unref(udev_device);

View file

@ -1695,8 +1695,8 @@ START_TEST(device_button_down_remove)
keyname = libevdev_event_code_get_name(EV_KEY, code);
if (!keyname ||
!strneq(keyname, "BTN_", 4) ||
strneq(keyname, "BTN_TOOL_", 9))
!strstartswith(keyname, "BTN_") ||
strstartswith(keyname, "BTN_TOOL_"))
continue;
if (!libevdev_has_event_code(lidev->evdev, EV_KEY, code))

View file

@ -342,7 +342,7 @@ START_TEST(keyboard_no_buttons)
continue;
name = libevdev_event_code_get_name(EV_KEY, code);
if (!name || !strneq(name, "KEY_", 4))
if (!strstartswith(name, "KEY_"))
continue;
litest_keyboard_key(dev, code, true);

View file

@ -481,7 +481,7 @@ START_TEST(path_device_sysname)
litest_assert_notnull(sysname);
litest_assert_int_gt(strlen(sysname), 1U);
litest_assert(strchr(sysname, '/') == NULL);
litest_assert(strneq(sysname, "event", 5));
litest_assert(strstartswith(sysname, "event"));
libinput_event_destroy(ev);
}

View file

@ -487,7 +487,7 @@ START_TEST(udev_device_sysname)
litest_assert_notnull(sysname);
litest_assert_int_gt(strlen(sysname), 1U);
litest_assert(strchr(sysname, '/') == NULL);
litest_assert(strneq(sysname, "event", 5));
litest_assert(strstartswith(sysname, "event"));
libinput_event_destroy(ev);
}

View file

@ -199,7 +199,7 @@ main(int argc, char **argv)
goto out;
path = argv[optind];
if (strneq(path, "/sys/", 5)) {
if (strstartswith(path, "/sys/")) {
device = udev_device_new_from_syspath(udev, path);
} else {
struct stat st;

View file

@ -1365,9 +1365,9 @@ print_system_header(FILE *fp)
while (fgets(osrstr, sizeof(osrstr), osrelease)) {
osrstr[strlen(osrstr) - 1] = '\0'; /* linebreak */
if (!distro && strneq(osrstr, "ID=", 3))
if (!distro && strstartswith(osrstr, "ID="))
distro = strstrip(&osrstr[3], "\"'");
else if (!version && strneq(osrstr, "VERSION_ID=", 11))
else if (!version && strstartswith(osrstr, "VERSION_ID="))
version = strstrip(&osrstr[11], "\"'");
if (distro && version) {
@ -1746,11 +1746,11 @@ print_udev_properties(struct record_device *dev)
key = udev_list_entry_get_name(entry);
if (strneq(key, "ID_INPUT", 8) ||
strneq(key, "LIBINPUT", 8) ||
strneq(key, "EVDEV_ABS", 9) ||
strneq(key, "MOUSE_DPI", 9) ||
strneq(key, "POINTINGSTICK_", 14)) {
if (strstartswith(key, "ID_INPUT") ||
strstartswith(key, "LIBINPUT") ||
strstartswith(key, "EVDEV_ABS") ||
strstartswith(key, "MOUSE_DPI") ||
strstartswith(key, "POINTINGSTICK_")) {
value = udev_list_entry_get_value(entry);
iprintf(dev->fp, I_UDEV_DATA, "- %s=%s\n", key, value);
}
@ -1899,7 +1899,7 @@ print_device_description(struct record_device *dev)
}
static int is_event_node(const struct dirent *dir) {
return strneq(dir->d_name, "event", 5);
return strstartswith(dir->d_name, "event");
}
static char *
@ -2599,7 +2599,7 @@ is_char_dev(const char *path)
{
struct stat st;
if (strneq(path, "/dev", 4))
if (strstartswith(path, "/dev"))
return F_DEVICE;
if (stat(path, &st) != 0) {

View file

@ -665,7 +665,7 @@ find_device(const char *udev_tag)
continue;
sysname = udev_device_get_sysname(device);
if (!strneq("event", sysname, 5)) {
if (!strstartswith("event", sysname)) {
udev_device_unref(device);
continue;
}