Develop Biology
The language of life
bio::string Namespace Reference

Classes

struct  Echelon
 

Typedefs

typedef std::vector< EchelonEchelons
 

Functions

void CloneInto (const char *source, const char *&target)
 
template<typename T >
std::string From (const T &value)
 
std::string FromVectorOfStrings (const CharStrings &v, char delimiter=',', bool trimLeadingSpaces=true)
 
std::string FromVectorOfStrings (const StdStrings &v, char delimiter=',', bool trimLeadingSpaces=true)
 
StdStrings Parse (const std::string &s, char delimiter=',', bool trimLeadingSpaces=true)
 
bool ToBool (const char *s, bool *returned)
 
CharStrings ToCharStrings (const StdStrings &strings)
 
bool ToFloat (const char *s, float *returned)
 
bool ToInt (const char *s, int32_t *returned)
 
StdStrings ToStdStrings (const CharStrings &strings)
 
bool ToUInt (const char *s, uint32_t *returned)
 

Typedef Documentation

◆ Echelons

typedef std::vector< Echelon > bio::string::Echelons

Definition at line 166 of file String.h.

Function Documentation

◆ CloneInto()

void bio::string::CloneInto ( const char *  source,
const char *&  target 
)

Copies the contents of source into a new const char* and sets target to point to the new value.

Parameters
source
target

Definition at line 236 of file String.cpp.

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}

Referenced by bio::physical::Identifiable< DIMENSION >::CloneIntoName(), bio::physical::Perspective< DIMENSION >::GetIdFromName(), and bio::genetic::Localization::SetNameOfSite().

◆ From()

template<typename T >
std::string bio::string::From ( const T &  value)

Converts the given value to a string. This is slower than the To____ methods but is more flexible.

Parameters
value
Returns
value as a string.

Definition at line 87 of file String.h.

88{
89 std::ostringstream str;
90 str << value;
91 return str.str();
92}

◆ FromVectorOfStrings() [1/2]

std::string bio::string::FromVectorOfStrings ( const CharStrings v,
char  delimiter = ',',
bool  trimLeadingSpaces = true 
)

Take a vector of char*s and outputs a single string with delimiter separating the strings.

Parameters
v
delimiter
trimLeadingSpaces
Returns

Definition at line 169 of file String.cpp.

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}

◆ FromVectorOfStrings() [2/2]

std::string bio::string::FromVectorOfStrings ( const StdStrings v,
char  delimiter = ',',
bool  trimLeadingSpaces = true 
)

Take a vector of strings and output as a single string with delimiter separating the strings.

Parameters
vvector of .
delimiteritem separator (e.g. ',').
trimLeadingSpacesif true, removes leading spaces from substrings.
Returns
a single string with all strings from v.

Definition at line 131 of file String.cpp.

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}

◆ Parse()

StdStrings bio::string::Parse ( const std::string &  s,
char  delimiter = ',',
bool  trimLeadingSpaces = true 
)

Convert a string containing substrings separated by delimiter to vector of substrings Example: s is "1,abc,3000" Results will be vector of 3 strings: "1" "abc" "3000"

Parameters
sthe string with substrings separated by delimiter.
delimiteritem separator (e.g. ',').
trimLeadingSpacesif true, removes leading spaces from substrings.
Returns
vector of substrings.

Definition at line 97 of file String.cpp.

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}
std::vector< std::string > StdStrings
Definition: Types.h:50

◆ ToBool()

bool bio::string::ToBool ( const char *  s,
bool *  returned 
)

Convert "true" or "false" to bool Case sensitive. TODO: make insensitive.

Parameters
s
returned
Returns
true on success, false if s is not an integer

Definition at line 28 of file String.cpp.

32{
33 *returned = s == "true";
34 if (!returned && s != "false")
35 {
36 return false;
37 }
38 return true;
39}

◆ ToCharStrings()

CharStrings bio::string::ToCharStrings ( const StdStrings strings)

Takes a vector of std::strings and converts it to a vector of const char*.

Parameters
strings
Returns
strings as CharStrings.

Definition at line 207 of file String.cpp.

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}
std::vector< const char * > CharStrings
Definition: Types.h:49

◆ ToFloat()

bool bio::string::ToFloat ( const char *  s,
float *  returned 
)

convert string s to a float and return result in value.

Parameters
s
returned
Returns
true on success, false if s is not a float.

Definition at line 79 of file String.cpp.

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}

◆ ToInt()

bool bio::string::ToInt ( const char *  s,
int32_t *  returned 
)

convert string s to an integer and return result in value

Parameters
s
returned
Returns
true on success, false if s is not an integer

Definition at line 41 of file String.cpp.

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}

◆ ToStdStrings()

StdStrings bio::string::ToStdStrings ( const CharStrings strings)

Takes a vector of const char*s and converts it to a vector of std::strings.

Parameters
strings
Returns
strings as StdStrings

Definition at line 221 of file String.cpp.

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}

◆ ToUInt()

bool bio::string::ToUInt ( const char *  s,
uint32_t *  returned 
)

convert string s to an unsigned integer and return result in value

Parameters
s
returned
Returns
true on success, false if s is not an unsigned integer

Definition at line 60 of file String.cpp.

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}