Open Lighting Architecture  0.9.0
 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  *
16  * StringUtils..h
17  * Random String functions.
18  * Copyright (C) 2005-2008 Simon Newton
19  */
20 
26 #ifndef INCLUDE_OLA_STRINGUTILS_H_
27 #define INCLUDE_OLA_STRINGUTILS_H_
28 
29 #include <stdint.h>
30 #include <sstream>
31 #include <string>
32 #include <vector>
33 
34 namespace ola {
35 
45 void StringSplit(const std::string &input,
46  std::vector<std::string> &tokens, // NOLINT
47  const std::string &delimiters=" ");
48 
53 void StringTrim(std::string *input);
54 
59 void ShortenString(std::string *input);
60 
67 bool StringEndsWith(const std::string &s, const std::string &suffix);
68 
74 std::string IntToString(int i);
75 
81 std::string IntToString(unsigned int i);
82 
98 void Escape(std::string *original);
99 
106 std::string EscapeString(const std::string &original);
107 
114 void ReplaceAll(std::string *original,
115  const std::string &find,
116  const std::string &replace);
117 
127 std::string EncodeString(const std::string &original);
128 
136 bool StringToBool(const std::string &value, bool *output);
137 
146 bool StringToInt(const std::string &value,
147  unsigned int *output,
148  bool strict = false);
149 
159 bool StringToInt(const std::string &value,
160  uint16_t *output,
161  bool strict = false);
162 
172 bool StringToInt(const std::string &value,
173  uint8_t *output,
174  bool strict = false);
175 
185 bool StringToInt(const std::string &value, int *output, bool strict = false);
186 
196 bool StringToInt(const std::string &value,
197  int16_t *output,
198  bool strict = false);
199 
209 bool StringToInt(const std::string &value, int8_t *output, bool strict = false);
210 
220 bool HexStringToInt(const std::string &value, uint8_t *output);
221 
231 bool HexStringToInt(const std::string &value, uint16_t *output);
232 
242 bool HexStringToInt(const std::string &value, uint32_t *output);
243 
253 bool HexStringToInt(const std::string &value, int8_t *output);
254 
264 bool HexStringToInt(const std::string &value, int16_t *output);
265 
275 bool HexStringToInt(const std::string &value, int32_t *output);
276 
281 void ToLower(std::string *s);
282 
287 void ToUpper(std::string *s);
288 
297 void CapitalizeLabel(std::string *s);
298 
308 void CustomCapitalizeLabel(std::string *s);
309 
322 void FormatData(std::ostream *out,
323  const uint8_t *data,
324  unsigned int length,
325  unsigned int indent = 0,
326  unsigned int byte_per_line = 8);
327 
331 template <typename int_type>
332 bool PrefixedHexStringToInt(const std::string &input, int_type *output) {
333  if ((input.find("0x") != 0) && (input.find("0X") != 0))
334  return false;
335  std::string modified_input = input.substr(2);
336  return HexStringToInt(modified_input, output);
337 }
338 
344 template<typename T>
345 std::string StringJoin(const std::string &delim, const T &input) {
346  std::ostringstream str;
347  typename T::const_iterator iter = input.begin();
348  while (iter != input.end()) {
349  str << *iter++;
350  if (iter != input.end())
351  str << delim;
352  }
353  return str.str();
354 }
355 } // namespace ola
356 #endif // INCLUDE_OLA_STRINGUTILS_H_