string_utils: add starts_with

Signed-off-by: Yuxuan Shui <yshuiv7@gmail.com>
This commit is contained in:
Yuxuan Shui 2024-04-29 20:07:17 +01:00
parent 7899bafb88
commit 7a69534633
No known key found for this signature in database
GPG Key ID: D3A4405BE6CC17F4
1 changed files with 9 additions and 0 deletions

View File

@ -2,7 +2,9 @@
// Copyright (c) Yuxuan Shui <yshuiv7@gmail.com>
#pragma once
#include <ctype.h>
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
#include "compiler.h"
@ -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, ...);