[librsvg] Reimplement normalize_default() with the magic of iterators
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg] Reimplement normalize_default() with the magic of iterators
- Date: Thu, 7 Dec 2017 18:18:47 +0000 (UTC)
commit 2a4d8289218cba82f3bafcd39312476090b034c2
Author: Federico Mena Quintero <federico gnome org>
Date: Thu Dec 7 12:15:50 2017 -0600
Reimplement normalize_default() with the magic of iterators
Figuring out whether this is faster/slower than the old one is left as
an exercise to the reader.
rust/src/space.rs | 40 +++++++---------------------------------
1 files changed, 7 insertions(+), 33 deletions(-)
---
diff --git a/rust/src/space.rs b/rust/src/space.rs
index f1e5c1c..58b52a1 100644
--- a/rust/src/space.rs
+++ b/rust/src/space.rs
@@ -29,39 +29,13 @@ pub fn xml_space_normalize(mode: XmlSpace, s: &str) -> String {
// leading and trailing space characters. Then, all contiguous space
// characters will be consolidated.
fn normalize_default(s: &str) -> String {
- #[derive(PartialEq)]
- enum State {
- SpacesAtStart,
- NonSpace,
- SpacesAtMiddle
- }
-
- let mut result = String::new();
- let mut state = State::SpacesAtStart;
-
- for ch in s.chars() {
- match ch {
- '\n' => continue,
- '\t' | ' ' => {
- match state {
- State::SpacesAtStart => continue,
- State::NonSpace => { state = State::SpacesAtMiddle; },
- State::SpacesAtMiddle => continue,
- }
- },
-
- _ => {
- if state == State::SpacesAtMiddle {
- result.push(' ');
- }
-
- result.push(ch);
- state = State::NonSpace;
- }
- }
- }
-
- result
+ s.chars()
+ .filter(|ch| *ch != '\n')
+ .collect::<String>()
+ .split(|ch| ch == ' ' || ch == '\t')
+ .filter(|s| s.len() > 0)
+ .collect::<Vec<_>>()
+ .join(" ")
}
// From https://www.w3.org/TR/SVG/text.html#WhiteSpace
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]