Parse functions - moved from StringFormatter
| 197 | |
| 198 | // Parse functions - moved from StringFormatter |
| 199 | float parseFloat(const char *str, fl::size len) { |
| 200 | float result = 0.0f; // The resulting number |
| 201 | float sign = 1.0f; // Positive or negative |
| 202 | float fraction = 0.0f; // Fractional part |
| 203 | float divisor = 1.0f; // Divisor for the fractional part |
| 204 | int isFractional = 0; // Whether the current part is fractional |
| 205 | |
| 206 | fl::size pos = 0; // Current position in the string |
| 207 | |
| 208 | // Handle empty input |
| 209 | if (len == 0) { |
| 210 | return 0.0f; |
| 211 | } |
| 212 | |
| 213 | // Skip leading whitespace (manual check instead of isspace) |
| 214 | while (pos < len && |
| 215 | (str[pos] == ' ' || str[pos] == '\t' || str[pos] == '\n' || |
| 216 | str[pos] == '\r' || str[pos] == '\f' || str[pos] == '\v')) { |
| 217 | pos++; |
| 218 | } |
| 219 | |
| 220 | // Handle optional sign |
| 221 | if (pos < len && str[pos] == '-') { |
| 222 | sign = -1.0f; |
| 223 | pos++; |
| 224 | } else if (pos < len && str[pos] == '+') { |
| 225 | pos++; |
| 226 | } |
| 227 | |
| 228 | // Main parsing loop |
| 229 | while (pos < len) { |
| 230 | if (str[pos] >= '0' && str[pos] <= '9') { |
| 231 | if (isFractional) { |
| 232 | divisor *= 10.0f; |
| 233 | fraction += (str[pos] - '0') / divisor; |
| 234 | } else { |
| 235 | result = result * 10.0f + (str[pos] - '0'); |
| 236 | } |
| 237 | } else if (str[pos] == '.' && !isFractional) { |
| 238 | isFractional = 1; |
| 239 | } else { |
| 240 | // Stop parsing at invalid characters |
| 241 | break; |
| 242 | } |
| 243 | pos++; |
| 244 | } |
| 245 | |
| 246 | // Combine integer and fractional parts |
| 247 | result = result + fraction; |
| 248 | |
| 249 | // Apply the sign |
| 250 | return sign * result; |
| 251 | } |
| 252 | |
| 253 | int parseInt(const char *str, fl::size len) { |
| 254 | int result = 0; |
no outgoing calls
no test coverage detected