Deluge Firmware 1.3.0
Build date: 2026.07.07
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(std::string_view 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(const String& other);
45 String& operator=(const String& other);
46
47 // String(String* otherString); // BEWARE - using this on stack instances sometimes just caused crashes and stuff.
48 // Made no sense. Instead, constructing then calling set() works
49 ~String();
50 void clear(bool destructing = false);
51 Error set(char const* newChars, int32_t newLength = -1);
52 Error set(std::string_view newStr) { return set(newStr.data(), newStr.size()); }
53 void set(String const* otherString);
54 void beenCloned() const;
55 size_t getLength();
56 Error shorten(int32_t newLength);
57 Error concatenateAtPos(char const* newChars, int32_t pos, int32_t newCharsLength = -1);
58 Error concatenateInt(int32_t number, int32_t minNumDigits = 1);
59 Error setInt(int32_t number, int32_t minNumDigits = 1);
60 Error setChar(char newChar, int32_t pos);
61 Error concatenate(String* otherString);
62 Error concatenate(std::string_view newChars);
63 Error get_new_memory(int32_t newCharsLength);
64 bool equals(char const* otherChars);
65 bool equalsCaseIrrespective(char const* otherChars);
66
67 inline bool contains(const char* otherChars) { return strstr(stringMemory, otherChars) != NULL; }
68 inline bool equals(String* otherString) {
69 if (stringMemory == otherString->stringMemory) {
70 return true; // Works if both lengths are 0, too
71 }
72 if (!stringMemory || !otherString->stringMemory) {
73 return false; // If just one is empty, then not equal
74 }
75 return equals(otherString->get());
76 }
77
78 inline bool equalsCaseIrrespective(String* otherString) {
79 if (stringMemory == otherString->stringMemory) {
80 return true; // Works if both lengths are 0, too
81 }
82 if (!stringMemory || !otherString->stringMemory) {
83 return false; // If just one is empty, then not equal
84 }
85 return equalsCaseIrrespective(otherString->get());
86 }
87
88 // consider removing the explicit inline since the method is already inlined
89 [[nodiscard]] inline char const* get() const {
90 if (!stringMemory) {
91 return &nothing;
92 }
93 return stringMemory;
94 }
95
96 inline bool isEmpty() { return !stringMemory; }
97 int32_t getNumReasons() const;
98
99private:
100 void setNumReasons(int32_t newNum) const;
101
102 char* stringMemory = nullptr;
103};
104
105#include "d_stringbuf.h"