Skip to content

Commit 16ec5a5

Browse files
authored
feat: rework of API and internal structures (#21)
* tweak * fmt * tweak2 * Remove once cell * Remove fields * Fix empty string sourcemap issue * feature-gate-sourcemap * tweak * update deps * bump version
1 parent df8ac4d commit 16ec5a5

28 files changed

+1373
-1501
lines changed

.editorconfig

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# EditorConfig is awesome: https://EditorConfig.org
2+
3+
# top-most EditorConfig file
4+
root = true
5+
6+
[*]
7+
indent_style = space
8+
indent_size = 2
9+
end_of_line = lf
10+
charset = utf-8
11+
trim_trailing_whitespace = false

Cargo.toml

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
[package]
22
name = "string_wizard"
3-
version = "0.0.21"
3+
version = "0.0.22"
44
edition = "2021"
55
license = "MIT"
6-
description = "manipulate string like wizards"
6+
description = "manipulate string like a wizard"
77

88
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
99

1010
[dependencies]
1111
index_vec = { version = "0.1.3" }
1212
rustc-hash = { version = "1.1.0" }
13-
once_cell = "1.18.0"
14-
oxc_sourcemap = { version = "~0.15.0" }
13+
oxc_sourcemap = { version = "0.25.0", optional = true}
1514

1615
[features]
1716
# Enable source map functionality
18-
source_map = []
17+
source_map = ['dep:oxc_sourcemap']
1918

2019
[dev-dependencies]
2120
glob = "0.3.1"

benches/joiner_join.rs

+36-44
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,46 @@
11
use criterion::{black_box, criterion_group, criterion_main, Criterion};
22

33
fn get_bunch_of_strings() -> Vec<String> {
4-
let files = glob::glob("fixtures/threejs_src/**/*.js").unwrap();
5-
let mut files = files
6-
.into_iter()
7-
.map(|p| p.unwrap().canonicalize().unwrap())
8-
.collect::<Vec<_>>();
9-
files.sort();
10-
let stirngs = files
11-
.iter()
12-
.map(|p| std::fs::read_to_string(p).unwrap())
13-
.collect::<Vec<_>>();
4+
let files = glob::glob("fixtures/threejs_src/**/*.js").unwrap();
5+
let mut files = files.into_iter().map(|p| p.unwrap().canonicalize().unwrap()).collect::<Vec<_>>();
6+
files.sort();
7+
let stirngs = files.iter().map(|p| std::fs::read_to_string(p).unwrap()).collect::<Vec<_>>();
148

15-
let mut ret = vec![];
16-
for _ in 0..10 {
17-
ret.extend(stirngs.clone());
18-
}
19-
ret
9+
let mut ret = vec![];
10+
for _ in 0..10 {
11+
ret.extend(stirngs.clone());
12+
}
13+
ret
2014
}
2115

2216
fn criterion_benchmark(c: &mut Criterion) {
23-
let bunch_of_strings = get_bunch_of_strings();
24-
25-
let mut joiner = string_wizard::Joiner::new();
26-
bunch_of_strings.clone().into_iter().for_each(|s| {
27-
joiner.append_raw(s);
28-
});
29-
c.bench_function("Joiner#join", |b| b.iter(|| black_box(joiner.join())));
30-
c.bench_function("Vec#concat", |b| {
31-
b.iter(|| black_box(bunch_of_strings.concat()))
32-
});
33-
c.bench_function("manual_push", |b| {
34-
b.iter(|| {
35-
let mut output = String::new();
36-
bunch_of_strings.iter().for_each(|s| {
37-
output.push_str(s);
38-
});
39-
black_box(output)
40-
})
41-
});
42-
c.bench_function("manual_push_with_cap", |b| {
43-
b.iter(|| {
44-
let cap: usize = bunch_of_strings.iter().map(|s| s.len()).sum();
45-
let mut output = String::with_capacity(cap);
46-
bunch_of_strings.iter().for_each(|s| {
47-
output.push_str(s);
48-
});
49-
black_box(output)
50-
})
51-
});
17+
let bunch_of_strings = get_bunch_of_strings();
18+
19+
let mut joiner = string_wizard::Joiner::new();
20+
bunch_of_strings.clone().into_iter().for_each(|s| {
21+
joiner.append_raw(s);
22+
});
23+
c.bench_function("Joiner#join", |b| b.iter(|| black_box(joiner.join())));
24+
c.bench_function("Vec#concat", |b| b.iter(|| black_box(bunch_of_strings.concat())));
25+
c.bench_function("manual_push", |b| {
26+
b.iter(|| {
27+
let mut output = String::new();
28+
bunch_of_strings.iter().for_each(|s| {
29+
output.push_str(s);
30+
});
31+
black_box(output)
32+
})
33+
});
34+
c.bench_function("manual_push_with_cap", |b| {
35+
b.iter(|| {
36+
let cap: usize = bunch_of_strings.iter().map(|s| s.len()).sum();
37+
let mut output = String::with_capacity(cap);
38+
bunch_of_strings.iter().for_each(|s| {
39+
output.push_str(s);
40+
});
41+
black_box(output)
42+
})
43+
});
5244
}
5345

5446
criterion_group!(benches, criterion_benchmark);

examples/source_map.rs

+11-18
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
11
use string_wizard::{MagicString, SourceMapOptions, UpdateOptions};
22

33
fn main() {
4-
let demo = "<div>\n hello, world\n</div>";
5-
let mut s = MagicString::new(demo);
4+
let demo = "<div>\n hello, world\n</div>";
5+
let mut s = MagicString::new(demo);
66

7-
let update_options = UpdateOptions {
8-
keep_original: true,
9-
..Default::default()
10-
};
11-
s.prepend("import React from 'react';\n")
12-
.update_with(1, 2, "v", update_options.clone())
13-
.update_with(3, 4, "d", update_options.clone())
14-
.update_with(demo.len() - 4, demo.len() - 1, "h1", update_options.clone());
7+
let update_options = UpdateOptions { keep_original: true, ..Default::default() };
8+
s.prepend("import React from 'react';\n")
9+
.update_with(1, 2, "v", update_options.clone())
10+
.update_with(3, 4, "d", update_options.clone())
11+
.update_with(demo.len() - 4, demo.len() - 1, "h1", update_options.clone());
1512

16-
let sm = s.source_map(SourceMapOptions {
17-
include_content: true,
18-
..Default::default()
19-
});
13+
let sm = s.source_map(SourceMapOptions { include_content: true, ..Default::default() });
2014

21-
std::fs::write("./demo.map.json", sm.to_json_string().unwrap())
22-
.unwrap();
23-
std::fs::write("./demo.jsx", s.to_string()).unwrap();
15+
std::fs::write("./demo.map.json", sm.to_json_string()).unwrap();
16+
std::fs::write("./demo.jsx", s.to_string()).unwrap();
2417

25-
println!("{:#?}", s.to_string());
18+
println!("{:#?}", s.to_string());
2619
}

rustfmt.toml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
tab_spaces = 2
2+
3+
# This is also the setting used by [rustc](https://github.com/rust-lang/rust/blob/master/rustfmt.toml)
4+
use_small_heuristics = "Max"
5+
6+
# Use field initialize shorthand if possible
7+
use_field_init_shorthand = true

src/basic_types.rs

-16
This file was deleted.

0 commit comments

Comments
 (0)