Skip to content

Commit 030243a

Browse files
coolreader18youknowone
authored andcommitted
Split out wtf8 into its own crate
1 parent 6b72d2e commit 030243a

17 files changed

+74
-14
lines changed

Cargo.lock

+14-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ template = "installer-config/installer.wxs"
115115
resolver = "2"
116116
members = [
117117
"compiler", "compiler/core", "compiler/codegen", "compiler/literal", "compiler/source",
118-
".", "common", "derive", "jit", "vm", "vm/sre_engine", "pylib", "stdlib", "derive-impl",
118+
".", "common", "derive", "jit", "vm", "vm/sre_engine", "pylib", "stdlib", "derive-impl", "wtf8",
119119
"wasm/lib",
120120
]
121121

@@ -141,6 +141,7 @@ rustpython-vm = { path = "vm", default-features = false, version = "0.4.0" }
141141
rustpython-pylib = { path = "pylib", version = "0.4.0" }
142142
rustpython-stdlib = { path = "stdlib", default-features = false, version = "0.4.0" }
143143
rustpython-sre_engine = { path = "vm/sre_engine", version = "0.4.0" }
144+
rustpython-wtf8 = { path = "wtf8", version = "0.4.0" }
144145
rustpython-doc = { git = "https://github.com/RustPython/__doc__", tag = "0.3.0", version = "0.3.0" }
145146

146147
ruff_python_parser = { git = "https://github.com/astral-sh/ruff.git", tag = "0.11.0" }

common/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ threading = ["parking_lot"]
1313

1414
[dependencies]
1515
rustpython-literal = { workspace = true }
16+
rustpython-wtf8 = { workspace = true }
1617

1718
ascii = { workspace = true }
1819
bitflags = { workspace = true }

common/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ pub mod static_cell;
2929
pub mod str;
3030
#[cfg(windows)]
3131
pub mod windows;
32-
pub mod wtf8;
32+
33+
pub use rustpython_wtf8 as wtf8;
3334

3435
pub mod vendored {
3536
pub use ascii;

compiler/codegen/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ license.workspace = true
1111

1212
[dependencies]
1313
# rustpython-ast = { workspace = true, features=["unparse", "constant-optimization"] }
14-
rustpython-common = { workspace = true }
1514
# rustpython-parser-core = { workspace = true }
1615
rustpython-compiler-core = { workspace = true }
1716
rustpython-compiler-source = {workspace = true }
17+
rustpython-wtf8 = { workspace = true }
1818
ruff_python_parser = { workspace = true }
1919
ruff_python_ast = { workspace = true }
2020
ruff_text_size = { workspace = true }

compiler/codegen/src/compile.rs

+33-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use ruff_python_ast::{
2828
};
2929
use ruff_source_file::OneIndexed;
3030
use ruff_text_size::{Ranged, TextRange};
31-
use rustpython_common::wtf8::Wtf8Buf;
31+
use rustpython_wtf8::Wtf8Buf;
3232
// use rustpython_ast::located::{self as located_ast, Located};
3333
use rustpython_compiler_core::{
3434
Mode,
@@ -3529,7 +3529,7 @@ impl EmitArg<bytecode::Label> for ir::BlockIdx {
35293529
/// The code has been ported from `_PyCompile_CleanDoc` in cpython.
35303530
/// `inspect.cleandoc` is also a good reference, but has a few incompatibilities.
35313531
fn clean_doc(doc: &str) -> String {
3532-
let doc = rustpython_common::str::expandtabs(doc, 8);
3532+
let doc = expandtabs(doc, 8);
35333533
// First pass: find minimum indentation of any non-blank lines
35343534
// after first line.
35353535
let margin = doc
@@ -3558,6 +3558,37 @@ fn clean_doc(doc: &str) -> String {
35583558
}
35593559
}
35603560

3561+
// copied from rustpython_common::str, so we don't have to depend on it just for this function
3562+
fn expandtabs(input: &str, tab_size: usize) -> String {
3563+
let tab_stop = tab_size;
3564+
let mut expanded_str = String::with_capacity(input.len());
3565+
let mut tab_size = tab_stop;
3566+
let mut col_count = 0usize;
3567+
for ch in input.chars() {
3568+
match ch {
3569+
'\t' => {
3570+
let num_spaces = tab_size - col_count;
3571+
col_count += num_spaces;
3572+
let expand = " ".repeat(num_spaces);
3573+
expanded_str.push_str(&expand);
3574+
}
3575+
'\r' | '\n' => {
3576+
expanded_str.push(ch);
3577+
col_count = 0;
3578+
tab_size = 0;
3579+
}
3580+
_ => {
3581+
expanded_str.push(ch);
3582+
col_count += 1;
3583+
}
3584+
}
3585+
if col_count >= tab_size {
3586+
tab_size += tab_stop;
3587+
}
3588+
}
3589+
expanded_str
3590+
}
3591+
35613592
fn split_doc<'a>(body: &'a [Stmt], opts: &CompileOpts) -> (Option<String>, &'a [Stmt]) {
35623593
if let Some((Stmt::Expr(expr), body_rest)) = body.split_first() {
35633594
let doc_comment = match &*expr.value {

compiler/codegen/src/string_parser.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use std::convert::Infallible;
99

1010
use ruff_python_ast::{AnyStringFlags, StringFlags};
11-
use rustpython_common::wtf8::{CodePoint, Wtf8, Wtf8Buf};
11+
use rustpython_wtf8::{CodePoint, Wtf8, Wtf8Buf};
1212

1313
// use ruff_python_parser::{LexicalError, LexicalErrorType};
1414
type LexicalError = Infallible;

compiler/core/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ license.workspace = true
1313
ruff_python_ast = { workspace = true }
1414
ruff_python_parser = { workspace = true }
1515
ruff_source_file = { workspace = true }
16-
rustpython-common = { workspace = true }
16+
rustpython-wtf8 = { workspace = true }
1717

1818
bitflags = { workspace = true }
1919
itertools = { workspace = true }

compiler/core/src/bytecode.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use num_complex::Complex64;
88
pub use ruff_python_ast::ConversionFlag;
99
// use rustpython_parser_core::source_code::{OneIndexed, SourceLocation};
1010
use ruff_source_file::{OneIndexed, SourceLocation};
11-
use rustpython_common::wtf8::{Wtf8, Wtf8Buf};
11+
use rustpython_wtf8::{Wtf8, Wtf8Buf};
1212
use std::marker::PhantomData;
1313
use std::{collections::BTreeSet, fmt, hash, mem};
1414

compiler/core/src/marshal.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::bytecode::*;
22
use malachite_bigint::{BigInt, Sign};
33
use num_complex::Complex64;
44
use ruff_source_file::{OneIndexed, SourceLocation};
5-
use rustpython_common::wtf8::Wtf8;
5+
use rustpython_wtf8::Wtf8;
66
use std::convert::Infallible;
77

88
pub const FORMAT_VERSION: u32 = 4;

vm/sre_engine/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ name = "benches"
1515
harness = false
1616

1717
[dependencies]
18-
rustpython-common = { workspace = true }
18+
rustpython-wtf8 = { workspace = true }
1919
num_enum = { workspace = true }
2020
bitflags = { workspace = true }
2121
optional = { workspace = true }

vm/sre_engine/src/string.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustpython_common::wtf8::Wtf8;
1+
use rustpython_wtf8::Wtf8;
22

33
#[derive(Debug, Clone, Copy)]
44
pub struct StringCursor {

wtf8/Cargo.toml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "rustpython-wtf8"
3+
description = "An implementation of WTF-8 for use in RustPython"
4+
version.workspace = true
5+
authors.workspace = true
6+
edition.workspace = true
7+
rust-version.workspace = true
8+
repository.workspace = true
9+
license.workspace = true
10+
11+
[dependencies]
12+
ascii = { workspace = true }
13+
bstr = { workspace = true }
14+
itertools = { workspace = true }
15+
memchr = { workspace = true }
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)