Open Lighting Architecture  Latest Git
StringUtils.h
Go to the documentation of this file.
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15  *
16  * StringUtils.h
17  * Random String functions.
18  * Copyright (C) 2005 Simon Newton
19  */
20 
26 #ifndef INCLUDE_OLA_STRINGUTILS_H_
27 #define INCLUDE_OLA_STRINGUTILS_H_
28 
29 #include <ola/strings/Format.h>
30 #include <stdint.h>
31 #include <iomanip>
32 #include <iostream>
33 #include <limits>
34 #include <ostream>
35 #include <sstream>
36 #include <string>
37 #include <vector>
38 
39 namespace ola {
40 
50 void StringSplit(const std::string &input,
51  std::vector<std::string> *tokens,
52  const std::string &delimiters = " ");
53 
64 inline void StringSplit(
65  const std::string &input,
66  std::vector<std::string> &tokens, // NOLINT(runtime/references)
67  const std::string &delimiters = " ") {
68  StringSplit(input, &tokens, delimiters);
69 }
70 
75 void StringTrim(std::string *input);
76 
81 void ShortenString(std::string *input);
82 
89 bool StringBeginsWith(const std::string &s, const std::string &prefix);
90 
97 bool StringEndsWith(const std::string &s, const std::string &suffix);
98 
105 bool StripPrefix(std::string *s, const std::string &prefix);
106 
113 bool StripSuffix(std::string *s, const std::string &suffix);
114 
121 inline std::string IntToString(int i) {
122  return ola::strings::IntToString(i);
123 }
124 
131 inline std::string IntToString(unsigned int i) {
132  return ola::strings::IntToString(i);
133 }
134 
145 std::string IntToHexString(unsigned int i, unsigned int width);
146 
154 inline std::string IntToHexString(uint8_t i) {
155  std::ostringstream str;
156  str << ola::strings::ToHex(i);
157  return str.str();
158 }
159 
167 inline std::string IntToHexString(uint16_t i) {
168  std::ostringstream str;
169  str << ola::strings::ToHex(i);
170  return str.str();
171 }
172 
180 inline std::string IntToHexString(uint32_t i) {
181  std::ostringstream str;
182  str << ola::strings::ToHex(i);
183  return str.str();
184 }
185 
201 void Escape(std::string *original);
202 
209 std::string EscapeString(const std::string &original);
210 
217 void ReplaceAll(std::string *original,
218  const std::string &find,
219  const std::string &replace);
220 
230 std::string EncodeString(const std::string &original);
231 
243 bool StringToBool(const std::string &value, bool *output);
244 
258 bool StringToBoolTolerant(const std::string &value, bool *output);
259 
268 bool StringToInt(const std::string &value,
269  unsigned int *output,
270  bool strict = false);
271 
281 bool StringToInt(const std::string &value,
282  uint16_t *output,
283  bool strict = false);
284 
294 bool StringToInt(const std::string &value,
295  uint8_t *output,
296  bool strict = false);
297 
307 bool StringToInt(const std::string &value, int *output, bool strict = false);
308 
318 bool StringToInt(const std::string &value,
319  int16_t *output,
320  bool strict = false);
321 
331 bool StringToInt(const std::string &value, int8_t *output, bool strict = false);
332 
342 template <typename int_type>
343 int_type StringToIntOrDefault(const std::string &value,
344  int_type alternative,
345  bool strict = false) {
346  int_type output;
347  return (StringToInt(value, &output, strict)) ? output : alternative;
348 }
349 
359 bool HexStringToInt(const std::string &value, uint8_t *output);
360 
370 bool HexStringToInt(const std::string &value, uint16_t *output);
371 
381 bool HexStringToInt(const std::string &value, uint32_t *output);
382 
392 bool HexStringToInt(const std::string &value, int8_t *output);
393 
403 bool HexStringToInt(const std::string &value, int16_t *output);
404 
414 bool HexStringToInt(const std::string &value, int32_t *output);
415 
420 void ToLower(std::string *s);
421 
426 void ToUpper(std::string *s);
427 
436 void CapitalizeLabel(std::string *s);
437 
455 void CustomCapitalizeLabel(std::string *s);
456 
461 void CapitalizeFirst(std::string *s);
462 
476 inline void FormatData(std::ostream *out,
477  const uint8_t *data,
478  unsigned int length,
479  unsigned int indent = 0,
480  unsigned int byte_per_line = 8) {
481  return ola::strings::FormatData(out, data, length, indent, byte_per_line);
482 }
483 
487 template <typename int_type>
488 bool PrefixedHexStringToInt(const std::string &input, int_type *output) {
489  if ((input.find("0x") != 0) && (input.find("0X") != 0))
490  return false;
491  std::string modified_input = input.substr(2);
492  return HexStringToInt(modified_input, output);
493 }
494 
500 template<typename T>
501 std::string StringJoin(const std::string &delim, const T &input) {
502  std::ostringstream str;
503  typename T::const_iterator iter = input.begin();
504  while (iter != input.end()) {
505  str << *iter++;
506  if (iter != input.end())
507  str << delim;
508  }
509  return str.str();
510 }
511 } // namespace ola
512 #endif // INCLUDE_OLA_STRINGUTILS_H_
void CapitalizeLabel(string *s)
Transform a string to a pretty-printed form.
Definition: StringUtils.cpp:414
string EscapeString(const string &original)
Escape a string, returning a copy.
Definition: StringUtils.cpp:296
bool StripPrefix(string *s, const string &prefix)
Strips a prefix from a string.
Definition: StringUtils.cpp:97
void CapitalizeFirst(string *s)
Transform a string by capitalizing the first character.
Definition: StringUtils.cpp:483
bool HexStringToInt(const string &value, uint8_t *output)
Convert a hex string to a uint8_t.
Definition: StringUtils.cpp:330
std::string StringJoin(const std::string &delim, const T &input)
Join a vector of a type.
Definition: StringUtils.h:501
string EncodeString(const string &original)
Encode any unprintable characters in a string as hex, returning a copy.
Definition: StringUtils.cpp:302
std::string IntToString(int i)
Convert an int to a string.
Definition: StringUtils.h:121
Formatting functions for basic types.
void StringTrim(string *input)
Trim leading and trailing whitespace from a string.
Definition: StringUtils.cpp:61
void ShortenString(string *input)
Truncate the string based on the presence of \0 characters.
Definition: StringUtils.cpp:73
bool StringBeginsWith(const string &s, const string &prefix)
Check if one string is a prefix of another.
Definition: StringUtils.cpp:80
void FormatData(std::ostream *out, const uint8_t *data, unsigned int length, unsigned int indent=0, unsigned int byte_per_line=8)
Write binary data to an ostream in a human readable form.
Definition: StringUtils.h:476
bool StringToBool(const string &value, bool *output)
Convert a string to a bool.
Definition: StringUtils.cpp:123
void FormatData(std::ostream *out, const uint8_t *data, unsigned int length, unsigned int indent, unsigned int byte_per_line)
Write binary data to an ostream in a human readable form.
Definition: Format.cpp:46
bool StringEndsWith(const string &s, const string &suffix)
Check if one string is a suffix of another.
Definition: StringUtils.cpp:88
_ToHex< T > ToHex(T v, bool prefix=true)
Convert a value to a hex string.
Definition: Format.h:68
bool StringToBoolTolerant(const string &value, bool *output)
Convert a string to a bool in a tolerant way.
Definition: StringUtils.cpp:136
void CustomCapitalizeLabel(string *s)
Similar to CapitalizeLabel() but this also capitalized known acronyms.
Definition: StringUtils.cpp:437
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
void ToUpper(string *s)
Convert a string to upper case.
Definition: StringUtils.cpp:409
bool StripSuffix(string *s, const string &suffix)
Strips a suffix from a string.
Definition: StringUtils.cpp:106
bool PrefixedHexStringToInt(const std::string &input, int_type *output)
Convert a hex string, prefixed with 0x or 0X to an int type.
Definition: StringUtils.h:488
void StringSplit(const string &input, vector< string > *tokens, const string &delimiters)
Split a string into pieces.
Definition: StringUtils.cpp:42
void ToLower(string *s)
Convert a string to lower case.
Definition: StringUtils.cpp:404
void ReplaceAll(string *original, const string &find, const string &replace)
Replace all instances of the find string with the replace string.
Definition: StringUtils.cpp:317
string IntToHexString(unsigned int i, unsigned int width)
Definition: StringUtils.cpp:115
int_type StringToIntOrDefault(const std::string &value, int_type alternative, bool strict=false)
Convert a string to an int type or return a default if it failed.
Definition: StringUtils.h:343
string IntToString(int i)
Convert an int to a string.
Definition: Format.cpp:34
bool StringToInt(const string &value, unsigned int *output, bool strict)
Convert a string to a unsigned int.
Definition: StringUtils.cpp:155
void Escape(string *original)
Escape a string with \ .
Definition: StringUtils.cpp:249