CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
StringUtils.h
Go to the documentation of this file.
1#pragma once
2
3#include <cctype>
4#include <cstddef>
5
6namespace cdc::core {
7
13inline const char* skipSpaces(const char* s) {
14 while (s && *s && std::isspace(static_cast<unsigned char>(*s))) {
15 s++;
16 }
17 return s;
18}
19
31inline const char* nextToken(const char* s, char* out, size_t outSize) {
32 if (!out || outSize == 0) return nullptr;
33 s = skipSpaces(s);
34 if (!s || !*s) return nullptr;
35 size_t i = 0;
36 while (*s && i + 1 < outSize) {
37 if (*s == '\\' && *(s + 1) == ' ') {
38 out[i++] = ' ';
39 s += 2;
40 continue;
41 }
42 if (std::isspace(static_cast<unsigned char>(*s))) break;
43 out[i++] = *s++;
44 }
45 out[i] = '\0';
46 while (*s) {
47 if (*s == '\\' && *(s + 1) == ' ') {
48 s += 2;
49 continue;
50 }
51 if (std::isspace(static_cast<unsigned char>(*s))) break;
52 s++;
53 }
54 return s;
55}
56
61inline void unescapeSpaces(char* s) {
62 if (!s) return;
63 char* w = s;
64 for (const char* r = s; *r; ) {
65 if (*r == '\\' && *(r + 1) == ' ') {
66 *w++ = ' ';
67 r += 2;
68 } else {
69 *w++ = *r++;
70 }
71 }
72 *w = '\0';
73}
74
75} // namespace cdc::core
const char * skipSpaces(const char *s)
Advances over leading ASCII whitespace in a C string.
Definition StringUtils.h:13
void unescapeSpaces(char *s)
Replaces every \ escape sequence with a single space character in-place.
Definition StringUtils.h:61
const char * nextToken(const char *s, char *out, size_t outSize)
Extracts one whitespace-delimited token from a string.
Definition StringUtils.h:31