From 5b9905b0f3fdb61a9371dfd029c03d71317b1cd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20H=C3=B6rnvall?= Date: Sat, 27 Feb 2021 12:45:18 +0100 Subject: [PATCH 1/4] Added CharIndices::offset function --- library/core/src/lib.rs | 1 + library/core/src/str/iter.rs | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index 64e2a95130999..2fcb2ca924846 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -137,6 +137,7 @@ #![feature(stmt_expr_attributes)] #![feature(str_split_as_str)] #![feature(str_split_inclusive_as_str)] +#![feature(char_indices_offset)] #![feature(trait_alias)] #![feature(transparent_unions)] #![feature(try_blocks)] diff --git a/library/core/src/str/iter.rs b/library/core/src/str/iter.rs index 83f484dc570c4..cd67c77378034 100644 --- a/library/core/src/str/iter.rs +++ b/library/core/src/str/iter.rs @@ -189,6 +189,29 @@ impl<'a> CharIndices<'a> { pub fn as_str(&self) -> &'a str { self.iter.as_str() } + + /// Returns the byte position of the next character, or the length + /// of the underlying string if there are no more characters. + /// + /// # Examples + /// + /// ``` + /// let mut chars = "a楽".char_indices(); + /// + /// assert_eq!(chars.offset(), 0); + /// assert_eq!(chars.next(), Some((0, 'a'))); + /// + /// assert_eq!(chars.offset(), 1); + /// assert_eq!(chars.next(), Some((1, '楽'))); + /// + /// assert_eq!(chars.offset(), 4); + /// assert_eq!(chars.next(), None); + /// ``` + #[inline] + #[unstable(feature = "char_indices_offset", issue = "none")] + pub fn offset(&self) -> usize { + self.front_offset + } } /// An iterator over the bytes of a string slice. From 772543aeff475bd96b2de17a7b245a0eb62ca6d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20H=C3=B6rnvall?= Date: Sat, 27 Feb 2021 13:10:00 +0100 Subject: [PATCH 2/4] Removed trailing whitespace --- library/core/src/str/iter.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/str/iter.rs b/library/core/src/str/iter.rs index cd67c77378034..642da2d2170d9 100644 --- a/library/core/src/str/iter.rs +++ b/library/core/src/str/iter.rs @@ -190,7 +190,7 @@ impl<'a> CharIndices<'a> { self.iter.as_str() } - /// Returns the byte position of the next character, or the length + /// Returns the byte position of the next character, or the length /// of the underlying string if there are no more characters. /// /// # Examples From 907eab8e62d44b262a224eb3f5f6f70d5737ff15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20H=C3=B6rnvall?= Date: Sat, 27 Feb 2021 13:33:55 +0100 Subject: [PATCH 3/4] Added feature flag to doc test --- library/core/src/str/iter.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/library/core/src/str/iter.rs b/library/core/src/str/iter.rs index 642da2d2170d9..b6502d192fbd8 100644 --- a/library/core/src/str/iter.rs +++ b/library/core/src/str/iter.rs @@ -196,6 +196,7 @@ impl<'a> CharIndices<'a> { /// # Examples /// /// ``` + /// #![feature(char_indices_offset)] /// let mut chars = "a楽".char_indices(); /// /// assert_eq!(chars.offset(), 0); From fa1624cf135b141c1e4a524d7e001a727b305755 Mon Sep 17 00:00:00 2001 From: TrolledWoods Date: Mon, 5 Apr 2021 09:18:00 +0200 Subject: [PATCH 4/4] Added tracking issue number --- library/core/src/str/iter.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/str/iter.rs b/library/core/src/str/iter.rs index b6502d192fbd8..14aff47e380b2 100644 --- a/library/core/src/str/iter.rs +++ b/library/core/src/str/iter.rs @@ -209,7 +209,7 @@ impl<'a> CharIndices<'a> { /// assert_eq!(chars.next(), None); /// ``` #[inline] - #[unstable(feature = "char_indices_offset", issue = "none")] + #[unstable(feature = "char_indices_offset", issue = "83871")] pub fn offset(&self) -> usize { self.front_offset }