ADTF
regularexpression_helper.h
Go to the documentation of this file.
1 
7 #ifndef _REGULAR_EXPRESSION_HELPER_CLASS_HEADER_
8 #define _REGULAR_EXPRESSION_HELPER_CLASS_HEADER_
9 
10 namespace A_UTILS_NS
11 {
12 
18 class DOEXPORT cStringPiece
19 {
21 
23  {
24  friend class cStringPiece;
25  public:
27  virtual ~cStringPiecePrivate(){};
28  };
29 
30  private:
31  const tChar* ptr_;
33 
34  public:
35  // We provide non-explicit singleton constructors so users can pass
36  // in a "const tChar*" or a "string" wherever a "cStringPiece" is
37  // expected.
42  : ptr_(nullptr)
43  , length_(0)
44  {
45  }
46 
51  cStringPiece(const cStringPiece& i_oOther) :
52  _d(), //currently no need to copy _d, not used
53  ptr_(i_oOther.GetPtr()),
54  length_(i_oOther.GetSize())
55  {
56  }
57 
63  cStringPiece& operator= (const cStringPiece& i_oOther)
64  {
65  if (this != &i_oOther)
66  {
67  _d = nullptr; //currently no need to copy _d, not used
68  ptr_ = i_oOther.GetPtr();
69  length_ = i_oOther.GetSize();
70  }
71  return *this;
72  }
73 
79  cStringPiece(const tChar* str)
80  : ptr_(str)
81  , length_(static_cast<tInt>(cStringUtil::GetLength(str)))
82  {
83  }
84 
90  cStringPiece(const cString& str)
91  : ptr_(str.GetPtr())
92  , length_(static_cast<tInt>(str.GetLength()))
93  {
94  }
95 
102  cStringPiece(const tChar* offset, tInt len)
103  : ptr_(offset)
104  , length_(len)
105  {
106  }
107 
119  const tChar* GetPtr() const
120  {
121  return ptr_;
122  }
123 
129  tInt GetSize() const
130  {
131  return length_;
132  }
133 
139  tBool IsEmpty() const
140  {
141  return length_ == 0;
142  }
143 
148  {
149  ptr_ = nullptr;
150  length_ = 0;
151  }
152 
160  tVoid Set(const tChar* buffer, tInt len)
161  {
162  ptr_ = buffer;
163  length_ = len;
164  }
165 
172  tVoid Set(const tChar* str)
173  {
174  if (nullptr != str)
175  {
176  ptr_ = str;
177  length_ = static_cast<tInt>(cStringUtil::GetLength(str));
178  }
179  }
180 
188  tVoid Set(const tVoid* buffer, tInt len)
189  {
190  if ((nullptr != buffer)
191  && (len >=0))
192  {
193  ptr_ = reinterpret_cast<const tChar*>(buffer);
194  length_ = len;
195  }
196  }
197 
206  {
207  if ((i > 0) &&
208  (i < length_))
209  {
210  return ptr_[i];
211  }
212  return 0;
213  }
214 
222  {
223  if (0 < length_)
224  {
225  ptr_ += n;
226  length_ -= n;
227  }
228  }
229 
237  {
238  if (0 < length_)
239  {
240  length_ -= n;
241  }
242  }
243 
251  tBool operator==(const cStringPiece& x) const
252  {
253  return ((length_ == x.length_) && (cMemoryBlock::MemCmp(ptr_, x.ptr_, length_) == 0));
254  }
255 
263  tBool operator!=(const cStringPiece& x) const
264  {
265  return !(*this == x);
266  }
267 
268  #define STRINGPIECE_BINARY_PREDICATE(cmp,auxcmp)\
269  tBool operator cmp (const cStringPiece& x) const {\
270  tInt r = cMemoryBlock::MemCmp(ptr_, x.ptr_, length_ < x.length_ ? length_ : x.length_);\
271  return ((r auxcmp 0) || ((r == 0) && (length_ cmp x.length_)));\
272  }
301  #undef STRINGPIECE_BINARY_PREDICATE
302 
310  tInt Compare(const cStringPiece& x) const
311  {
312  tInt r = cMemoryBlock::MemCmp(ptr_, x.ptr_, length_ < x.length_ ? length_ : x.length_);
313  if (r == 0)
314  {
315  if (length_ < x.length_)
316  {
317  r = -1;
318  }
319  else if (length_ > x.length_)
320  {
321  r = +1;
322  }
323  }
324  return r;
325  }
326 
333  {
334  if (nullptr == GetPtr())
335  {
336  return cString::Empty;
337  }
338  cString strTmp;
339  CopyToString(&strTmp);
340  return strTmp;
341  }
342 
350  tVoid CopyToString(cString* target) const
351  {
352  if ((nullptr != ptr_) &&
353  (nullptr != target))
354  {
355  target->Set(ptr_, length_);
356  }
357  }
358 
359  // Does "this" start with "x"
367  tBool StartsWith(const cStringPiece& x) const
368  {
369  if ((nullptr == GetPtr())
370  || (nullptr == x.GetPtr()))
371  {
372  return tFalse;
373  }
374  return ((length_ >= x.length_) && (cMemoryBlock::MemCmp(ptr_, x.ptr_, x.length_) == 0));
375  }
376 };
377 
379 typedef tBool (*fnRegExpParser)(const tChar* str, tInt n, tVoid* dest);
380 
381 
383 template<bool B, class T = void>
385 {
386 };
387 
391 template<class T>
392 struct a_utils_enable_if<true, T>
393 {
395  typedef T type;
396 };
397 
399 template<class T>
401 {
403  static const tSize nSize = sizeof(T);
404 };
405 
409 template<>
411 {
413  static const tSize nSize = 0;
414 };
415 
421 class DOEXPORT cRegularExpressionArg
422 {
423  private:
424  tVoid* arg_;
425  fnRegExpParser parser_;
426 
427  static tBool parse_null (const tChar* str, tInt n, tVoid* dest);
428  static tBool parse_tChar (const tChar* str, tInt n, tVoid* dest);
429  static tBool parse_tFloat32 (const tChar* str, tInt n, tVoid* dest);
430  static tBool parse_tFloat64 (const tChar* str, tInt n, tVoid* dest);
431  static tBool parse_stdstring (const tChar* str, tInt n, tVoid* dest);
432 
433  static tBool parse_string (const tChar* str, tInt n, tVoid* dest);
434  static tBool parse_stringpiece(const tChar* str, tInt n, tVoid* dest);
435  static tBool A_UTILS_DEPRECATED(parse_int)(const tChar* str, tInt n, tVoid* dest);
436 
437  #define AEVREGEXP_DECLARE_INTEGER_PARSER(name)\
438  private:\
439  static tBool parse_ ## name(const tChar* str, tInt n, tVoid* dest);\
440  static tBool parse_ ## name ## _radix(\
441  const tChar* str, tInt n, tVoid* dest, tInt radix);\
442  public:\
443  static tBool parse_ ## name ## _hex(const tChar* str, tInt n, tVoid* dest);\
444  static tBool parse_ ## name ## _octal(const tChar* str, tInt n, tVoid* dest);\
445  static tBool parse_ ## name ## _cradix(const tChar* str, tInt n, tVoid* dest)
446 
447  AEVREGEXP_DECLARE_INTEGER_PARSER(tInt8);
448  AEVREGEXP_DECLARE_INTEGER_PARSER(tUInt8);
449  AEVREGEXP_DECLARE_INTEGER_PARSER(tInt16);
450  AEVREGEXP_DECLARE_INTEGER_PARSER(tUInt16);
451  AEVREGEXP_DECLARE_INTEGER_PARSER(tInt32);
452  AEVREGEXP_DECLARE_INTEGER_PARSER(tUInt32);
453  AEVREGEXP_DECLARE_INTEGER_PARSER(tInt64);
454  AEVREGEXP_DECLARE_INTEGER_PARSER(tUInt64);
455 
456  #undef AEVREGEXP_DECLARE_INTEGER_PARSER
457 
458  public:
462  cRegularExpressionArg(): arg_(nullptr), parser_(parse_null)
463  {
464  }
465 
473  {
474  arg_ = pData;
475  parser_ = pParser;
476  }
477 
485  tVoid Set(fnRegExpParser pParser, tVoid* pData)
486  {
487  arg_ = pData;
488  parser_ = pParser;
489  }
490 
496  tVoid* GetData() const { return arg_; };
502  fnRegExpParser GetParser() const { return parser_; };
503 
504  // Type-specific parsers
505  #define REG_EXP_CONST_INTEGER(size, name) \
506  template<class T> \
507  cRegularExpressionArg(T* pRef, fnRegExpParser parser = name, \
508  typename a_utils_enable_if<std::numeric_limits<T>::is_integer, T>::type* = nullptr, \
509  typename a_utils_enable_if<std::numeric_limits<T>::is_signed, T>::type* = nullptr, \
510  typename a_utils_enable_if<a_utils_size_of_helper<T>::nSize == size, T>::type* = nullptr) \
511  : arg_(pRef), parser_(parser) {}
512 
513  #define REG_EXP_CONST_UNSIGNED_INTEGER(size, name) \
514  template<class T> \
515  cRegularExpressionArg(T* pRef, fnRegExpParser parser = name, \
516  typename a_utils_enable_if<std::numeric_limits<T>::is_integer, T>::type* = nullptr, \
517  typename a_utils_enable_if<!std::numeric_limits<T>::is_signed, T>::type* = nullptr, \
518  typename a_utils_enable_if<a_utils_size_of_helper<T>::nSize == size, T>::type* = nullptr) \
519  : arg_(pRef), parser_(parser) {}
520 
521  #define REG_EXP_CONST_FLOAT(size, name) \
522  template<class T> \
523  cRegularExpressionArg(T* pRef, fnRegExpParser parser = name, \
524  typename a_utils_enable_if<!std::numeric_limits<T>::is_integer, T>::type* = nullptr, \
525  typename a_utils_enable_if<a_utils_size_of_helper<T>::nSize == size, T>::type* = nullptr) \
526  : arg_(pRef), parser_(parser) {}
527 
528  REG_EXP_CONST_INTEGER(1, parse_tInt8)
529  REG_EXP_CONST_UNSIGNED_INTEGER(1, parse_tUInt8)
530  REG_EXP_CONST_INTEGER(2, parse_tInt16)
531  REG_EXP_CONST_UNSIGNED_INTEGER(2, parse_tUInt16)
532  REG_EXP_CONST_INTEGER(4, parse_tInt32)
533  REG_EXP_CONST_UNSIGNED_INTEGER(4, parse_tUInt32)
534  REG_EXP_CONST_INTEGER(8, parse_tInt64)
535  REG_EXP_CONST_UNSIGNED_INTEGER(8, parse_tUInt64)
536 
537  REG_EXP_CONST_FLOAT(4, parse_tFloat32)
538  REG_EXP_CONST_FLOAT(8, parse_tFloat64)
539 
540 
545  cRegularExpressionArg(tChar* p) : arg_(p), parser_(parse_tChar) {}
546 
552  cRegularExpressionArg(cString* p) : arg_(p), parser_(parse_string) {}
553 
559  cRegularExpressionArg(cStringPiece* p) : arg_(p), parser_(parse_stringpiece) {}
560 
566  cRegularExpressionArg(tVoid* pRef): arg_(pRef), parser_(parse_null) {}
567 
568  #undef REG_EXP_CONST_FLOAT
569  #undef REG_EXP_CONST_UNSIGNED_INTEGER
570  #undef REG_EXP_CONST_INTEGER
571 
580  tBool Parse(const tChar* str, tInt n) const
581  {
582  return (*parser_)(str, n, arg_);
583  }
584 };
585 
587 #define A_UTILS_MAKE_INTEGER_PARSER_HELPER(size, parser, func, parser_name) \
588 template<class T> \
589 inline cRegularExpressionArg func(T* pArg, \
590  typename a_utils_enable_if<std::numeric_limits<T>::is_integer, T>::type* = nullptr, \
591  typename a_utils_enable_if<std::numeric_limits<T>::is_signed, T>::type* = nullptr, \
592  typename a_utils_enable_if<a_utils_size_of_helper<T>::nSize == size, T>::type* = nullptr) \
593 { \
594  return cRegularExpressionArg(pArg, cRegularExpressionArg::parse_tInt ## parser ## parser_name); \
595 } \
596 
597 #define A_UTILS_MAKE_INTEGER_PARSER(size, parser) \
598  A_UTILS_MAKE_INTEGER_PARSER_HELPER(size, parser, Hex, _hex) \
599  A_UTILS_MAKE_INTEGER_PARSER_HELPER(size, parser, Octal, _octal) \
600  A_UTILS_MAKE_INTEGER_PARSER_HELPER(size, parser, CRadix, _cradix)
601 
602 #define A_UTILS_MAKE_UNSIGNED_INTEGER_PARSER_HELPER(size, parser, func, parser_name) \
603 template<class T> \
604 inline cRegularExpressionArg func(T* pArg, \
605  typename a_utils_enable_if<std::numeric_limits<T>::is_integer, T>::type* = nullptr, \
606  typename a_utils_enable_if<!std::numeric_limits<T>::is_signed, T>::type* = nullptr, \
607  typename a_utils_enable_if<a_utils_size_of_helper<T>::nSize == size, T>::type* = nullptr) \
608 { \
609  return cRegularExpressionArg(pArg, cRegularExpressionArg::parse_tUInt ## parser ## parser_name); \
610 } \
611 
612 #define A_UTILS_MAKE_UNSIGNED_INTEGER_PARSER(size, parser) \
613  A_UTILS_MAKE_UNSIGNED_INTEGER_PARSER_HELPER(size, parser, Hex, _hex) \
614  A_UTILS_MAKE_UNSIGNED_INTEGER_PARSER_HELPER(size, parser, Octal, _octal) \
615  A_UTILS_MAKE_UNSIGNED_INTEGER_PARSER_HELPER(size, parser, CRadix, _cradix)
616 
618 A_UTILS_MAKE_INTEGER_PARSER(1, 8)
619 A_UTILS_MAKE_UNSIGNED_INTEGER_PARSER(1, 8)
620 A_UTILS_MAKE_INTEGER_PARSER(2, 16)
621 A_UTILS_MAKE_UNSIGNED_INTEGER_PARSER(2, 16)
622 A_UTILS_MAKE_INTEGER_PARSER(4, 32)
623 A_UTILS_MAKE_UNSIGNED_INTEGER_PARSER(4, 32)
624 A_UTILS_MAKE_INTEGER_PARSER(8, 64)
625 A_UTILS_MAKE_UNSIGNED_INTEGER_PARSER(8, 64)
627 
628 #undef A_UTILS_MAKE_INTEGER_PARSER
629 #undef A_UTILS_MAKE_INTEGER_PARSER_HELPER
630 #undef A_UTILS_MAKE_UNSIGNED_INTEGER_PARSER
631 #undef A_UTILS_MAKE_UNSIGNED_INTEGER_PARSER_HELPER
632 
633 } // namespace A_UTILS_NS
634 
635 #endif // _REGULAR_EXPRESSION_HELPER_CLASS_HEADER_
uint8_t tUInt8
type definition for unsigned integer values (8bit) (platform and compiler independent type).
char tChar
The tChar defines the type for platform character set (platform and compiler dependent type).
int64_t tInt64
type definition for signed integer values (64bit) (platform and compiler independent type).
int16_t tInt16
type definition for signed integer values (16bit) (platform and compiler independent type).
int32_t tInt32
type definition for signed integer values (32bit) (platform and compiler independent type).
void tVoid
The tVoid is always the definition for the void (non-type).
uint16_t tUInt16
type definition for unsigned integer values (16bit) (platform and compiler independent type).
int tInt
type definition for signed integer value (platform and compiler dependent type).
bool tBool
The tBool defines the type for the Values tTrue and tFalse (platform and compiler dependent).
uint32_t tUInt32
type definition for unsigned integer values (32bit) (platform and compiler independent type).
size_t tSize
type definition for a array size values, map size values etc.
int8_t tInt8
type definition for signed integer values (8bit) (platform and compiler independent type).
uint64_t tUInt64
type definition for unsigned integer values (64bit) (platform and compiler independent type).
static tInt MemCmp(const tVoid *pMem1, const tVoid *pMem2, tSize nCount)
Compares the data bytes of two memory buffers.
Regular expression argument class.
fnRegExpParser GetParser() const
Retrieve the parser.
tVoid Set(fnRegExpParser pParser, tVoid *pData)
Assign parser and data.
cRegularExpressionArg(fnRegExpParser pParser, tVoid *pData)
Copy constructor.
cRegularExpressionArg()
Empty constructor so we can declare arrays of cRegularExpressionArg.
tBool Parse(const tChar *str, tInt n) const
Parse the given string.
tVoid * GetData() const
Retrieve arguments.
Class that represents string pieces.
tVoid RemovePrefix(tInt n)
Remove prefix of given length.
tVoid CopyToString(cString *target) const
Return string piece as standard string.
tInt Compare(const cStringPiece &x) const
Compare two string pieces.
STRINGPIECE_BINARY_PREDICATE(<,<)
Compare two string pieces.
tBool operator==(const cStringPiece &x) const
Compare two string pieces for equality.
STRINGPIECE_BINARY_PREDICATE(>, >)
Compare two string pieces.
const tChar * GetPtr() const
Retrieve character pointer.
tVoid RemoveSuffix(tInt n)
Remove suffix of given length.
cStringPiece(const cString &str)
Constructor.
STRINGPIECE_BINARY_PREDICATE(<=,<)
Compare two string pieces.
const tChar * ptr_
Character pointer.
tBool StartsWith(const cStringPiece &x) const
Check if this string piece starts with the given string piece.
tChar operator[](tInt i) const
Retrieve character at the given position.
cStringPiece()
Default constructor.
tVoid Set(const tChar *str)
Assigns the string to the string piece.
cString AsString() const
Return string piece as standard string.
tVoid Clear()
Clears the string piece.
cStringPiece(const tChar *str)
Constructor.
tVoid Set(const tChar *buffer, tInt len)
Assigns the buffer to the string piece.
STRINGPIECE_BINARY_PREDICATE(>=, >)
Compare two string pieces.
tBool IsEmpty() const
Check if string piece is empty.
cStringPiece(const tChar *offset, tInt len)
Constructor.
cStringPiece(const cStringPiece &i_oOther)
Copy constructor.
tInt GetSize() const
Retrieve the size of the string piece.
tVoid Set(const tVoid *buffer, tInt len)
Assigns the buffer to the string piece.
tBool operator!=(const cStringPiece &x) const
Compare two string pieces for inequality.
String Helper Class.
Definition: stringutil.h:22
static tSize GetLength(const tChar *pcStr)
Returns the length of the string.
Template to implement the Private class of the global d_pointer definitions.
Definition: d_ptr.h:22
_myType & Set(const _myType &strStringToSet, tSize nLength=InvalidPos)
Assigns a new value to a cStringA object.
Definition: string.h:246
static const _myType Empty
Internally used empty string.
Definition: string.h:54
#define tFalse
Value for tBool.
Definition: constants.h:60
#define A_UTILS_DEPRECATED(_func_)
Mark a function or variable as deprecated.
Definition: constants.h:267
#define A_UTILS_D(__pclassname_)
Helper macro for d-pattern definitions.
Definition: d_ptr.h:270
ADTF A_UTIL Namespace - Within adtf this is used as adtf::util or adtf_util.
Definition: d_ptr.h:11
tBool(* fnRegExpParser)(const tChar *str, tInt n, tVoid *dest)
Type of regular expression parser function.
template for SFINAE selection of the right constructor
template for handling sizeof(void) in SFINAE below (required for Visual Studio)
static const tSize nSize
the size of template class