str.h (1906B)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | #ifndef STR_H_ #define STR_H_ /* * str.h * * String utility functions. */ /* length of given string of size n */ static inline size_t str_len(const char *s, size_t n) { return strnlen(s, n); } /* length of given null-terminated string */ static inline size_t strz_len(const char *s) { return strlen(s); } /* length of given constant string literal */ #define strz_len_lit(x) (sizeof((x)) - 1) /* string catenation */ static inline void strz_cat(char *restrict dst, const char *restrict src) { strcat(dst, src); } /* string equality test, testing at most n characters */ static inline bool str_equal(const char *restrict s1, const char *restrict s2, size_t n) { return strncmp(s1, s2, n) == 0; } #define str_equal_fixed(s1, s2) str_equal((s1), (s2), sizeof((s1))) /* null-terminated string equality test (avoid using this) */ static inline bool strz_equal(const char *restrict s1, const char *restrict s2) { return strcmp(s1, s2) == 0; } /* string copy (implemented as strlcpy) */ size_t str_copy (char *restrict dst, const char *restrict src, size_t n); char *str_pcopy(char *restrict dst, const char *restrict src, size_t n); #define str_copy_fixed(dst, src) str_copy((dst), (src), sizeof((dst))) /* replacements for strspn and strcspn (because they are shit and don't allow * us to specify string length). */ size_t str_spn (const char *restrict s, const char *restrict accept, size_t n); size_t str_cspn (const char *restrict s, const char *restrict reject, size_t n); /* locate char in string. * (May rename to something else?) */ int strz_ichr(const char *s, char c); char *strz_chr (char *s, char c); int str_ichr (const char *s, char c, size_t n); char *str_chr (char *s, char c, size_t n); /* Parse integer from string of at most n bytes (safer atoi/strtol) */ long str_tol (const char *buf, size_t n); #endif |