From b96840ce567db68489779a34b2eb2dfa94b0d083 Mon Sep 17 00:00:00 2001 From: Matt Brubeck Date: Sun, 5 Feb 2017 08:46:37 -0800 Subject: [PATCH] Add as_str methods to iterator types --- Cargo.toml | 2 +- README.md | 20 +++++++++++++++++++- src/grapheme.rs | 38 ++++++++++++++++++++++++++++++++++++++ src/lib.rs | 2 +- src/word.rs | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 93 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3338a67..a7d093d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "unicode-segmentation" -version = "1.0.3" +version = "1.1.0" authors = ["kwantam "] homepage = "https://github.com/unicode-rs/unicode-segmentation" diff --git a/README.md b/README.md index 292a71a..5543e4f 100644 --- a/README.md +++ b/README.md @@ -40,5 +40,23 @@ to your `Cargo.toml`: ```toml [dependencies] -unicode-segmentation = "1.0.2" +unicode-segmentation = "1.1.0" ``` + +# Change Log + +## 1.1.0 + +* Add `as_str` methods to the iterator types. + +## 1.0.3 + +* Code cleanup and additional tests. + +## 1.0.1 + +* Fix a bug affecting some grapheme clusters containing Prepend characters. + +## 1.0.0 + +* Upgrade to Unicode 9.0.0. diff --git a/src/grapheme.rs b/src/grapheme.rs index fed1777..6768904 100644 --- a/src/grapheme.rs +++ b/src/grapheme.rs @@ -19,6 +19,25 @@ pub struct GraphemeIndices<'a> { iter: Graphemes<'a>, } +impl<'a> GraphemeIndices<'a> { + #[inline] + /// View the underlying data (the part yet to be iterated) as a slice of the original string. + /// + /// ```rust + /// # use unicode_segmentation::UnicodeSegmentation; + /// let mut iter = "abc".grapheme_indices(true); + /// assert_eq!(iter.as_str(), "abc"); + /// iter.next(); + /// assert_eq!(iter.as_str(), "bc"); + /// iter.next(); + /// iter.next(); + /// assert_eq!(iter.as_str(), ""); + /// ``` + pub fn as_str(&self) -> &'a str { + self.iter.as_str() + } +} + impl<'a> Iterator for GraphemeIndices<'a> { type Item = (usize, &'a str); @@ -51,6 +70,25 @@ pub struct Graphemes<'a> { regional_count_back: Option, } +impl<'a> Graphemes<'a> { + #[inline] + /// View the underlying data (the part yet to be iterated) as a slice of the original string. + /// + /// ```rust + /// # use unicode_segmentation::UnicodeSegmentation; + /// let mut iter = "abc".graphemes(true); + /// assert_eq!(iter.as_str(), "abc"); + /// iter.next(); + /// assert_eq!(iter.as_str(), "bc"); + /// iter.next(); + /// iter.next(); + /// assert_eq!(iter.as_str(), ""); + /// ``` + pub fn as_str(&self) -> &'a str { + self.string + } +} + // state machine for cluster boundary rules #[derive(Copy,Clone,PartialEq,Eq)] enum GraphemeState { diff --git a/src/lib.rs b/src/lib.rs index c3997f7..96e6e3e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -46,7 +46,7 @@ //! //! ```toml //! [dependencies] -//! unicode-segmentation = "1.0.2" +//! unicode-segmentation = "1.1.0" //! ``` #![deny(missing_docs, unsafe_code)] diff --git a/src/word.rs b/src/word.rs index 8175805..436582c 100644 --- a/src/word.rs +++ b/src/word.rs @@ -50,6 +50,24 @@ pub struct UWordBoundIndices<'a> { iter: UWordBounds<'a>, } +impl<'a> UWordBoundIndices<'a> { + #[inline] + /// View the underlying data (the part yet to be iterated) as a slice of the original string. + /// + /// ```rust + /// # use unicode_segmentation::UnicodeSegmentation; + /// let mut iter = "Hello world".split_word_bound_indices(); + /// assert_eq!(iter.as_str(), "Hello world"); + /// iter.next(); + /// assert_eq!(iter.as_str(), " world"); + /// iter.next(); + /// assert_eq!(iter.as_str(), "world"); + /// ``` + pub fn as_str(&self) -> &'a str { + self.iter.as_str() + } +} + impl<'a> Iterator for UWordBoundIndices<'a> { type Item = (usize, &'a str); @@ -569,6 +587,22 @@ impl<'a> DoubleEndedIterator for UWordBounds<'a> { } impl<'a> UWordBounds<'a> { + #[inline] + /// View the underlying data (the part yet to be iterated) as a slice of the original string. + /// + /// ```rust + /// # use unicode_segmentation::UnicodeSegmentation; + /// let mut iter = "Hello world".split_word_bounds(); + /// assert_eq!(iter.as_str(), "Hello world"); + /// iter.next(); + /// assert_eq!(iter.as_str(), " world"); + /// iter.next(); + /// assert_eq!(iter.as_str(), "world"); + /// ``` + pub fn as_str(&self) -> &'a str { + self.string + } + #[inline] fn get_next_cat(&self, idx: usize) -> Option { use tables::word as wd; pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy