Skip to content
This repository was archived by the owner on Mar 22, 2024. It is now read-only.

Commit 9c95994

Browse files
committed
Modify to work outside of rustpython-vm
1 parent db5bd64 commit 9c95994

3 files changed

Lines changed: 25 additions & 27 deletions

File tree

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ authors = ["Kangzhi Shi <shikangzhi@gmail.com>", "RustPython Team"]
55
edition = "2018"
66

77
[dependencies]
8+
num_enum = "0.5"
9+
bitflags = "1.2"

src/engine.rs

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
// good luck to those that follow; here be dragons
22

3-
use super::_sre::MAXREPEAT;
43
use super::constants::{SreAtCode, SreCatCode, SreFlag, SreOpcode};
5-
use crate::builtins::PyBytes;
6-
use crate::bytesinner::is_py_ascii_whitespace;
7-
use crate::pyobject::{IntoPyObject, PyObjectRef};
8-
use crate::VirtualMachine;
4+
use super::MAXREPEAT;
95
use std::collections::HashMap;
106
use std::convert::TryFrom;
11-
use std::unreachable;
7+
8+
const fn is_py_ascii_whitespace(b: u8) -> bool {
9+
matches!(b, b'\t' | b'\n' | b'\x0C' | b'\r' | b' ' | b'\x0B')
10+
}
1211

1312
#[derive(Debug)]
14-
pub(crate) struct State<'a> {
13+
pub struct State<'a> {
1514
pub string: StrDrive<'a>,
1615
pub start: usize,
1716
pub end: usize,
@@ -29,7 +28,7 @@ pub(crate) struct State<'a> {
2928
}
3029

3130
impl<'a> State<'a> {
32-
pub(crate) fn new(
31+
pub fn new(
3332
string: StrDrive<'a>,
3433
start: usize,
3534
end: usize,
@@ -150,7 +149,7 @@ impl<'a> State<'a> {
150149
}
151150

152151
#[derive(Debug, Clone, Copy)]
153-
pub(crate) enum StrDrive<'a> {
152+
pub enum StrDrive<'a> {
154153
Str(&'a str),
155154
Bytes(&'a [u8]),
156155
}
@@ -219,21 +218,6 @@ impl<'a> StrDrive<'a> {
219218
StrDrive::Bytes(_) => offset - skip,
220219
}
221220
}
222-
223-
pub fn slice_to_pyobject(&self, start: usize, end: usize, vm: &VirtualMachine) -> PyObjectRef {
224-
match *self {
225-
StrDrive::Str(s) => s
226-
.chars()
227-
.take(end)
228-
.skip(start)
229-
.collect::<String>()
230-
.into_pyobject(vm),
231-
StrDrive::Bytes(b) => {
232-
PyBytes::from(b.iter().take(end).skip(start).cloned().collect::<Vec<u8>>())
233-
.into_pyobject(vm)
234-
}
235-
}
236-
}
237221
}
238222

239223
#[derive(Debug, Clone, Copy)]
@@ -972,7 +956,7 @@ fn is_loc_word(ch: u32) -> bool {
972956
fn is_linebreak(ch: u32) -> bool {
973957
ch == '\n' as u32
974958
}
975-
pub(crate) fn lower_ascii(ch: u32) -> u32 {
959+
pub fn lower_ascii(ch: u32) -> u32 {
976960
u8::try_from(ch)
977961
.map(|x| x.to_ascii_lowercase() as u32)
978962
.unwrap_or(ch)
@@ -1044,13 +1028,13 @@ fn is_uni_alnum(ch: u32) -> bool {
10441028
fn is_uni_word(ch: u32) -> bool {
10451029
ch == '_' as u32 || is_uni_alnum(ch)
10461030
}
1047-
pub(crate) fn lower_unicode(ch: u32) -> u32 {
1031+
pub fn lower_unicode(ch: u32) -> u32 {
10481032
// TODO: check with cpython
10491033
char::try_from(ch)
10501034
.map(|x| x.to_lowercase().next().unwrap() as u32)
10511035
.unwrap_or(ch)
10521036
}
1053-
pub(crate) fn upper_unicode(ch: u32) -> u32 {
1037+
pub fn upper_unicode(ch: u32) -> u32 {
10541038
// TODO: check with cpython
10551039
char::try_from(ch)
10561040
.map(|x| x.to_uppercase().next().unwrap() as u32)

src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
11
pub mod constants;
22
pub mod engine;
3+
4+
pub const CODESIZE: usize = 4;
5+
6+
#[cfg(target_pointer_width = "32")]
7+
pub const MAXREPEAT: usize = usize::MAX;
8+
#[cfg(target_pointer_width = "64")]
9+
pub const MAXREPEAT: usize = u32::MAX as usize;
10+
11+
#[cfg(target_pointer_width = "32")]
12+
pub const MAXGROUPS: usize = MAXREPEAT / 4 / 2;
13+
#[cfg(target_pointer_width = "64")]
14+
pub const MAXGROUPS: usize = MAXREPEAT / 2;

0 commit comments

Comments
 (0)