Skip to content

Commit e4ca165

Browse files
committed
Implement most string module constants
whitespace and thus printable are excluded because i haven't found a way to make tests pass.
1 parent 4c5b584 commit e4ca165

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

tests/snippets/test_string.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import string
2+
3+
4+
assert string.ascii_letters == 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
5+
assert string.ascii_lowercase == 'abcdefghijklmnopqrstuvwxyz'
6+
assert string.ascii_uppercase == 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
7+
assert string.digits == '0123456789'
8+
assert string.hexdigits == '0123456789abcdefABCDEF'
9+
assert string.octdigits == '01234567'
10+
assert string.punctuation == '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
11+
# FIXME
12+
#assert string.whitespace == ' \t\n\r\x0b\x0c', string.whitespace
13+
#assert string.printable == '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'

vm/src/stdlib/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod math;
66
mod pystruct;
77
mod random;
88
mod re;
9+
mod string;
910
mod time_module;
1011
mod tokenize;
1112
mod types;
@@ -25,6 +26,7 @@ pub fn get_module_inits() -> HashMap<String, StdlibInitFunc> {
2526
modules.insert("math".to_string(), math::mk_module as StdlibInitFunc);
2627
modules.insert("re".to_string(), re::mk_module as StdlibInitFunc);
2728
modules.insert("random".to_string(), random::mk_module as StdlibInitFunc);
29+
modules.insert("string".to_string(), string::mk_module as StdlibInitFunc);
2830
modules.insert("struct".to_string(), pystruct::mk_module as StdlibInitFunc);
2931
modules.insert("time".to_string(), time_module::mk_module as StdlibInitFunc);
3032
modules.insert(

vm/src/stdlib/string.rs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* String builtin module
2+
*
3+
*
4+
*/
5+
6+
use super::super::pyobject::{PyContext, PyObjectRef};
7+
8+
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
9+
let py_mod = ctx.new_module(&"string".to_string(), ctx.new_scope(None));
10+
11+
let ascii_lowercase = "abcdefghijklmnopqrstuvwxyz".to_string();
12+
let ascii_uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".to_string();
13+
let ascii_letters = format!("{}{}", ascii_lowercase, ascii_uppercase);
14+
let digits = "0123456789".to_string();
15+
let hexdigits = "0123456789abcdefABCDEF".to_string();
16+
let octdigits = "01234567".to_string();
17+
let punctuation = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~".to_string();
18+
/* FIXME
19+
let whitespace = " \t\n\r\x0b\x0c".to_string();
20+
let printable = format!("{}{}{}{}", digits, ascii_letters, punctuation, whitespace);
21+
*/
22+
23+
// Constants:
24+
ctx.set_attr(&py_mod, "ascii_letters", ctx.new_str(ascii_letters.clone()));
25+
ctx.set_attr(
26+
&py_mod,
27+
"ascii_lowercase",
28+
ctx.new_str(ascii_lowercase.clone()),
29+
);
30+
ctx.set_attr(
31+
&py_mod,
32+
"ascii_uppercase",
33+
ctx.new_str(ascii_uppercase.clone()),
34+
);
35+
ctx.set_attr(&py_mod, "digits", ctx.new_str(digits.clone()));
36+
ctx.set_attr(&py_mod, "hexdigits", ctx.new_str(hexdigits.clone()));
37+
ctx.set_attr(&py_mod, "octdigits", ctx.new_str(octdigits.clone()));
38+
// ctx.set_attr(&py_mod, "printable", ctx.new_str(printable.clone()));
39+
ctx.set_attr(&py_mod, "punctuation", ctx.new_str(punctuation.clone()));
40+
// ctx.set_attr(&py_mod, "whitespace", ctx.new_str(whitespace.clone()));
41+
42+
py_mod
43+
}

0 commit comments

Comments
 (0)