@@ -255,6 +255,17 @@ impl CharEq for extern "Rust" fn(char) -> bool {
255
255
fn only_ascii ( & self ) -> bool { false }
256
256
}
257
257
258
+ impl < ' self , C : CharEq > CharEq for & ' self [ C ] {
259
+ #[ inline( always) ]
260
+ fn matches ( & self , c : char ) -> bool {
261
+ self . iter ( ) . any ( |m| m. matches ( c) )
262
+ }
263
+
264
+ fn only_ascii ( & self ) -> bool {
265
+ self . iter ( ) . all ( |m| m. only_ascii ( ) )
266
+ }
267
+ }
268
+
258
269
259
270
/// An iterator over the substrings of a string, separated by `sep`.
260
271
pub struct StrCharSplitIterator < ' self , Sep > {
@@ -1249,9 +1260,9 @@ pub trait StrSlice<'self> {
1249
1260
fn trim(&self) -> &'self str;
1250
1261
fn trim_left(&self) -> &'self str;
1251
1262
fn trim_right(&self) -> &'self str;
1252
- fn trim_chars(&self, chars_to_trim : &[char] ) -> &'self str;
1253
- fn trim_left_chars(&self, chars_to_trim : &[char] ) -> &'self str;
1254
- fn trim_right_chars(&self, chars_to_trim : &[char] ) -> &'self str;
1263
+ fn trim_chars<C: CharEq> (&self, to_trim : &C ) -> &'self str;
1264
+ fn trim_left_chars<C: CharEq> (&self, to_trim : &C ) -> &'self str;
1265
+ fn trim_right_chars<C: CharEq> (&self, to_trim : &C ) -> &'self str;
1255
1266
fn replace(&self, from: &str, to: &str) -> ~str;
1256
1267
fn to_owned(&self) -> ~str;
1257
1268
fn to_managed(&self) -> @str;
@@ -1542,49 +1553,51 @@ impl<'self> StrSlice<'self> for &'self str {
1542
1553
/// Returns a string with leading whitespace removed
1543
1554
#[inline]
1544
1555
fn trim_left(&self) -> &'self str {
1545
- match self.find(|c| !char::is_whitespace(c)) {
1546
- None => " ",
1547
- Some(first) => unsafe { raw::slice_bytes(*self, first, self.len()) }
1548
- }
1556
+ self.trim_left_chars(&char::is_whitespace)
1549
1557
}
1550
1558
/// Returns a string with trailing whitespace removed
1551
1559
#[inline]
1552
1560
fn trim_right(&self) -> &'self str {
1553
- match self.rfind(|c| !char::is_whitespace(c)) {
1554
- None => " ",
1555
- Some(last) => {
1556
- let next = self.char_range_at(last).next;
1557
- unsafe { raw::slice_bytes(*self, 0u, next) }
1558
- }
1559
- }
1561
+ self.trim_right_chars(&char::is_whitespace)
1560
1562
}
1561
1563
1562
1564
/**
1563
- * Returns a string with leading and trailing `chars_to_trim ` removed.
1565
+ * Returns a string with characters that match `to_trim ` removed.
1564
1566
*
1565
1567
* # Arguments
1566
1568
*
1567
- * * chars_to_trim - A vector of chars
1569
+ * * to_trim - a character matcher
1570
+ *
1571
+ * # Example
1568
1572
*
1573
+ * ~~~
1574
+ * assert_eq!(" 11 foo1bar11".trim_chars(&'1'), " foo1bar")
1575
+ * assert_eq!(" 12 foo1bar12".trim_chars(& &['1', '2']), " foo1bar")
1576
+ * assert_eq!(" 123 foo1bar123".trim_chars(&|c: char| c.is_digit()), " foo1bar")
1577
+ * ~~~
1569
1578
*/
1570
1579
#[inline]
1571
- fn trim_chars(&self, chars_to_trim : &[char] ) -> &'self str {
1572
- self.trim_left_chars(chars_to_trim ).trim_right_chars(chars_to_trim )
1580
+ fn trim_chars<C: CharEq> (&self, to_trim : &C ) -> &'self str {
1581
+ self.trim_left_chars(to_trim ).trim_right_chars(to_trim )
1573
1582
}
1574
1583
/**
1575
1584
* Returns a string with leading `chars_to_trim` removed.
1576
1585
*
1577
1586
* # Arguments
1578
1587
*
1579
- * * s - A string
1580
- * * chars_to_trim - A vector of chars
1588
+ * * to_trim - a character matcher
1589
+ *
1590
+ * # Example
1581
1591
*
1592
+ * ~~~
1593
+ * assert_eq!(" 11 foo1bar11".trim_left_chars(&'1'), " foo1bar11")
1594
+ * assert_eq!(" 12 foo1bar12".trim_left_chars(& &['1', '2']), " foo1bar12")
1595
+ * assert_eq!(" 123 foo1bar123".trim_left_chars(&|c: char| c.is_digit()), " foo1bar123")
1596
+ * ~~~
1582
1597
*/
1583
1598
#[inline]
1584
- fn trim_left_chars(&self, chars_to_trim: &[char]) -> &'self str {
1585
- if chars_to_trim.is_empty() { return *self; }
1586
-
1587
- match self.find(|c| !chars_to_trim.contains(&c)) {
1599
+ fn trim_left_chars<C: CharEq>(&self, to_trim: &C) -> &'self str {
1600
+ match self.find(|c: char| !to_trim.matches(c)) {
1588
1601
None => " ",
1589
1602
Some(first) => unsafe { raw::slice_bytes(*self, first, self.len()) }
1590
1603
}
@@ -1594,15 +1607,19 @@ impl<'self> StrSlice<'self> for &'self str {
1594
1607
*
1595
1608
* # Arguments
1596
1609
*
1597
- * * s - A string
1598
- * * chars_to_trim - A vector of chars
1610
+ * * to_trim - a character matcher
1611
+ *
1612
+ * # Example
1599
1613
*
1614
+ * ~~~
1615
+ * assert_eq!(" 11 foo1bar11".trim_right_chars(&'1'), " 11 foo1bar")
1616
+ * assert_eq!(" 12 foo1bar12".trim_right_chars(& &['1', '2']), " 12 foo1bar")
1617
+ * assert_eq!(" 123 foo1bar123".trim_right_chars(&|c: char| c.is_digit()), " 123 foo1bar")
1618
+ * ~~~
1600
1619
*/
1601
1620
#[inline]
1602
- fn trim_right_chars(&self, chars_to_trim: &[char]) -> &'self str {
1603
- if chars_to_trim.is_empty() { return *self; }
1604
-
1605
- match self.rfind(|c| !chars_to_trim.contains(&c)) {
1621
+ fn trim_right_chars<C: CharEq>(&self, to_trim: &C) -> &'self str {
1622
+ match self.rfind(|c: char| !to_trim.matches(c)) {
1606
1623
None => " ",
1607
1624
Some(last) => {
1608
1625
let next = self.char_range_at(last).next;
@@ -2661,26 +2678,41 @@ mod tests {
2661
2678
2662
2679
#[ test]
2663
2680
fn test_trim_left_chars( ) {
2664
- assert_eq!( " *** foo *** " . trim_left_chars( [ ] ) , " *** foo *** " ) ;
2665
- assert_eq!( " *** foo *** " . trim_left_chars( [ '*' , ' ' ] ) , "foo *** " ) ;
2666
- assert_eq!( " *** *** " . trim_left_chars( [ '*' , ' ' ] ) , "" ) ;
2667
- assert_eq!( "foo *** " . trim_left_chars( [ '*' , ' ' ] ) , "foo *** " ) ;
2681
+ let v: & [ char ] = & [ ] ;
2682
+ assert_eq!( " *** foo *** " . trim_left_chars( & v) , " *** foo *** " ) ;
2683
+ assert_eq!( " *** foo *** " . trim_left_chars( & & [ '*' , ' ' ] ) , "foo *** " ) ;
2684
+ assert_eq!( " *** *** " . trim_left_chars( & & [ '*' , ' ' ] ) , "" ) ;
2685
+ assert_eq!( "foo *** " . trim_left_chars( & & [ '*' , ' ' ] ) , "foo *** " ) ;
2686
+
2687
+ assert_eq!( "11foo1bar11" . trim_left_chars( & '1' ) , "foo1bar11" ) ;
2688
+ assert_eq!( "12foo1bar12" . trim_left_chars( & & [ '1' , '2' ] ) , "foo1bar12" ) ;
2689
+ assert_eq!( "123foo1bar123" . trim_left_chars( & |c: char | c. is_digit( ) ) , "foo1bar123" ) ;
2668
2690
}
2669
2691
2670
2692
#[ test]
2671
2693
fn test_trim_right_chars( ) {
2672
- assert_eq!( " *** foo *** " . trim_right_chars( [ ] ) , " *** foo *** " ) ;
2673
- assert_eq!( " *** foo *** " . trim_right_chars( [ '*' , ' ' ] ) , " *** foo" ) ;
2674
- assert_eq!( " *** *** " . trim_right_chars( [ '*' , ' ' ] ) , "" ) ;
2675
- assert_eq!( " *** foo" . trim_right_chars( [ '*' , ' ' ] ) , " *** foo" ) ;
2694
+ let v: & [ char ] = & [ ] ;
2695
+ assert_eq!( " *** foo *** " . trim_right_chars( & v) , " *** foo *** " ) ;
2696
+ assert_eq!( " *** foo *** " . trim_right_chars( & & [ '*' , ' ' ] ) , " *** foo" ) ;
2697
+ assert_eq!( " *** *** " . trim_right_chars( & & [ '*' , ' ' ] ) , "" ) ;
2698
+ assert_eq!( " *** foo" . trim_right_chars( & & [ '*' , ' ' ] ) , " *** foo" ) ;
2699
+
2700
+ assert_eq!( "11foo1bar11" . trim_right_chars( & '1' ) , "11foo1bar" ) ;
2701
+ assert_eq!( "12foo1bar12" . trim_right_chars( & & [ '1' , '2' ] ) , "12foo1bar" ) ;
2702
+ assert_eq!( "123foo1bar123" . trim_right_chars( & |c: char | c. is_digit( ) ) , "123foo1bar" ) ;
2676
2703
}
2677
2704
2678
2705
#[ test]
2679
2706
fn test_trim_chars( ) {
2680
- assert_eq!( " *** foo *** " . trim_chars( [ ] ) , " *** foo *** " ) ;
2681
- assert_eq!( " *** foo *** " . trim_chars( [ '*' , ' ' ] ) , "foo" ) ;
2682
- assert_eq!( " *** *** " . trim_chars( [ '*' , ' ' ] ) , "" ) ;
2683
- assert_eq!( "foo" . trim_chars( [ '*' , ' ' ] ) , "foo" ) ;
2707
+ let v: & [ char ] = & [ ] ;
2708
+ assert_eq!( " *** foo *** " . trim_chars( & v) , " *** foo *** " ) ;
2709
+ assert_eq!( " *** foo *** " . trim_chars( & & [ '*' , ' ' ] ) , "foo" ) ;
2710
+ assert_eq!( " *** *** " . trim_chars( & & [ '*' , ' ' ] ) , "" ) ;
2711
+ assert_eq!( "foo" . trim_chars( & & [ '*' , ' ' ] ) , "foo" ) ;
2712
+
2713
+ assert_eq!( "11foo1bar11" . trim_chars( & '1' ) , "foo1bar" ) ;
2714
+ assert_eq!( "12foo1bar12" . trim_chars( & & [ '1' , '2' ] ) , "foo1bar" ) ;
2715
+ assert_eq!( "123foo1bar123" . trim_chars( & |c: char | c. is_digit( ) ) , "foo1bar" ) ;
2684
2716
}
2685
2717
2686
2718
#[ test]
0 commit comments