Merge pull request #235 from xrmx/stringconstants

Implement most string module constants
This commit is contained in:
Windel Bouwman
2018-12-29 15:29:28 +01:00
committed by GitHub
3 changed files with 58 additions and 0 deletions

View File

@@ -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'

View File

@@ -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<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(

43
vm/src/stdlib/string.rs Normal file
View File

@@ -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
}