Skip to content

Commit

Permalink
feat: rework of API and internal structures (#21)
Browse files Browse the repository at this point in the history
* tweak

* fmt

* tweak2

* Remove once cell

* Remove fields

* Fix empty string sourcemap issue

* feature-gate-sourcemap

* tweak

* update deps

* bump version
  • Loading branch information
hyf0 authored Aug 25, 2024
1 parent df8ac4d commit 16ec5a5
Show file tree
Hide file tree
Showing 28 changed files with 1,373 additions and 1,501 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = false
9 changes: 4 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
[package]
name = "string_wizard"
version = "0.0.21"
version = "0.0.22"
edition = "2021"
license = "MIT"
description = "manipulate string like wizards"
description = "manipulate string like a wizard"

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

[dependencies]
index_vec = { version = "0.1.3" }
rustc-hash = { version = "1.1.0" }
once_cell = "1.18.0"
oxc_sourcemap = { version = "~0.15.0" }
oxc_sourcemap = { version = "0.25.0", optional = true}

[features]
# Enable source map functionality
source_map = []
source_map = ['dep:oxc_sourcemap']

[dev-dependencies]
glob = "0.3.1"
Expand Down
80 changes: 36 additions & 44 deletions benches/joiner_join.rs
Original file line number Diff line number Diff line change
@@ -1,54 +1,46 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};

fn get_bunch_of_strings() -> Vec<String> {
let files = glob::glob("fixtures/threejs_src/**/*.js").unwrap();
let mut files = files
.into_iter()
.map(|p| p.unwrap().canonicalize().unwrap())
.collect::<Vec<_>>();
files.sort();
let stirngs = files
.iter()
.map(|p| std::fs::read_to_string(p).unwrap())
.collect::<Vec<_>>();
let files = glob::glob("fixtures/threejs_src/**/*.js").unwrap();
let mut files = files.into_iter().map(|p| p.unwrap().canonicalize().unwrap()).collect::<Vec<_>>();
files.sort();
let stirngs = files.iter().map(|p| std::fs::read_to_string(p).unwrap()).collect::<Vec<_>>();

let mut ret = vec![];
for _ in 0..10 {
ret.extend(stirngs.clone());
}
ret
let mut ret = vec![];
for _ in 0..10 {
ret.extend(stirngs.clone());
}
ret
}

fn criterion_benchmark(c: &mut Criterion) {
let bunch_of_strings = get_bunch_of_strings();

let mut joiner = string_wizard::Joiner::new();
bunch_of_strings.clone().into_iter().for_each(|s| {
joiner.append_raw(s);
});
c.bench_function("Joiner#join", |b| b.iter(|| black_box(joiner.join())));
c.bench_function("Vec#concat", |b| {
b.iter(|| black_box(bunch_of_strings.concat()))
});
c.bench_function("manual_push", |b| {
b.iter(|| {
let mut output = String::new();
bunch_of_strings.iter().for_each(|s| {
output.push_str(s);
});
black_box(output)
})
});
c.bench_function("manual_push_with_cap", |b| {
b.iter(|| {
let cap: usize = bunch_of_strings.iter().map(|s| s.len()).sum();
let mut output = String::with_capacity(cap);
bunch_of_strings.iter().for_each(|s| {
output.push_str(s);
});
black_box(output)
})
});
let bunch_of_strings = get_bunch_of_strings();

let mut joiner = string_wizard::Joiner::new();
bunch_of_strings.clone().into_iter().for_each(|s| {
joiner.append_raw(s);
});
c.bench_function("Joiner#join", |b| b.iter(|| black_box(joiner.join())));
c.bench_function("Vec#concat", |b| b.iter(|| black_box(bunch_of_strings.concat())));
c.bench_function("manual_push", |b| {
b.iter(|| {
let mut output = String::new();
bunch_of_strings.iter().for_each(|s| {
output.push_str(s);
});
black_box(output)
})
});
c.bench_function("manual_push_with_cap", |b| {
b.iter(|| {
let cap: usize = bunch_of_strings.iter().map(|s| s.len()).sum();
let mut output = String::with_capacity(cap);
bunch_of_strings.iter().for_each(|s| {
output.push_str(s);
});
black_box(output)
})
});
}

criterion_group!(benches, criterion_benchmark);
Expand Down
29 changes: 11 additions & 18 deletions examples/source_map.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
use string_wizard::{MagicString, SourceMapOptions, UpdateOptions};

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

let update_options = UpdateOptions {
keep_original: true,
..Default::default()
};
s.prepend("import React from 'react';\n")
.update_with(1, 2, "v", update_options.clone())
.update_with(3, 4, "d", update_options.clone())
.update_with(demo.len() - 4, demo.len() - 1, "h1", update_options.clone());
let update_options = UpdateOptions { keep_original: true, ..Default::default() };
s.prepend("import React from 'react';\n")
.update_with(1, 2, "v", update_options.clone())
.update_with(3, 4, "d", update_options.clone())
.update_with(demo.len() - 4, demo.len() - 1, "h1", update_options.clone());

let sm = s.source_map(SourceMapOptions {
include_content: true,
..Default::default()
});
let sm = s.source_map(SourceMapOptions { include_content: true, ..Default::default() });

std::fs::write("./demo.map.json", sm.to_json_string().unwrap())
.unwrap();
std::fs::write("./demo.jsx", s.to_string()).unwrap();
std::fs::write("./demo.map.json", sm.to_json_string()).unwrap();
std::fs::write("./demo.jsx", s.to_string()).unwrap();

println!("{:#?}", s.to_string());
println!("{:#?}", s.to_string());
}
7 changes: 7 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
tab_spaces = 2

# This is also the setting used by [rustc](https://github.com/rust-lang/rust/blob/master/rustfmt.toml)
use_small_heuristics = "Max"

# Use field initialize shorthand if possible
use_field_init_shorthand = true
16 changes: 0 additions & 16 deletions src/basic_types.rs

This file was deleted.

Loading

0 comments on commit 16ec5a5

Please sign in to comment.