Reduce allocation more

String::strip_formatting will do most of operations in place.
This commit is contained in:
Eunchong Yu 2018-08-19 11:28:45 +09:00
parent 4feafc681a
commit b108c833bb

View file

@ -39,33 +39,31 @@ impl<'a> FormattedStringExt<'a> for &'a str {
if !self.is_formatted() { if !self.is_formatted() {
return Cow::Borrowed(self); return Cow::Borrowed(self);
} }
Cow::Owned(internal::strip_formatting(self)) let mut s = String::from(self);
strip_formatting(&mut s);
Cow::Owned(s)
} }
} }
mod internal { // to reduce commit diff fn strip_formatting(buf: &mut String) {
use super::*;
pub(super) fn strip_formatting(input: &str) -> String {
let mut parser = Parser { let mut parser = Parser {
state: ParserState::Text, state: ParserState::Text,
}; };
let mut prev: char = '\x00'; let mut prev: char = '\x00';
input buf.retain(|cur| {
.chars()
.filter(move |cur| {
let result = match parser.state { let result = match parser.state {
ParserState::Text | ParserState::Foreground1 | ParserState::Foreground2 if *cur == '\x03' => { ParserState::Text | ParserState::Foreground1 | ParserState::Foreground2 if cur == '\x03' => {
parser.state = ParserState::ColorCode; parser.state = ParserState::ColorCode;
false false
}, },
ParserState::Text => !['\x02', '\x1F', '\x16', '\x0F'].contains(cur), ParserState::Text => !['\x02', '\x1F', '\x16', '\x0F'].contains(&cur),
ParserState::ColorCode if (*cur).is_digit(10) => { ParserState::ColorCode if cur.is_digit(10) => {
parser.state = ParserState::Foreground1; parser.state = ParserState::Foreground1;
false false
}, },
ParserState::Foreground1 if (*cur).is_digit(6) => { ParserState::Foreground1 if cur.is_digit(6) => {
// can only consume another digit if previous char was 1. // can only consume another digit if previous char was 1.
if (prev) == '1' { if prev == '1' {
parser.state = ParserState::Foreground2; parser.state = ParserState::Foreground2;
false false
} else { } else {
@ -73,22 +71,22 @@ mod internal { // to reduce commit diff
true true
} }
}, },
ParserState::Foreground1 if *cur == ',' => { ParserState::Foreground1 if cur == ',' => {
parser.state = ParserState::Comma; parser.state = ParserState::Comma;
false false
}, },
ParserState::Foreground2 if *cur == ',' => { ParserState::Foreground2 if cur == ',' => {
parser.state = ParserState::Comma; parser.state = ParserState::Comma;
false false
}, },
ParserState::Comma if ((*cur).is_digit(10)) => { ParserState::Comma if (cur.is_digit(10)) => {
parser.state = ParserState::Background1; parser.state = ParserState::Background1;
false false
}, },
ParserState::Background1 if (*cur).is_digit(6) => { ParserState::Background1 if cur.is_digit(6) => {
// can only consume another digit if previous char was 1. // can only consume another digit if previous char was 1.
parser.state = ParserState::Text; parser.state = ParserState::Text;
if (prev) == '1' { if prev == '1' {
false false
} else { } else {
true true
@ -99,22 +97,21 @@ mod internal { // to reduce commit diff
true true
} }
}; };
prev = *cur; prev = cur;
return result return result
}) });
.collect()
}
} }
impl FormattedStringExt<'static> for String { impl FormattedStringExt<'static> for String {
fn is_formatted(&self) -> bool { fn is_formatted(&self) -> bool {
self.as_str().is_formatted() self.as_str().is_formatted()
} }
fn strip_formatting(self) -> Cow<'static, str> { fn strip_formatting(mut self) -> Cow<'static, str> {
if !self.is_formatted() { if !self.is_formatted() {
return Cow::Owned(self); return Cow::Owned(self);
} }
Cow::Owned(internal::strip_formatting(&self)) strip_formatting(&mut self);
Cow::Owned(self)
} }
} }