diff --git a/tests/snippets/test_string.py b/tests/snippets/test_string.py new file mode 100644 index 0000000000..aa4186042e --- /dev/null +++ b/tests/snippets/test_string.py @@ -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' diff --git a/vm/src/stdlib/mod.rs b/vm/src/stdlib/mod.rs index ae608de156..02d131f3b0 100644 --- a/vm/src/stdlib/mod.rs +++ b/vm/src/stdlib/mod.rs @@ -6,6 +6,7 @@ mod math; mod pystruct; mod random; mod re; +mod string; mod time_module; mod tokenize; mod types; @@ -25,6 +26,7 @@ pub fn get_module_inits() -> HashMap { 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( diff --git a/vm/src/stdlib/string.rs b/vm/src/stdlib/string.rs new file mode 100644 index 0000000000..4e666901be --- /dev/null +++ b/vm/src/stdlib/string.rs @@ -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 +}