37void intToHex(uint32_t number,
char* output, int32_t numChars = 8);
39uint32_t hexToIntFixedLength(
char const* __restrict__ hexChars, int32_t length);
52 void clear(
bool destructing =
false);
53 Error set(
char const* newChars, int32_t newLength = -1);
54 void set(String
const* otherString);
57 Error shorten(int32_t newLength);
58 Error concatenateAtPos(
char const* newChars, int32_t pos, int32_t newCharsLength = -1);
59 Error concatenateInt(int32_t number, int32_t minNumDigits = 1);
60 Error setInt(int32_t number, int32_t minNumDigits = 1);
61 Error setChar(
char newChar, int32_t pos);
62 Error concatenate(String* otherString);
63 Error concatenate(
char const* newChars);
64 bool equals(
char const* otherChars);
65 bool equalsCaseIrrespective(
char const* otherChars);
67 inline bool contains(
const char* otherChars) {
return strstr(stringMemory, otherChars) != NULL; }
68 inline bool equals(String* otherString) {
69 if (stringMemory == otherString->stringMemory) {
72 if (!stringMemory || !otherString->stringMemory) {
75 return equals(otherString->get());
78 inline bool equalsCaseIrrespective(String* otherString) {
79 if (stringMemory == otherString->stringMemory) {
82 if (!stringMemory || !otherString->stringMemory) {
85 return equalsCaseIrrespective(otherString->get());
88 inline char const* get() {
95 inline bool isEmpty() {
return !stringMemory; }
98 int32_t getNumReasons();
99 void setNumReasons(int32_t newNum);
101 char* stringMemory =
nullptr;
109 StringBuf(
char* buf,
size_t capacity) : capacity_(capacity), buf_(buf) { memset(buf_,
'\0', capacity_); }
111#pragma GCC diagnostic push
112#pragma GCC diagnostic ignored "-Wstringop-overflow="
115 void append(std::string_view str) { ::strncat(buf_, str.data(), std::min(str.length(), capacity_ - size() - 1)); }
116#pragma GCC diagnostic pop
118 void append(
char c) { ::strncat(buf_, &c, 1); }
119 void removeSpaces() {
122 for (
size_t i = 0; i <= size(); i++) {
123 if (isspace(buf_[i])) {
127 buf_[i - removed] = buf_[i];
131 void clear() { buf_[0] = 0; }
132 void truncate(
size_t newSize) {
133 if (newSize < capacity_) {
134 buf_[newSize] =
'\0';
139 void appendInt(
int i,
int minChars = 1) { intToString(i, buf_ + size(), minChars); }
140 void appendHex(
int i,
int minChars = 1) { intToHex(i, buf_ + size(), minChars); }
141 void appendFloat(
float f, int32_t minDecimals, int32_t maxDecimals) {
142 floatToString(f, buf_ + size(), minDecimals, maxDecimals);
145 [[nodiscard]]
char* data() {
return buf_; }
146 [[nodiscard]]
const char* data()
const {
return buf_; }
147 [[nodiscard]]
const char* c_str()
const {
return buf_; }
149 [[nodiscard]]
size_t capacity()
const {
return capacity_; }
150 [[nodiscard]]
size_t size()
const {
return std::strlen(buf_); }
151 [[nodiscard]]
size_t length()
const {
return std::strlen(buf_); }
153 [[nodiscard]]
bool empty()
const {
return buf_[0] ==
'\0'; }
155 bool operator==(
const char* rhs)
const {
return std::strcmp(buf_, rhs) == 0; }
156 bool operator==(
const StringBuf& rhs)
const {
return std::strcmp(buf_, rhs.c_str()) == 0; }
158 operator std::string_view()
const {
return std::string_view{buf_}; }