Skip to content

Commit db7baed

Browse files
committed
Implement some of AsciiExts methods for AsciiStr directly in no_std mode
The methods implemented are eq_ignore_case(), make_ascii_uppercase() and make_ascii_lowercase().
1 parent 14b594a commit db7baed

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

src/ascii_str.rs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,26 @@ impl AsciiStr {
166166
.rev().take_while(|a| a.is_whitespace() ).count();
167167
&self[..self.len()-trimmed]
168168
}
169+
170+
#[cfg(feature = "no_std")]
171+
pub fn eq_ignore_ascii_case(&self, other: &Self) -> bool {
172+
self.len() == other.len() &&
173+
self.slice.iter().zip(other.slice.iter()).all(|(a, b)| a.eq_ignore_ascii_case(b) )
174+
}
175+
176+
#[cfg(feature = "no_std")]
177+
pub fn make_ascii_uppercase(&mut self) {
178+
for a in &mut self.slice {
179+
*a = a.to_ascii_uppercase();
180+
}
181+
}
182+
183+
#[cfg(feature = "no_std")]
184+
pub fn make_ascii_lowercase(&mut self) {
185+
for a in &mut self.slice {
186+
*a = a.to_ascii_lowercase();
187+
}
188+
}
169189
}
170190

171191
impl PartialEq<str> for AsciiStr {
@@ -317,7 +337,7 @@ impl AsciiExt for AsciiStr {
317337

318338
fn eq_ignore_ascii_case(&self, other: &Self) -> bool {
319339
self.len() == other.len() &&
320-
self.slice.iter().zip(other.slice.iter()).all(|(a, b)| a.eq_ignore_ascii_case(b))
340+
self.slice.iter().zip(other.slice.iter()).all(|(a, b)| a.eq_ignore_ascii_case(b) )
321341
}
322342

323343
fn make_ascii_uppercase(&mut self) {
@@ -465,9 +485,9 @@ impl AsMutAsciiStr for str {
465485
#[cfg(test)]
466486
mod tests {
467487
use AsciiChar;
468-
use super::{AsciiStr, AsAsciiStr, AsAsciiStrError};
488+
use super::{AsciiStr, AsAsciiStr, AsMutAsciiStr, AsAsciiStrError};
469489
#[cfg(not(feature = "no_std"))]
470-
use super::AsMutAsciiStr;
490+
use std::ascii::AsciiExt;
471491

472492
#[test]
473493
fn generic_as_ascii_str() {
@@ -521,6 +541,19 @@ mod tests {
521541
assert_eq!(AsRef::<[u8]>::as_ref(v), b"( ;");
522542
}
523543

544+
#[test]
545+
fn ascii_case() {
546+
let mut bytes = ([b'a',b'@',b'A'], [b'A',b'@',b'a']);
547+
let mut a = bytes.0.as_mut_ascii_str().unwrap();
548+
let mut b = bytes.1.as_mut_ascii_str().unwrap();
549+
assert!(a.eq_ignore_ascii_case(b));
550+
assert!(b.eq_ignore_ascii_case(a));
551+
a.make_ascii_lowercase();
552+
b.make_ascii_uppercase();
553+
assert_eq!(a, "a@a");
554+
assert_eq!(b, "A@A");
555+
}
556+
524557
#[test]
525558
#[cfg(not(feature = "no_std"))]
526559
fn fmt_ascii_str() {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy