Deluge Firmware 1.3.0
Build date: 2025.09.12
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(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 void set(String const* otherString);
53 void beenCloned() const;
54 size_t getLength();
55 Error shorten(int32_t newLength);
56 Error concatenateAtPos(char const* newChars, int32_t pos, int32_t newCharsLength = -1);
57 Error concatenateInt(int32_t number, int32_t minNumDigits = 1);
58 Error setInt(int32_t number, int32_t minNumDigits = 1);
59 Error setChar(char newChar, int32_t pos);
60 Error concatenate(String* otherString);
61 Error concatenate(char const* newChars);
62 Error get_new_memory(int32_t newCharsLength);
63 bool equals(char const* otherChars);
64 bool equalsCaseIrrespective(char const* otherChars);
65
66 inline bool contains(const char* otherChars) { return strstr(stringMemory, otherChars) != NULL; }
67 inline bool equals(String* otherString) {
68 if (stringMemory == otherString->stringMemory) {
69 return true; // Works if both lengths are 0, too
70 }
71 if (!stringMemory || !otherString->stringMemory) {
72 return false; // If just one is empty, then not equal
73 }
74 return equals(otherString->get());
75 }
76
77 inline bool equalsCaseIrrespective(String* otherString) {
78 if (stringMemory == otherString->stringMemory) {
79 return true; // Works if both lengths are 0, too
80 }
81 if (!stringMemory || !otherString->stringMemory) {
82 return false; // If just one is empty, then not equal
83 }
84 return equalsCaseIrrespective(otherString->get());
85 }
86
87 [[nodiscard]] inline char const* get() const {
88 if (!stringMemory) {
89 return &nothing;
90 }
91 return stringMemory;
92 }
93
94 inline bool isEmpty() { return !stringMemory; }
95 int32_t getNumReasons() const;
96
97private:
98 void setNumReasons(int32_t newNum) const;
99
100 char* stringMemory = nullptr;
101};
102
103#include "d_stringbuf.h"