-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathre8map.cpp
196 lines (172 loc) · 4.18 KB
/
re8map.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "re8map.h"
#include "pcre8.h"
#include "ucode8.h"
#include "parameter.h"
#include "ustr8.h"
#include "idlist.h"
#include "recap8.h"
#include <sstream>
using namespace pun;
const char* Re8map::PHP_NAME = "Pun\\Re8map";
void
Re8map::setup_ext(Php::Extension& ext)
{
Php::Class<Re8map> re8(Re8map::PHP_NAME);
re8.method<&Re8map::setPreg> ("setPreg");
re8.method<&Re8map::hasPreg> ("hasPreg");
re8.method<&Re8map::unsetPreg> ("unsetPreg");
re8.method<&Re8map::getPreg> ("getPreg");
re8.method<&Re8map::addMapIds> ("addMapIds");
re8.method<&Re8map::getIds> ("getIds");
re8.method<&Re8map::count> ("count");
re8.method<&Re8map::firstMatch> ("firstMatch");
ext.add(std::move(re8));
}
Re8map::Re8map()
{
_remap = std::make_shared<Pcre8_map>();
}
Re8map::~Re8map()
{
}
Php::Value
Re8map::setPreg(Php::Parameters& params)
{
auto sp = Pcre8::fromParameters(params);
_remap.get()->setRex(sp);
return Php::Value(params[0]);
}
Php::Value
Re8map::getIds() const
{
Php::Value result;
auto map = _remap.get();
auto m1 = map->_map.begin();
auto mend = map->_map.end();
int i = 0;
while(m1 != mend) {
result[i] = Php::Value(m1->first);
++i;
++m1;
}
return result;
}
Php::Value
Re8map::getPreg(Php::Parameters& params)
{
pun::check_Int(params);
int index = params[0];
Pcre8_share sp;
auto map = _remap.get();
if (!map->getRex(index,sp))
{
return Php::Value(false);
}
auto p8 = new Pcre8();
p8->setImp(sp);
auto result = Php::Object(Pcre8::PHP_NAME, p8);
return result;
}
Php::Value Re8map::addMapIds(Php::Parameters& params)
{
int mapIndex;
int shared = 0;
Re8map* obj = pun::check_Re8map(params,0);
auto mFrom = obj->getImp().get();
auto mTo = _remap.get();
if (pun::option_Array(params,1)) {
auto& Ar = params[1];
for(auto& iter : Ar)
{
mapIndex = iter.second;
if (mFrom->hasKey(mapIndex) && !mTo->hasKey(mapIndex)) {
Pcre8_share fresh;
mFrom->getRex(mapIndex,fresh);
mTo->setRex(fresh);
shared++;
}
}
}
else {
// Assign all the keys
auto pit = mFrom->_map.begin();
auto pend = mFrom->_map.end();
while(pit != pend) {
mapIndex = pit->first;
if (mFrom->hasKey(mapIndex) && !mTo->hasKey(mapIndex)) {
Pcre8_share fresh;
mFrom->getRex(mapIndex,fresh);
mTo->setRex(fresh);
shared++;
}
}
}
return Php::Value(shared);
}
// Has key value in map
Php::Value Re8map::count() const
{
auto map = _remap.get();
return Php::Value((int)map->_map.size());
}
// Has key value in map
Php::Value Re8map::hasPreg(Php::Parameters& params) const
{
pun::check_Int(params);
int index = params[0];
auto map = _remap.get();
return Php::Value(map->hasKey(index));
}
// Remove key value and expression data from map
Php::Value Re8map::unsetPreg(Php::Parameters& params)
{
pun::check_Int(params);
int index = params[0];
auto map = _remap.get();
return Php::Value(map->eraseRex(index));
}
Php::Value
Re8map::firstMatch(Php::Parameters& param)
{
UStr8* u8 = nullptr;;
Recap8* caps = nullptr;;
IntList* intlist = nullptr;
IdList ids;
svx::string_view target;
bool checked = true;
if (param.size() < 3) {
checked = false;
}
if (checked) {
Php::Value& v = param[0];
if (v.isObject()) {
u8 = UStr8::get_UStr8(v);
if (u8 != nullptr) {
target = u8->fn_getView();
}
}
else if (v.isString())
{
target = svx::string_view(v, v.size());
}
checked = target.size() > 0;
}
if (checked && (caps = Recap8::get_Recap8(param[1])) == nullptr) {
checked = false;
}
if (checked) {
Php::Value& idArray = param[2];
if (idArray.isArray()) {
ids = toIdList(idArray);
}
else {
checked = ((intlist = IntList::get_IntList(param[2])) != nullptr);
}
}
if (!checked) {
throw Php::Exception("firstMatch(UStr8,Recap8,List<int>) parameter missing");
}
auto spm = _remap.get();
const IdList& tests = (intlist==nullptr) ? ids : intlist->_store;
return spm->firstMatch(target, tests, caps->_match );
}