@@ -1166,88 +1166,6 @@ fn match_at<'a,'b>(haystack: &'a str, needle: &'b str, at: uint) -> bool {
1166
1166
return true ;
1167
1167
}
1168
1168
1169
- /**
1170
- * Returns the byte index of the first matching substring
1171
- *
1172
- * # Arguments
1173
- *
1174
- * * `haystack` - The string to search
1175
- * * `needle` - The string to search for
1176
- *
1177
- * # Return value
1178
- *
1179
- * An `option` containing the byte index of the first matching substring
1180
- * or `none` if there is no match
1181
- */
1182
- pub fn find_str < ' a , ' b > ( haystack : & ' a str , needle : & ' b str ) -> Option < uint > {
1183
- find_str_between ( haystack, needle, 0 u, haystack. len ( ) )
1184
- }
1185
-
1186
- /**
1187
- * Returns the byte index of the first matching substring beginning
1188
- * from a given byte offset
1189
- *
1190
- * # Arguments
1191
- *
1192
- * * `haystack` - The string to search
1193
- * * `needle` - The string to search for
1194
- * * `start` - The byte index to begin searching at, inclusive
1195
- *
1196
- * # Return value
1197
- *
1198
- * An `option` containing the byte index of the last matching character
1199
- * or `none` if there is no match
1200
- *
1201
- * # Failure
1202
- *
1203
- * `start` must be less than or equal to `s.len()`
1204
- */
1205
- pub fn find_str_from < ' a , ' b > ( haystack : & ' a str ,
1206
- needle : & ' b str ,
1207
- start : uint )
1208
- -> Option < uint > {
1209
- find_str_between ( haystack, needle, start, haystack. len ( ) )
1210
- }
1211
-
1212
- /**
1213
- * Returns the byte index of the first matching substring within a given range
1214
- *
1215
- * # Arguments
1216
- *
1217
- * * `haystack` - The string to search
1218
- * * `needle` - The string to search for
1219
- * * `start` - The byte index to begin searching at, inclusive
1220
- * * `end` - The byte index to end searching at, exclusive
1221
- *
1222
- * # Return value
1223
- *
1224
- * An `option` containing the byte index of the first matching character
1225
- * or `none` if there is no match
1226
- *
1227
- * # Failure
1228
- *
1229
- * `start` must be less than or equal to `end` and `end` must be less than
1230
- * or equal to `s.len()`.
1231
- */
1232
- pub fn find_str_between < ' a , ' b > ( haystack : & ' a str ,
1233
- needle : & ' b str ,
1234
- start : uint ,
1235
- end : uint )
1236
- -> Option < uint > {
1237
- // See Issue #1932 for why this is a naive search
1238
- assert ! ( end <= haystack. len( ) ) ;
1239
- let needle_len = needle. len ( ) ;
1240
- if needle_len == 0 u { return Some ( start) ; }
1241
- if needle_len > end { return None ; }
1242
-
1243
- let mut i = start;
1244
- let e = end - needle_len;
1245
- while i <= e {
1246
- if match_at ( haystack, needle, i) { return Some ( i) ; }
1247
- i += 1 u;
1248
- }
1249
- return None ;
1250
- }
1251
1169
1252
1170
/**
1253
1171
* Returns true if one string contains another
@@ -1258,7 +1176,7 @@ pub fn find_str_between<'a,'b>(haystack: &'a str,
1258
1176
* * needle - The string to look for
1259
1177
*/
1260
1178
pub fn contains < ' a , ' b > ( haystack : & ' a str , needle : & ' b str ) -> bool {
1261
- find_str ( haystack , needle) . is_some ( )
1179
+ haystack . find_str ( needle) . is_some ( )
1262
1180
}
1263
1181
1264
1182
/**
@@ -2096,6 +2014,7 @@ pub trait StrSlice<'self> {
2096
2014
2097
2015
fn find<C: CharEq>(&self, search: C) -> Option<uint>;
2098
2016
fn rfind<C: CharEq>(&self, search: C) -> Option<uint>;
2017
+ fn find_str(&self, &str) -> Option<uint>;
2099
2018
}
2100
2019
2101
2020
/// Extension methods for strings
@@ -2341,6 +2260,28 @@ impl<'self> StrSlice<'self> for &'self str {
2341
2260
2342
2261
None
2343
2262
}
2263
+
2264
+ /**
2265
+ * Returns the byte index of the first matching substring
2266
+ *
2267
+ * # Arguments
2268
+ *
2269
+ * * `needle` - The string to search for
2270
+ *
2271
+ * # Return value
2272
+ *
2273
+ * `Some` containing the byte index of the first matching substring
2274
+ * or `None` if there is no match
2275
+ */
2276
+ fn find_str(&self, needle: &str) -> Option<uint> {
2277
+ if needle.is_empty() {
2278
+ Some(0)
2279
+ } else {
2280
+ self.matches_index_iter(needle)
2281
+ .next()
2282
+ .map_consume(|(start, _end)| start)
2283
+ }
2284
+ }
2344
2285
}
2345
2286
2346
2287
#[allow(missing_doc)]
@@ -2550,43 +2491,31 @@ mod tests {
2550
2491
#[test]
2551
2492
fn test_find_str() {
2552
2493
// byte positions
2553
- assert!(find_str(" banana", " apple pie").is_none());
2554
- assert_eq!(find_str(" ", " "), Some(0u));
2555
-
2556
- let data = " ประเทศไทย中华Việt Nam ";
2557
- assert_eq!(find_str(data, " "), Some(0u));
2558
- assert_eq!(find_str(data, " ประเ"), Some( 0u));
2559
- assert_eq!(find_str(data, " ะเ"), Some( 6u));
2560
- assert_eq!(find_str(data, " 中华"), Some(27u));
2561
- assert!(find_str(data, " ไท华").is_none());
2562
- }
2563
-
2564
- #[test]
2565
- fn test_find_str_between() {
2566
- // byte positions
2567
- assert_eq!(find_str_between(" ", " ", 0u, 0u), Some(0u));
2494
+ assert_eq!(" ".find_str(" "), Some(0u));
2495
+ assert!(" banana".find_str(" apple pie").is_none());
2568
2496
2569
2497
let data = " abcabc";
2570
- assert_eq!(find_str_between( data, " ab", 0u, 6u ), Some(0u));
2571
- assert_eq!(find_str_between( data, " ab", 2u, 6u ), Some(3u));
2572
- assert!(find_str_between( data, " ab", 2u, 4u ).is_none());
2498
+ assert_eq!(data.slice(0u, 6u).find_str( " ab"), Some(0u));
2499
+ assert_eq!(data.slice(2u, 6u).find_str( " ab"), Some(3u));
2500
+ assert!(data.slice(2u, 4u).find_str( " ab").is_none());
2573
2501
2574
2502
let mut data = ~" ประเทศไทย中华Việt Nam ";
2575
2503
data = data + data;
2576
- assert_eq!(find_str_between(data, " ", 0u, 43u), Some(0u));
2577
- assert_eq!(find_str_between(data, " ", 6u, 43u), Some(6u));
2578
-
2579
- assert_eq!(find_str_between(data, " ประ", 0u, 43u), Some( 0u));
2580
- assert_eq!(find_str_between(data, " ทศไ", 0u, 43u), Some(12u));
2581
- assert_eq!(find_str_between(data, " ย中", 0u, 43u), Some(24u));
2582
- assert_eq!(find_str_between(data, " iệt", 0u, 43u), Some(34u));
2583
- assert_eq!(find_str_between(data, " Nam ", 0u, 43u), Some(40u));
2584
-
2585
- assert_eq!(find_str_between(data, " ประ", 43u, 86u), Some(43u));
2586
- assert_eq!(find_str_between(data, " ทศไ", 43u, 86u), Some(55u));
2587
- assert_eq!(find_str_between(data, " ย中", 43u, 86u), Some(67u));
2588
- assert_eq!(find_str_between(data, " iệt", 43u, 86u), Some(77u));
2589
- assert_eq!(find_str_between(data, " Nam ", 43u, 86u), Some(83u));
2504
+ assert!(data.find_str(" ไท华").is_none());
2505
+ assert_eq!(data.slice(0u, 43u).find_str(" "), Some(0u));
2506
+ assert_eq!(data.slice(6u, 43u).find_str(" "), Some(6u - 6u));
2507
+
2508
+ assert_eq!(data.slice(0u, 43u).find_str(" ประ"), Some( 0u));
2509
+ assert_eq!(data.slice(0u, 43u).find_str(" ทศไ"), Some(12u));
2510
+ assert_eq!(data.slice(0u, 43u).find_str(" ย中"), Some(24u));
2511
+ assert_eq!(data.slice(0u, 43u).find_str(" iệt"), Some(34u));
2512
+ assert_eq!(data.slice(0u, 43u).find_str(" Nam "), Some(40u));
2513
+
2514
+ assert_eq!(data.slice(43u, 86u).find_str(" ประ"), Some(43u - 43u));
2515
+ assert_eq!(data.slice(43u, 86u).find_str(" ทศไ"), Some(55u - 43u));
2516
+ assert_eq!(data.slice(43u, 86u).find_str(" ย中"), Some(67u - 43u));
2517
+ assert_eq!(data.slice(43u, 86u).find_str(" iệt"), Some(77u - 43u));
2518
+ assert_eq!(data.slice(43u, 86u).find_str(" Nam "), Some(83u - 43u));
2590
2519
}
2591
2520
2592
2521
#[test]
0 commit comments