FastLED 3.9.12
Loading...
Searching...
No Matches
str.cpp
1#include <stdlib.h>
2
3#include "fl/str.h"
4#include "fl/namespace.h"
5
6
7namespace fl {
8
9namespace string_functions {
10
11static int itoa(int value, char *sp, int radix) {
12 char tmp[16]; // be careful with the length of the buffer
13 char *tp = tmp;
14 int i;
15 unsigned v;
16
17 int sign = (radix == 10 && value < 0);
18 if (sign)
19 v = -value;
20 else
21 v = (unsigned)value;
22
23 while (v || tp == tmp) {
24 i = v % radix;
25 v = radix ? v / radix : 0;
26 if (i < 10)
27 *tp++ = i + '0';
28 else
29 *tp++ = i + 'a' - 10;
30 }
31
32 int len = tp - tmp;
33
34 if (sign) {
35 *sp++ = '-';
36 len++;
37 }
38
39 while (tp > tmp)
40 *sp++ = *--tp;
41
42 return len;
43}
44
45static float atoff(const char *str, size_t len) {
46 float result = 0.0f; // The resulting number
47 float sign = 1.0f; // Positive or negative
48 float fraction = 0.0f; // Fractional part
49 float divisor = 1.0f; // Divisor for the fractional part
50 int isFractional = 0; // Whether the current part is fractional
51
52 size_t pos = 0; // Current position in the string
53
54 // Handle empty input
55 if (len == 0) {
56 return 0.0f;
57 }
58
59 // Skip leading whitespace (manual check instead of isspace)
60 while (pos < len && (str[pos] == ' ' || str[pos] == '\t' || str[pos] == '\n' || str[pos] == '\r' || str[pos] == '\f' || str[pos] == '\v')) {
61 pos++;
62 }
63
64 // Handle optional sign
65 if (pos < len && str[pos] == '-') {
66 sign = -1.0f;
67 pos++;
68 } else if (pos < len && str[pos] == '+') {
69 pos++;
70 }
71
72 // Main parsing loop
73 while (pos < len) {
74 if (str[pos] >= '0' && str[pos] <= '9') {
75 if (isFractional) {
76 divisor *= 10.0f;
77 fraction += (str[pos] - '0') / divisor;
78 } else {
79 result = result * 10.0f + (str[pos] - '0');
80 }
81 } else if (str[pos] == '.' && !isFractional) {
82 isFractional = 1;
83 } else {
84 // Stop parsing at invalid characters
85 break;
86 }
87 pos++;
88 }
89
90 // Combine integer and fractional parts
91 result = result + fraction;
92
93 // Apply the sign
94 return sign * result;
95}
96
97} // namespace string_functions
98
99void StringFormatter::append(int32_t val, StrN<64> *dst) {
100 char buf[63] = {0};
101 string_functions::itoa(val, buf, 10);
102 dst->write(buf, strlen(buf));
103}
104StringHolder::StringHolder(const char *str) {
105 mLength = strlen(str); // Don't include null terminator in length
106 mCapacity = mLength + 1; // Capacity includes null terminator
107 mData = new char[mCapacity];
108 memcpy(mData, str, mLength);
109 mData[mLength] = '\0';
110}
111StringHolder::StringHolder(size_t length) {
112 mData = (char *)malloc(length + 1);
113 if (mData) {
114 mLength = length;
115 mData[mLength] = '\0';
116 } else {
117 mLength = 0;
118 }
119 mCapacity = mLength;
120}
121
122
123StringHolder::StringHolder(const char *str, size_t length) {
124 mData = (char *)malloc(length + 1);
125 if (mData) {
126 mLength = length;
127 memcpy(mData, str, mLength);
128 mData[mLength] = '\0';
129 } else {
130 mLength = 0;
131 }
132 mCapacity = mLength;
133}
134
135StringHolder::~StringHolder() {
136 free(mData); // Release the memory
137}
138
139void StringHolder::grow(size_t newLength) {
140 if (newLength <= mCapacity) {
141 // New length must be greater than current length
142 mLength = newLength;
143 return;
144 }
145 char *newData = (char *)realloc(mData, newLength + 1);
146 if (newData) {
147 mData = newData;
148 mLength = newLength;
149 mCapacity = newLength;
150 mData[mLength] = '\0'; // Ensure null-termination
151 } else {
152 // handle re-allocation failure.
153 char *newData = (char *)malloc(newLength + 1);
154 if (newData) {
155 memcpy(newData, mData, mLength + 1);
156 free(mData);
157 mData = newData;
158 mLength = newLength;
159 mCapacity = mLength;
160 } else {
161 // memory failure.
162 }
163 }
164}
165
166float StringFormatter::parseFloat(const char *str, size_t len) {
167 return string_functions::atoff(str, len);
168}
169
170
171}
Implements the FastLED namespace macros.
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16