Deluge Firmware 1.3.0
Build date: 2025.06.05
Loading...
Searching...
No Matches
d_string.h
1/*
2 * Copyright © 2019-2023 Synthstrom Audible Limited
3 *
4 * This file is part of The Synthstrom Audible Deluge Firmware.
5 *
6 * The Synthstrom Audible Deluge Firmware is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software Foundation,
8 * either version 3 of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along with this program.
15 * If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20#include "definitions_cxx.hpp"
21#include <cctype>
22#include <cstdint>
23#include <cstring>
24
25extern "C" {
26#include "util/cfunctions.h"
27}
28
29[[gnu::always_inline]] static inline void intToString(int32_t number, char* buffer) {
30 intToString(number, buffer, 1);
31}
32
33bool memIsNumericChars(char const* mem, int32_t size);
34bool stringIsNumericChars(char const* str);
35
36void byteToHex(uint8_t number, char* buffer);
37uint8_t hexToByte(char const* firstChar);
38
39extern const char nothing;
40
41class String {
42public:
43 String() = default;
44 // String(String* otherString); // BEWARE - using this on stack instances sometimes just caused crashes and stuff.
45 // Made no sense. Instead, constructing then calling set() works
46 ~String();
47 void clear(bool destructing = false);
48 Error set(char const* newChars, int32_t newLength = -1);
49 void set(String const* otherString);
50 void beenCloned();
51 size_t getLength();
52 Error shorten(int32_t newLength);
53 Error concatenateAtPos(char const* newChars, int32_t pos, int32_t newCharsLength = -1);
54 Error concatenateInt(int32_t number, int32_t minNumDigits = 1);
55 Error setInt(int32_t number, int32_t minNumDigits = 1);
56 Error setChar(char newChar, int32_t pos);
57 Error concatenate(String* otherString);
58 Error concatenate(char const* newChars);
59 bool equals(char const* otherChars);
60 bool equalsCaseIrrespective(char const* otherChars);
61
62 inline bool contains(const char* otherChars) { return strstr(stringMemory, otherChars) != NULL; }
63 inline bool equals(String* otherString) {
64 if (stringMemory == otherString->stringMemory) {
65 return true; // Works if both lengths are 0, too
66 }
67 if (!stringMemory || !otherString->stringMemory) {
68 return false; // If just one is empty, then not equal
69 }
70 return equals(otherString->get());
71 }
72
73 inline bool equalsCaseIrrespective(String* otherString) {
74 if (stringMemory == otherString->stringMemory) {
75 return true; // Works if both lengths are 0, too
76 }
77 if (!stringMemory || !otherString->stringMemory) {
78 return false; // If just one is empty, then not equal
79 }
80 return equalsCaseIrrespective(otherString->get());
81 }
82
83 [[nodiscard]] inline char const* get() const {
84 if (!stringMemory) {
85 return &nothing;
86 }
87 return stringMemory;
88 }
89
90 inline bool isEmpty() { return !stringMemory; }
91
92private:
93 int32_t getNumReasons();
94 void setNumReasons(int32_t newNum);
95
96 char* stringMemory = nullptr;
97};
98
99#include "d_stringbuf.h"