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 <string.h>
11
16
28static inline void cstring_fit(char *dst, const char *src, size_t size)
29{
30 if (size == 0)
31 {
32 return;
33 }
34
35 if (!src)
36 {
37 dst[0] = '\0';
38 return;
39 }
40
41 strncpy(dst, src, size - 1);
42 dst[size - 1] = '\0';
43}
44
57static inline bool cstring_fit_same(const char *current, const char *value, size_t size)
58{
59 if (size == 0)
60 {
61 return true; // nowhere to put it, so nothing about the buffer can move
62 }
63
64 size_t room = size - 1;
65 size_t incoming = strlen(value);
66 if (incoming > room)
67 {
68 incoming = room;
69 }
70
71 return strlen(current) == incoming && strncmp(current, value, incoming) == 0;
72}
73
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:57
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:28