| 1 | pub mod strrust-analyzer cargo handlebars 5.1.2 support/str/ { |
| 2 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#, Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#}; |
| 3 | |
| 4 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 5 | pub struct StringWriterrust-analyzer cargo handlebars 5.1.2 support/str/StringWriter# { |
| 6 | bufrust-analyzer cargo handlebars 5.1.2 support/str/StringWriter#buf.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, |
| 7 | } |
| 8 | |
| 9 | impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for StringWriterrust-analyzer cargo handlebars 5.1.2 support/str/StringWriter# { |
| 10 | fn defaultrust-analyzer cargo handlebars 5.1.2 support/str/impl#[StringWriter][Default]default().() -> Selfrust-analyzer cargo handlebars 5.1.2 support/str/impl#[StringWriter][Default] { |
| 11 | Selfrust-analyzer cargo handlebars 5.1.2 support/str/impl#[StringWriter][Default]::newrust-analyzer cargo handlebars 5.1.2 support/str/impl#[StringWriter]new().() |
| 12 | } |
| 13 | } |
| 14 | |
| 15 | impl StringWriterrust-analyzer cargo handlebars 5.1.2 support/str/StringWriter# { |
| 16 | pub fn newrust-analyzer cargo handlebars 5.1.2 support/str/impl#[StringWriter]new().() -> StringWriterrust-analyzer cargo handlebars 5.1.2 support/str/StringWriter# { |
| 17 | StringWriterrust-analyzer cargo handlebars 5.1.2 support/str/StringWriter# { |
| 18 | bufrust-analyzer cargo handlebars 5.1.2 support/str/StringWriter#buf.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]with_capacity().(8 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().1024), |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | pub fn into_stringrust-analyzer cargo handlebars 5.1.2 support/str/impl#[StringWriter]into_string().(self) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# { |
| 23 | if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(s) = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::from_utf8rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]from_utf8().(self.bufrust-analyzer cargo handlebars 5.1.2 support/str/StringWriter#buf.) { |
| 24 | s |
| 25 | } else { |
| 26 | Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().() |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | impl Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write# for StringWriterrust-analyzer cargo handlebars 5.1.2 support/str/StringWriter# { |
| 32 | fn writerust-analyzer cargo handlebars 5.1.2 support/str/impl#[StringWriter][Write]write().(&mut self, buf: &[u8]) -> Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<usize> { |
| 33 | self.bufrust-analyzer cargo handlebars 5.1.2 support/str/StringWriter#buf..extend_from_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]extend_from_slice().(buf); |
| 34 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(buf.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()) |
| 35 | } |
| 36 | |
| 37 | fn flushrust-analyzer cargo handlebars 5.1.2 support/str/impl#[StringWriter][Write]flush().(&mut self) -> Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> { |
| 38 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | /// See https://github.com/handlebars-lang/handlebars.js/blob/37411901da42200ced8e1a7fc2f67bf83526b497/lib/handlebars/utils.js#L1 |
| 43 | pub fn escape_htmlrust-analyzer cargo handlebars 5.1.2 support/str/escape_html().(s: &str) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# { |
| 44 | let mut output = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().(); |
| 45 | for c in s.charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().() { |
| 46 | match c { |
| 47 | '<' => output.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().("<"), |
| 48 | '>' => output.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(">"), |
| 49 | '"' => output.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().("""), |
| 50 | '&' => output.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().("&"), |
| 51 | '\'' => output.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().("'"), |
| 52 | '`' => output.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().("`"), |
| 53 | '=' => output.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().("="), |
| 54 | _ => output.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(c), |
| 55 | } |
| 56 | } |
| 57 | output |
| 58 | } |
| 59 | |
| 60 | /// add indent for lines but last |
| 61 | pub fn with_indentrust-analyzer cargo handlebars 5.1.2 support/str/with_indent().(s: &str, indent: &str) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# { |
| 62 | let mut output = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().(); |
| 63 | |
| 64 | let mut it = s.charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().().peekablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#peekable().(); |
| 65 | while let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(c) = it.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/peekable/impl#[`Peekable<I>`][Iterator]next().() { |
| 66 | output.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(c); |
| 67 | // check if c is not the last character, we don't append |
| 68 | // indent for last line break |
| 69 | if c rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq().'\n' && it.peekrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/peekable/impl#[`Peekable<I>`]peek().().is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().() { |
| 70 | output.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(indent); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | output |
| 75 | } |
| 76 | |
| 77 | #[inline] |
| 78 | pub(craterust-analyzer cargo handlebars 5.1.2 crate/) fn whitespace_matcherrust-analyzer cargo handlebars 5.1.2 support/str/whitespace_matcher().(c: char) -> bool { |
| 79 | c rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq().' ' || c rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq().'\t' |
| 80 | } |
| 81 | |
| 82 | #[inline] |
| 83 | pub(craterust-analyzer cargo handlebars 5.1.2 crate/) fn newline_matcherrust-analyzer cargo handlebars 5.1.2 support/str/newline_matcher().(c: char) -> bool { |
| 84 | c rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq().'\n' || c rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq().'\r' |
| 85 | } |
| 86 | |
| 87 | #[inline] |
| 88 | pub(craterust-analyzer cargo handlebars 5.1.2 crate/) fn strip_first_newlinerust-analyzer cargo handlebars 5.1.2 support/str/strip_first_newline().(s: &str) -> &str { |
| 89 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(s) = s.strip_prefixrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]strip_prefix().("\r\n") { |
| 90 | s |
| 91 | } else if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(s) = s.strip_prefixrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]strip_prefix().('\n') { |
| 92 | s |
| 93 | } else { |
| 94 | s |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | pub(craterust-analyzer cargo handlebars 5.1.2 crate/) fn find_trailing_whitespace_charsrust-analyzer cargo handlebars 5.1.2 support/str/find_trailing_whitespace_chars().(s: &str) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&str> { |
| 99 | let trimmed = s.trim_end_matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim_end_matches().(whitespace_matcherrust-analyzer cargo handlebars 5.1.2 support/str/whitespace_matcher().); |
| 100 | if trimmed.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().s.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().() { |
| 101 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 102 | } else { |
| 103 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&s[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().trimmed.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().()..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().) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | pub(craterust-analyzer cargo handlebars 5.1.2 crate/) fn ends_with_empty_linerust-analyzer cargo handlebars 5.1.2 support/str/ends_with_empty_line().(text: &str) -> bool { |
| 108 | let s = text.trim_end_matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim_end_matches().(whitespace_matcherrust-analyzer cargo handlebars 5.1.2 support/str/whitespace_matcher().); |
| 109 | // also matches when text is just whitespaces |
| 110 | s.ends_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]ends_with().(newline_matcherrust-analyzer cargo handlebars 5.1.2 support/str/newline_matcher().) || s.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() |
| 111 | } |
| 112 | |
| 113 | pub(craterust-analyzer cargo handlebars 5.1.2 crate/) fn starts_with_empty_linerust-analyzer cargo handlebars 5.1.2 support/str/starts_with_empty_line().(text: &str) -> bool { |
| 114 | text.trim_start_matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim_start_matches().(whitespace_matcherrust-analyzer cargo handlebars 5.1.2 support/str/whitespace_matcher().) |
| 115 | .starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().(newline_matcherrust-analyzer cargo handlebars 5.1.2 support/str/newline_matcher().) |
| 116 | } |
| 117 | |
| 118 | #[cfg(test)] |
| 119 | mod testrust-analyzer cargo handlebars 5.1.2 support/str/test/ { |
| 120 | use craterust-analyzer cargo handlebars 5.1.2 crate/::supportrust-analyzer cargo handlebars 5.1.2 support/::strrust-analyzer cargo handlebars 5.1.2 support/str/::StringWriterrust-analyzer cargo handlebars 5.1.2 support/str/StringWriter#; |
| 121 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#; |
| 122 | |
| 123 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 124 | fn test_string_writerrust-analyzer cargo handlebars 5.1.2 support/str/test/test_string_writer().() { |
| 125 | let mut sw = StringWriterrust-analyzer cargo handlebars 5.1.2 support/str/StringWriter#::newrust-analyzer cargo handlebars 5.1.2 support/str/impl#[StringWriter]new().(); |
| 126 | |
| 127 | let _ = sw.writerust-analyzer cargo handlebars 5.1.2 support/str/impl#[StringWriter][Write]write().("hello".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().().into_bytesrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]into_bytes().().as_refrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`AsRef<[T]>`]as_ref().()); |
| 128 | let _ = sw.writerust-analyzer cargo handlebars 5.1.2 support/str/impl#[StringWriter][Write]write().("world".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().().into_bytesrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]into_bytes().().as_refrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`AsRef<[T]>`]as_ref().()); |
| 129 | |
| 130 | let s = sw.into_stringrust-analyzer cargo handlebars 5.1.2 support/str/impl#[StringWriter]into_string().(); |
| 131 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(s, "helloworld".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()); |
| 132 | } |
| 133 | } |
| 134 | } |