Open Lighting Architecture  0.9.1
 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 StringEndsWith(const std::string &s, const std::string &suffix);
72 
78 std::string IntToString(int i);
79 
85 std::string IntToString(unsigned int i);
86 
95 std::string IntToHexString(unsigned int i, unsigned int width);
96 
102 inline std::string IntToHexString(uint8_t i) {
103  return IntToHexString(i, (std::numeric_limits<uint8_t>::digits /
104  HEX_BIT_WIDTH));
105 }
106 
112 inline std::string IntToHexString(uint16_t i) {
113  return IntToHexString(i, (std::numeric_limits<uint16_t>::digits /
114  HEX_BIT_WIDTH));
115 }
116 
122 inline std::string IntToHexString(uint32_t i) {
123  return IntToHexString(i, (std::numeric_limits<uint32_t>::digits /
124  HEX_BIT_WIDTH));
125 }
126 
142 void Escape(std::string *original);
143 
150 std::string EscapeString(const std::string &original);
151 
158 void ReplaceAll(std::string *original,
159  const std::string &find,
160  const std::string &replace);
161 
171 std::string EncodeString(const std::string &original);
172 
180 bool StringToBool(const std::string &value, bool *output);
181 
190 bool StringToInt(const std::string &value,
191  unsigned int *output,
192  bool strict = false);
193 
203 bool StringToInt(const std::string &value,
204  uint16_t *output,
205  bool strict = false);
206 
216 bool StringToInt(const std::string &value,
217  uint8_t *output,
218  bool strict = false);
219 
229 bool StringToInt(const std::string &value, int *output, bool strict = false);
230 
240 bool StringToInt(const std::string &value,
241  int16_t *output,
242  bool strict = false);
243 
253 bool StringToInt(const std::string &value, int8_t *output, bool strict = false);
254 
264 bool HexStringToInt(const std::string &value, uint8_t *output);
265 
275 bool HexStringToInt(const std::string &value, uint16_t *output);
276 
286 bool HexStringToInt(const std::string &value, uint32_t *output);
287 
297 bool HexStringToInt(const std::string &value, int8_t *output);
298 
308 bool HexStringToInt(const std::string &value, int16_t *output);
309 
319 bool HexStringToInt(const std::string &value, int32_t *output);
320 
325 void ToLower(std::string *s);
326 
331 void ToUpper(std::string *s);
332 
341 void CapitalizeLabel(std::string *s);
342 
352 void CustomCapitalizeLabel(std::string *s);
353 
366 void FormatData(std::ostream *out,
367  const uint8_t *data,
368  unsigned int length,
369  unsigned int indent = 0,
370  unsigned int byte_per_line = 8);
371 
375 template <typename int_type>
376 bool PrefixedHexStringToInt(const std::string &input, int_type *output) {
377  if ((input.find("0x") != 0) && (input.find("0X") != 0))
378  return false;
379  std::string modified_input = input.substr(2);
380  return HexStringToInt(modified_input, output);
381 }
382 
388 template<typename T>
389 std::string StringJoin(const std::string &delim, const T &input) {
390  std::ostringstream str;
391  typename T::const_iterator iter = input.begin();
392  while (iter != input.end()) {
393  str << *iter++;
394  if (iter != input.end())
395  str << delim;
396  }
397  return str.str();
398 }
399 } // namespace ola
400 #endif // INCLUDE_OLA_STRINGUTILS_H_