feat: add case preserving search and replace
This commit is contained in:
parent
4dc1042c1d
commit
23868e9df1
5 changed files with 39 additions and 4 deletions
11
Cargo.lock
generated
11
Cargo.lock
generated
|
|
@ -196,6 +196,16 @@ version = "0.8.19"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "248e3bacc7dc6baa3b21e405ee045c3047101a49145e7e9eca583ab4c2ca5345"
|
checksum = "248e3bacc7dc6baa3b21e405ee045c3047101a49145e7e9eca583ab4c2ca5345"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cruet"
|
||||||
|
version = "0.14.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6132609543972496bc97b1e01f1ce6586768870aeb4cabeb3385f4e05b5caead"
|
||||||
|
dependencies = [
|
||||||
|
"once_cell",
|
||||||
|
"regex",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cucumber"
|
name = "cucumber"
|
||||||
version = "0.20.2"
|
version = "0.20.2"
|
||||||
|
|
@ -272,6 +282,7 @@ name = "dev_case"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
|
"cruet",
|
||||||
"cucumber",
|
"cucumber",
|
||||||
"futures",
|
"futures",
|
||||||
"regex",
|
"regex",
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = { version = "4.5.4", features = ["derive"] }
|
clap = { version = "4.5.4", features = ["derive"] }
|
||||||
|
cruet = "0.14.0"
|
||||||
regex = "1.10.4"
|
regex = "1.10.4"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
|
|
||||||
22
src/lib.rs
22
src/lib.rs
|
|
@ -1,9 +1,23 @@
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
|
||||||
|
fn concert_replacement(original: &str, replacement: &str) -> String {
|
||||||
|
if cruet::is_camel_case(original) {
|
||||||
|
cruet::to_camel_case(replacement)
|
||||||
|
} else if cruet::is_kebab_case(original) {
|
||||||
|
cruet::to_kebab_case(replacement)
|
||||||
|
} else if cruet::is_pascal_case(original) {
|
||||||
|
cruet::to_pascal_case(replacement)
|
||||||
|
} else if cruet::is_snake_case(original) {
|
||||||
|
cruet::to_snake_case(replacement)
|
||||||
|
} else {
|
||||||
|
replacement.to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn replace(search: &String, replace: String, input: String) -> String {
|
pub fn replace(search: &String, replace: String, input: String) -> String {
|
||||||
let mut index = 0;
|
let mut index = 0;
|
||||||
let mut output = input;
|
let mut output = input;
|
||||||
let search_pattern = Regex::new(search).unwrap();
|
let search_pattern = Regex::new(&format!("(?i){search}")).unwrap();
|
||||||
|
|
||||||
while let Some(search_match) = search_pattern.find_at(&output, index) {
|
while let Some(search_match) = search_pattern.find_at(&output, index) {
|
||||||
let start = search_match.start();
|
let start = search_match.start();
|
||||||
|
|
@ -19,8 +33,10 @@ pub fn replace(search: &String, replace: String, input: String) -> String {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
index = start + replacement.len();
|
let converted_replacement = concert_replacement(&output[start..end], &replacement);
|
||||||
output.replace_range(start..end, &replacement);
|
output.replace_range(start..end, &converted_replacement);
|
||||||
|
|
||||||
|
index = start + converted_replacement.len();
|
||||||
}
|
}
|
||||||
|
|
||||||
output
|
output
|
||||||
|
|
|
||||||
7
tests/features/case-perserving.feature
Normal file
7
tests/features/case-perserving.feature
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
Feature: Case preserving search and replace
|
||||||
|
|
||||||
|
Scenario: You can search and replace with with a regular expression
|
||||||
|
Given Search is 'productid'
|
||||||
|
And Replace is 'catalogId'
|
||||||
|
And Input is 'function GetProductId(productId)'
|
||||||
|
Then Output is 'function GetCatalogId(catalogId)'
|
||||||
|
|
@ -3,7 +3,7 @@ Feature: Regex search and replace
|
||||||
Scenario: You can search and replace with with a regular expression
|
Scenario: You can search and replace with with a regular expression
|
||||||
Given Search is '(\w+)'
|
Given Search is '(\w+)'
|
||||||
And Replace is 'new'
|
And Replace is 'new'
|
||||||
And Input is 'This is a'
|
And Input is 'this is a'
|
||||||
Then Output is 'new new new'
|
Then Output is 'new new new'
|
||||||
|
|
||||||
Scenario: You can use a '$' to replace a match group
|
Scenario: You can use a '$' to replace a match group
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue