Skip to content

Commit 80109b1

Browse files
committed
rustpython-literal
1 parent bd64603 commit 80109b1

File tree

9 files changed

+773
-7
lines changed

9 files changed

+773
-7
lines changed

ast/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ license = "MIT"
1111
default = ["constant-optimization", "fold"]
1212
constant-optimization = ["fold"]
1313
fold = []
14-
unparse = ["rustpython-common"]
14+
unparse = ["rustpython-literal"]
1515

1616
[dependencies]
1717
rustpython-compiler-core = { path = "../core", version = "0.2.0" }
18-
rustpython-common = { path = "../../common", version = "0.2.0", optional = true }
18+
rustpython-literal = { path = "../literal", version = "0.2.0", optional = true }
1919

2020
num-bigint = { workspace = true }

ast/src/constant.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,17 @@ impl From<BigInt> for Constant {
3535
}
3636
}
3737

38-
#[cfg(feature = "rustpython-common")]
38+
#[cfg(feature = "rustpython-literal")]
3939
impl std::fmt::Display for Constant {
4040
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
4141
match self {
4242
Constant::None => f.pad("None"),
4343
Constant::Bool(b) => f.pad(if *b { "True" } else { "False" }),
44-
Constant::Str(s) => rustpython_common::escape::UnicodeEscape::new_repr(s.as_str())
44+
Constant::Str(s) => rustpython_literal::escape::UnicodeEscape::new_repr(s.as_str())
4545
.str_repr()
4646
.write(f),
4747
Constant::Bytes(b) => {
48-
let escape = rustpython_common::escape::AsciiEscape::new_repr(b);
48+
let escape = rustpython_literal::escape::AsciiEscape::new_repr(b);
4949
let repr = escape.bytes_repr().to_string().unwrap();
5050
f.pad(&repr)
5151
}
@@ -64,7 +64,7 @@ impl std::fmt::Display for Constant {
6464
f.write_str(")")
6565
}
6666
}
67-
Constant::Float(fp) => f.pad(&rustpython_common::float_ops::to_string(*fp)),
67+
Constant::Float(fp) => f.pad(&rustpython_literal::float::to_string(*fp)),
6868
Constant::Complex { real, imag } => {
6969
if *real == 0.0 {
7070
write!(f, "{imag}j")

ast/src/unparse.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ impl<'a> Unparser<'a> {
511511
} else {
512512
self.p("f")?;
513513
let body = to_string_fmt(|f| Unparser::new(f).unparse_fstring_body(values, is_spec));
514-
rustpython_common::escape::UnicodeEscape::new_repr(&body)
514+
rustpython_literal::escape::UnicodeEscape::new_repr(&body)
515515
.str_repr()
516516
.write(&mut self.f)
517517
}

literal/Cargo.toml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "rustpython-literal"
3+
version = "0.2.0"
4+
description = "Common literal handling utilities mostly useful for unparse and repr."
5+
authors = ["RustPython Team"]
6+
edition = "2021"
7+
repository = "https://github.com/RustPython/RustPython"
8+
license = "MIT"
9+
10+
[dependencies]
11+
num-traits = { workspace = true }
12+
13+
hexf-parse = "0.2.1"
14+
lexical-parse-float = { version = "0.8.0", features = ["format"] }
15+
unic-ucd-category = "0.9"
16+
17+
[dev-dependencies]
18+
rand = { workspace = true }

literal/src/char.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use unic_ucd_category::GeneralCategory;
2+
3+
/// According to python following categories aren't printable:
4+
/// * Cc (Other, Control)
5+
/// * Cf (Other, Format)
6+
/// * Cs (Other, Surrogate)
7+
/// * Co (Other, Private Use)
8+
/// * Cn (Other, Not Assigned)
9+
/// * Zl Separator, Line ('\u2028', LINE SEPARATOR)
10+
/// * Zp Separator, Paragraph ('\u2029', PARAGRAPH SEPARATOR)
11+
/// * Zs (Separator, Space) other than ASCII space('\x20').
12+
pub fn is_printable(c: char) -> bool {
13+
let cat = GeneralCategory::of(c);
14+
!(cat.is_other() || cat.is_separator())
15+
}

0 commit comments

Comments
 (0)