Xpace
matchstring.h
Go to the documentation of this file.
1 
2 /**********************************************************//**
3  **
4  ** @file util/matchstring.h
5  **
6  ** Copyright (C) 2012 Xpace, LLC. All rights reserved
7  **
8  ** www.xpace.net
9  **
10  **************************************************************/
11 
12 #ifndef XPACE_MATCHSTRING_H
13 #define XPACE_MATCHSTRING_H
14 
15 #include <unordered_map>
16 
17 namespace Xpace
18 {
19  template <typename FUNCTION>
20  struct IntWrapper
21  {
23  (FUNCTION* f) : f(f)
24  {
25  }
26 
27  bool operator()
28  (int64 n)
29  {
30  return (*f)(n);
31  }
32 
33  FUNCTION* f;
34  };
35 
36  template <typename STR>
38  {
39  public:
40  typedef typename STR::charType CH;
41 
42  //typedef std::function<bool(CH*, size_t)> string_func;
43  typedef std::function<bool(int64)> intFunc;
44 
46  (const String& fmt);
47 
48  const STR& getNativeFormat
49  ()
50  const
51  {
52  return format;
53  }
54 
55  const String& getStringFormat
56  ()
57  const
58  {
59  return string_format;
60  }
61 
62  bool add
63  (const CH ch,
64  intFunc fn);
65 
66  bool match
67  (const STR str)
68  const;
69 
70  private:
71  const String string_format;
72 
73  struct fchar
74  {
75  fchar
76  (CH c);
77  CH ch;
78  bool isDelim;
79  };
80  std::vector<fchar> format;
81 
82  std::unordered_map<CH, intFunc> int_funcs;
83 
84  static
85  int64 to_int64
86  (const CH* b,
87  CH* e);
88  };
89 
90  // ==========================================================
91  // ==========================================================
92  // ==========================================================
93 
94  template<>
95  inline
97  (const String& fmt) :
98  string_format(fmt)
99  {
100  auto fv(fmt.toUtf8());
101  for (auto f(fv.begin()); f != fv.end(); ++f)
102  format.push_back(*f);
103  format.push_back(0);
104  }
105 
106  template<>
107  inline
109  (const String& fmt) :
110  string_format(fmt)
111  {
112  #ifndef NDEBUG
113  QString qfmt(fmt);
114  #endif
115 
116  auto f(fmt.toUtf16());
117  do
118  format.push_back(*f);
119  while (*f++);
120  }
121 
122  template <typename STR>
123  inline
125  (const CH ch,
126  intFunc fn)
127  {
128  bool found(false);
129  for (auto f(format.begin()); f != format.end(); ++f)
130  if (f->ch == ch)
131  {
132  f->isDelim = false;
133  found = true;
134  }
135 
136  return (found) ? (int_funcs[ch] = fn, true) : false;
137  }
138 
139  template <typename STR>
140  inline
142  (const STR str)
143  const
144  {
145  auto f(format.begin());
146  auto s(str.data);
147  auto scur(s);
148  auto send(str.data + str.length);
149 
150  CH sub(0);
151 
152  while (f->ch)
153  {
154  while (!f->isDelim)
155  sub = f++->ch;
156  while ((*scur != f->ch) && (scur != send))
157  ++scur;
158  if (sub)
159  {
160  if (!int_funcs.find(sub)->second(to_int64(s, scur)))
161  return false;
162  if (f->ch)
163  ++f;
164  s = ++scur;
165  sub = 0;
166  }
167  else if (f->isDelim)
168  {
169  if (f->ch == *s)
170  {
171  ++f;
172  ++s;
173  ++scur;
174  }
175  else // no match - quit
176  return true;
177  }
178  }
179  return true;
180  }
181 
182  template <typename STR>
183  inline
185  (CH c) :
186  ch(c),
187  isDelim(true)
188  {
189  }
190 
191  template<>
192  inline
194  (const CH* b,
195  CH* e)
196  {
197  return
198  #ifdef _WIN32
199  _strtoi64
200  #else
201  strtoll
202  #endif
203  (reinterpret_cast<const char*>(b), reinterpret_cast<char**>(&e), 10);
204  }
205 
206  template<>
207  inline
209  (const CH* b,
210  CH* e)
211  {
212  return
213  #ifdef _WIN32
214  _wcstoi64
215  #else
216  wcstoll
217  #endif
218  (reinterpret_cast<const wchar_t*>(b), reinterpret_cast<wchar_t**>(&e), 10);
219  }
220 }
221 
222 #endif
MatchString(const String &fmt)
FUNCTION * f
Definition: matchstring.h:33
bool match(const STR str) const
Definition: matchstring.h:142
bool add(const CH ch, intFunc fn)
Definition: matchstring.h:125
A string, Unicode UTF-16 and reference-counted.
Definition: types.h:269
const utf16_t * toUtf16() const
std::function< bool(int64)> intFunc
Definition: matchstring.h:43
IntWrapper(FUNCTION *f)
Definition: matchstring.h:23
STR::charType CH
Definition: matchstring.h:40
long long int64
Definition: types.h:86
Xpace project main namespace
Definition: datetime.h:18
utf8_t * toUtf8(utf8_t *buf, size_t bufLen) const
convert a String into a (null-terminated) UTF-8 string, writing it into a buffer

current as of Wed Jun 10 2026 12:00:05