-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregex.cpp
More file actions
152 lines (130 loc) · 3.49 KB
/
regex.cpp
File metadata and controls
152 lines (130 loc) · 3.49 KB
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
/*
* Covariant Script Regex Extension
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright (C) 2017-2023 Michael Lee(李登淳)
*
* Email: lee@covariant.cn, mikecovlee@163.com
* Github: https://github.com/mikecovlee
* Website: http://covscript.org.cn
*/
#include <covscript/cni.hpp>
#include <covscript/dll.hpp>
#include "pcre2.hpp"
static cs::namespace_t regex_ext = cs::make_shared_namespace<cs::name_space>();
static cs::namespace_t regex_result_ext = cs::make_shared_namespace<cs::name_space>();
namespace cs_impl {
template <>
cs::namespace_t &get_ext<pcre2_regex_t>()
{
return regex_ext;
}
template <>
cs::namespace_t &get_ext<pcre2_smatch>()
{
return regex_result_ext;
}
template <>
constexpr const char *get_name_of_type<pcre2_regex_t>()
{
return "cs::regex";
}
template <>
constexpr const char *get_name_of_type<pcre2_smatch>()
{
return "cs::regex::result";
}
} // namespace cs_impl
namespace regex_cs_ext {
using namespace cs;
pcre2_regex_t build(const string &str)
{
return std::make_shared<pcre2_regex>(str, false);
}
pcre2_regex_t build_optimize(const string &str)
{
return std::make_shared<pcre2_regex>(str, true);
}
pcre2_smatch match(pcre2_regex_t ®, const string &str)
{
return pcre2_regex_match(reg, str, PCRE2_ANCHORED | PCRE2_ENDANCHORED);
}
pcre2_smatch search(pcre2_regex_t ®, const string &str)
{
return pcre2_regex_match(reg, str, 0);
}
string replace(pcre2_regex_t ®, const string &str, const string &fmt)
{
return pcre2_regex_replace(reg, str, fmt);
}
bool ready(const pcre2_smatch &m)
{
return m.ready;
}
bool empty(const pcre2_smatch &m)
{
return m.empty();
}
numeric size(const pcre2_smatch &m)
{
return m.size();
}
numeric length(const pcre2_smatch &m, numeric index)
{
return m.str(index.as_integer()).size();
}
numeric position(const pcre2_smatch &m, numeric index)
{
return m.position(index.as_integer());
}
string str(const pcre2_smatch &m, numeric index)
{
return string(m.str(index.as_integer()));
}
string prefix(const pcre2_smatch &m)
{
return m.prefix();
}
string suffix(const pcre2_smatch &m)
{
return m.suffix();
}
void init(name_space *ns)
{
(*ns)
.add_var("result", make_namespace(regex_result_ext))
.add_var("build", make_cni(build))
.add_var("build_optimize", make_cni(build_optimize))
.add_var("match", make_cni(match))
.add_var("search", make_cni(search))
.add_var("replace", make_cni(replace));
(*regex_ext)
.add_var("match", make_cni(match))
.add_var("search", make_cni(search))
.add_var("replace", make_cni(replace));
(*regex_result_ext)
.add_var("ready", make_cni(ready))
.add_var("empty", make_cni(empty))
.add_var("size", make_cni(size))
.add_var("length", make_cni(length))
.add_var("position", make_cni(position))
.add_var("str", make_cni(str))
.add_var("prefix", make_cni(prefix))
.add_var("suffix", make_cni(suffix));
}
} // namespace regex_cs_ext
void cs_extension_main(cs::name_space *ns)
{
regex_cs_ext::init(ns);
}