Cafu Engine
String.hpp
1 /*
2 Cafu Engine, http://www.cafu.de/
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
5 */
6 
7 #ifndef CAFU_STRING_HPP_INCLUDED
8 #define CAFU_STRING_HPP_INCLUDED
9 
10 #include <string>
11 
12 
13 namespace cf
14 {
15  /// The String namespace gathers auxiliary string functions that are not found in that standard library.
16  namespace String
17  {
18  /// Returns whether the given string \c String ends with the given \c Suffix.
19  inline bool EndsWith(const std::string& String, const std::string& Suffix)
20  {
21  const size_t StringLength=String.length();
22  const size_t SuffixLength=Suffix.length();
23 
24  if (StringLength<SuffixLength) return false;
25 
26  std::string StringSuffix=&(String.c_str()[StringLength-SuffixLength]);
27 
28  return StringSuffix==Suffix;
29  }
30 
31  /// Assumes that the given string \c s is a filename, removes the extension, if any, and returns the rest.
32  inline std::string StripExt(std::string s)
33  {
34  const size_t PosDot=s.find_last_of('.');
35  const size_t PosSep=s.find_last_of("/\\");
36 
37  if (PosDot==std::string::npos) return s; // "." not found in s?
38  if (PosSep!=std::string::npos && PosDot<PosSep) return s; // Last "." found before last "/"?
39 
40  s.erase(PosDot);
41  return s;
42  }
43 
44  /// Assumes that the given string \c s is a filename of pattern "path/filename.ext" and returns the path portion.
45  inline std::string GetPath(std::string s)
46  {
47  const size_t PosSep=s.find_last_of("/\\");
48 
49  if (PosSep==std::string::npos) return ""; // "/" not found in s?
50 
51  s.erase(PosSep);
52  return s;
53  }
54 
55  /// Replaces in \c s all occurrences of \c search by \c replace, and returns the new string.
56  inline std::string Replace(std::string s, const std::string& search, const std::string& replace)
57  {
58  const size_t len_search =search.length(); if (len_search==0) return s;
59  const size_t len_replace=replace.length();
60 
61  size_t pos=s.find(search);
62 
63  while (pos!=std::string::npos)
64  {
65  s.replace(pos, len_search, replace);
66  pos=s.find(search, pos+len_replace);
67  }
68 
69  return s;
70  }
71 
72  /// Modifies the given string `s` as necessary to turn it into a valid Lua 5.2 identifier and returns the result.
73  inline std::string ToLuaIdentifier(std::string s)
74  {
75  // Must not be empty.
76  if (s == "")
77  return "var";
78 
79  // Must not start with a digit.
80  if (isdigit(s[0]))
81  s = "var_" + s;
82 
83  // Must only consist of locale-independent letters, digits and underscores.
84  for (size_t i = 0; i < s.length(); i++)
85  {
86  if (isalnum(s[i]) && s[i] < 128) continue;
87  if (s[i] == '_') continue;
88 
89  s[i] = '_';
90  }
91 
92  // Must not be one of the reserved keywords.
93  const char* Keywords[] =
94  {
95  "and", "break", "do", "else", "elseif", "end",
96  "false", "for", "function", "goto", "if", "in",
97  "local", "nil", "not", "or", "repeat", "return",
98  "then", "true", "until", "while", NULL
99  };
100 
101  for (unsigned int i = 0; Keywords[i]; i++)
102  if (s == Keywords[i])
103  {
104  s = "var_" + s;
105  break;
106  }
107 
108  // Must not be a variable name that is reserved for Lua.
109  if (s[0] == '_')
110  {
111  size_t i;
112 
113  for (i = 1; i < s.length(); i++)
114  if (!isupper(s[i]))
115  break;
116 
117  if (i >= s.length())
118  s = "var" + s;
119  }
120 
121  return s;
122  }
123  }
124 }
125 
126 #endif
bool EndsWith(const std::string &String, const std::string &Suffix)
Returns whether the given string String ends with the given Suffix.
Definition: String.hpp:19
std::string GetPath(std::string s)
Assumes that the given string s is a filename of pattern "path/filename.ext" and returns the path por...
Definition: String.hpp:45
std::string Replace(std::string s, const std::string &search, const std::string &replace)
Replaces in s all occurrences of search by replace, and returns the new string.
Definition: String.hpp:56
std::string ToLuaIdentifier(std::string s)
Modifies the given string s as necessary to turn it into a valid Lua 5.2 identifier and returns the r...
Definition: String.hpp:73
std::string StripExt(std::string s)
Assumes that the given string s is a filename, removes the extension, if any, and returns the rest...
Definition: String.hpp:32