@@ -1157,25 +1157,21 @@ impl str {
1157
1157
core_str:: StrExt :: rmatches ( self , pat)
1158
1158
}
1159
1159
1160
- /// An iterator over the start and end indices of the disjoint matches
1161
- /// of a pattern within `self` .
1160
+ /// An iterator over the disjoint matches of a pattern within `self` as well
1161
+ /// as the index that the match starts at .
1162
1162
///
1163
1163
/// For matches of `pat` within `self` that overlap, only the indices
1164
- /// corresponding to the first
1165
- /// match are returned.
1164
+ /// corresponding to the first match are returned.
1166
1165
///
1167
- /// The pattern can be a simple `&str`, `char`, or a closure that
1168
- /// determines if a character matches.
1169
- /// Additional libraries might provide more complex patterns like
1170
- /// regular expressions.
1166
+ /// The pattern can be a simple `&str`, `char`, or a closure that determines
1167
+ /// if a character matches. Additional libraries might provide more complex
1168
+ /// patterns like regular expressions.
1171
1169
///
1172
1170
/// # Iterator behavior
1173
1171
///
1174
1172
/// The returned iterator will be double ended if the pattern allows a
1175
- /// reverse search
1176
- /// and forward/reverse search yields the same elements. This is true for,
1177
- /// eg, `char` but not
1178
- /// for `&str`.
1173
+ /// reverse search and forward/reverse search yields the same elements. This
1174
+ /// is true for, eg, `char` but not for `&str`.
1179
1175
///
1180
1176
/// If the pattern allows a reverse search but its results might differ
1181
1177
/// from a forward search, `rmatch_indices()` can be used.
@@ -1185,42 +1181,36 @@ impl str {
1185
1181
/// ```
1186
1182
/// #![feature(str_match_indices)]
1187
1183
///
1188
- /// let v: Vec<(usize, usize) > = "abcXXXabcYYYabc".match_indices("abc").collect();
1189
- /// assert_eq!(v, [(0, 3 ), (6, 9 ), (12, 15 )]);
1184
+ /// let v: Vec<_ > = "abcXXXabcYYYabc".match_indices("abc").collect();
1185
+ /// assert_eq!(v, [(0, "abc" ), (6, "abc" ), (12, "abc" )]);
1190
1186
///
1191
- /// let v: Vec<(usize, usize) > = "1abcabc2".match_indices("abc").collect();
1192
- /// assert_eq!(v, [(1, 4 ), (4, 7 )]);
1187
+ /// let v: Vec<_ > = "1abcabc2".match_indices("abc").collect();
1188
+ /// assert_eq!(v, [(1, "abc" ), (4, "abc" )]);
1193
1189
///
1194
- /// let v: Vec<(usize, usize) > = "ababa".match_indices("aba").collect();
1195
- /// assert_eq!(v, [(0, 3 )]); // only the first `aba`
1190
+ /// let v: Vec<_ > = "ababa".match_indices("aba").collect();
1191
+ /// assert_eq!(v, [(0, "aba" )]); // only the first `aba`
1196
1192
/// ```
1197
1193
#[ unstable( feature = "str_match_indices" ,
1198
1194
reason = "might have its iterator type changed" ,
1199
1195
issue = "27743" ) ]
1200
- // NB: Right now MatchIndices yields `(usize, usize)`, but it would
1201
- // be more consistent with `matches` and `char_indices` to return `(usize, &str)`
1202
1196
pub fn match_indices < ' a , P : Pattern < ' a > > ( & ' a self , pat : P ) -> MatchIndices < ' a , P > {
1203
1197
core_str:: StrExt :: match_indices ( self , pat)
1204
1198
}
1205
1199
1206
- /// An iterator over the start and end indices of the disjoint matches of
1207
- /// a pattern within
1208
- /// `self`, yielded in reverse order.
1200
+ /// An iterator over the disjoint matches of a pattern within `self`,
1201
+ /// yielded in reverse order along with the index of the match.
1209
1202
///
1210
1203
/// For matches of `pat` within `self` that overlap, only the indices
1211
- /// corresponding to the last
1212
- /// match are returned.
1204
+ /// corresponding to the last match are returned.
1213
1205
///
1214
- /// The pattern can be a simple `&str`, `char`, or a closure that
1215
- /// determines if a character matches.
1216
- /// Additional libraries might provide more complex patterns like
1217
- /// regular expressions.
1206
+ /// The pattern can be a simple `&str`, `char`, or a closure that determines
1207
+ /// if a character matches. Additional libraries might provide more complex
1208
+ /// patterns like regular expressions.
1218
1209
///
1219
1210
/// # Iterator behavior
1220
1211
///
1221
- /// The returned iterator requires that the pattern supports a
1222
- /// reverse search,
1223
- /// and it will be double ended if a forward/reverse search yields
1212
+ /// The returned iterator requires that the pattern supports a reverse
1213
+ /// search, and it will be double ended if a forward/reverse search yields
1224
1214
/// the same elements.
1225
1215
///
1226
1216
/// For iterating from the front, `match_indices()` can be used.
@@ -1230,20 +1220,18 @@ impl str {
1230
1220
/// ```
1231
1221
/// #![feature(str_match_indices)]
1232
1222
///
1233
- /// let v: Vec<(usize, usize) > = "abcXXXabcYYYabc".rmatch_indices("abc").collect();
1234
- /// assert_eq!(v, [(12, 15 ), (6, 9 ), (0, 3 )]);
1223
+ /// let v: Vec<_ > = "abcXXXabcYYYabc".rmatch_indices("abc").collect();
1224
+ /// assert_eq!(v, [(12, "abc" ), (6, "abc" ), (0, "abc" )]);
1235
1225
///
1236
- /// let v: Vec<(usize, usize) > = "1abcabc2".rmatch_indices("abc").collect();
1237
- /// assert_eq!(v, [(4, 7 ), (1, 4 )]);
1226
+ /// let v: Vec<_ > = "1abcabc2".rmatch_indices("abc").collect();
1227
+ /// assert_eq!(v, [(4, "abc" ), (1, "abc" )]);
1238
1228
///
1239
- /// let v: Vec<(usize, usize) > = "ababa".rmatch_indices("aba").collect();
1240
- /// assert_eq!(v, [(2, 5 )]); // only the last `aba`
1229
+ /// let v: Vec<_ > = "ababa".rmatch_indices("aba").collect();
1230
+ /// assert_eq!(v, [(2, "aba" )]); // only the last `aba`
1241
1231
/// ```
1242
1232
#[ unstable( feature = "str_match_indices" ,
1243
1233
reason = "might have its iterator type changed" ,
1244
1234
issue = "27743" ) ]
1245
- // NB: Right now RMatchIndices yields `(usize, usize)`, but it would
1246
- // be more consistent with `rmatches` and `char_indices` to return `(usize, &str)`
1247
1235
pub fn rmatch_indices < ' a , P : Pattern < ' a > > ( & ' a self , pat : P ) -> RMatchIndices < ' a , P >
1248
1236
where P :: Searcher : ReverseSearcher < ' a >
1249
1237
{
@@ -1416,10 +1404,10 @@ impl str {
1416
1404
pub fn replace ( & self , from : & str , to : & str ) -> String {
1417
1405
let mut result = String :: new ( ) ;
1418
1406
let mut last_end = 0 ;
1419
- for ( start, end ) in self . match_indices ( from) {
1407
+ for ( start, part ) in self . match_indices ( from) {
1420
1408
result. push_str ( unsafe { self . slice_unchecked ( last_end, start) } ) ;
1421
1409
result. push_str ( to) ;
1422
- last_end = end ;
1410
+ last_end = start + part . len ( ) ;
1423
1411
}
1424
1412
result. push_str ( unsafe { self . slice_unchecked ( last_end, self . len ( ) ) } ) ;
1425
1413
result
0 commit comments