7 #ifndef CAFU_STRING_HPP_INCLUDED
8 #define CAFU_STRING_HPP_INCLUDED
19 inline bool EndsWith(
const std::string& String,
const std::string& Suffix)
21 const size_t StringLength=String.length();
22 const size_t SuffixLength=Suffix.length();
24 if (StringLength<SuffixLength)
return false;
26 std::string StringSuffix=&(String.c_str()[StringLength-SuffixLength]);
28 return StringSuffix==Suffix;
34 const size_t PosDot=s.find_last_of(
'.');
35 const size_t PosSep=s.find_last_of(
"/\\");
37 if (PosDot==std::string::npos)
return s;
38 if (PosSep!=std::string::npos && PosDot<PosSep)
return s;
47 const size_t PosSep=s.find_last_of(
"/\\");
49 if (PosSep==std::string::npos)
return "";
56 inline std::string
Replace(std::string s,
const std::string& search,
const std::string& replace)
58 const size_t len_search =search.length();
if (len_search==0)
return s;
59 const size_t len_replace=replace.length();
61 size_t pos=s.find(search);
63 while (pos!=std::string::npos)
65 s.replace(pos, len_search, replace);
66 pos=s.find(search, pos+len_replace);
84 for (
size_t i = 0; i < s.length(); i++)
86 if (isalnum(s[i]) && s[i] < 128)
continue;
87 if (s[i] ==
'_')
continue;
93 const char* Keywords[] =
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
101 for (
unsigned int i = 0; Keywords[i]; i++)
102 if (s == Keywords[i])
113 for (i = 1; i < s.length(); i++)
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