WarpTwin
Documentation for WarpTwin models and classes.
Loading...
Searching...
No Matches
stringutils.hpp
Go to the documentation of this file.
1/******************************************************************************
2* Copyright (c) ATTX INC 2025. All Rights Reserved.
3*
4* This software and associated documentation (the "Software") are the
5* proprietary and confidential information of ATTX, INC. The Software is
6* furnished under a license agreement between ATTX and the user organization
7* and may be used or copied only in accordance with the terms of the agreement.
8* Refer to 'license/attx_license.adoc' for standard license terms.
9*
10* EXPORT CONTROL NOTICE: THIS SOFTWARE MAY INCLUDE CONTENT CONTROLLED UNDER THE
11* INTERNATIONAL TRAFFIC IN ARMS REGULATIONS (ITAR) OR THE EXPORT ADMINISTRATION
12* REGULATIONS (EAR99). No part of the Software may be used, reproduced, or
13* transmitted in any form or by any means, for any purpose, without the express
14* written permission of ATTX, INC.
15******************************************************************************/
16#ifndef UTILS_STRINGUTILS_H
17#define UTILS_STRINGUTILS_H
18
19#include <stddef.h>
20#include <stdint.h>
21#include <cstring>
22#include <cstdio>
23#include <cstdlib>
24#include <array>
25
26#include "types.h"
28
29namespace clockwerk {
30 // Constant definition for single value conversion to string
32
33 // Constant definition for array/vector conversion to string
34 const uint16 MAX_CHARS_PER_ARRAY_STRING = 200;
35
41 void toUpper(char* input);
42
48 void toLower(char* input);
49
57 bool caseInsensitiveEqual(const char* a, const char* b);
58
69 int16 strip(const char* input, char* output, size_t output_size);
70
83 int16 stringReplace(const char* source, const char* orig, const char* repl,
84 char* output, size_t output_size);
85
100 int16 splitString(const char* text, const char* delimiter,
101 char** tokens, size_t max_tokens,
102 char* token_buf, size_t token_buf_size);
103
110 bool isValidNumber(const char* s);
111
122 template <typename T>
123 int16 setArrayFromString(const char* s, T* array, size_t array_size);
124
133 template<size_t N>
134 int16 numberToString(const std::array<char, N>& val, char* retval, size_t size);
135
144 template<typename Tn, size_t N>
145 int16 numberToString(const std::array<Tn, N>& val, char* retval, size_t size);
146
155 int16 numberToString(bool num, char* out, size_t out_size);
156 int16 numberToString(int8 num, char* out, size_t out_size);
157 int16 numberToString(int16 num, char* out, size_t out_size);
158 int16 numberToString(int32 num, char* out, size_t out_size);
159 int16 numberToString(int64 num, char* out, size_t out_size);
160 int16 numberToString(uint8 num, char* out, size_t out_size);
161 int16 numberToString(uint16 num, char* out, size_t out_size);
162 int16 numberToString(uint32 num, char* out, size_t out_size);
163 int16 numberToString(uint64 num, char* out, size_t out_size);
164
173 int16 numberToString(char num, char* out, size_t out_size);
174
184 int16 numberToString(float num, char* out, size_t out_size, uint8 precision = 6);
185
195 int16 numberToString(float16 num, char* out, size_t out_size, uint8 precision = 3);
196
206 int16 numberToString(double num, char* out, size_t out_size, uint8 precision = 6);
207
212 int16 numberFromString(const char* s, uint8& num);
213 int16 numberFromString(const char* s, uint16& num);
214 int16 numberFromString(const char* s, uint32& num);
215 int16 numberFromString(const char* s, uint64& num);
216 int16 numberFromString(const char* s, int8& num);
217 int16 numberFromString(const char* s, int16& num);
218 int16 numberFromString(const char* s, int32& num);
219 int16 numberFromString(const char* s, int64& num);
220 int16 numberFromString(const char* s, float& num);
221 int16 numberFromString(const char* s, double& num);
222
227 size_t strnlen(const char *s, size_t maxlen);
228
229 template <typename T>
230 int16 setArrayFromString(const char* s, T* array, size_t array_size) {
231 if (!s || !array) {
232 return ERROR_NULLPTR;
233 }
234
235 // Note implicit assumption that we have a valid string
236 size_t len = strlen(s);
237 if(len < 1) {
238 return ERROR_DIMENSIONS;
239 }
240
241 // Loop through our string, looking for the commas that separate
242 // our char elements and parsing accordingly
243 uint32 filled_num = 0;
244 uint32 start_idx = 0;
245 uint32 end_idx = 0;
246 int16 err = NO_ERROR;
247 while(filled_num < array_size && end_idx < len) {
248 // Find the next end index of our string
249 while(s[end_idx] != ',' && s[end_idx] != '\0') {
250 end_idx++;
251 }
252
253 // Try to pull out of our string and into our number.
254 char num_arr[end_idx - start_idx + 1];
255 memcpy(num_arr, s + start_idx, end_idx - start_idx);
256 num_arr[end_idx - start_idx] = '\0';
257 err = numberFromString(num_arr, array[filled_num]);
258 if(err) {return err;}
259
260 // And increment
261 if(end_idx < len - 1) {
262 start_idx = ++end_idx;
263 }
264 filled_num++;
265 }
266
267 // Check that we actually filled the thing correctly
268 if(filled_num != array_size) {
269 return ERROR_DIMENSIONS;
270 }
271
272 return NO_ERROR;
273 }
274
275 template<typename Tn, size_t N>
276 int16 numberToString(const std::array<Tn, N>& val, char* retval, size_t size) {
277 if(retval == nullptr) {return ERROR_NULLPTR;}
278
279 bool dimensions_error = false;
280 uint32 val_idx = 0;
281 int num_chars_filled = 0;
282 while (val_idx < N && !dimensions_error) {
283 // Call number to string conversion, starting at open space in our buffer
284 int16 err = numberToString(val[val_idx], retval + num_chars_filled, size - num_chars_filled);
285 if(err) {dimensions_error = true;}
286
287 if(!dimensions_error) {
288 // Calculate updated parameters
289 num_chars_filled = strnlen(retval, size);
290
291 // Add in comma to separate values
292 if(size - num_chars_filled > 1) {
293 if(val_idx < N - 1) {
294 strcpy(retval + num_chars_filled, ",");
295 num_chars_filled += 1;
296 }
297 } else {
298 dimensions_error = true;
299 }
300 }
301
302 val_idx++;
303 }
304
305 if(dimensions_error) {return ERROR_DIMENSIONS;}
306 return NO_ERROR;
307 }
308
309 template<size_t N>
310 int16 numberToString(const std::array<char, N>& val, char* retval, size_t size) {
311 if(retval == nullptr) {return ERROR_NULLPTR;}
312
313 // Determine length
314 uint32 length = strnlen(val.data(), N);
315 if(length >= size) {return ERROR_DIMENSIONS;}
316
317 // And copy over
318 memcpy(retval, val.data(), length);
319 retval[length] = '\0';
320
321 return NO_ERROR;
322 }
323
324}
325
326#endif
#define NO_ERROR
Error code in the case where matrix math executed successfully.
Definition clockwerkerrors.h:34
#define ERROR_NULLPTR
Error code in case of a null pointer.
Definition clockwerkerrors.h:60
#define ERROR_DIMENSIONS
Definition clockwerkerrors.h:41
Definition CircularBuffer.hpp:28
bool isValidNumber(const char *s)
Checks whether a C-style string represents a valid number.
Definition stringutils.cpp:148
int16 splitString(const char *text, const char *delimiter, char **tokens, size_t max_tokens, char *token_buf, size_t token_buf_size)
Splits a string using a specified delimiter.
Definition stringutils.cpp:106
int16 stringReplace(const char *source, const char *orig, const char *repl, char *output, size_t output_size)
Replaces the first occurrence of a substring with another string.
Definition stringutils.cpp:76
void toLower(char *input)
Converts a C-style string to lowercase in place.
Definition stringutils.cpp:33
int16 numberFromString(const char *s, uint8 &num)
Overloaded - convert string to number.
Definition stringutils.cpp:318
int16 strip(const char *input, char *output, size_t output_size)
Removes leading and trailing whitespace from an input string.
Definition stringutils.cpp:54
int16 numberToString(int64 num, char *out, size_t out_size)
Definition stringutils.cpp:156
bool caseInsensitiveEqual(const char *a, const char *b)
Compares two C-style strings case-insensitively.
Definition stringutils.cpp:41
const uint16 MAX_CHARS_PER_ARRAY_STRING
Definition stringutils.hpp:34
size_t strnlen(const char *s, size_t maxlen)
Function to calculate the string length with a length bound.
Definition stringutils.cpp:428
const uint8 MAX_CHARS_PER_STRING_VALUE
Definition stringutils.hpp:31
void toUpper(char *input)
Converts a C-style string to uppercase in place.
Definition stringutils.cpp:25
int16 setArrayFromString(const char *s, T *array, size_t array_size)
Converts a bracket-enclosed comma-separated string to a float array.
Definition stringutils.hpp:230