i6engine  1.0
i6eString.h
Go to the documentation of this file.
1 /*
2  * i6engine
3  * Copyright (2016) Daniel Bonrath, Michael Baer, All rights reserved.
4  *
5  * This file is part of i6engine; i6engine is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
25 #ifndef __I6ENGINE_UTILS_I6ESTRING_H__
26 #define __I6ENGINE_UTILS_I6ESTRING_H__
27 
28 #include <iomanip>
29 #include <sstream>
30 #include <string>
31 #include <vector>
32 
34 
35 namespace i6e {
36 namespace utils {
37 
42  inline std::vector<std::string> split(const std::string & str, const std::string & delim) {
43  std::vector<std::string> ret;
44 
45  size_t n = 0;
46  size_t n2 = str.find(delim);
47 
48  while (n2 != std::string::npos) {
49  std::string s = str.substr(n, n2 - n);
50  n = n2 + 1;
51  n2 = str.find(delim, n);
52 
53  if (!s.empty()) {
54  ret.push_back(s);
55  }
56  }
57 
58  if (str.size() - n > 0) {
59  ret.push_back(str.substr(n, str.size() - n));
60  }
61 
62  return ret;
63  }
64 
68  template<typename T>
69  typename std::enable_if<std::is_floating_point<T>::value, std::string>::type to_string_with_precision(const T a_value, const int n) {
70  std::ostringstream out;
71  out << std::setprecision(n) << a_value;
72  return out.str();
73  }
74 
75 } /* namespace utils */
76 } /* namespace i6e */
77 
78 #endif /* __I6ENGINE_UTILS_I6ESTRING_H__ */
79 
std::vector< std::string > split(const std::string &str, const std::string &delim)
returns a vector of strings separated by the given delimitter e.g. a;b;c with delimitter ; will retur...
Definition: i6eString.h:42
std::enable_if< std::is_floating_point< T >::value, std::string >::type to_string_with_precision(const T a_value, const int n)
returns a string from a floating point value with given precision
Definition: i6eString.h:69