-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcre8.cpp
169 lines (145 loc) · 3.62 KB
/
pcre8.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
#include "pcre8.h"
#include "recap8.h"
#include "parameter.h"
#include <ostream>
#include "ustr8.h"
using namespace pun;
const char* Pcre8::PHP_NAME = "Pun\\Preg";
void
Pcre8::setup_ext(Php::Extension& ext)
{
Php::Class<Pcre8> preg(Pcre8::PHP_NAME);
preg.method<&Pcre8::__construct>("__construct");
preg.method<&Pcre8::setPreg> ("setPreg");
preg.method<&Pcre8::getPreg> ("getPreg");
preg.method<&Pcre8::getId> ("getId");
preg.method<&Pcre8::isCompiled> ("isCompiled");
preg.method<&Pcre8::match> ("match");
preg.method<&Pcre8::matchAll> ("matchAll");
ext.add(std::move(preg));
}
Pcre8::Pcre8()
{
}
Pcre8::~Pcre8()
{
}
void Pcre8::__construct(Php::Parameters& params)
{
(*this).setPreg(params);
}
Pcre8_share
Pcre8::fromParameters(Php::Parameters& params)
{
int index = pun::check_IntString(params);
const char* str = params[1];
auto stringSize = params[1].size();
Pcre8_share sp = pun::makeSharedRe(index, str, stringSize);
return sp;
}
/** param 0 - id, param 1 - string pcre2 */
void Pcre8::setPreg(Php::Parameters& params)
{
_imp = Pcre8::fromParameters(params);
}
void Pcre8::setImp(const Pcre8_share& sptr) {
_imp = sptr;
}
Php::Value Pcre8::getPreg() const
{
auto pimp = _imp.get();
return Php::Value(pimp->_eStr);
}
Php::Value Pcre8::getId() const
{
auto pimp = _imp.get();
return Php::Value(pimp->_id);
}
Php::Value Pcre8::isCompiled() const
{
return Php::Value(_imp.get()->isCompiled());
}
Php::Value
Pcre8::matchAll(Php::Parameters& param)
{
UStr8* u8 = nullptr;;
Pcre8_matchAll matchSet;
svx::string_view target;
bool checked = true;
if (param.size() < 1) {
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) {
throw Php::Exception("Empty target string argument");
}
auto sre = _imp.get();
//Php::out << "Target " << target << std::endl;
auto rct = sre->doMatchAll(target, matchSet);
Php::Array result;
if (rct > 0) {
for(int i = 0; i < rct; i++) {
Recap8* cap = new Recap8();
cap->_match = std::move(matchSet[i]);
result[i] = Php::Object(Recap8::PHP_NAME, cap);
}
}
return result;
}
Php::Value
Pcre8::match(Php::Parameters& param)
{
svx::string_view target;
bool check = param.size() >= 1;
if (check) {
Php::Value& v = param[0];
if (v.isString()) {
target = svx::string_view(v, v.size());
}
else if (v.isObject())
{
UStr8* u8 = UStr8::get_UStr8(v);
if (u8 != nullptr)
{
target = u8->fn_getView();
}
}
check = target.size() > 0;
}
if (!check) {
throw Php::Exception("Need non-empty string or UStr8 for 1st argument");
}
Php::Value result;
Recap8* cap = nullptr;
if (param.size() >= 2) {
Php::Value& v = param[1];
if (v.isObject()) {
cap = Recap8::get_Recap8(v);
if (cap != nullptr) {
result = v;
}
}
}
if (cap == nullptr) {
cap = new Recap8();
result = Php::Object(Recap8::PHP_NAME, cap);
}
Pcre8_match matches;
auto pimp = _imp.get();
pimp->doMatch(target, matches);
cap->_match = std::move(matches);
return result;
}