Loading...
Searching...
No Matches
cstring_fit.h
Go to the documentation of this file.
1
7#pragma once
8#include <stdbool.h>
9#include <stddef.h>
10#include <stdint.h>
11#include <string.h>
12
17
29static inline void cstring_fit(char *dst, const char *src, size_t size)
30{
31 if (size == 0)
32 {
33 return;
34 }
35
36 if (!src)
37 {
38 dst[0] = '\0';
39 return;
40 }
41
42 strncpy(dst, src, size - 1);
43 dst[size - 1] = '\0';
44}
45
58static inline bool cstring_fit_same(const char *current, const char *value, size_t size)
59{
60 if (size == 0)
61 {
62 return true; // nowhere to put it, so nothing about the buffer can move
63 }
64
65 size_t room = size - 1;
66 size_t incoming = strlen(value);
67 if (incoming > room)
68 {
69 incoming = room;
70 }
71
72 return strlen(current) == incoming && strncmp(current, value, incoming) == 0;
73}
74
87void copy_bounded(char *dst, uint8_t cap, const uint8_t *src, uint8_t len);
88
101bool cstring_is_clean(const char *s, uint16_t size);
102
115bool cstring_setting_is_clean(const char *s, uint16_t size, const char *default_str);
116
static bool cstring_fit_same(const char *current, const char *value, size_t size)
Whether copying value into a buffer would leave it reading exactly as it does now.
Definition cstring_fit.h:58
void copy_bounded(char *dst, uint8_t cap, const uint8_t *src, uint8_t len)
Copy bytes into a fixed buffer, taking what fits and terminating it.
Definition cstring_fit.c:9
static void cstring_fit(char *dst, const char *src, size_t size)
Copy a string into a fixed buffer, keeping only what fits and always terminating it.
Definition cstring_fit.h:29
bool cstring_setting_is_clean(const char *s, uint16_t size, const char *default_str)
Whether a saved text setting reads as one, where an empty setting can be the real value.
Definition cstring_fit.c:59
bool cstring_is_clean(const char *s, uint16_t size)
Whether a byte range holds a string worth keeping.
Definition cstring_fit.c:30