-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
206 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# Project Dependencies | ||
Package: trimothy | ||
Version: 0.1.7 | ||
Generated: 2023-06-01 20:20:18 UTC | ||
Version: 0.2.0 | ||
Generated: 2023-10-04 04:34:44 UTC | ||
|
||
This package has no dependencies. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "trimothy" | ||
version = "0.1.8" | ||
version = "0.2.0" | ||
authors = ["Blobfolio, LLC. <[email protected]>"] | ||
edition = "2021" | ||
rust-version = "1.56" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/*! | ||
# Trimothy - Normalized Whitespace Iterator | ||
*/ | ||
|
||
use core::{ | ||
iter::Iterator, | ||
slice::Iter, | ||
str::Chars, | ||
}; | ||
use crate::TrimSlice; | ||
|
||
|
||
|
||
/// # Normalized Whitespace Iterator. | ||
/// | ||
/// This trait adds a `normalized_whitespace` method to byte and string slices | ||
/// for iterating over their contents with the edges trimmed, and all | ||
/// contiguous inner whitespace converted to a single horizontal space. | ||
pub trait NormalizeWhitespace<T> { | ||
/// # Normalized Whitespace Iterator. | ||
/// | ||
/// Return an iterator over the byte/char contents with the edges trimmed, | ||
/// and all contiguous inner whitespace converted to a single horizontal | ||
/// space. | ||
fn normalized_whitespace(&self) -> NormalizedWhitespace<T>; | ||
} | ||
|
||
impl<'a> NormalizeWhitespace<Iter<'a, u8>> for &'a [u8] { | ||
/// # Normalized Whitespace Iterator. | ||
/// | ||
/// Return an iterator over the byte/char contents with the edges trimmed, | ||
/// and all contiguous inner whitespace converted to a single horizontal | ||
/// space. | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ``` | ||
/// use trimothy::NormalizeWhitespace; | ||
/// | ||
/// let abnormal: &[u8] = b" Hello World!\n"; | ||
/// let normal: Vec<u8> = abnormal.normalized_whitespace().collect(); | ||
/// assert_eq!(normal, b"Hello World!"); | ||
/// ``` | ||
fn normalized_whitespace(&self) -> NormalizedWhitespace<Iter<'a, u8>> { | ||
NormalizedWhitespace { | ||
iter: self.trim().iter(), | ||
ws: false, | ||
} | ||
} | ||
} | ||
|
||
impl<'a> NormalizeWhitespace<Chars<'a>> for &'a str { | ||
/// # Normalized Whitespace Iterator. | ||
/// | ||
/// Return an iterator over the byte/char contents with the edges trimmed, | ||
/// and all contiguous inner whitespace converted to a single horizontal | ||
/// space. | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ``` | ||
/// use trimothy::NormalizeWhitespace; | ||
/// | ||
/// let abnormal: &str = " Hello World!\n"; | ||
/// let normal: String = abnormal.normalized_whitespace().collect(); | ||
/// assert_eq!(normal, "Hello World!"); | ||
/// ``` | ||
fn normalized_whitespace(&self) -> NormalizedWhitespace<Chars<'a>> { | ||
NormalizedWhitespace { | ||
iter: self.trim().chars(), | ||
ws: false, | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
#[derive(Debug)] | ||
/// # (Actual) Normalized Whitespace Iterator. | ||
/// | ||
/// This is the actual iterator returned by a | ||
/// `NormalizeWhitespace::normalized_whitespace` implementation. | ||
pub struct NormalizedWhitespace<T> { | ||
iter: T, | ||
ws: bool | ||
} | ||
|
||
impl<'a> Iterator for NormalizedWhitespace<Iter<'a, u8>> { | ||
type Item = u8; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
loop { | ||
let next = self.iter.next()?; | ||
if next.is_ascii_whitespace() { | ||
if ! self.ws { | ||
self.ws = true; | ||
return Some(b' '); | ||
} | ||
} | ||
else { | ||
self.ws = false; | ||
return Some(*next); | ||
} | ||
} | ||
} | ||
|
||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
let upper = self.iter.len(); | ||
(0, Some(upper)) | ||
} | ||
} | ||
|
||
impl<'a> Iterator for NormalizedWhitespace<Chars<'a>> { | ||
type Item = char; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
loop { | ||
let next = self.iter.next()?; | ||
if next.is_whitespace() { | ||
if ! self.ws { | ||
self.ws = true; | ||
return Some(' '); | ||
} | ||
} | ||
else { | ||
self.ws = false; | ||
return Some(next); | ||
} | ||
} | ||
} | ||
|
||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
let (_, upper) = self.iter.size_hint(); | ||
(0, upper) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters