WarpTwin
Documentation for WarpTwin models and classes.
Loading...
Searching...
No Matches
stdstringutils.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_STDSTRINGUTILS_HPP
17#define UTILS_STDSTRINGUTILS_HPP
18
19#include <string>
20#include <array>
21#include <vector>
22
24
25namespace warptwin {
29 std::string toUpper(const std::string &input);
30
34 std::string toLower(const std::string &input);
35
40 bool caseInsensitiveEqual(const std::string &in_a, const std::string &in_b);
41
47 std::string stringReplace(const std::string &source, const std::string &orig_str, const std::string &new_str);
48
62 std::string strip(const std::string& input);
63
80 int splitString(const std::string& text, const std::string& delimiter,
81 std::vector<std::string>& tokens);
82
92 bool isValidNumber(const std::string& s);
93
99 template <typename T, long unsigned int N>
100 int setArrayFromString(const std::string &s, std::array<T, N>& retval) {
101 // Verify our string is formatted correctly
102 if(s.size() < 2 || s.front() != '[' || s.back() != ']') {
104 }
105
106 // Extract the indices of our commas
107 std::vector<unsigned int> comma_indices;
108 unsigned int start_idx = 0;
109 std::size_t found = s.find(",", start_idx);
110 while(found != std::string::npos) {
111 comma_indices.push_back(found);
112 start_idx = found + 1;
113 found = s.find(",", start_idx);
114 }
115 comma_indices.push_back(s.size() - 1);
116
117 // Verify our array and raise error if no match
118 if(comma_indices.size() != N) {
119 return ERROR_DIMENSIONS;
120 }
121
122 // Now set the values of our array
123 unsigned int idx = 0;
124 std::string num_str;
125 for(unsigned int i = 0; i < comma_indices.size(); i++) {
126 // Pull the string for our number
127 num_str = s.substr(idx+1, comma_indices[i] - idx - 1);
128
129 // Extract our number and push it onto our array
130 if(isValidNumber(num_str)) {
131 retval[i] = (T)stod(num_str);
132 } else {
134 }
135
136 idx = comma_indices[i];
137 }
138
139 return NO_ERROR;
140 }
141
147 template <typename T>
148 int setVectorFromString(const std::string &s, std::vector<T>& retval) {
149 // Verify our string is formatted correctly
150 if(s.size() < 2 || s.front() != '[' || s.back() != ']') {
152 }
153
154 // Extract the indices of our commas
155 std::vector<unsigned int> comma_indices;
156 unsigned int start_idx = 0;
157 std::size_t found = s.find(",", start_idx);
158 while(found != std::string::npos) {
159 comma_indices.push_back(found);
160 start_idx = found + 1;
161 found = s.find(",", start_idx);
162 }
163 comma_indices.push_back(s.size() - 1);
164
165 // Now set the values of our array
166 unsigned int idx = 0;
167 std::string num_str;
168 for(unsigned int i = 0; i < comma_indices.size(); i++) {
169 // Pull the string for our number
170 num_str = s.substr(idx+1, comma_indices[i] - idx - 1);
171
172 // Extract our number and push it onto our array
173 if(isValidNumber(num_str)) {
174 if(retval.size() > i) {
175 retval[i] = (T)stod(num_str);
176 } else {
177 retval.push_back((T)stod(num_str));
178 }
179 } else {
181 }
182
183 idx = comma_indices[i];
184 }
185
186 return NO_ERROR;
187 }
188}
189
190#endif
#define ERROR_DIMENSIONS
Definition clockwerkerrors.h:41
#define ERROR_CANNOT_CONVERT_STRING
Error in the case where dynamics model overruns steps or gets out of whack.
Definition clockwerkerrors.h:182
Class to propagate CR3BP dynamics in characteristic units.
Definition statistics.hpp:22
std::string strip(const std::string &input)
Removes leading and trailing whitespace characters from a string.
Definition stdstringutils.cpp:60
bool caseInsensitiveEqual(const std::string &in_a, const std::string &in_b)
Function to compare two strings independent of case.
Definition stdstringutils.cpp:43
int splitString(const std::string &text, const std::string &delimiter, std::vector< std::string > &tokens)
Splits a string into a vector of substrings based on a specified string delimiter.
Definition stdstringutils.cpp:110
bool isValidNumber(const std::string &s)
Checks if the given string is a valid numeric representation.
Definition stdstringutils.cpp:92
std::string toUpper(const std::string &input)
Function to convert a string to all upper case.
Definition stdstringutils.cpp:23
std::string stringReplace(const std::string &source, const std::string &orig_str, const std::string &new_str)
Replace the first element of a source string with a new element.
Definition stdstringutils.cpp:77
const int NO_ERROR
Definition simlicense.cpp:30
int setArrayFromString(const std::string &s, std::array< T, N > &retval)
Set a std::array from a string.
Definition stdstringutils.hpp:100
std::string toLower(const std::string &input)
Function to convert a string to all lower case.
Definition stdstringutils.cpp:33
int setVectorFromString(const std::string &s, std::vector< T > &retval)
Set a std::array from a string.
Definition stdstringutils.hpp:148