Skip to content

Commit 72c8f37

Browse files
committed
Prepared most StrExt pattern using methods for stabilization
Made iterator-returning methods return newtypes Adjusted some docs to be forwards compatible with a generic pattern API
1 parent ead198c commit 72c8f37

File tree

5 files changed

+274
-172
lines changed

5 files changed

+274
-172
lines changed

Diff for: src/libcollections/str.rs

+101-78
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,14 @@ use vec::Vec;
8080

8181
pub use core::str::{from_utf8, CharEq, Chars, CharIndices};
8282
pub use core::str::{Bytes, CharSplits, is_utf8};
83-
pub use core::str::{CharSplitsN, Lines, LinesAny, MatchIndices, StrSplits};
83+
pub use core::str::{CharSplitsN, Lines, LinesAny, MatchIndices, StrSplits, SplitStr};
8484
pub use core::str::{CharRange};
8585
pub use core::str::{FromStr, from_str, Utf8Error};
8686
pub use core::str::Str;
8787
pub use core::str::{from_utf8_unchecked, from_c_str};
8888
pub use unicode::str::{Words, Graphemes, GraphemeIndices};
89+
pub use core::str::{Split, SplitTerminator};
90+
pub use core::str::{SplitN, RSplitN};
8991

9092
// FIXME(conventions): ensure bit/char conventions are followed by str's API
9193

