Develop Biology
The language of life
String.cpp
Go to the documentation of this file.
1/*
2 * This file is a part of the Biology project by eons LLC.
3 * Biology (aka Develop Biology) is a framework for approaching software
4 * development from a natural sciences perspective.
5 *
6 * Copyright (C) 2021 Séon O'Shannon & eons LLC
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as
10 * published by the Free Software Foundation, either version 3 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22#include "bio/common/String.h"
23
24#include <cstring>
25
26using namespace bio;
27
29 const char* s,
30 bool* returned
31)
32{
33 *returned = s == "true";
34 if (!returned && s != "false")
35 {
36 return false;
37 }
38 return true;
39}
40
42 const char* s,
43 int32_t* value
44)
45{
46 if (strlen(s) == 0)
47 {
48 return false; //FAIL: empty string
49 }
50
51 char* endptr = NULL;
52 *value = strtol(
53 s,
54 &endptr,
55 10
56 );
57 return ((*endptr) == '\0');
58}
59
61 const char* s,
62 uint32_t* value
63)
64{
65 if (strlen(s) == 0)
66 {
67 return false; //FAIL: empty string
68 }
69
70 char* endptr = NULL;
71 *value = strtoul(
72 s,
73 &endptr,
74 10
75 );
76 return ((*endptr) == '\0');
77}
78
80 const char* s,
81 float* value
82)
83{
84 if (strlen(s) == 0)
85 {
86 return false; //FAIL: empty string
87 }
88
89 char* endptr = NULL;
90 *value = strtof(
91 s,
92 &endptr
93 );
94 return ((*endptr) == '\0');
95}
96
98 const std::string& s,
99 char delimiter,
100 bool trimLeadingSpaces
101)
102{
103 std::stringstream ss(s);
104 StdStrings result;
105 while (ss.good())
106 {
107
108 std::string substring;
109 std::getline(
110 ss,
111 substring,
112 delimiter
113 );
114
115 if (trimLeadingSpaces)
116 {
117 //trim leading spaces from substring
118 size_t firstcharpos = substring.find_first_not_of(' ');
119 if (firstcharpos != std::string::npos)
120 {
121 substring = substring.substr(firstcharpos);
122 }
123 } //else: don't alter substring
124
125 result.push_back(substring);
126 }
127
128 return result;
129}
130
132 const StdStrings& v,
133 char delimiter,
134 bool trimLeadingSpaces
135)
136{
137 std::string result;
138 for (
139 StdStrings::const_iterator iter = v.begin();
140 iter != v.end();
141 ++iter
142 )
143 {
144 std::string substring = *iter;
145
146 if (trimLeadingSpaces)
147 {
148 //trim leading spaces from substring
149 size_t firstcharpos = substring.find_first_not_of(' ');
150 if (firstcharpos != std::string::npos)
151 {
152 substring = substring.substr(firstcharpos);
153 }
154 } //else: don't alter substring
155
156 if (result.length() == 0)
157 {
158 result = substring;
159 }
160 else
161 {
162 result += (delimiter + substring);
163 }
164 }
165
166 return result;
167}
168
170 const CharStrings& v,
171 char delimiter,
172 bool trimLeadingSpaces
173)
174{
175 std::string result;
176 for (
177 CharStrings::const_iterator iter = v.begin();
178 iter != v.end();
179 ++iter
180 )
181 {
182 std::string substring(*iter);
183
184 if (trimLeadingSpaces)
185 {
186 //trim leading spaces from substring
187 size_t firstcharpos = substring.find_first_not_of(' ');
188 if (firstcharpos != std::string::npos)
189 {
190 substring = substring.substr(firstcharpos);
191 }
192 } //else: don't alter substring
193
194 if (result.length() == 0)
195 {
196 result = substring;
197 }
198 else
199 {
200 result += (delimiter + substring);
201 }
202 }
203
204 return result;
205}
206
208{
209 CharStrings ret;
210 for (
211 StdStrings::const_iterator str = strings.begin();
212 str != strings.end();
213 ++str
214 )
215 {
216 ret.push_back(str->c_str());
217 }
218 return ret;
219}
220
222{
223 StdStrings ret;
224 for (
225 CharStrings::const_iterator chr = strings.begin();
226 chr != strings.end();
227 ++chr
228 )
229 {
230 std::string str(*chr);
231 ret.push_back(str);
232 }
233 return ret;
234}
235
237 const char* source,
238 const char*& target
239)
240{
241 //NOTE: because "new" is used here, a delete needs to be called either here or in the caller.
242 const size_t len = strlen(source);
243 char* tmpName = new char[len + 1];
244 strncpy(
245 tmpName,
246 source,
247 len
248 );
249 tmpName[len] = '\0';
250 target = tmpName;
251}
CharStrings ToCharStrings(const StdStrings &strings)
Definition: String.cpp:207
void CloneInto(const char *source, const char *&target)
Definition: String.cpp:236
StdStrings ToStdStrings(const CharStrings &strings)
Definition: String.cpp:221
bool ToBool(const char *s, bool *returned)
Definition: String.cpp:28
bool ToFloat(const char *s, float *returned)
Definition: String.cpp:79
bool ToInt(const char *s, int32_t *returned)
Definition: String.cpp:41
std::string FromVectorOfStrings(const StdStrings &v, char delimiter=',', bool trimLeadingSpaces=true)
Definition: String.cpp:131
bool ToUInt(const char *s, uint32_t *returned)
Definition: String.cpp:60
StdStrings Parse(const std::string &s, char delimiter=',', bool trimLeadingSpaces=true)
Definition: String.cpp:97
Definition: Cell.h:31
std::vector< std::string > StdStrings
Definition: Types.h:50
std::vector< const char * > CharStrings
Definition: Types.h:49