Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions tests/snippets/test_string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import string


assert string.ascii_letters == 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
assert string.ascii_lowercase == 'abcdefghijklmnopqrstuvwxyz'
assert string.ascii_uppercase == 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
assert string.digits == '0123456789'
assert string.hexdigits == '0123456789abcdefABCDEF'
assert string.octdigits == '01234567'
assert string.punctuation == '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
# FIXME
#assert string.whitespace == ' \t\n\r\x0b\x0c', string.whitespace
#assert string.printable == '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'
2 changes: 2 additions & 0 deletions vm/src/stdlib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod math;
mod pystruct;
mod random;
mod re;
mod string;
mod time_module;
mod tokenize;
mod types;
Expand All @@ -25,6 +26,7 @@ pub fn get_module_inits() -> HashMap<String, StdlibInitFunc> {
modules.insert("math".to_string(), math::mk_module as StdlibInitFunc);
modules.insert("re".to_string(), re::mk_module as StdlibInitFunc);
modules.insert("random".to_string(), random::mk_module as StdlibInitFunc);
modules.insert("string".to_string(), string::mk_module as StdlibInitFunc);
modules.insert("struct".to_string(), pystruct::mk_module as StdlibInitFunc);
modules.insert("time".to_string(), time_module::mk_module as StdlibInitFunc);
modules.insert(
Expand Down
43 changes: 43 additions & 0 deletions vm/src/stdlib/string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* String builtin module
*
*
*/

use super::super::pyobject::{PyContext, PyObjectRef};

pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
let py_mod = ctx.new_module(&"string".to_string(), ctx.new_scope(None));

let ascii_lowercase = "abcdefghijklmnopqrstuvwxyz".to_string();
let ascii_uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".to_string();
let ascii_letters = format!("{}{}", ascii_lowercase, ascii_uppercase);
let digits = "0123456789".to_string();
let hexdigits = "0123456789abcdefABCDEF".to_string();
let octdigits = "01234567".to_string();
let punctuation = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~".to_string();
/* FIXME
let whitespace = " \t\n\r\x0b\x0c".to_string();
let printable = format!("{}{}{}{}", digits, ascii_letters, punctuation, whitespace);
*/

// Constants:
ctx.set_attr(&py_mod, "ascii_letters", ctx.new_str(ascii_letters.clone()));
ctx.set_attr(
&py_mod,
"ascii_lowercase",
ctx.new_str(ascii_lowercase.clone()),
);
ctx.set_attr(
&py_mod,
"ascii_uppercase",
ctx.new_str(ascii_uppercase.clone()),
);
ctx.set_attr(&py_mod, "digits", ctx.new_str(digits.clone()));
ctx.set_attr(&py_mod, "hexdigits", ctx.new_str(hexdigits.clone()));
ctx.set_attr(&py_mod, "octdigits", ctx.new_str(octdigits.clone()));
// ctx.set_attr(&py_mod, "printable", ctx.new_str(printable.clone()));
ctx.set_attr(&py_mod, "punctuation", ctx.new_str(punctuation.clone()));
// ctx.set_attr(&py_mod, "whitespace", ctx.new_str(whitespace.clone()));

py_mod
}