Skip to content

Commit

Permalink
string_utils: add starts_with
Browse files Browse the repository at this point in the history
Signed-off-by: Yuxuan Shui <[email protected]>
  • Loading branch information
yshui committed May 5, 2024
1 parent 7899baf commit 7a69534
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/string_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// Copyright (c) Yuxuan Shui <[email protected]>
#pragma once
#include <ctype.h>
#include <stdbool.h>
#include <stddef.h>
#include <string.h>

#include "compiler.h"

Expand Down Expand Up @@ -59,6 +61,13 @@ static inline char *skip_space_mut(char *src) {
#define skip_space(x) \
_Generic((x), char *: skip_space_mut, const char *: skip_space_const)(x)

static inline bool starts_with(const char *str, const char *needle, bool ignore_case) {
if (ignore_case) {
return strncasecmp(str, needle, strlen(needle));
}
return strncmp(str, needle, strlen(needle));
}

/// Similar to `asprintf`, but it reuses the allocated memory pointed to by `*strp`, and
/// reallocates it if it's not big enough.
int asnprintf(char **strp, size_t *capacity, const char *fmt, ...);

0 comments on commit 7a69534

Please sign in to comment.