@@ -721,7 +723,7 @@ pub trait StrExt for Sized?: ops::Slice<uint, str> {
721723
/// // not found, so no change.
722724
/// assert_eq!(s.replace("cookie monster", "little lamb"), s);
723725
/// ```
724-
#[unstable = "awaiting pattern/matcher stabilization"]
726+
#[stable]
725727
fn replace(&self, from: &str, to: &str) -> String {
726728
let mut result = String::new();
727729
let mut last_end = 0;
@@ -828,36 +830,36 @@ pub trait StrExt for Sized?: ops::Slice<uint, str> {
828830
}
829831
}
830832

831-
/// Returns true if one string contains another
833+
/// Returns true if a string contains a string pattern.
832834
///
833835
/// # Arguments
834836
///
835-
/// - needle - The string to look for
837+
/// - pat - The string pattern to look for
836838
///
837839
/// # Example
838840
///
839841
/// ```rust
840842
/// assert!("bananas".contains("nana"));
841843
/// ```
842-
#[unstable = "awaiting pattern/matcher stabilization"]
843-
fn contains(&self, needle: &str) -> bool {
844-
core_str::StrExt::contains(self[], needle)
844+
#[stable]
845+
fn contains(&self, pat: &str) -> bool {
846+
core_str::StrExt::contains(self[], pat)
845847
}
846848

847-
/// Returns true if a string contains a char.
849+
/// Returns true if a string contains a char pattern.
848850
///
849851
/// # Arguments
850852
///
851-
/// - needle - The char to look for
853+
/// - pat - The char pattern to look for
852854
///
853855
/// # Example
854856
///
855857
/// ```rust
856858
/// assert!("hello".contains_char('e'));
857859
/// ```
858-
#[unstable = "awaiting pattern/matcher stabilization"]
859-
fn contains_char(&self, needle: char) -> bool {
860-
core_str::StrExt::contains_char(self[], needle)
860+
#[unstable = "might get removed in favour of a more generic contains()"]
861+
fn contains_char<P: CharEq>(&self, pat: P) -> bool {
862+
core_str::StrExt::contains_char(self[], pat)
861863
}
862864

863865
/// An iterator over the characters of `self`. Note, this iterates
@@ -894,7 +896,7 @@ pub trait StrExt for Sized?: ops::Slice<uint, str> {
894896
}
895897

896898
/// An iterator over substrings of `self`, separated by characters
897-
/// matched by `sep`.
899+
/// matched by the pattern `pat`.
898900
///
899901
/// # Example
900902
///
@@ -911,13 +913,13 @@ pub trait StrExt for Sized?: ops::Slice<uint, str> {
911913
/// let v: Vec<&str> = "".split('X').collect();
912914
/// assert_eq!(v, vec![""]);
913915
/// ```
914-
#[unstable = "awaiting pattern/matcher stabilization"]
915-
fn split<Sep: CharEq>(&self, sep: Sep) -> CharSplits<Sep> {
916-
core_str::StrExt::split(self[], sep)
916+
#[stable]
917+
fn split<P: CharEq>(&self, pat: P) -> Split<P> {
918+
core_str::StrExt::split(self[], pat)
917919
}
918920

919921
/// An iterator over substrings of `self`, separated by characters
920-
/// matched by `sep`, restricted to splitting at most `count`
922+
/// matched by the pattern `pat`, restricted to splitting at most `count`
921923
/// times.
922924
///
923925
/// # Example
@@ -938,13 +940,13 @@ pub trait StrExt for Sized?: ops::Slice<uint, str> {
938940
/// let v: Vec<&str> = "".splitn(1, 'X').collect();
939941
/// assert_eq!(v, vec![""]);
940942
/// ```
941-
#[unstable = "awaiting pattern/matcher stabilization"]
942-
fn splitn<Sep: CharEq>(&self, count: uint, sep: Sep) -> CharSplitsN<Sep> {
943-
core_str::StrExt::splitn(self[], count, sep)
943+
#[stable]
944+
fn splitn<P: CharEq>(&self, count: uint, pat: P) -> SplitN<P> {
945+
core_str::StrExt::splitn(self[], count, pat)
944946
}
945947

946948
/// An iterator over substrings of `self`, separated by characters
947-
/// matched by `sep`.
949+
/// matched by the pattern `pat`.
948950
///
949951
/// Equivalent to `split`, except that the trailing substring
950952
/// is skipped if empty (terminator semantics).
@@ -967,13 +969,13 @@ pub trait StrExt for Sized?: ops::Slice<uint, str> {
967969
/// let v: Vec<&str> = "lionXXtigerXleopard".split('X').rev().collect();
968970
/// assert_eq!(v, vec!["leopard", "tiger", "", "lion"]);
969971
/// ```
970-
#[unstable = "awaiting pattern/matcher stabilization"]
971-
fn split_terminator<Sep: CharEq>(&self, sep: Sep) -> CharSplits<Sep> {
972-
core_str::StrExt::split_terminator(self[], sep)
972+
#[unstable = "might get removed"]
973+
fn split_terminator<P: CharEq>(&self, pat: P) -> SplitTerminator<P> {
974+
core_str::StrExt::split_terminator(self[], pat)
973975
}
974976

975977
/// An iterator over substrings of `self`, separated by characters
976-
/// matched by `sep`, starting from the end of the string.
978+
/// matched by the pattern `pat`, starting from the end of the string.
977979
/// Restricted to splitting at most `count` times.
978980
///
979981
/// # Example
@@ -988,13 +990,13 @@ pub trait StrExt for Sized?: ops::Slice<uint, str> {
988990
/// let v: Vec<&str> = "lionXXtigerXleopard".rsplitn(2, 'X').collect();
989991
/// assert_eq!(v, vec!["leopard", "tiger", "lionX"]);
990992
/// ```
991-
#[unstable = "awaiting pattern/matcher stabilization"]
992-
fn rsplitn<Sep: CharEq>(&self, count: uint, sep: Sep) -> CharSplitsN<Sep> {
993-
core_str::StrExt::rsplitn(self[], count, sep)
993+
#[stable]
994+
fn rsplitn<P: CharEq>(&self, count: uint, pat: P) -> RSplitN<P> {
995+
core_str::StrExt::rsplitn(self[], count, pat)
994996
}
995997

996998
/// An iterator over the start and end indices of the disjoint
997-
/// matches of `sep` within `self`.
999+
/// matches of the pattern `pat` within `self`.
9981000
///
9991001
/// That is, each returned value `(start, end)` satisfies
10001002
/// `self.slice(start, end) == sep`. For matches of `sep` within
@@ -1013,12 +1015,12 @@ pub trait StrExt for Sized?: ops::Slice<uint, str> {
10131015
/// let v: Vec<(uint, uint)> = "ababa".match_indices("aba").collect();
10141016
/// assert_eq!(v, vec![(0, 3)]); // only the first `aba`
10151017
/// ```
1016-
#[unstable = "awaiting pattern/matcher stabilization"]
1017-
fn match_indices<'a>(&'a self, sep: &'a str) -> MatchIndices<'a> {
1018-
core_str::StrExt::match_indices(self[], sep)
1018+
#[unstable = "might have its iterator type changed"]
1019+
fn match_indices<'a>(&'a self, pat: &'a str) -> MatchIndices<'a> {
1020+
core_str::StrExt::match_indices(self[], pat)
10191021
}
10201022

1021-
/// An iterator over the substrings of `self` separated by `sep`.
1023+
/// An iterator over the substrings of `self` separated by the pattern `sep`.
10221024
///
10231025
/// # Example
10241026
///
@@ -1029,9 +1031,9 @@ pub trait StrExt for Sized?: ops::Slice<uint, str> {
10291031
/// let v: Vec<&str> = "1abcabc2".split_str("abc").collect();
10301032
/// assert_eq!(v, vec!["1", "", "2"]);
10311033
/// ```
1032-
#[unstable = "awaiting pattern/matcher stabilization"]
1033-
fn split_str<'a>(&'a self, s: &'a str) -> StrSplits<'a> {
1034-
core_str::StrExt::split_str(self[], s)
1034+
#[unstable = "might get removed in the future in favor of a more generic split()"]
1035+
fn split_str<'a>(&'a self, pat: &'a str) -> StrSplits<'a> {
1036+
core_str::StrExt::split_str(self[], pat)
10351037
}
10361038

10371039
/// An iterator over the lines of a string (subsequences separated
@@ -1204,85 +1206,106 @@ pub trait StrExt for Sized?: ops::Slice<uint, str> {
12041206
core_str::StrExt::slice_unchecked(self[], begin, end)
12051207
}
12061208

1207-
/// Returns true if `needle` is a prefix of the string.
1209+
/// Returns true if the pattern `pat` is a prefix of the string.
12081210
///
12091211
/// # Example
12101212
///
12111213
/// ```rust
12121214
/// assert!("banana".starts_with("ba"));
12131215
/// ```
1214-
#[unstable = "awaiting pattern/matcher stabilization"]
1215-
fn starts_with(&self, needle: &str) -> bool {
1216-
core_str::StrExt::starts_with(self[], needle)
1216+
#[stable]
1217+
fn starts_with(&self, pat: &str) -> bool {
1218+
core_str::StrExt::starts_with(self[], pat)
12171219
}
12181220

1219-
/// Returns true if `needle` is a suffix of the string.
1221+
/// Returns true if the pattern `pat` is a suffix of the string.
12201222
///
12211223
/// # Example
12221224
///
12231225
/// ```rust
12241226
/// assert!("banana".ends_with("nana"));
12251227
/// ```
1226-
#[unstable = "awaiting pattern/matcher stabilization"]
1227-
fn ends_with(&self, needle: &str) -> bool {
1228-
core_str::StrExt::ends_with(self[], needle)
1228+
#[stable]
1229+
fn ends_with(&self, pat: &str) -> bool {
1230+
core_str::StrExt::ends_with(self[], pat)
12291231
}
12301232

1231-
/// Returns a string with characters that match `to_trim` removed from the left and the right.
1233+
/// Returns a string with all pre- and suffixes that match
1234+
/// the pattern `pat` repeatedly removed.
12321235
///
12331236
/// # Arguments
12341237
///
1235-
/// * to_trim - a character matcher
1238+
/// * pat - a string pattern
12361239
///
12371240
/// # Example
12381241
///
12391242
/// ```rust
1240-
/// assert_eq!("11foo1bar11".trim_chars('1'), "foo1bar");
1243+
/// assert_eq!("11foo1bar11".trim_matches('1'), "foo1bar");
12411244
/// let x: &[_] = &['1', '2'];
1242-
/// assert_eq!("12foo1bar12".trim_chars(x), "foo1bar");
1243-
/// assert_eq!("123foo1bar123".trim_chars(|&: c: char| c.is_numeric()), "foo1bar");
1245+
/// assert_eq!("12foo1bar12".trim_matches(x), "foo1bar");
1246+
/// assert_eq!("123foo1bar123".trim_matches(|&: c: char| c.is_numeric()), "foo1bar");
12441247
/// ```
1245-
#[unstable = "awaiting pattern/matcher stabilization"]
1246-
fn trim_chars<C: CharEq>(&self, to_trim: C) -> &str {
1247-
core_str::StrExt::trim_chars(self[], to_trim)
1248+
#[stable]
1249+
fn trim_matches<P: CharEq>(&self, pat: P) -> &str {
1250+
core_str::StrExt::trim_matches(self[], pat)
12481251
}
12491252

1250-
/// Returns a string with leading `chars_to_trim` removed.
1253+
/// Deprecated
1254+
#[deprecated = "Replaced by `trim_matches`"]
1255+
fn trim_chars<'a, C: CharEq>(&'a self, to_trim: C) -> &'a str {
1256+
self.trim_matches(to_trim)
1257+
}
1258+
1259+
/// Returns a string with all prefixes that match
1260+
/// the pattern `pat` repeatedly removed.
12511261
///
12521262
/// # Arguments
12531263
///
1254-
/// * to_trim - a character matcher
1264+
/// * pat - a string pattern
12551265
///
12561266
/// # Example
12571267
///
12581268
/// ```rust
1259-
/// assert_eq!("11foo1bar11".trim_left_chars('1'), "foo1bar11");
1269+
/// assert_eq!("11foo1bar11".trim_left_matches('1'), "foo1bar11");
12601270
/// let x: &[_] = &['1', '2'];
1261-
/// assert_eq!("12foo1bar12".trim_left_chars(x), "foo1bar12");
1262-
/// assert_eq!("123foo1bar123".trim_left_chars(|&: c: char| c.is_numeric()), "foo1bar123");
1271+
/// assert_eq!("12foo1bar12".trim_left_matches(x), "foo1bar12");
1272+
/// assert_eq!("123foo1bar123".trim_left_matches(|&: c: char| c.is_numeric()), "foo1bar123");
12631273
/// ```
1264-
#[unstable = "awaiting pattern/matcher stabilization"]
1265-
fn trim_left_chars<C: CharEq>(&self, to_trim: C) -> &str {
1266-
core_str::StrExt::trim_left_chars(self[], to_trim)
1274+
#[stable]
1275+
fn trim_left_matches<P: CharEq>(&self, pat: P) -> &str {
1276+
core_str::StrExt::trim_left_matches(self[], pat)
1277+
}
1278+
1279+
/// Deprecated
1280+
#[deprecated = "Replaced by `trim_left_matches`"]
1281+
fn trim_left_chars<'a, C: CharEq>(&'a self, to_trim: C) -> &'a str {
1282+
self.trim_left_matches(to_trim)
12671283
}
12681284

1269-
/// Returns a string with trailing `chars_to_trim` removed.
1285+
/// Returns a string with all suffixes that match
1286+
/// the pattern `pat` repeatedly removed.
12701287
///
12711288
/// # Arguments
12721289
///
1273-
/// * to_trim - a character matcher
1290+
/// * pat - a string pattern
12741291
///
12751292
/// # Example
12761293
///
12771294
/// ```rust
1278-
/// assert_eq!("11foo1bar11".trim_right_chars('1'), "11foo1bar");
1295+
/// assert_eq!("11foo1bar11".trim_right_matches('1'), "11foo1bar");
12791296
/// let x: &[_] = &['1', '2'];
1280-
/// assert_eq!("12foo1bar12".trim_right_chars(x), "12foo1bar");
1281-
/// assert_eq!("123foo1bar123".trim_right_chars(|&: c: char| c.is_numeric()), "123foo1bar");
1297+
/// assert_eq!("12foo1bar12".trim_right_matches(x), "12foo1bar");
1298+
/// assert_eq!("123foo1bar123".trim_right_matches(|&: c: char| c.is_numeric()), "123foo1bar");
12821299
/// ```
1283-
#[unstable = "awaiting pattern/matcher stabilization"]
1284-
fn trim_right_chars<C: CharEq>(&self, to_trim: C) -> &str {
1285-
core_str::StrExt::trim_right_chars(self[], to_trim)
1300+
#[stable]
1301+
fn trim_right_matches<P: CharEq>(&self, pat: P) -> &str {
1302+
core_str::StrExt::trim_right_matches(self[], pat)
1303+
}
1304+
1305+
/// Deprecated
1306+
#[deprecated = "Replaced by `trim_right_matches`"]
1307+
fn trim_right_chars<'a, C: CharEq>(&'a self, to_trim: C) -> &'a str {
1308+
self.trim_right_matches(to_trim)
12861309
}
12871310

12881311
/// Check that `index`-th byte lies at the start and/or end of a
@@ -1430,7 +1453,7 @@ pub trait StrExt for Sized?: ops::Slice<uint, str> {
14301453
}
14311454

14321455
/// Returns the byte index of the first character of `self` that
1433-
/// matches `search`.
1456+
/// matches the pattern `pat`.
14341457
///
14351458
/// # Return value
14361459
///
@@ -1452,13 +1475,13 @@ pub trait StrExt for Sized?: ops::Slice<uint, str> {
14521475
/// let x: &[_] = &['1', '2'];
14531476
/// assert_eq!(s.find(x), None);
14541477
/// ```
1455-
#[unstable = "awaiting pattern/matcher stabilization"]
1456-
fn find<C: CharEq>(&self, search: C) -> Option<uint> {
1457-
core_str::StrExt::find(self[], search)
1478+
#[unstable = "might be superseded by match_indices"]
1479+
fn find<P: CharEq>(&self, pat: P) -> Option<uint> {
1480+
core_str::StrExt::find(self[], pat)
14581481
}
14591482

14601483
/// Returns the byte index of the last character of `self` that
1461-
/// matches `search`.
1484+
/// matches the pattern `pat`.
14621485
///
14631486
/// # Return value
14641487
///
@@ -1480,9 +1503,9 @@ pub trait StrExt for Sized?: ops::Slice<uint, str> {
14801503
/// let x: &[_] = &['1', '2'];
14811504
/// assert_eq!(s.rfind(x), None);
14821505
/// ```
1483-
#[unstable = "awaiting pattern/matcher stabilization"]
1484-
fn rfind<C: CharEq>(&self, search: C) -> Option<uint> {
1485-
core_str::StrExt::rfind(self[], search)
1506+
#[unstable = "might be superseded by match_indices"]
1507+
fn rfind<P: CharEq>(&self, pat: P) -> Option<uint> {
1508+
core_str::StrExt::rfind(self[], pat)
14861509
}
14871510

14881511
/// Returns the byte index of the first matching substring
@@ -1504,7 +1527,7 @@ pub trait StrExt for Sized?: ops::Slice<uint, str> {
15041527
/// assert_eq!(s.find_str("老虎 L"), Some(6));
15051528
/// assert_eq!(s.find_str("muffin man"), None);
15061529
/// ```
1507-
#[unstable = "awaiting pattern/matcher stabilization"]
1530+
#[unstable = "might get removed in favor of a more generic find in the future"]
15081531
fn find_str(&self, needle: &str) -> Option<uint> {
15091532
core_str::StrExt::find_str(self[], needle)
15101533
}
@@ -1546,7 +1569,7 @@ pub trait StrExt for Sized?: ops::Slice<uint, str> {
15461569
/// assert!(string.subslice_offset(lines[1]) == 2); // &"b"
15471570
/// assert!(string.subslice_offset(lines[2]) == 4); // &"c"
15481571
/// ```
1549-
#[unstable = "awaiting pattern/matcher stabilization"]
1572+
#[unstable = "awaiting convention about comparability of arbitrary slices"]
15501573
fn subslice_offset(&self, inner: &str) -> uint {
15511574
core_str::StrExt::subslice_offset(self[], inner)
15521575
}

0 commit comments

Comments
 (0)