remove re module

This commit is contained in:
blueloveTH 2024-01-07 15:46:25 +08:00
parent 2cd22d40c5
commit 0310d46296
5 changed files with 18 additions and 106 deletions

View File

@ -3,19 +3,6 @@ icon: package
label: re
---
### `re.match(pattern, string)`
Return a match object if the string matches the pattern, and `None` otherwise. (force match at the beginning of the string)
### `re.search(pattern, string)`
Return a match object if the string matches the pattern, and `None` otherwise.
### `re.split(pattern, string)`
Split the string by the occurrences of the pattern.
### `re.sub(pattern, repl, string)`
Return a copy of the string with all occurrences of the pattern replaced by the replacement string.
!!!
This module was removed in `v1.3.7`.
!!!

View File

@ -3,7 +3,7 @@ output: .retype
url: https://pocketpy.dev
branding:
title: pocketpy
label: v1.3.6
label: v1.3.7
logo: "./static/logo.png"
favicon: "./static/logo.png"
meta:

View File

@ -4,7 +4,6 @@
#include <cstring>
#include <sstream>
#include <regex>
#include <stdexcept>
#include <vector>
#include <string>
@ -20,7 +19,6 @@
#include <variant>
#include <type_traits>
#include <random>
#include <bitset>
#include <deque>
#define PK_VERSION "1.3.7"

View File

@ -2,81 +2,8 @@
namespace pkpy{
struct ReMatch {
PY_CLASS(ReMatch, re, Match)
i64 start;
i64 end;
std::cmatch m;
ReMatch(i64 start, i64 end, std::cmatch m) : start(start), end(end), m(m) {}
static void _register(VM* vm, PyObject* mod, PyObject* type){
vm->bind_notimplemented_constructor<ReMatch>(type);
vm->bind_method<0>(type, "start", PK_LAMBDA(VAR(_CAST(ReMatch&, args[0]).start)));
vm->bind_method<0>(type, "end", PK_LAMBDA(VAR(_CAST(ReMatch&, args[0]).end)));
vm->bind_method<0>(type, "span", [](VM* vm, ArgsView args) {
auto& self = _CAST(ReMatch&, args[0]);
return VAR(Tuple({VAR(self.start), VAR(self.end)}));
});
vm->bind_method<1>(type, "group", [](VM* vm, ArgsView args) {
auto& self = _CAST(ReMatch&, args[0]);
int index = CAST(int, args[1]);
index = vm->normalized_index(index, self.m.size());
return VAR(self.m[index].str());
});
}
};
static PyObject* _regex_search(const Str& pattern, const Str& string, bool from_start, VM* vm){
std::regex re(pattern.begin(), pattern.end());
std::cmatch m;
if(std::regex_search(string.begin(), string.end(), m, re)){
if(from_start && m.position() != 0) return vm->None;
i64 start = string._byte_index_to_unicode(m.position());
i64 end = string._byte_index_to_unicode(m.position() + m.length());
return VAR_T(ReMatch, start, end, m);
}
return vm->None;
};
void add_module_re(VM* vm){
PyObject* mod = vm->new_module("re");
ReMatch::register_class(vm, mod);
vm->bind_func<2>(mod, "match", [](VM* vm, ArgsView args) {
const Str& pattern = CAST(Str&, args[0]);
const Str& string = CAST(Str&, args[1]);
return _regex_search(pattern, string, true, vm);
});
vm->bind_func<2>(mod, "search", [](VM* vm, ArgsView args) {
const Str& pattern = CAST(Str&, args[0]);
const Str& string = CAST(Str&, args[1]);
return _regex_search(pattern, string, false, vm);
});
vm->bind_func<3>(mod, "sub", [](VM* vm, ArgsView args) {
const Str& pattern = CAST(Str&, args[0]);
const Str& repl = CAST(Str&, args[1]);
const Str& string = CAST(Str&, args[2]);
std::regex re(pattern.begin(), pattern.end());
return VAR(std::regex_replace(string.str(), re, repl.str()));
});
vm->bind_func<2>(mod, "split", [](VM* vm, ArgsView args) {
const Str& pattern = CAST(Str&, args[0]);
const Str& string = CAST(Str&, args[1]);
std::regex re(pattern.begin(), pattern.end());
std::cregex_token_iterator it(string.begin(), string.end(), re, -1);
std::cregex_token_iterator end;
List vec;
for(; it != end; ++it){
vec.push_back(VAR(it->str()));
}
return VAR(vec);
});
// PyObject* mod = vm->new_module("re");
}
} // namespace pkpy

View File

@ -1,19 +1,19 @@
import re
# import re
# test match, search, sub, split
# # test match, search, sub, split
m = re.search('测试','123测试测试')
assert m.span() == (3,5)
assert m.group(0) == '测试'
# m = re.search('测试','123测试测试')
# assert m.span() == (3,5)
# assert m.group(0) == '测试'
assert re.match('测试','123测试测试') is None
assert re.sub('测试','xxx','123测试12321测试') == '123xxx12321xxx'
# assert re.match('测试','123测试测试') is None
# assert re.sub('测试','xxx','123测试12321测试') == '123xxx12321xxx'
# this is different from cpython, the last empty string is not included
assert re.split('测试','测试123测试12321测试') == ['', '123', '12321']
# # this is different from cpython, the last empty string is not included
# assert re.split('测试','测试123测试12321测试') == ['', '123', '12321']
assert re.split(',','123,456,789,10') == ['123', '456', '789', '10']
assert re.split(',',',123,456,789,10') == ['', '123', '456', '789', '10']
assert re.split(',','123,456,789,10,') == ['123', '456', '789', '10']
# assert re.split(',','123,456,789,10') == ['123', '456', '789', '10']
# assert re.split(',',',123,456,789,10') == ['', '123', '456', '789', '10']
# assert re.split(',','123,456,789,10,') == ['123', '456', '789', '10']
assert re.match('1','1') is not None
# assert re.match('1','1') is not None