diff --git a/docs/modules/re.md b/docs/modules/re.md index e55d1837..ab7ba83a 100644 --- a/docs/modules/re.md +++ b/docs/modules/re.md @@ -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`. +!!! \ No newline at end of file diff --git a/docs/retype.yml b/docs/retype.yml index 194b051c..0d65f133 100644 --- a/docs/retype.yml +++ b/docs/retype.yml @@ -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: diff --git a/include/pocketpy/common.h b/include/pocketpy/common.h index 6405ecf5..74b46350 100644 --- a/include/pocketpy/common.h +++ b/include/pocketpy/common.h @@ -4,7 +4,6 @@ #include #include -#include #include #include #include @@ -20,7 +19,6 @@ #include #include #include -#include #include #define PK_VERSION "1.3.7" diff --git a/src/re.cpp b/src/re.cpp index 55de6e00..d132ec6d 100644 --- a/src/re.cpp +++ b/src/re.cpp @@ -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(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 \ No newline at end of file diff --git a/tests/70_re.py b/tests/70_re.py index 247d5d37..bbc11e03 100644 --- a/tests/70_re.py +++ b/tests/70_re.py @@ -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 \ No newline at end of file +# assert re.match('1','1') is not None \ No newline at end of file