Phasor  01.00.10.059
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MyString.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <vector>
5 #include <stdarg.h>
6 
7 std::string NarrowString(const std::wstring& wide);
8 std::wstring WidenString(const std::string& narrow);
9 // Remove all trailing \n characters from the input string.
10 std::wstring StripTrailingEndl(const std::wstring& str);
11 std::string StripTrailingEndl(const std::string& str);
12 
13 void ToLowercase(std::string& str);
14 void ToLowercase(std::wstring& str);
15 void CStrToLower(char* str);
16 
17 std::string FormatVarArgs(const char* fmt, va_list marker);
18 std::wstring FormatVarArgsW(const wchar_t* fmt, va_list marker);
19 std::string m_sprintf(const char* _Format, ...);
20 std::wstring m_swprintf(const wchar_t* _Format, ...);
21 
22 //
23 // Number functions
24 // -----------------------------------------------------------------------
25 // Helper function for StringToNumber
26 template <class T>
27 T _StringToNumber(const char* start, char** end);
28 
29 // Parse the input string as a number. If there are any characters that
30 // would produce a malformed number, false is returned.
31 template <class T>
32 bool StringToNumber(const std::string& str, T& out)
33 {
34  const char* start = str.c_str(), *expected_end = start + str.size();
35  char* end;
36  T value = _StringToNumber<T>(start, &end);
37  if (end != expected_end) return false;
38  out = value;
39  return true;
40 }
41 
42 //
43 // Tokenization functions
44 // -----------------------------------------------------------------------
45 
46 // Get the substring ending at the next occurrence of c.
47 // start is the position (inclusive) where to start searching from.
48 // end is the position after the next occurrence, or npos if none.
49 template <class T, class _Tc>
50 T GetStringEndingAtNext(const T& input, _Tc c, size_t start, size_t& end);
51 template <class T>
52 const T ArgsSearchString();
53 
54 // Tokenize a string into its constituent arguments, an argument
55 // ends at a space unless within ' or " in which case it ends at the
56 // next escaping ' or ".
57 template <class T, class _Tc>
58 std::vector<T> TokenizeArgsT(const T& in)
59 {
60  using namespace std;
61  vector<T> out;
62  const T tofind = ArgsSearchString<T>(); // " ' or space
63 
64  size_t curpos = 0;
65  while (curpos != in.npos)
66  {
67  curpos = in.find_first_not_of(_Tc(' '), curpos);
68  if (curpos == in.npos) break;
69  size_t nextpos = in.find_first_of(tofind, curpos);
70  if (nextpos == in.npos) { // no more matches, copy everything.
71  out.push_back(in.substr(curpos, in.npos));
72  break;
73  }
74  _Tc c = in.at(nextpos);
75  size_t startfrom = c == _Tc(' ') ? curpos : curpos + 1;
76  T token = GetStringEndingAtNext<T, _Tc>(in, c, startfrom, curpos);
77  if (token.size()) out.push_back(token);
78  }
79  return out;
80 }
81 
82 typedef std::vector<std::string> (*tokargs_t)(const std::string&);
83 typedef std::vector<std::wstring> (*tokargsw_t)(const std::wstring&);
84 tokargs_t const TokenizeArgs = &TokenizeArgsT<std::string, char>;
85 tokargsw_t const TokenizeWArgs = &TokenizeArgsT<std::wstring, wchar_t>;
86 
87 template <class T>
88 std::vector<T> Tokenize(const T& str, const T& delim)
89 {
90  // http://www.gamedev.net/community/forums/topic.asp?topic_id=381544#TokenizeString
91  std::vector<T> tokens;
92  size_t p0 = 0, p1 = T::npos;
93  while (p0 != T::npos) {
94  p1 = str.find_first_of(delim, p0);
95  if(p1 != p0) {
96  T token = str.substr(p0, p1 - p0);
97  tokens.push_back(token);
98  }
99  p0 = str.find_first_not_of(delim, p1);
100  }
101  return tokens;
102 }