Skip to content

Commit 5b9905b

Browse files
committed
Added CharIndices::offset function
1 parent 8e863eb commit 5b9905b

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

library/core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@
137137
#![feature(stmt_expr_attributes)]
138138
#![feature(str_split_as_str)]
139139
#![feature(str_split_inclusive_as_str)]
140+
#![feature(char_indices_offset)]
140141
#![feature(trait_alias)]
141142
#![feature(transparent_unions)]
142143
#![feature(try_blocks)]

library/core/src/str/iter.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,29 @@ impl<'a> CharIndices<'a> {
189189
pub fn as_str(&self) -> &'a str {
190190
self.iter.as_str()
191191
}
192+
193+
/// Returns the byte position of the next character, or the length
194+
/// of the underlying string if there are no more characters.
195+
///
196+
/// # Examples
197+
///
198+
/// ```
199+
/// let mut chars = "a楽".char_indices();
200+
///
201+
/// assert_eq!(chars.offset(), 0);
202+
/// assert_eq!(chars.next(), Some((0, 'a')));
203+
///
204+
/// assert_eq!(chars.offset(), 1);
205+
/// assert_eq!(chars.next(), Some((1, '楽')));
206+
///
207+
/// assert_eq!(chars.offset(), 4);
208+
/// assert_eq!(chars.next(), None);
209+
/// ```
210+
#[inline]
211+
#[unstable(feature = "char_indices_offset", issue = "none")]
212+
pub fn offset(&self) -> usize {
213+
self.front_offset
214+
}
192215
}
193216

194217
/// An iterator over the bytes of a string slice.

0 commit comments

Comments
 (0)