Skip to content

Commit

Permalink
Feature/&str (#49)
Browse files Browse the repository at this point in the history
* Changed from `String` to `&str` to gain extra speed and fewer allocations.

This moves the library to use `&str` as the input for most casting to
avoid the extra allocation required for `to_string()` or `to_owned()`.

Overall on local testing the benchmarks show a gain in every area.

* Updated version

* Updated changelog and readme to reflect changes

See #47

* Passed char as &char

* Updated readme with codecov

* Extra cargo meta-data
  • Loading branch information
whatisinternet committed Mar 10, 2017
1 parent bddf89c commit 613ef36
Show file tree
Hide file tree
Showing 23 changed files with 414 additions and 386 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# 0.9.0

## Breaking changes:
- Changed type signature for all casting methods to use `&str`. This gives the
crate a >10% increase in performance in a majority of conversions. Although
most users only use:
`"my_string".to_camel_case()`
or
`"myString".to_string().to_snake_case()`
For those using the `to_camel_case("something".to_string())` this will be a
breaking change as the new syntax demands `to_camel_case("something")`.


# 0.8.1

## Bugfixes:
Expand Down
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "Inflector"
version = "0.8.1"
version = "0.9.0"
authors = ["Josh Teeter<joshteeter@gmail.com>"]
exclude = [".travis.yml", ".gitignore"]
readme = "README.md"
Expand All @@ -14,6 +14,8 @@ Adds String based inflections for Rust. Snake, kebab, camel, sentence, class, ti
keywords = ["pluralize", "string", "camel", "snake", "inflection"]
categories = ["text-processing", "value-formatting"]

[badges]
travis-ci = { repository = "whatisinternet/inflector" }

[features]
default = ['heavyweight']
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# Rust Inflector


[![Build Status](https://travis-ci.org/whatisinternet/inflector.svg?branch=master)](https://travis-ci.org/whatisinternet/inflector) [![Crates.io](https://img.shields.io/crates/v/inflector.svg)](https://crates.io/crates/inflector)
[![Build Status](https://travis-ci.org/whatisinternet/inflector.svg?branch=master)](https://travis-ci.org/whatisinternet/inflector) [![codecov](https://codecov.io/gh/whatisinternet/inflector/branch/master/graph/badge.svg)](https://codecov.io/gh/whatisinternet/inflector) [![Crates.io](https://img.shields.io/crates/v/inflector.svg)](https://crates.io/crates/inflector)


Adds String based inflections for Rust. Snake, kebab, train, camel,
sentence, class, and title cases as well as ordinalize,
deordinalize, demodulize, deconstantize, foreign key, table case, and pluralize/singularize are supported as both traits and pure functions
acting on String types.
acting on &str and String types.

-----
## Documentation:
Expand Down Expand Up @@ -53,7 +54,7 @@ Or

```rust
...
// to use methods like to_snake_case(String);
// to use methods like to_snake_case(&str);
extern crate inflector;

// use inflector::cases::classcase::to_class_case;
Expand Down Expand Up @@ -100,7 +101,7 @@ extern crate inflector;
...
fn main() {
...
let camel_case_string: String = to_camel_case("some_string".to_string());
let camel_case_string: String = to_camel_case("some_string");
...
}

Expand Down
58 changes: 29 additions & 29 deletions src/cases/camelcase/mod.rs
Original file line number Diff line number Diff line change
@@ -1,81 +1,81 @@
#![deny(warnings)]
use cases::case::*;

/// Converts a `String` to camelCase `String`
/// Converts a `&str` to camelCase `String`
///
/// ```
/// use inflector::cases::camelcase::to_camel_case;
/// let mock_string: String = "fooBar".to_string();
/// let mock_string: &str = "fooBar";
/// let expected_string: String = "fooBar".to_string();
/// let asserted_string: String = to_camel_case(mock_string);
/// assert!(asserted_string == expected_string);
///
/// ```
/// ```
/// use inflector::cases::camelcase::to_camel_case;
/// let mock_string: String = "FOO_BAR".to_string();
/// let mock_string: &str = "FOO_BAR";
/// let expected_string: String = "fooBar".to_string();
/// let asserted_string: String = to_camel_case(mock_string);
/// assert!(asserted_string == expected_string);
///
/// ```
/// ```
/// use inflector::cases::camelcase::to_camel_case;
/// let mock_string: String = "Foo Bar".to_string();
/// let mock_string: &str = "Foo Bar";
/// let expected_string: String = "fooBar".to_string();
/// let asserted_string: String = to_camel_case(mock_string);
/// assert!(asserted_string == expected_string);
///
/// ```
/// ```
/// use inflector::cases::camelcase::to_camel_case;
/// let mock_string: String = "foo_bar".to_string();
/// let mock_string: &str = "foo_bar";
/// let expected_string: String = "fooBar".to_string();
/// let asserted_string: String = to_camel_case(mock_string);
/// assert!(asserted_string == expected_string);
///
/// ```
/// ```
/// use inflector::cases::camelcase::to_camel_case;
/// let mock_string: String = "Foo bar".to_string();
/// let mock_string: &str = "Foo bar";
/// let expected_string: String = "fooBar".to_string();
/// let asserted_string: String = to_camel_case(mock_string);
/// assert!(asserted_string == expected_string);
///
/// ```
/// ```
/// use inflector::cases::camelcase::to_camel_case;
/// let mock_string: String = "foo-bar".to_string();
/// let mock_string: &str = "foo-bar";
/// let expected_string: String = "fooBar".to_string();
/// let asserted_string: String = to_camel_case(mock_string);
/// assert!(asserted_string == expected_string);
///
/// ```
/// ```
/// use inflector::cases::camelcase::to_camel_case;
/// let mock_string: String = "FooBar".to_string();
/// let mock_string: &str = "FooBar";
/// let expected_string: String = "fooBar".to_string();
/// let asserted_string: String = to_camel_case(mock_string);
/// assert!(asserted_string == expected_string);
///
/// ```
/// ```
/// use inflector::cases::camelcase::to_camel_case;
/// let mock_string: String = "FooBar3".to_string();
/// let mock_string: &str = "FooBar3";
/// let expected_string: String = "fooBar3".to_string();
/// let asserted_string: String = to_camel_case(mock_string);
/// assert!(asserted_string == expected_string);
///
/// ```
/// ```
/// use inflector::cases::camelcase::to_camel_case;
/// let mock_string: String = "Foo-Bar".to_string();
/// let mock_string: &str = "Foo-Bar";
/// let expected_string: String = "fooBar".to_string();
/// let asserted_string: String = to_camel_case(mock_string);
/// assert!(asserted_string == expected_string);
///
/// ```
pub fn to_camel_case(non_camelized_string: String) -> String {
pub fn to_camel_case(non_camelized_string: &str) -> String {
let options = CamelOptions {
new_word: false,
last_char: ' ',
Expand All @@ -84,91 +84,91 @@ pub fn to_camel_case(non_camelized_string: String) -> String {
has_seperator: false,
inverted: false,
};
to_case_camel_like(non_camelized_string, options)
to_case_camel_like(&non_camelized_string, options)
}

/// Determines if a `String` is camelCase bool``
/// Determines if a `&str` is camelCase bool``
///
/// ```
/// use inflector::cases::camelcase::is_camel_case;
/// let mock_string: String = "Foo".to_string();
/// let mock_string: &str = "Foo";
/// let asserted_bool: bool = is_camel_case(mock_string);
/// assert!(asserted_bool == false);
///
///
/// ```
/// ```
/// use inflector::cases::camelcase::is_camel_case;
/// let mock_string: String = "foo".to_string();
/// let mock_string: &str = "foo";
/// let asserted_bool: bool = is_camel_case(mock_string);
/// assert!(asserted_bool == true);
///
///
/// ```
/// ```
/// use inflector::cases::camelcase::is_camel_case;
/// let mock_string: String = "foo-bar-string-that-is-really-really-long".to_string();
/// let mock_string: &str = "foo-bar-string-that-is-really-really-long";
/// let asserted_bool: bool = is_camel_case(mock_string);
/// assert!(asserted_bool == false);
///
///
/// ```
/// ```
/// use inflector::cases::camelcase::is_camel_case;
/// let mock_string: String = "FooBarIsAReallyReallyLongString".to_string();
/// let mock_string: &str = "FooBarIsAReallyReallyLongString";
/// let asserted_bool: bool = is_camel_case(mock_string);
/// assert!(asserted_bool == false);
///
///
/// ```
/// ```
/// use inflector::cases::camelcase::is_camel_case;
/// let mock_string: String = "fooBarIsAReallyReally3LongString".to_string();
/// let mock_string: &str = "fooBarIsAReallyReally3LongString";
/// let asserted_bool: bool = is_camel_case(mock_string);
/// assert!(asserted_bool == true);
///
///
/// ```
/// ```
/// use inflector::cases::camelcase::is_camel_case;
/// let mock_string: String = "fooBarIsAReallyReallyLongString".to_string();
/// let mock_string: &str = "fooBarIsAReallyReallyLongString";
/// let asserted_bool: bool = is_camel_case(mock_string);
/// assert!(asserted_bool == true);
///
///
/// ```
/// ```
/// use inflector::cases::camelcase::is_camel_case;
/// let mock_string: String = "FOO_BAR_STRING_THAT_IS_REALLY_REALLY_LONG".to_string();
/// let mock_string: &str = "FOO_BAR_STRING_THAT_IS_REALLY_REALLY_LONG";
/// let asserted_bool: bool = is_camel_case(mock_string);
/// assert!(asserted_bool == false);
///
///
/// ```
/// ```
/// use inflector::cases::camelcase::is_camel_case;
/// let mock_string: String = "foo_bar_string_that_is_really_really_long".to_string();
/// let mock_string: &str = "foo_bar_string_that_is_really_really_long";
/// let asserted_bool: bool = is_camel_case(mock_string);
/// assert!(asserted_bool == false);
///
///
/// ```
/// ```
/// use inflector::cases::camelcase::is_camel_case;
/// let mock_string: String = "Foo bar string that is really really long".to_string();
/// let mock_string: &str = "Foo bar string that is really really long";
/// let asserted_bool: bool = is_camel_case(mock_string);
/// assert!(asserted_bool == false);
///
///
/// ```
/// ```
/// use inflector::cases::camelcase::is_camel_case;
/// let mock_string: String = "Foo Bar Is A Really Really Long String".to_string();
/// let mock_string: &str = "Foo Bar Is A Really Really Long String";
/// let asserted_bool: bool = is_camel_case(mock_string);
/// assert!(asserted_bool == false);
/// ```
pub fn is_camel_case(test_string: String) -> bool {
to_camel_case(test_string.clone()) == test_string
pub fn is_camel_case(test_string: &str) -> bool {
to_camel_case(&test_string.clone()) == test_string
}

#[cfg(all(feature = "unstable", test))]
Expand All @@ -179,31 +179,31 @@ mod tests {
#[bench]
fn bench_camel0(b: &mut Bencher) {
b.iter(|| {
let test_string = "Foo bar".to_string();
let test_string = "Foo bar";
super::to_camel_case(test_string)
});
}

#[bench]
fn bench_camel1(b: &mut Bencher) {
b.iter(|| {
let test_string = "foo_bar".to_string();
let test_string = "foo_bar";
super::to_camel_case(test_string)
});
}

#[bench]
fn bench_camel2(b: &mut Bencher) {
b.iter(|| {
let test_string = "fooBar".to_string();
let test_string = "fooBar";
super::to_camel_case(test_string)
});
}

#[bench]
fn bench_is_camel(b: &mut Bencher) {
b.iter(|| {
let test_string: String = "Foo bar".to_string();
let test_string: &str = "Foo bar";
super::is_camel_case(test_string)
});
}
Expand Down
Loading

0 comments on commit 613ef36

Please sign in to comment.