Deluge Firmware
1.3.0
Build date: 2026.08.07
Toggle main menu visibility
Loading...
Searching...
No Matches
d_stringbuf.h
1
#pragma once
2
#include "definitions_cxx.hpp"
3
#include <cstring>
4
5
extern
"C"
{
6
#include "util/cfunctions.h"
7
}
8
9
char
halfByteToHexChar(uint8_t thisHalfByte);
10
void
intToHex(uint32_t number,
char
* output, int32_t numChars = 8);
11
uint32_t hexToInt(
char
const
*
string
);
12
uint32_t hexToIntFixedLength(
char
const
* __restrict__ hexChars, int32_t length);
13
16
class
StringBuf {
17
// Not templated to optimize binary size.
18
public
:
19
StringBuf(
char
* buf,
size_t
capacity) : capacity_(capacity), buf_(buf) { memset(buf_,
'\0'
, capacity_); }
20
void
append(std::string_view str);
21
void
append(
char
c) { ::strncat(buf_, &c, 1); }
22
void
removeSpaces() {
23
size_t
removed = 0;
24
// upto size, not below it -- we want the null as well
25
for
(
size_t
i = 0; i <= size(); i++) {
26
if
(isspace(buf_[i])) {
27
removed++;
28
}
29
else
{
30
buf_[i - removed] = buf_[i];
31
}
32
}
33
}
34
void
clear() { buf_[0] = 0; }
35
void
truncate(
size_t
newSize) {
36
if
(newSize < capacity_) {
37
buf_[newSize] =
'\0'
;
38
}
39
}
40
41
// TODO: Validate buffer size. These can overflow
42
void
appendInt(
int
i,
int
minChars = 1) { intToString(i, buf_ + size(), minChars); }
43
void
appendHex(
int
i,
int
minChars = 1) { intToHex(i, buf_ + size(), minChars); }
44
void
appendFloat(
float
f, int32_t minDecimals, int32_t maxDecimals) {
45
floatToString(f, buf_ + size(), minDecimals, maxDecimals);
46
}
47
48
[[nodiscard]]
char
* data() {
return
buf_; }
49
[[nodiscard]]
const
char
* data()
const
{
return
buf_; }
50
[[nodiscard]]
const
char
* c_str()
const
{
return
buf_; }
51
52
[[nodiscard]]
size_t
capacity()
const
{
return
capacity_; }
53
[[nodiscard]]
size_t
size()
const
{
return
std::strlen(buf_); }
54
[[nodiscard]]
size_t
length()
const
{
return
std::strlen(buf_); }
55
56
[[nodiscard]]
bool
empty()
const
{
return
buf_[0] ==
'\0'
; }
57
58
bool
operator==(
const
char
* rhs)
const
{
return
std::strcmp(buf_, rhs) == 0; }
59
bool
operator==(
const
StringBuf& rhs)
const
{
return
std::strcmp(buf_, rhs.c_str()) == 0; }
60
61
operator
std::string_view()
const
{
return
std::string_view{buf_}; }
62
63
private
:
64
size_t
capacity_;
65
char
* buf_;
66
};
67
69
#define DEF_STACK_STRING_BUF(name, capacity) \
70
char name##__buf[capacity] = {0}; \
71
StringBuf name = {name##__buf, capacity}