Gobelijn API documentation  - generated for commit a0cbea7
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
StringUtils.h
Go to the documentation of this file.
1 #pragma once
2 /*
3  * This file is part of the gobelijn software.
4  * Gobelijn is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation, either version 3 of the License, or any later
7  * version. Gobelijn is distributed in the hope that it will be useful, but
8  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
9  * or FITNESS FOR A PARTICULAR PURPOSE.
10  * See the GNU General Public License for details. You should have received
11  * a copy of the GNU General Public License along with the software. If not,
12  * see <http://www.gnu.org/licenses/>.
13  *
14  * Copyright 2012, Jan Broeckhove.
15  */
22 #include <algorithm>
23 #include <cctype>
24 #include <sstream>
25 #include <string>
26 
27 namespace UA_CoMP {
28 namespace Util {
29 
34 {
35 public:
37  template <typename T>
38  inline static T FromString(std::string const& s)
39  {
40  std::stringstream ss(s);
41  T t;
42  ss >> t;
43  return t;
44  }
45 
47  template <typename T>
48  inline static std::string ToString(T const& value)
49  {
50  std::stringstream ss;
51  ss << value;
52  return ss.str();
53  }
54 
56  static std::string ToUpper(std::string const& source)
57  {
58  auto upper = [](int c) -> int { return std::toupper(c); };
59  std::string copy;
60  std::transform(source.begin(), source.end(), std::back_inserter(copy), upper);
61  return copy;
62  }
63 
65  static std::string TrimRight(std::string const& source, std::string const& t = " ")
66  {
67  std::string str = source;
68  return str.erase(str.find_last_not_of(t) + 1);
69  }
70 
72  static std::string TrimLeft(std::string const& source, std::string const& t = " ")
73  {
74  std::string str = source;
75  return str.erase(0, source.find_first_not_of(t));
76  }
77 
79  static std::string Trim(std::string const& source, std::string const& t = " ")
80  {
81  std::string str = source;
82  return TrimLeft(TrimRight(str, t), t);
83  }
84 };
85 
86 } // namespace Util
87 } // namespace UA_CoMP
static std::string TrimRight(std::string const &source, std::string const &t=" ")
Trim characters at right end of string.
Definition: StringUtils.h:65
static std::string Trim(std::string const &source, std::string const &t=" ")
Trim characters at both ends of string.
Definition: StringUtils.h:79
String utilities.
Definition: StringUtils.h:33
static std::string ToUpper(std::string const &source)
Builds a string with upper case characters only.
Definition: StringUtils.h:56
static std::string TrimLeft(std::string const &source, std::string const &t=" ")
Trim characters at left end of string.
Definition: StringUtils.h:72
static std::string ToString(T const &value)
Builds a string representation of a value of type T.
Definition: StringUtils.h:48
static T FromString(std::string const &s)
Builds a value of type T representation from a string.
Definition: StringUtils.h:38