JODA  0.13.1 (59b41972)
JSON On-Demand Analysis
SubStringProvider.h
Go to the documentation of this file.
1 //
2 // Created by Nico Schäfer
3 //
4 
5 #ifndef JODA_SUBSTRINGPROVIDER_H
6 #define JODA_SUBSTRINGPROVIDER_H
7 
8 #include <regex>
9 
10 #include "IValueProvider.h"
11 namespace joda::query {
16  public:
17  explicit SubStringProvider(
18  std::vector<std::unique_ptr<IValueProvider>> &&parameters);
19  std::unique_ptr<IValueProvider> duplicate() const override;
20  bool isConst() const override;
21 
23  RJMemoryPoolAlloc &alloc) const override;
24  const RJValue *getValue(const RapidJsonDocument &json,
25  RJMemoryPoolAlloc &alloc) const override;
26 
27  bool isAtom() const override;
28  std::string getName() const override;
29  IValueType getReturnType() const override;
30 
32 
33  private:
34  // http://www.zedwood.com/article/cpp-utf-8-mb_substr-function
35  static std::string utf8_substr(const std::string &str, int start,
36  int length = INT_MAX) {
37  {
38  int i, ix, j, realstart, reallength;
39  if (length == 0) return "";
40  if (start < 0 || length < 0) {
41  // find j=utf8_strlen(str);
42  for (j = 0, i = 0, ix = str.length(); i < ix; i += 1, j++) {
43  unsigned char c = str[i];
44  if (c >= 0 && c <= 127)
45  i += 0;
46  else if (c >= 192 && c <= 223)
47  i += 1;
48  else if (c >= 224 && c <= 239)
49  i += 2;
50  else if (c >= 240 && c <= 247)
51  i += 3;
52  else if (c >= 248 && c <= 255)
53  return ""; // invalid utf8
54  }
55  if (length != INT_MAX && j + length - start <= 0) return "";
56  if (start < 0) start += j;
57  if (length < 0) length = j + length - start;
58  }
59 
60  j = 0, realstart = 0, reallength = 0;
61  for (i = 0, ix = str.length(); i < ix; i += 1, j++) {
62  if (j == start) {
63  realstart = i;
64  }
65  if (j >= start && (length == INT_MAX || j <= start + length)) {
66  reallength = i - realstart;
67  }
68  unsigned char c = str[i];
69  if (c >= 0 && c <= 127)
70  i += 0;
71  else if (c >= 192 && c <= 223)
72  i += 1;
73  else if (c >= 224 && c <= 239)
74  i += 2;
75  else if (c >= 240 && c <= 247)
76  i += 3;
77  else if (c >= 248 && c <= 255)
78  return ""; // invalid utf8
79  }
80  if (j == start) {
81  realstart = i;
82  }
83  if (j >= start && (length == INT_MAX || j <= start + length)) {
84  reallength = i - realstart;
85  }
86 
87  return str.substr(realstart, reallength);
88  }
89  }
90 };
91 } // namespace joda::query
92 
93 #endif // JODA_SUBSTRINGPROVIDER_H
#define CREATE_FACTORY(FCLASS)
Definition: IValueProvider.h:20
rapidjson::MemoryPoolAllocator< RJBaseAlloc > RJMemoryPoolAlloc
Definition: RJFwd.h:26
rapidjson::GenericValue< RJChar, RJMemoryPoolAlloc > RJValue
Definition: RJFwd.h:29
Definition: RapidJsonDocument.h:22
Definition: IValueProvider.h:143
Definition: SubStringProvider.h:15
RJValue getAtomValue(const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc) const override
Definition: SubStringProvider.cpp:23
bool isConst() const override
Definition: SubStringProvider.cpp:15
IValueType getReturnType() const override
Definition: SubStringProvider.cpp:78
bool isAtom() const override
Definition: SubStringProvider.cpp:55
std::string getName() const override
Definition: SubStringProvider.cpp:76
SubStringProvider(std::vector< std::unique_ptr< IValueProvider >> &&parameters)
Definition: SubStringProvider.cpp:63
const RJValue * getValue(const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc) const override
Definition: SubStringProvider.cpp:57
std::unique_ptr< IValueProvider > duplicate() const override
Definition: SubStringProvider.cpp:10
Definition: AttributeStatAggregator.h:12
IValueType
Definition: IValueProvider.h:33