1 #include <ail/string.hpp>
2 #include <ail/array.hpp>
6 bool is_alpha(char input
)
8 return (input
>= 'A' && input
<= 'Z') || (input
>= 'a' && input
<= 'z');
11 bool is_digit(char input
)
13 return input
>= '0' && input
<= '9';
16 bool is_oct_digit(char input
)
18 return input
>= '0' && input
<= '7';
21 bool is_hex_digit(char input
)
23 return is_digit(input
) || (input
>= 'a' && input
<= 'f') || (input
>= 'A' && input
<= 'F');
26 bool is_whitespace(char input
)
28 return input
== '\t' || input
== '\n' || input
== '\r' || input
== ' ';
31 char to_upper(char input
)
34 if(output
>= 'a' && output
<= 'z')
39 char to_lower(char input
)
42 if(output
>= 'A' && output
<= 'Z')
47 std::string
to_upper(std::string
const & input
)
49 std::string output
= input
;
50 for(std::string::iterator i
= output
.begin(), end
= output
.end(); i
!= end
; ++i
)
52 char & current_char
= *i
;
53 current_char
= to_upper(current_char
);
58 std::string
to_lower(std::string
const & input
)
60 std::string output
= input
;
61 for(std::string::iterator i
= output
.begin(), end
= output
.end(); i
!= end
; ++i
)
63 char & current_char
= *i
;
64 current_char
= to_lower(current_char
);
69 std::string
left_trim(std::string
const & input
)
71 std::string output
= input
;
73 begin
= output
.begin(),
81 if(is_whitespace(*begin
) == false)
85 output
.erase(output
.begin(), begin
);
90 std::string
right_trim(std::string
const & input
)
92 std::string output
= input
;
96 for(end
-= 1; is_whitespace(*end
); --end
);
98 output
.erase(end
+ 1, output
.end());
103 std::string
trim(std::string
const & input
)
105 std::string output
= input
;
106 std::string::iterator
107 begin
= output
.begin(),
115 if(is_whitespace(*begin
) == false)
119 for(end
-= 1; is_whitespace(*end
); --end
);
121 std::size_t offset_begin
= begin
- output
.begin();
122 std::size_t offset_end
= end
- output
.begin();
123 output
= output
.substr(offset_begin
, offset_end
- offset_begin
+ 1);
128 unsigned long count_strings(std::string
const & input
, std::string
const & target
)
130 if(input
.empty() == true)
133 if(target
.empty() == true)
134 throw exception("Empty target");
136 unsigned long output
= 0;
138 std::size_t target_length
= target
.length();
139 std::size_t result
= input
.find(target
);
141 while(result
!= std::string::npos
)
144 result
= input
.find(target
, result
+ target_length
);
150 std::string
erase_string(std::string
const & input
, std::string
const & target
)
152 std::stringstream output
;
158 target_length
= target
.length(),
159 offset
= input
.find(target
),
162 while(offset
!= std::string::npos
)
164 output
<< input
.substr(last_offset
, offset
- last_offset
);
165 offset
+= target_length
;
166 last_offset
= offset
;
167 offset
= input
.find(target
, offset
);
170 output
<< input
.substr(last_offset
);
175 std::string
replace_string(std::string
const & input
, std::string
const & target
, std::string
const & replacement
)
177 std::string output
= input
;
179 if(target
.empty() == true)
180 throw exception("Empty target");
182 if(target
== replacement
)
185 std::size_t target_length
= target
.length();
186 std::size_t replacement_length
= replacement
.length();
188 std::size_t result
= output
.find(target
);
190 while(result
!= std::string::npos
)
192 output
.replace(result
, target_length
, replacement
);
193 result
= output
.find(target
, result
);
200 void tokenise(std::string const & input, string_vector & output, char delimiter)
203 std::istringstream stream(input);
204 while(std::getline(stream, token, delimiter))
205 output.push_back(token);
209 void tokenise(std::string
const & input
, std::string
const & delimiter
, string_vector
& output
)
212 delimiter_length
= delimiter
.length(),
214 right
= input
.find(delimiter
),
217 while(right
!= std::string::npos
)
219 std::string
new_token(input
.substr(left
, right
- left
));
220 left
= right
+ delimiter_length
;
221 right
= input
.find(delimiter
, left
);
226 right
= input
.find(delimiter
);
228 output
.reserve(count
);
230 while(right
!= std::string::npos
)
232 std::string
new_token(input
.substr(left
, right
- left
));
233 output
.push_back(new_token
);
234 left
= right
+ delimiter_length
;
235 right
= input
.find(delimiter
, left
);
238 output
.push_back(input
.substr(left
));
241 string_vector
tokenise(std::string
const & input
, std::string
const & delimiter
)
243 string_vector output
;
244 tokenise(input
, delimiter
, output
);
248 std::string
join_strings(std::vector
<std::string
> const & input
, std::string
const & delimiter
)
251 std::vector
<std::string
>::const_iterator
257 for(++i
; i
!= end
; ++i
)
259 output
+= delimiter
+ *i
;
265 bool string_to_bool(std::string
const & input
, bool & output
)
267 if(input
== "true" || input
== "1")
272 else if(input
== "false" || input
== "0")
281 std::string
bool_to_string(bool input
)
283 return input
? "true" : "false";
286 bool wildcard_match(std::string
const & target
, std::string
const & wildcard_string
)
288 std::string::const_iterator target_iterator
= target
.begin(), target_end
= target
.end();
289 for(std::string::const_iterator i
= wildcard_string
.begin(), end
= wildcard_string
.end(); i
!= end
; ++i
, ++target_iterator
)
291 char current_char
= *i
;
302 if(target_iterator
== target_end
)
304 if(*target_iterator
== next_char
)
313 if(target_iterator
== target_end
)
320 if(target_iterator
== target_end
)
322 if(current_char
!= *target_iterator
)
328 return (target_iterator
== target_end
);
331 std::string
consolify(std::string
const & input
)
333 std::string output
= input
;
334 output
= replace_string(output
, "\b", "\\b");
335 output
= replace_string(output
, "\t", "\\t");
336 output
= replace_string(output
, "\n", "\\n");
337 output
= replace_string(output
, "\v", "\\v");
338 output
= replace_string(output
, "\f", "\\f");
339 output
= replace_string(output
, "\r", "\\r");
340 output
= replace_string(output
, "\x07", "\\x07");
344 std::size_t locate_string(std::string
const & input
, std::string
const & target
, long occurence
, std::size_t offset
)
346 for(; occurence
> 0; occurence
--)
348 offset
= input
.find(target
, offset
+ 1);
349 if(offset
== std::string::npos
)
355 bool extract_string(std::string
const & input
, std::string
const & left_target
, std::string
const & right_target
, std::string
& output
, long occurence
)
357 std::size_t left_offset
= locate_string(input
, left_target
, occurence
, 0);
358 if(left_offset
== std::string::npos
)
360 left_offset
+= left_target
.length();
361 std::size_t right_offset
= input
.find(right_target
, left_offset
);
362 if(right_offset
== std::string::npos
)
364 output
= input
.substr(left_offset
, right_offset
- left_offset
);
368 string_vector
extract_strings(std::string
const & input
, std::string
const & left_target
, std::string
const & right_target
)
370 string_vector output
;
371 std::size_t last_offset
= 0;
374 std::size_t left_offset
= input
.find(left_target
, last_offset
);
375 if(left_offset
== std::string::npos
)
377 left_offset
+= left_target
.length();
378 std::size_t right_offset
= input
.find(right_target
, left_offset
);
379 if(right_offset
== std::string::npos
)
381 std::string element
= input
.substr(left_offset
, right_offset
- left_offset
);
382 output
.push_back(element
);
383 last_offset
= right_offset
+ right_target
.length();
388 bool split_string(std::string
const & input
, std::string
const & delimiter
, std::string
& left_string
, std::string
& right_string
)
390 std::size_t offset
= input
.find(delimiter
);
391 if(offset
== std::string::npos
)
394 right_string
.clear();
397 left_string
= input
.substr(0, offset
);
398 std::size_t right_offset
= offset
+ delimiter
.length();
399 right_string
= input
.substr(right_offset
);
403 std::string
get_size_string(ullong size
)
405 std::string
const size_strings
[] =
414 double adjusted_size
= static_cast<double>(size
);
415 ullong
const base
= 1024;
416 double const divisor
= static_cast<double>(base
);
417 std::size_t size_index
= 0;
418 for(std::size_t maximum
= ail::countof(size_strings
) - 1; adjusted_size
> divisor
&& size_index
< maximum
; adjusted_size
/= base
, size_index
++);
420 std::stringstream stream
;
421 stream
<< std::fixed
;
426 stream
<< adjusted_size
<< " " << size_strings
[size_index
];
431 stream
<< size
<< " " << size_strings
[size_index
];
432 std::string output
= stream
.str();
439 ulong
binary_string_to_number(std::string
const & input
, bool big_endian
)
451 limit
= input
.size();
456 initial_value
= input
.size() - 1;
461 for(std::size_t i
= initial_value
; i
!= limit
; i
+= direction
)
464 output
|= input
[i
] & 1;
470 std::string
hex_string(ullong input
, std::size_t bytes
, bool prefix
= true)
472 std::stringstream output
;
475 output
.width(2 * bytes
);
477 output
<< std::hex
<< input
;
481 std::string
hex_string_8(ulong input
)
483 return hex_string(input
, 1);
486 std::string
hex_string_16(ulong input
)
488 return hex_string(input
, 2);
491 std::string
hex_string_32(ulong input
)
493 return hex_string(input
, 4);
496 std::string
hex_string_64(ullong input
)
498 return hex_string(input
, 8);
501 std::string
hex_string(std::string
const & input
)
503 uchar
const * data
= reinterpret_cast<uchar
const *>(input
.c_str());
506 for(std::size_t i
= 0, end
= input
.size(); i
< end
; i
++)
512 output
+= hex_string(data
[i
], 1, false);
518 std::string
operator+(char left
, std::string
const & right
)
522 return string
+ right
;
525 std::string
operator+(char const * left
, std::string
const & right
)
527 return std::string(left
) + right
;