|
3 | 3 | #ifndef STRINGS_H |
4 | 4 | #define STRINGS_H |
5 | 5 |
|
| 6 | +#include <ctype.h> |
| 7 | +#include <stdlib.h> |
6 | 8 | #include <stdbool.h> |
| 9 | +#include <string.h> |
| 10 | + |
| 11 | +/** |
| 12 | + * Safe string duplication |
| 13 | + * |
| 14 | + * @param str String to duplicate |
| 15 | + * @return Pointer to duplicated string or NULL on failure |
| 16 | + */ |
| 17 | +char *safe_strdup(const char *str); |
| 18 | + |
| 19 | +/** |
| 20 | + * Safe string copy with size checking. It is safe to pass an unterminated |
| 21 | + * string in `src`: the string will not be checked beyond `src_size` bytes. |
| 22 | + * |
| 23 | + * @param dest Destination buffer |
| 24 | + * @param src Source string |
| 25 | + * @param dst_size Size of destination buffer |
| 26 | + * @param src_size Size of source buffer if not null-terminated |
| 27 | + * @return 0 on success (including if the string is truncated), -1 on failure |
| 28 | + */ |
| 29 | +int safe_strcpy(char *dest, const char *src, size_t dst_size, size_t src_size); |
| 30 | + |
| 31 | +/** |
| 32 | + * Safe string concatenation with size checking |
| 33 | + * |
| 34 | + * @param dest Destination buffer |
| 35 | + * @param src Source string |
| 36 | + * @param size Size of destination buffer |
| 37 | + * @return 0 on success, -1 on failure |
| 38 | + */ |
| 39 | +int safe_strcat(char *dest, const char *src, size_t size); |
7 | 40 |
|
8 | 41 | /** |
9 | 42 | * Check if a string ends with a given suffix |
|
14 | 47 | */ |
15 | 48 | bool ends_with(const char *str, const char *suffix); |
16 | 49 |
|
| 50 | +/** |
| 51 | + * Returns a pointer to the first printable non-space character in the input string |
| 52 | + * and terminates the string after the last printable non-space character. |
| 53 | + * |
| 54 | + * @param value The input string |
| 55 | + * @return A pointer into the original string |
| 56 | + */ |
| 57 | +char *trim_ascii_whitespace(char *value); |
| 58 | + |
| 59 | +/** |
| 60 | + * Copies up to `output_size` bytes of the input string excluding any leading |
| 61 | + * and trailing whitespace or non-printing characters into the output buffer. |
| 62 | + * Guaranteed to null-terminate the output buffer. |
| 63 | + * |
| 64 | + * @param output The output buffer |
| 65 | + * @param output_size The size of the output buffer |
| 66 | + * @param input The input string |
| 67 | + * @param input_size The maximum size of the input string to check, or zero to |
| 68 | + * not limit the input string size |
| 69 | + * @return The number of bytes copied, not counting the terminator |
| 70 | + */ |
| 71 | +size_t copy_trimmed_value(char *output, size_t output_size, const char *input, size_t input_size); |
| 72 | + |
| 73 | +/** |
| 74 | + * Returns a pointer to the first printable character in the input string. |
| 75 | + */ |
| 76 | +static inline const char *ltrim_pos(const char *input) { |
| 77 | + if (!input) { |
| 78 | + return NULL; |
| 79 | + } |
| 80 | + |
| 81 | + unsigned char *start = (unsigned char *)input; |
| 82 | + while (*start && !isgraph(*start)) { |
| 83 | + start++; |
| 84 | + } |
| 85 | + return (const char *)start; |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * Returns a pointer to the byte _after_ the last printable character |
| 90 | + * in the input string. Set the returned pointer to '\0' to terminate |
| 91 | + * the string after the last printable character. |
| 92 | + */ |
| 93 | +static inline const char *rtrim_pos(const char *input, size_t input_size) { |
| 94 | + if (!input) { |
| 95 | + return NULL; |
| 96 | + } |
| 97 | + |
| 98 | + const char *end; |
| 99 | + if (input_size > 0) { |
| 100 | + end = (input + strnlen(input, input_size) - 1); |
| 101 | + } else { |
| 102 | + // If input_size is zero, use the unbounded strlen |
| 103 | + end = (input + strlen(input) - 1); |
| 104 | + } |
| 105 | + // `end` will now point to the last non-null character. If the input is |
| 106 | + // empty, `end` will be (input-1), the character *before* the terminating |
| 107 | + // null. |
| 108 | + while (end > input && !isgraph((unsigned char)*end)) { |
| 109 | + end--; |
| 110 | + } |
| 111 | + // Point to the character after the last printable character. If input was |
| 112 | + // empty, this will now point to the terminating null. |
| 113 | + return end + 1; |
| 114 | +} |
| 115 | + |
17 | 116 | #endif //STRINGS_H |
0 commit comments