From a5b8d71feba5b1c1763a8079e5956c08689ef968 Mon Sep 17 00:00:00 2001 From: Dean Karn Date: Sun, 24 Jan 2021 17:27:50 -0800 Subject: [PATCH 1/5] benchmark updates + linter suggestions --- Cargo.toml | 4 +- benches/bench.rs | 100 ++++++++++++++++++++++++++--------------------- src/unicode.rs | 11 ++---- 3 files changed, 60 insertions(+), 55 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 27175c7..b823191 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,7 @@ name = "bench" [dependencies] [dev-dependencies] -criterion = "0.2.11" +criterion = { version = "0.3.4", features = ["html_reports"] } [lib] -bench = false \ No newline at end of file +bench = false diff --git a/benches/bench.rs b/benches/bench.rs index c769a6e..45eb106 100644 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -1,56 +1,66 @@ #[macro_use] extern crate criterion; -use criterion::{Benchmark, Criterion, Throughput}; +use criterion::{BenchmarkId, Criterion, Throughput}; use snakecase::ascii::to_snakecase as to_snakecase_ascii; use snakecase::unicode::to_snakecase as to_snakecase_unicode; -fn criterion_benchmark(c: &mut Criterion) { - macro_rules! snakecase_ascii_bench { - ($name:expr,$s:expr) => { - c.bench( - "ascii", - Benchmark::new($name, |b| b.iter(|| to_snakecase_ascii($s))) - .throughput(Throughput::Bytes($s.as_bytes().len() as u32)), - ); - }; +fn bench_ascii(c: &mut Criterion) { + let mut group = c.benchmark_group("ascii"); + + for (name, input) in [ + ("ascii_owned_simple", "sample text"), + ("ascii_borrowed_simple", "sample_text"), + ("ascii_owned_long", "inviteYourCustomersAddInvites"), + ("ascii_borrowed_long", "invite_your_customers_add_invites"), + ( + "ascii_owned_long_special_chars", + "FOO:BAR$BAZ__Sample Text___", + ), + ("ascii_owned_unicode", "ẞ•¶§ƒ˚foo˙∆˚¬"), + ("ascii_borrowed_unicode", "ß_ƒ_foo"), + ("ascii_digit_uppercase", "5TEst"), + ("ascii_starts_with_garbage", "@%#&5TEst"), + ("ascii_complex_random", "lk0B@bFmjrLQ_Z6YL"), + ] + .iter() + { + group.throughput(Throughput::Bytes(input.len() as u64)); + group.bench_with_input(BenchmarkId::new(*name, input), input, |b, input| { + b.iter(|| to_snakecase_ascii(*input)) + }); } - snakecase_ascii_bench!("ascii_owned_simple", "sample text"); - snakecase_ascii_bench!("ascii_borrowed_simple", "sample_text"); - snakecase_ascii_bench!("ascii_owned_long", "inviteYourCustomersAddInvites"); - snakecase_ascii_bench!("ascii_borrowed_long", "invite_your_customers_add_invites"); - snakecase_ascii_bench!( - "ascii_owned_long_special_chars", - "FOO:BAR$BAZ__Sample Text___" - ); - snakecase_ascii_bench!("ascii_owned_unicode", "ẞ•¶§ƒ˚foo˙∆˚¬"); - snakecase_ascii_bench!("ascii_borrowed_unicode", "ß_ƒ_foo"); - snakecase_ascii_bench!("ascii_digit_uppercase", "5TEst"); - snakecase_ascii_bench!("ascii_starts_with_garbage", "@%#&5TEst"); - snakecase_ascii_bench!("ascii_complex_random", "lk0B@bFmjrLQ_Z6YL"); - - macro_rules! snakecase_bench { - ($name:expr,$s:expr) => { - c.bench( - "unicode", - Benchmark::new($name, |b| b.iter(|| to_snakecase_unicode($s))) - .throughput(Throughput::Bytes($s.as_bytes().len() as u32)), - ); - }; + + group.finish(); +} + +fn bench_unicode(c: &mut Criterion) { + let mut group = c.benchmark_group("unicode"); + + for (name, input) in [ + ("unicode_owned_simple", "sample text"), + ("unicode_borrowed_simple", "sample_text"), + ("unicode_borrowed_long", "invite_your_customers_add_invites"), + ( + "unicode_owned_long_special_chars", + "FOO:BAR$BAZ__Sample Text___", + ), + ("unicode_owned_unicode", "ẞ•¶§ƒ˚foo˙∆˚¬"), + ("unicode_borrowed_unicode", "ß_ƒ_foo"), + ("unicode_digit_uppercase", "5TEst"), + ("unicode_starts_with_garbage", "@%#&5TEst"), + ("unicode_complex_random", "lk0B@bFmjrLQ_Z6YL"), + ] + .iter() + { + group.throughput(Throughput::Bytes(input.len() as u64)); + group.bench_with_input(BenchmarkId::new(*name, input), input, |b, input| { + b.iter(|| to_snakecase_unicode(*input)) + }); } - snakecase_bench!("unicode_owned_simple", "sample text"); - snakecase_bench!("unicode_borrowed_simple", "sample_text"); - snakecase_bench!("unicode_borrowed_long", "invite_your_customers_add_invites"); - snakecase_bench!( - "unicode_owned_long_special_chars", - "FOO:BAR$BAZ__Sample Text___" - ); - snakecase_bench!("unicode_owned_unicode", "ẞ•¶§ƒ˚foo˙∆˚¬"); - snakecase_bench!("unicode_borrowed_unicode", "ß_ƒ_foo"); - snakecase_bench!("unicode_digit_uppercase", "5TEst"); - snakecase_bench!("unicode_starts_with_garbage", "@%#&5TEst"); - snakecase_bench!("unicode_complex_random", "lk0B@bFmjrLQ_Z6YL"); + + group.finish(); } -criterion_group!(benches, criterion_benchmark); +criterion_group!(benches, bench_ascii, bench_unicode); criterion_main!(benches); diff --git a/src/unicode.rs b/src/unicode.rs index 566a795..573e323 100644 --- a/src/unicode.rs +++ b/src/unicode.rs @@ -25,13 +25,13 @@ where } else if i > 0 && c == UNDERSCORE_CHAR { if let Some((_, c)) = chars.peek() { if c.is_lowercase() { - chars.nth(0); + chars.next(); has_lower = true; has_underscore = true; has_lower_since_underscore = true; continue; } else if c.is_numeric() { - chars.nth(0); + chars.next(); has_underscore = true; has_lower_since_underscore = false; continue; @@ -197,12 +197,7 @@ mod tests { false ); snakecase_test!(camel_case, "CStringRef", "cstring_ref", false); - snakecase_test!( - unicode_mixed, - "ẞ•¶§ƒ˚foo˙∆˚¬", - "ß_ƒ_foo", - false - ); + snakecase_test!(unicode_mixed, "ẞ•¶§ƒ˚foo˙∆˚¬", "ß_ƒ_foo", false); snakecase_test!(unicode_uppercase, "ẞ", "ß", false); // capitol unicode german to lowercase snakecase_test!( special_chars_long, From ef29020a89ca7ef65bc7697a002fc3eaa73cbd54 Mon Sep 17 00:00:00 2001 From: Dean Karn Date: Sun, 24 Jan 2021 17:29:13 -0800 Subject: [PATCH 2/5] update to use GitHub Actions --- .github/workflows/rust.yml | 50 ++++++++++++++++++++++++++++++++++++++ .travis.yml | 27 -------------------- 2 files changed, 50 insertions(+), 27 deletions(-) create mode 100644 .github/workflows/rust.yml delete mode 100644 .travis.yml diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml new file mode 100644 index 0000000..cf92557 --- /dev/null +++ b/.github/workflows/rust.yml @@ -0,0 +1,50 @@ +name: Lint & Test +on: + pull_request: + types: [opened, edited, reopened, synchronize] +jobs: + test: + strategy: + matrix: + platform: [ubuntu-latest, macos-latest, windows-latest] + runs-on: ${{ matrix.platform }} + steps: + - name: Install Rust Stable + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + + - name: Install Cargo + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + components: clippy + + - name: Checkout code + uses: actions/checkout@v2 + + - name: Cache cargo registry + uses: actions/cache@v1 + with: + path: ~/.cargo/registry + key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }} + + - name: Cache cargo index + uses: actions/cache@v1 + with: + path: ~/.cargo/git + key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }} + + - name: Cache cargo build + uses: actions/cache@v1 + with: + path: target + key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }} + + - name: Lint + uses: actions-rs/clippy@master + with: + args: --all-features --all-targets + + - name: Test + run: cargo test --all-features \ No newline at end of file diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 02cc7e2..0000000 --- a/.travis.yml +++ /dev/null @@ -1,27 +0,0 @@ -language: rust - -matrix: - include: - - rust: stable - script: - - cargo test - - cargo build - - - rust: nightly - script: - - cargo test - - cargo build - - - rust: nightly - name: Clippy - script: - - rustup component add clippy || travis_terminate 0 - - cargo clippy -- -D clippy::all - - allow_failures: - - rust: nightly - name: Clippy - -script: - - cargo test - - cargo build From bdc22153964f6ec013cb57d683e8b9e761ffd2a8 Mon Sep 17 00:00:00 2001 From: Dean Karn Date: Sun, 24 Jan 2021 17:29:45 -0800 Subject: [PATCH 3/5] update to version 0.2.0 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index b823191..6bf087e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ authors = ["Dean Karn "] license = "MIT OR Apache-2.0" edition = "2018" name = "snakecase" -version = "0.1.0" +version = "0.2.0" description = "Snakecase is a general purpose snakecase implementation supporting both ascii and unicode." repository = "https://github.com/rust-playground/snakecase" readme = "README.md" From 5cb9dee41749e1e4424f3bd66d85889f469ca851 Mon Sep 17 00:00:00 2001 From: Dean Karn Date: Sun, 24 Jan 2021 18:16:51 -0800 Subject: [PATCH 4/5] update README badges --- Cargo.toml | 3 --- README.md | 5 ++--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6bf087e..529f4a1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,9 +9,6 @@ repository = "https://github.com/rust-playground/snakecase" readme = "README.md" keywords = ["snakecase"] -[badges] -travis-ci = { repository = "go-playground/snakecase" } - [[bench]] harness = false name = "bench" diff --git a/README.md b/README.md index d5c10e0..694b4dc 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ -# Snakecase   [![Build Status]][travis] [![Latest Version]][crates.io] +# Snakecase   ![Build Status] [![Latest Version]][crates.io] -[Build Status]: https://api.travis-ci.org/rust-playground/snakecase.svg?branch=master -[travis]: https://travis-ci.org/rust-playground/snakecase +[Build Status]: https://github.com/rust-playground/snakecase/workflows/Lint%20&%20Test/badge.svg [Latest Version]: https://img.shields.io/crates/v/snakecase.svg [crates.io]: https://crates.io/crates/snakecase From 43f9392d711dcc46abe99286730e064d22842404 Mon Sep 17 00:00:00 2001 From: Dean Karn Date: Sun, 24 Jan 2021 18:21:35 -0800 Subject: [PATCH 5/5] remove uneeded main fn main in doc tests --- src/lib.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 8a54c74..868d76a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,10 +7,8 @@ //! ```rust //! use snakecase::ascii::to_snakecase; //! -//! fn main() { //! let input = "sample text"; //! println!("{} => {}", input, to_snakecase(input)); -//! } //! ``` //! //! or when you need unicode support @@ -18,10 +16,8 @@ //! ```rust //! use snakecase::unicode::to_snakecase; //! -//! fn main() { //! let input = "ƒun sample text"; //! println!("{} => {}", input, to_snakecase(input)); -//! } //! ``` //!