Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Open Source > Development Lib > Programming Language/Scripting Language &&
Donate
Please sign in before you donate.
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
13 Star 69 Fork 22

johnsonyl/cpps

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (1)
Tags (4)
master
v1.0.3-beta
v1.0.3
v1.0.2
v1.0.0
master
Branches (1)
Tags (4)
master
v1.0.3-beta
v1.0.3
v1.0.2
v1.0.0
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (1)
Tags (4)
master
v1.0.3-beta
v1.0.3
v1.0.2
v1.0.0
cpps
/
libs
/
re
/
re.cpp
cpps
/
libs
/
re
/
re.cpp
re.cpp 9.91 KB
Copy Edit Raw Blame History
johnsonyl authored 2021年01月26日 14:47 +08:00 . 2021年01月26日 更新
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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include <re2/stringpiece.h>
#include <re2/filtered_re2.h>
#include <re2/re2.h>
#include <re2/set.h>
#include <cpps/cpps.h>
using namespace cpps;
using namespace std;
using namespace re2;
static inline re2::StringPiece FromBytes(object& val) {
if (val.isstring()) {
std::string* str = cpps_get_string(val.getval());
return re2::StringPiece(str->data(), str->size());
}
return re2::StringPiece("");
}
class Set {
public:
// Set(RE2::Anchor anchor, const RE2::Options& options)
// : set_(options, anchor) {}
Set(){
set_ = NULL;
}
~Set()
{
if (set_) {
CPPSDELETE(set_);
set_ = NULL;
}
}
void constructor(object anchor, RE2::Options* options) {
set_ = CPPSNEW( RE2::Set)(*options,(RE2::Anchor) anchor.toint());
}
// Not copyable or movable.
Set(const Set&) = delete;
Set& operator=(const Set&) = delete;
int Add(object buffer) {
auto pattern = FromBytes(buffer);
int index = set_->Add(pattern, /*error=*/NULL); // -1 on error
return index;
}
bool Compile() {
// Compiling can fail.
return set_->Compile();
}
object Match(C* c, object buffer) const {
object::vector ret = object::vector::create(c);
auto text = FromBytes(buffer);
std::vector<int> matches;
set_->Match(text, &matches);
for (auto& it : matches) {
ret.push_back(object::create(c, it));
}
return ret.toobject();
}
private:
RE2::Set *set_;
};
class Filter {
public:
Filter() = default;
~Filter() = default;
// Not copyable or movable.
Filter(const Filter&) = delete;
Filter& operator=(const Filter&) = delete;
int Add(object buffer, RE2::Options* options) {
auto pattern = FromBytes(buffer);
int index = -1; // not clobbered on error
filter_.Add(pattern, *options, &index);
return index;
}
bool Compile() {
std::vector<std::string> atoms;
filter_.Compile(&atoms);
RE2::Options options;
options.set_literal(true);
options.set_case_sensitive(false);
set_ = std::unique_ptr<RE2::Set>(new RE2::Set(options, RE2::UNANCHORED));
for (int i = 0; i < static_cast<int>(atoms.size()); ++i) {
if (set_->Add(atoms[i], /*error=*/NULL) != i) {
// Should never happen: the atom is a literal!
cpps::fail("set_->Add() failed");
}
}
// Compiling can fail.
return set_->Compile();
}
object Match(C*c,object buffer, bool potential) const {
object::vector ret = object::vector::create(c);
auto text = FromBytes(buffer);
std::vector<int> atoms;
set_->Match(text, &atoms);
std::vector<int> matches;
if (potential) {
filter_.AllPotentials(atoms, &matches);
}
else {
filter_.AllMatches(text, atoms, &matches);
}
for (auto& it : matches) {
ret.push_back(object::create(c, it));
}
return ret.toobject();
}
private:
re2::FilteredRE2 filter_;
std::unique_ptr<RE2::Set> set_;
};
static inline int OneCharLen(const char* ptr) {
return "1円1円1円1円1円1円1円1円1円1円1円1円2円2円3円4円"[(*ptr & 0xFF) >> 4];
}
ssize_t CharLenToBytes(object buffer, ssize_t pos, ssize_t len) {
auto text = FromBytes(buffer);
auto ptr = text.data() + pos;
auto end = text.data() + text.size();
while (ptr < end && len > 0) {
ptr += OneCharLen(ptr);
--len;
}
return ptr - (text.data() + pos);
}
// Helper function for when Python decodes bytes to Text and then needs to
// convert bytes offsets to Text offsets. Assumes that text is valid UTF-8.
ssize_t BytesToCharLen(object buffer, ssize_t pos, ssize_t endpos) {
auto text = FromBytes(buffer);
auto ptr = text.data() + pos;
auto end = text.data() + endpos;
ssize_t len = 0;
while (ptr < end) {
ptr += OneCharLen(ptr);
++len;
}
return len;
}
class _RE2
{
public:
_RE2()
{
_re2 = NULL;
}
~_RE2() {
if (_re2) {
CPPSDELETE(_re2);
}
}
void constructor(object buffer, RE2::Options* options) {
auto pattern = FromBytes(buffer);
_re2 = CPPSNEW(RE2)(pattern, *options);
}
bool ok() {
return _re2->ok();
}
std::string error() {
return _re2->error();
}
RE2::Options* RE2_options() {
return (RE2::Options*) & _re2->options();
}
//std::vector<std::pair<py::bytes, int>> RE2NamedCapturingGroupsShim(
object RE2NamedCapturingGroupsShim( C* c) {
object::map groups = object::map::create(c);
for (const auto& it : _re2->NamedCapturingGroups()) {
groups.insert(cpps::object::create(c, it.first), cpps::object::create(c, it.second));
}
return groups.toobject();
}
object RE2ProgramFanoutShim( C* c) {
object::vector ret = object::vector::create(c);
std::vector<int> histogram;
_re2->ProgramFanout(&histogram);
for (auto& it : histogram) {
ret.push_back(object::create(c, it));
}
return ret.toobject();
}
object RE2ReverseProgramFanoutShim( C* c) {
object::vector ret = object::vector::create(c);
std::vector<int> histogram;
_re2->ReverseProgramFanout(&histogram);
for (auto& it : histogram) {
ret.push_back(object::create(c, it));
}
return ret.toobject();
}
object RE2MatchShim( C* c,
object anchor,
object buffer,
ssize_t pos,
ssize_t endpos) {
auto text = FromBytes(buffer);
const int num_groups = _re2->NumberOfCapturingGroups() + 1; // need 0ドル
std::vector<re2::StringPiece> groups;
groups.resize(num_groups);
if (pos == endpos)
{
for (auto& it : groups) {
it = re2::StringPiece();
}
}
else {
if (!_re2->Match(text, pos, endpos, (RE2::Anchor) anchor.toint(), groups.data(), (int)groups.size())) {
// Ensure that groups are null before converting to spans!
for (auto& it : groups) {
it = re2::StringPiece();
}
}
}
object::vector spans = object::vector::create(c);
for (const auto& it : groups) {
if (it.data() == NULL) {
object::pair _pair = object::pair::create(c, object::create(c, -1), object::create(c, -1));
spans.push_back(_pair.toobject());
}
else {
if (it.size() == 0) {
object::pair _pair = object::pair::create(c, object::create(c, text.size()),
object::create(c, text.size()));
spans.push_back(_pair.toobject());
}
else {
object::pair _pair = object::pair::create(c, object::create(c, it.data() - text.data()),
object::create(c, it.data() - text.data() + it.size()));
spans.push_back(_pair.toobject());
}
}
}
return spans.toobject();
}
cpps_integer NumberOfCapturingGroups() {
return (cpps_integer)_re2->NumberOfCapturingGroups();
}
cpps_integer ProgramSize() {
return (cpps_integer)_re2->ProgramSize();
}
cpps_integer ReverseProgramSize() {
return (cpps_integer)_re2->ReverseProgramSize();
}
RE2* _re2;
};
cpps_integer RE2_OPTIONS_ENCODING(RE2::Options* self) {
return (cpps_integer)self->encoding();
}
void RE2_OPTIONS_SET_ENCODING(RE2::Options* self, object v) {
return self->set_encoding((RE2::Options::Encoding)v.toint());
}
std::string RE2QuoteMeta(object buffer)
{
auto text = FromBytes(buffer);
return RE2::QuoteMeta(text);
}
cpps_export_void cpps_attach(cpps::C* c)
{
cpps::cpps_init_cpps_class(c);
cpps::_module(c, "re")[
def("CharLenToBytes", CharLenToBytes),
def("BytesToCharLen", BytesToCharLen),
def("QuoteMeta", RE2QuoteMeta),
_enum(c,"Anchor")
.value("UNANCHORED", RE2::Anchor::UNANCHORED)
.value("ANCHOR_START", RE2::Anchor::ANCHOR_START)
.value("ANCHOR_BOTH", RE2::Anchor::ANCHOR_BOTH),
_enum(c,"Encoding")
.value("UTF8", RE2::Options::Encoding::EncodingUTF8)
.value("LATIN1", RE2::Options::Encoding::EncodingLatin1),
_class<_RE2>("RE2")
.def("constructor", &_RE2::constructor)
.def("ok", &_RE2::ok)
.def("error", &_RE2::error)
.def("options", &_RE2::RE2_options)
.def("NumberOfCapturingGroups", &_RE2::NumberOfCapturingGroups)
.def_inside("NamedCapturingGroups", &_RE2::RE2NamedCapturingGroupsShim)
.def("ProgramSize", &_RE2::ProgramSize)
.def("ReverseProgramSize", &_RE2::ReverseProgramSize)
.def_inside("ProgramFanout", &_RE2::RE2ProgramFanoutShim)
.def_inside("ReverseProgramFanout", &_RE2::RE2ReverseProgramFanoutShim)
.def_inside("Match", &_RE2::RE2MatchShim),
_class<RE2::Options>("Options")
.def("encoding", RE2_OPTIONS_ENCODING)
.def("set_encoding", RE2_OPTIONS_SET_ENCODING)
.def("posix_syntax",&RE2::Options::posix_syntax)
.def("set_posix_syntax",&RE2::Options::set_posix_syntax)
.def("longest_match",&RE2::Options::longest_match)
.def("set_longest_match",&RE2::Options::set_longest_match)
.def("log_errors",&RE2::Options::log_errors)
.def("set_log_errors",&RE2::Options::set_log_errors)
.def("max_mem",&RE2::Options::max_mem)
.def("set_max_mem",&RE2::Options::set_max_mem)
.def("literal",&RE2::Options::literal)
.def("set_literal",&RE2::Options::set_literal)
.def("never_nl",&RE2::Options::never_nl)
.def("set_never_nl",&RE2::Options::set_never_nl)
.def("dot_nl",&RE2::Options::dot_nl)
.def("set_dot_nl",&RE2::Options::set_dot_nl)
.def("never_capture",&RE2::Options::never_capture)
.def("set_never_capture",&RE2::Options::set_never_capture)
.def("case_sensitive",&RE2::Options::case_sensitive)
.def("set_case_sensitive",&RE2::Options::set_case_sensitive)
.def("perl_classes",&RE2::Options::perl_classes)
.def("set_perl_classes",&RE2::Options::set_perl_classes)
.def("word_boundary",&RE2::Options::word_boundary)
.def("set_word_boundary",&RE2::Options::set_word_boundary)
.def("one_line",&RE2::Options::one_line)
.def("set_one_line",&RE2::Options::set_one_line),
_class<Set>("RE2Set")
.def("constructor", &Set::constructor)
.def("Add", &Set::Add)
.def("Compile", &Set::Compile)
.def_inside("Match", &Set::Match),
_class<Filter>("RE2Filter")
.def("Add", &Filter::Add)
.def("Compile", &Filter::Compile)
.def_inside("Match", &Filter::Match)
];
}
cpps_export_void cpps_detach(cpps::C * c)
{
cpps::_unmodule(c, "re");
}
cpps_export_finish
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

