Open Lighting Architecture  0.9.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 <stdint.h>
30 #include <limits>
31 #include <sstream>
32 #include <string>
33 #include <vector>
34 
35 namespace ola {
36 
38 enum { HEX_BIT_WIDTH = 4 };
39 
49 void StringSplit(const std::string &input,
50  std::vector<std::string> &tokens, // NOLINT
51  const std::string &delimiters=" ");
52 
57 void StringTrim(std::string *input);
58 
63 void ShortenString(std::string *input);
64 
71 bool StringBeginsWith(const std::string &s, const std::string &prefix);
72 
79 bool StringEndsWith(const std::string &s, const std::string &suffix);
80 
87 bool StripPrefix(std::string *s, const std::string &prefix);
88 
95 bool StripSuffix(std::string *s, const std::string &suffix);
96 
102 std::string IntToString(int i);
103 
109 std::string IntToString(unsigned int i);
110 
119 std::string IntToHexString(unsigned int i, unsigned int width);
120 
126 inline std::string IntToHexString(uint8_t i) {
127  return IntToHexString(i, (std::numeric_limits<uint8_t>::digits /
128  HEX_BIT_WIDTH));
129 }
130 
136 inline std::string IntToHexString(uint16_t i) {
137  return IntToHexString(i, (std::numeric_limits<uint16_t>::digits /
138  HEX_BIT_WIDTH));
139 }
140 
146 inline std::string IntToHexString(uint32_t i) {
147  return IntToHexString(i, (std::numeric_limits<uint32_t>::digits /
148  HEX_BIT_WIDTH));
149 }
150 
166 void Escape(std::string *original);
167 
174 std::string EscapeString(const std::string &original);
175 
182 void ReplaceAll(std::string *original,
183  const std::string &find,
184  const std::string &replace);
185 
195 std::string EncodeString(const std::string &original);
196 
208 bool StringToBool(const std::string &value, bool *output);
209 
223 bool StringToBoolTolerant(const std::string &value, bool *output);
224 
233 bool StringToInt(const std::string &value,
234  unsigned int *output,
235  bool strict = false);
236 
246 bool StringToInt(const std::string &value,
247  uint16_t *output,
248  bool strict = false);
249 
259 bool StringToInt(const std::string &value,
260  uint8_t *output,
261  bool strict = false);
262 
272 bool StringToInt(const std::string &value, int *output, bool strict = false);
273 
283 bool StringToInt(const std::string &value,
284  int16_t *output,
285  bool strict = false);
286 
296 bool StringToInt(const std::string &value, int8_t *output, bool strict = false);
297 
307 bool HexStringToInt(const std::string &value, uint8_t *output);
308 
318 bool HexStringToInt(const std::string &value, uint16_t *output);
319 
329 bool HexStringToInt(const std::string &value, uint32_t *output);
330 
340 bool HexStringToInt(const std::string &value, int8_t *output);
341 
351 bool HexStringToInt(const std::string &value, int16_t *output);
352 
362 bool HexStringToInt(const std::string &value, int32_t *output);
363 
368 void ToLower(std::string *s);
369 
374 void ToUpper(std::string *s);
375 
384 void CapitalizeLabel(std::string *s);
385 
395 void CustomCapitalizeLabel(std::string *s);
396 
409 void FormatData(std::ostream *out,
410  const uint8_t *data,
411  unsigned int length,
412  unsigned int indent = 0,
413  unsigned int byte_per_line = 8);
414 
418 template <typename int_type>
419 bool PrefixedHexStringToInt(const std::string &input, int_type *output) {
420  if ((input.find("0x") != 0) && (input.find("0X") != 0))
421  return false;
422  std::string modified_input = input.substr(2);
423  return HexStringToInt(modified_input, output);
424 }
425 
431 template<typename T>
432 std::string StringJoin(const std::string &delim, const T &input) {
433  std::ostringstream str;
434  typename T::const_iterator iter = input.begin();
435  while (iter != input.end()) {
436  str << *iter++;
437  if (iter != input.end())
438  str << delim;
439  }
440  return str.str();
441 }
442 } // namespace ola
443 #endif // INCLUDE_OLA_STRINGUTILS_H_