| 1 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::borrowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/::Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#; |
| 2 | |
| 3 | use regexrust-analyzer cargo regex 1.12.3 crate/::{Regexrust-analyzer cargo regex 1.12.3 regex/string/Regex#, RegexBuilderrust-analyzer cargo regex 1.12.3 builders/string/RegexBuilder#}; |
| 4 | |
| 5 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 6 | pub struct RegexReplacementrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/RegexReplacement# { |
| 7 | regexrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/RegexReplacement#regex.: Regexrust-analyzer cargo regex 1.12.3 regex/string/Regex#, |
| 8 | replacementrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/RegexReplacement#replacement.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 9 | replace_allrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/RegexReplacement#replace_all.: bool, |
| 10 | } |
| 11 | |
| 12 | impl RegexReplacementrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/RegexReplacement# { |
| 13 | pub fn from_sed_commandrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/impl#[RegexReplacement]from_sed_command().(sed_command: &str) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Selfrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/impl#[RegexReplacement]> { |
| 14 | let sep = sed_command.charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().().nthrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#nth().(1)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 15 | let mut parts = sed_command[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().2..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..splitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split().(sep); |
| 16 | let regex = parts.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Split<'a, P>`][Iterator]next().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 17 | let replacement = parts.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Split<'a, P>`][Iterator]next().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch()..to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(); |
| 18 | let flags = parts.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Split<'a, P>`][Iterator]next().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 19 | let mut re_builder = RegexBuilderrust-analyzer cargo regex 1.12.3 builders/string/RegexBuilder#::newrust-analyzer cargo regex 1.12.3 builders/string/impl#[RegexBuilder]new().(regex); |
| 20 | let mut replace_all = false; |
| 21 | for flag in flags.charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().() { |
| 22 | match flag { |
| 23 | 'g' => { |
| 24 | replace_all = true; |
| 25 | } |
| 26 | 'i' => { |
| 27 | re_builder.case_insensitiverust-analyzer cargo regex 1.12.3 builders/string/impl#[RegexBuilder]case_insensitive().(true); |
| 28 | } |
| 29 | 'm' => { |
| 30 | re_builder.multi_linerust-analyzer cargo regex 1.12.3 builders/string/impl#[RegexBuilder]multi_line().(true); |
| 31 | } |
| 32 | 's' => { |
| 33 | re_builder.dot_matches_new_linerust-analyzer cargo regex 1.12.3 builders/string/impl#[RegexBuilder]dot_matches_new_line().(true); |
| 34 | } |
| 35 | 'U' => { |
| 36 | re_builder.swap_greedrust-analyzer cargo regex 1.12.3 builders/string/impl#[RegexBuilder]swap_greed().(true); |
| 37 | } |
| 38 | 'x' => { |
| 39 | re_builder.ignore_whitespacerust-analyzer cargo regex 1.12.3 builders/string/impl#[RegexBuilder]ignore_whitespace().(true); |
| 40 | } |
| 41 | _ => {} |
| 42 | } |
| 43 | } |
| 44 | let regex = re_builder.buildrust-analyzer cargo regex 1.12.3 builders/string/impl#[RegexBuilder]build().().okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 45 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(RegexReplacementrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/RegexReplacement# { |
| 46 | regexrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/RegexReplacement#regex., |
| 47 | replacementrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/RegexReplacement#replacement., |
| 48 | replace_allrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/RegexReplacement#replace_all., |
| 49 | }) |
| 50 | } |
| 51 | |
| 52 | pub fn executerust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/impl#[RegexReplacement]execute().<'t>(&self, s: &'t str) -> Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'t, str> { |
| 53 | if self.replace_allrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/RegexReplacement#replace_all. { |
| 54 | self.regexrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/RegexReplacement#regex..replace_allrust-analyzer cargo regex 1.12.3 regex/string/impl#[Regex]replace_all().(s, &self.replacementrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/RegexReplacement#replacement.) |
| 55 | } else { |
| 56 | self.regexrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/RegexReplacement#regex..replacerust-analyzer cargo regex 1.12.3 regex/string/impl#[Regex]replace().(s, &self.replacementrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/RegexReplacement#replacement.) |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | #[cfg(test)] |
| 62 | mod testsrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/tests/ { |
| 63 | use superrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/::*; |
| 64 | |
| 65 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 66 | fn test_sed_commandrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/tests/test_sed_command().() { |
| 67 | let command = "s,foo,bar,"; |
| 68 | let rr = RegexReplacementrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/RegexReplacement#::from_sed_commandrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/impl#[RegexReplacement]from_sed_command().(command).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 69 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(rr.regexrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/RegexReplacement#regex..as_strrust-analyzer cargo regex 1.12.3 regex/string/impl#[Regex]as_str().(), "foo"); |
| 70 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(rr.replacementrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/RegexReplacement#replacement., "bar"); |
| 71 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().rr.replace_allrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/RegexReplacement#replace_all.); |
| 72 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(rr.executerust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/impl#[RegexReplacement]execute().("foo"), "bar"); |
| 73 | } |
| 74 | |
| 75 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 76 | fn test_sed_command_i_flagrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/tests/test_sed_command_i_flag().() { |
| 77 | let command = "s,FOO,bar,"; |
| 78 | let rr = RegexReplacementrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/RegexReplacement#::from_sed_commandrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/impl#[RegexReplacement]from_sed_command().(command).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 79 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(rr.executerust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/impl#[RegexReplacement]execute().("foo"), "foo"); |
| 80 | let command = "s,FOO,bar,i"; |
| 81 | let rr = RegexReplacementrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/RegexReplacement#::from_sed_commandrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/impl#[RegexReplacement]from_sed_command().(command).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 82 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(rr.executerust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/impl#[RegexReplacement]execute().("foo"), "bar"); |
| 83 | } |
| 84 | |
| 85 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 86 | fn test_sed_command_g_flagrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/tests/test_sed_command_g_flag().() { |
| 87 | let command = "s,foo,bar,"; |
| 88 | let rr = RegexReplacementrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/RegexReplacement#::from_sed_commandrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/impl#[RegexReplacement]from_sed_command().(command).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 89 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(rr.executerust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/impl#[RegexReplacement]execute().("foofoo"), "barfoo"); |
| 90 | let command = "s,foo,bar,g"; |
| 91 | let rr = RegexReplacementrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/RegexReplacement#::from_sed_commandrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/impl#[RegexReplacement]from_sed_command().(command).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 92 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(rr.executerust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/impl#[RegexReplacement]execute().("foofoo"), "barbar"); |
| 93 | } |
| 94 | |
| 95 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 96 | fn test_sed_command_with_named_capturesrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/tests/test_sed_command_with_named_captures().() { |
| 97 | let command = r"s/(?P<last>[^,\s]+),\s+(?P<first>\S+)/$first $last/"; |
| 98 | let rr = RegexReplacementrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/RegexReplacement#::from_sed_commandrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/impl#[RegexReplacement]from_sed_command().(command).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 99 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(rr.executerust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/impl#[RegexReplacement]execute().("Springsteen, Bruce"), "Bruce Springsteen"); |
| 100 | } |
| 101 | |
| 102 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 103 | fn test_sed_command_invalidrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/tests/test_sed_command_invalid().() { |
| 104 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(RegexReplacementrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/RegexReplacement#::from_sed_commandrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/impl#[RegexReplacement]from_sed_command().("").is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().()); |
| 105 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(RegexReplacementrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/RegexReplacement#::from_sed_commandrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/impl#[RegexReplacement]from_sed_command().("s").is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().()); |
| 106 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(RegexReplacementrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/RegexReplacement#::from_sed_commandrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/impl#[RegexReplacement]from_sed_command().("s,").is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().()); |
| 107 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(RegexReplacementrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/RegexReplacement#::from_sed_commandrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/impl#[RegexReplacement]from_sed_command().("s,,").is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().()); |
| 108 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(RegexReplacementrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/RegexReplacement#::from_sed_commandrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/impl#[RegexReplacement]from_sed_command().("s,,i").is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().()); |
| 109 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(RegexReplacementrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/RegexReplacement#::from_sed_commandrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/impl#[RegexReplacement]from_sed_command().("s,,,").is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().()); |
| 110 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(RegexReplacementrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/RegexReplacement#::from_sed_commandrust-analyzer cargo git-delta 0.19.2 utils/regex_replacement/impl#[RegexReplacement]from_sed_command().("s,,,i").is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().()); |
| 111 | } |
| 112 | } |