CPPS是一种轻量级的嵌入式脚本语言,其语法类似于C++。它具有当前主流语言的许多特性,包括协程、面向对象、lambda、闭包、泛型变量、自定义模块支持、GC垃圾收集和跨平台。CPPS将程序解释为字节码,通过内置语法解析在虚拟机中运行
Cancel

Releases (3)

All

The Open Source Evaluation Index is derived from the OSS Compass evaluation system, which evaluates projects around the following three dimensions

1. Open source ecosystem

  • Productivity: To evaluate the ability of open-source projects to output software artifacts and open-source value.
  • Innovation: Used to evaluate the degree of diversity of open source software and its ecosystem.
  • Robustness: Used to evaluate the ability of open-source projects to resist internal and external interference and self recover in the face of changing development environments.

2. Collaboration, People, Software

  • Collaboration: represents the degree and depth of collaboration in open source development behavior.
  • Observe the influence of core personnel in open source projects, and examine the evaluations of users and developers on open source projects from a third-party perspective.
  • Software: Evaluate the value of products exported from open-source projects and their ultimate destination. It is also a concrete manifestation of "open source software", one of the oldest mainstream directions in open source evaluation.

3. Evaluation model

    Based on the dimensions of "open source ecosystem" and "collaboration, people, and software", identify quantifiable indicators directly or indirectly related to this goal, quantitatively evaluate the health and ecology of open source projects, and ultimately form an open source evaluation index.

Contributors

All

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/cppscript/cpps.git
git@gitee.com:cppscript/cpps.git
cppscript
cpps
cpps
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /