SimpleAI
 All Classes Namespaces Files Functions Variables Typedefs Macros Groups Pages
String.h
Go to the documentation of this file.
1 
4 #pragma once
5 
6 #include "Math.h"
7 #include <vector>
8 #include <string>
9 #include <sstream>
10 #include <algorithm>
11 
12 namespace ai {
13 namespace Str {
14 
15 inline std::string toString(const glm::vec3& pos) {
16  char buf[128];
17  std::snprintf(buf, sizeof(buf), "%f:%f:%f", pos.x, pos.y, pos.z);
18  return buf;
19 }
20 
21 inline bool startsWith(const std::string& string, const std::string& token) {
22  return !string.compare(0, token.size(), token);
23 }
24 
25 inline float strToFloat(const std::string& str) {
26  return static_cast<float>(::atof(str.c_str()));
27 }
28 
29 inline std::string eraseAllSpaces(const std::string& str) {
30  if (str.empty())
31  return str;
32  std::string tmp = str;
33  tmp.erase(std::remove(tmp.begin(), tmp.end(), ' '), tmp.end());
34  return tmp;
35 }
36 
37 inline void splitString(const std::string& string, std::vector<std::string>& tokens, const std::string& delimiters = "()") {
38  // Skip delimiters at beginning.
39  std::string::size_type lastPos = string.find_first_not_of(delimiters, 0);
40  // Find first "non-delimiter".
41  std::string::size_type pos = string.find_first_of(delimiters, lastPos);
42 
43  while (std::string::npos != pos || std::string::npos != lastPos) {
44  // Found a token, add it to the vector.
45  tokens.push_back(string.substr(lastPos, pos - lastPos));
46  // Skip delimiters. Note the "not_of"
47  lastPos = string.find_first_not_of(delimiters, pos);
48  // Find next "non-delimiter"
49  pos = string.find_first_of(delimiters, lastPos);
50  }
51 }
52 
53 }
54 
55 }