1+ /*
2+ * Covariant Script Regex Extension
3+ *
4+ * This program is free software: you can redistribute it and/or modify
5+ * it under the terms of the GNU Affero General Public License as published
6+ * by the Free Software Foundation, either version 3 of the License, or
7+ * (at your option) any later version.
8+ *
9+ * This program is distributed in the hope that it will be useful,
10+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+ * GNU Affero General Public License for more details.
13+ *
14+ * You should have received a copy of the GNU Affero General Public License
15+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
16+ *
17+ * Copyright (C) 2017 Michael Lee(李登淳)
18+ * Email: mikecovlee@163.com
19+ * Github: https://github.com/mikecovlee
20+ */
21+ #include < regex>
22+ #include < covscript/extension.hpp>
23+ #include < covscript/cni.hpp>
24+ #include < covscript/extensions/string.hpp>
25+
26+ static cs::extension regex_ext;
27+ static cs::extension regex_result_ext;
28+ static cs::extension_t regex_ext_shared = cs::make_shared_extension(regex_ext);
29+ static cs::extension_t regex_result_ext_shared = cs::make_shared_extension(regex_result_ext);
30+ namespace cs_impl {
31+ template <>
32+ cs::extension_t &get_ext<std::regex>()
33+ {
34+ return regex_ext_shared;
35+ }
36+
37+ template <>
38+ cs::extension_t &get_ext<std::smatch>()
39+ {
40+ return regex_result_ext_shared;
41+ }
42+
43+ template <>
44+ constexpr const char *get_name_of_type<std::regex>()
45+ {
46+ return " cs::regex" ;
47+ }
48+
49+ template <>
50+ constexpr const char *get_name_of_type<std::smatch>()
51+ {
52+ return " cs::regex::result" ;
53+ }
54+ }
55+ namespace regex_cs_ext {
56+ using namespace cs ;
57+
58+ var build (const string &str)
59+ {
60+ return var::make<std::regex>(str);
61+ }
62+
63+ std::smatch match (std::regex ®, const string &str)
64+ {
65+ std::smatch m;
66+ std::regex_search (str, m, reg);
67+ return std::move (m);
68+ }
69+
70+ std::smatch search (std::regex ®, const string &str)
71+ {
72+ std::smatch m;
73+ std::regex_search (str, m, reg);
74+ return std::move (m);
75+ }
76+
77+ string replace (std::regex ®, const string &str, const string &fmt)
78+ {
79+ return std::regex_replace (str, reg, fmt);
80+ }
81+
82+ bool ready (const std::smatch &m)
83+ {
84+ return m.ready ();
85+ }
86+
87+ bool empty (const std::smatch &m)
88+ {
89+ return m.empty ();
90+ }
91+
92+ number size (const std::smatch &m)
93+ {
94+ return m.size ();
95+ }
96+
97+ number length (const std::smatch &m, number index)
98+ {
99+ return m.length (index);
100+ }
101+
102+ number position (const std::smatch &m, number index)
103+ {
104+ return m.position (index);
105+ }
106+
107+ string str (const std::smatch &m, number index)
108+ {
109+ return m.str (index);
110+ }
111+
112+ string prefix (const std::smatch &m)
113+ {
114+ return m.prefix ().str ();
115+ }
116+
117+ string suffix (const std::smatch &m)
118+ {
119+ return m.suffix ().str ();
120+ }
121+
122+ void init ()
123+ {
124+ string_cs_ext::init ();
125+ regex_ext.add_var (" result" , var::make_protect<extension_t >(regex_result_ext_shared));
126+ regex_ext.add_var (" build" , var::make_protect<callable>(cni (build)));
127+ regex_ext.add_var (" match" , var::make_protect<callable>(cni (match)));
128+ regex_ext.add_var (" search" , var::make_protect<callable>(cni (search)));
129+ regex_ext.add_var (" replace" , var::make_protect<callable>(cni (replace)));
130+ regex_result_ext.add_var (" ready" , var::make_protect<callable>(cni (ready)));
131+ regex_result_ext.add_var (" empty" , var::make_protect<callable>(cni (empty)));
132+ regex_result_ext.add_var (" size" , var::make_protect<callable>(cni (size)));
133+ regex_result_ext.add_var (" length" , var::make_protect<callable>(cni (length)));
134+ regex_result_ext.add_var (" position" , var::make_protect<callable>(cni (position)));
135+ regex_result_ext.add_var (" str" , var::make_protect<callable>(cni (str)));
136+ regex_result_ext.add_var (" prefix" , var::make_protect<callable>(cni (prefix)));
137+ regex_result_ext.add_var (" suffix" , var::make_protect<callable>(cni (suffix)));
138+ }
139+ }
140+
141+ cs::extension *cs_extension ()
142+ {
143+ regex_cs_ext::init ();
144+ return ®ex_ext;
145+ }
0 commit comments