Debian Codegraph @

src/colors.rs

raw symbols SCIP
1 //! An extension trait that provides the ability to strip IRC colors from a string
2 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#;
3
4 enum ParserStaterust-analyzer cargo irc-proto 1.1.0 colors/ParserState# {
5 Textrust-analyzer cargo irc-proto 1.1.0 colors/ParserState#Text#,
6 ColorCoderust-analyzer cargo irc-proto 1.1.0 colors/ParserState#ColorCode#,
7 Foreground1rust-analyzer cargo irc-proto 1.1.0 colors/ParserState#Foreground1#(char),
8 Foreground2rust-analyzer cargo irc-proto 1.1.0 colors/ParserState#Foreground2#,
9 Commarust-analyzer cargo irc-proto 1.1.0 colors/ParserState#Comma#,
10 Background1rust-analyzer cargo irc-proto 1.1.0 colors/ParserState#Background1#(char),
11 }
12 struct Parserrust-analyzer cargo irc-proto 1.1.0 colors/Parser# {
13 staterust-analyzer cargo irc-proto 1.1.0 colors/Parser#state.: ParserStaterust-analyzer cargo irc-proto 1.1.0 colors/ParserState#,
14 }
15
16 /// An extension trait giving strings a function to strip IRC colors
17 pub trait FormattedStringExtrust-analyzer cargo irc-proto 1.1.0 colors/FormattedStringExt#<'a> {
18 /// Returns true if the string contains color, bold, underline or italics
19 fn is_formattedrust-analyzer cargo irc-proto 1.1.0 colors/FormattedStringExt#is_formatted().(&self) -> bool;
20
21 /// Returns the string with all color, bold, underline and italics stripped
22 fn strip_formattingrust-analyzer cargo irc-proto 1.1.0 colors/FormattedStringExt#strip_formatting().(self) -> Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'a, str>;
23 }
24
25 const FORMAT_CHARACTERSrust-analyzer cargo irc-proto 1.1.0 colors/FORMAT_CHARACTERS.: &[char] = &[
26 '\x02', // bold
27 '\x1F', // underline
28 '\x16', // reverse
29 '\x0F', // normal
30 '\x03', // color
31 ];
32
33 impl<'a> FormattedStringExtrust-analyzer cargo irc-proto 1.1.0 colors/FormattedStringExt#<'a> for &'a str {
34 fn is_formattedrust-analyzer cargo irc-proto 1.1.0 colors/impl#[`&'a str`][`FormattedStringExt<'a>`]is_formatted().(&self) -> bool {
35 self.containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]contains().(FORMAT_CHARACTERSrust-analyzer cargo irc-proto 1.1.0 colors/FORMAT_CHARACTERS.)
36 }
37
38 fn strip_formattingrust-analyzer cargo irc-proto 1.1.0 colors/impl#[`&'a str`][`FormattedStringExt<'a>`]strip_formatting().(self) -> Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'a, str> {
39 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().self.is_formattedrust-analyzer cargo irc-proto 1.1.0 colors/impl#[`&'a str`][`FormattedStringExt<'a>`]is_formatted().() {
40 return Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Borrowedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Borrowed#(self);
41 }
42 let mut s = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::fromrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`From<&str>`]from().(self);
43 strip_formattingrust-analyzer cargo irc-proto 1.1.0 colors/strip_formatting().(&mut s);
44 Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Owned#(s)
45 }
46 }
47
48 fn strip_formattingrust-analyzer cargo irc-proto 1.1.0 colors/strip_formatting().(buf: &mut Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) {
49 let mut parser = Parserrust-analyzer cargo irc-proto 1.1.0 colors/Parser#::newrust-analyzer cargo irc-proto 1.1.0 colors/impl#[Parser]new().();
50 buf.retainrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]retain().(|cur| parser.nextrust-analyzer cargo irc-proto 1.1.0 colors/impl#[Parser]next().(cur));
51 }
52
53 impl Parserrust-analyzer cargo irc-proto 1.1.0 colors/Parser# {
54 fn newrust-analyzer cargo irc-proto 1.1.0 colors/impl#[Parser]new().() -> Selfrust-analyzer cargo irc-proto 1.1.0 colors/impl#[Parser] {
55 Parserrust-analyzer cargo irc-proto 1.1.0 colors/Parser# {
56 staterust-analyzer cargo irc-proto 1.1.0 colors/Parser#state.: ParserStaterust-analyzer cargo irc-proto 1.1.0 colors/ParserState#::Textrust-analyzer cargo irc-proto 1.1.0 colors/ParserState#Text#,
57 }
58 }
59
60 fn nextrust-analyzer cargo irc-proto 1.1.0 colors/impl#[Parser]next().(&mut self, cur: char) -> bool {
61 use selfrust-analyzer cargo irc-proto 1.1.0 colors/::ParserStaterust-analyzer cargo irc-proto 1.1.0 colors/ParserState#::*;
62 match self.staterust-analyzer cargo irc-proto 1.1.0 colors/Parser#state. {
63 Textrust-analyzer cargo irc-proto 1.1.0 colors/ParserState#Text# | Foreground1rust-analyzer cargo irc-proto 1.1.0 colors/ParserState#Foreground1#(_) | Foreground2rust-analyzer cargo irc-proto 1.1.0 colors/ParserState#Foreground2# if cur 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().'\x03' => {
64 self.staterust-analyzer cargo irc-proto 1.1.0 colors/Parser#state. = ColorCoderust-analyzer cargo irc-proto 1.1.0 colors/ParserState#ColorCode#;
65 false
66 }
67 Textrust-analyzer cargo irc-proto 1.1.0 colors/ParserState#Text# => !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().FORMAT_CHARACTERSrust-analyzer cargo irc-proto 1.1.0 colors/FORMAT_CHARACTERS..containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]contains().(&cur),
68 ColorCoderust-analyzer cargo irc-proto 1.1.0 colors/ParserState#ColorCode# if cur.is_ascii_digitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]is_ascii_digit().() => {
69 self.staterust-analyzer cargo irc-proto 1.1.0 colors/Parser#state. = Foreground1rust-analyzer cargo irc-proto 1.1.0 colors/ParserState#Foreground1#(cur);
70 false
71 }
72 Foreground1rust-analyzer cargo irc-proto 1.1.0 colors/ParserState#Foreground1#('0') if cur.is_ascii_digitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]is_ascii_digit().() => {
73 // can consume another digit if previous char was 0.
74 self.staterust-analyzer cargo irc-proto 1.1.0 colors/Parser#state. = Foreground2rust-analyzer cargo irc-proto 1.1.0 colors/ParserState#Foreground2#;
75 false
76 }
77 Foreground1rust-analyzer cargo irc-proto 1.1.0 colors/ParserState#Foreground1#('1') if cur.is_digitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]is_digit().(6) => {
78 // can consume another digit if previous char was 1.
79 self.staterust-analyzer cargo irc-proto 1.1.0 colors/Parser#state. = Foreground2rust-analyzer cargo irc-proto 1.1.0 colors/ParserState#Foreground2#;
80 false
81 }
82 Foreground1rust-analyzer cargo irc-proto 1.1.0 colors/ParserState#Foreground1#(_) if cur.is_digitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]is_digit().(6) => {
83 self.staterust-analyzer cargo irc-proto 1.1.0 colors/Parser#state. = Textrust-analyzer cargo irc-proto 1.1.0 colors/ParserState#Text#;
84 true
85 }
86 Foreground1rust-analyzer cargo irc-proto 1.1.0 colors/ParserState#Foreground1#(_) if cur 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().',' => {
87 self.staterust-analyzer cargo irc-proto 1.1.0 colors/Parser#state. = Commarust-analyzer cargo irc-proto 1.1.0 colors/ParserState#Comma#;
88 false
89 }
90 Foreground2rust-analyzer cargo irc-proto 1.1.0 colors/ParserState#Foreground2# if cur 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().',' => {
91 self.staterust-analyzer cargo irc-proto 1.1.0 colors/Parser#state. = Commarust-analyzer cargo irc-proto 1.1.0 colors/ParserState#Comma#;
92 false
93 }
94 Commarust-analyzer cargo irc-proto 1.1.0 colors/ParserState#Comma# if (cur.is_ascii_digitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]is_ascii_digit().()) => {
95 self.staterust-analyzer cargo irc-proto 1.1.0 colors/Parser#state. = Background1rust-analyzer cargo irc-proto 1.1.0 colors/ParserState#Background1#(cur);
96 false
97 }
98 Background1rust-analyzer cargo irc-proto 1.1.0 colors/ParserState#Background1#(prev) if cur.is_digitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]is_digit().(6) => {
99 // can only consume another digit if previous char was 1.
100 self.staterust-analyzer cargo irc-proto 1.1.0 colors/Parser#state. = Textrust-analyzer cargo irc-proto 1.1.0 colors/ParserState#Text#;
101 prev rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]ne().'1'
102 }
103 _ => {
104 self.staterust-analyzer cargo irc-proto 1.1.0 colors/Parser#state. = Textrust-analyzer cargo irc-proto 1.1.0 colors/ParserState#Text#;
105 !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().FORMAT_CHARACTERSrust-analyzer cargo irc-proto 1.1.0 colors/FORMAT_CHARACTERS..containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]contains().(&cur)
106 }
107 }
108 }
109 }
110
111 impl FormattedStringExtrust-analyzer cargo irc-proto 1.1.0 colors/FormattedStringExt#<'static> for Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
112 fn is_formattedrust-analyzer cargo irc-proto 1.1.0 colors/impl#[String][`FormattedStringExt<'static>`]is_formatted().(&self) -> bool {
113 self.as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().().is_formattedrust-analyzer cargo irc-proto 1.1.0 colors/impl#[`&'a str`][`FormattedStringExt<'a>`]is_formatted().()
114 }
115 fn strip_formattingrust-analyzer cargo irc-proto 1.1.0 colors/impl#[String][`FormattedStringExt<'static>`]strip_formatting().(mut self) -> Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'static, str> {
116 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().self.is_formattedrust-analyzer cargo irc-proto 1.1.0 colors/impl#[String][`FormattedStringExt<'static>`]is_formatted().() {
117 return Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Owned#(self);
118 }
119 strip_formattingrust-analyzer cargo irc-proto 1.1.0 colors/strip_formatting().(&mut self);
120 Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Owned#(self)
121 }
122 }
123
124 #[cfg(test)]
125 mod testrust-analyzer cargo irc-proto 1.1.0 colors/test/ {
126 use craterust-analyzer cargo irc-proto 1.1.0 crate/::colorsrust-analyzer cargo irc-proto 1.1.0 colors/::FormattedStringExtrust-analyzer cargo irc-proto 1.1.0 colors/FormattedStringExt#;
127 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#;
128
129 macro_rules! test_formatted_string_extrust-analyzer cargo irc-proto 1.1.0 colors/test/test_formatted_string_ext! {
130 { $( $name:ident ( $($line:tt)* ), )* } => {
131 $(
132 mod $name {
133 use super::*;
134 test_formatted_string_ext!(@ $($line)*);
135 }
136 )*
137 };
138 (@ $text:expr, should stripped into $expected:expr) => {
139 #[test]
140 fn test_formatted() {
141 assert!($text.is_formatted());
142 }
143 #[test]
144 fn test_strip() {
145 assert_eq!($text.strip_formatting(), $expected);
146 }
147 };
148 (@ $text:expr, is not formatted) => {
149 #[test]
150 fn test_formatted() {
151 assert!(!$text.is_formatted());
152 }
153 #[test]
154 fn test_strip() {
155 assert_eq!($text.strip_formatting(), $text);
156 }
157 }
158 }
159
160 test_formatted_string_extrust-analyzer cargo irc-proto 1.1.0 colors/test/test_formatted_string_ext!! {
161 blankrust-analyzer cargo irc-proto 1.1.0 colors/test/blank/("", is not formatted),
162 blank2rust-analyzer cargo irc-proto 1.1.0 colors/test/blank2/(" ", is not formatted),
163 blank3rust-analyzer cargo irc-proto 1.1.0 colors/test/blank3/("\t\r\n", is not formatted),
164 boldrust-analyzer cargo irc-proto 1.1.0 colors/test/bold/("l\x02ol", should stripped into "lol"),
165 bold_from_stringrust-analyzer cargo irc-proto 1.1.0 colors/test/bold_from_string/(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::fromrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`From<&str>`]from().("l\x02ol"), should stripped into "lol"),
166 bold_hangulrust-analyzer cargo irc-proto 1.1.0 colors/test/bold_hangul/("우왕\x02굳", should stripped into "우왕굳"),
167 fg_colorrust-analyzer cargo irc-proto 1.1.0 colors/test/fg_color/("l\x033ol", should stripped into "lol"),
168 fg_color2rust-analyzer cargo irc-proto 1.1.0 colors/test/fg_color2/("l\x0312ol", should stripped into "lol"),
169 fg_color3rust-analyzer cargo irc-proto 1.1.0 colors/test/fg_color3/("l\x0302ol", should stripped into "lol"),
170 fg_color4rust-analyzer cargo irc-proto 1.1.0 colors/test/fg_color4/("l\x030ol", should stripped into "lol"),
171 fg_color5rust-analyzer cargo irc-proto 1.1.0 colors/test/fg_color5/("l\x0309ol", should stripped into "lol"),
172 fg_bg_11rust-analyzer cargo irc-proto 1.1.0 colors/test/fg_bg_11/("l\x031,2ol", should stripped into "lol"),
173 fg_bg_21rust-analyzer cargo irc-proto 1.1.0 colors/test/fg_bg_21/("l\x0312,3ol", should stripped into "lol"),
174 fg_bg_12rust-analyzer cargo irc-proto 1.1.0 colors/test/fg_bg_12/("l\x031,12ol", should stripped into "lol"),
175 fg_bg_22rust-analyzer cargo irc-proto 1.1.0 colors/test/fg_bg_22/("l\x0312,13ol", should stripped into "lol"),
176 fg_boldrust-analyzer cargo irc-proto 1.1.0 colors/test/fg_bold/("l\x0309\x02ol", should stripped into "lol"),
177 string_with_multiple_colorsrust-analyzer cargo irc-proto 1.1.0 colors/test/string_with_multiple_colors/("hoo\x034r\x033a\x0312y", should stripped into "hooray"),
178 string_with_digit_after_colorrust-analyzer cargo irc-proto 1.1.0 colors/test/string_with_digit_after_color/("\x0344\x0355\x0366", should stripped into "456"),
179 string_with_multiple_2digit_colorsrust-analyzer cargo irc-proto 1.1.0 colors/test/string_with_multiple_2digit_colors/("hoo\x0310r\x0311a\x0312y", should stripped into "hooray"),
180 string_with_digit_after_2digit_colorrust-analyzer cargo irc-proto 1.1.0 colors/test/string_with_digit_after_2digit_color/("\x031212\x031111\x031010", should stripped into "121110"),
181 thinkingrust-analyzer cargo irc-proto 1.1.0 colors/test/thinking/("🤔...", is not formatted),
182 unformattedrust-analyzer cargo irc-proto 1.1.0 colors/test/unformatted/("a plain text", is not formatted),
183 }
184
185 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
186 fn test_strip_no_allocation_for_unformatted_textrust-analyzer cargo irc-proto 1.1.0 colors/test/test_strip_no_allocation_for_unformatted_text().() {
187 if let Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Borrowedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Borrowed#(formatted) = "plain text".strip_formattingrust-analyzer cargo irc-proto 1.1.0 colors/impl#[`&'a str`][`FormattedStringExt<'a>`]strip_formatting().() {
188 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(formatted, "plain text");
189 } else {
190 panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("allocation detected");
191 }
192 }
193 }