| 1 |
use crate::{ |
| 2 |
debug_fmt_char_iter, decode_utf16, decode_utf32, |
| 3 |
iter::{DecodeUtf16, DecodeUtf32}, |
| 4 |
}; |
| 5 |
#[allow(unused_imports)] |
| 6 |
use core::{ |
| 7 |
fmt::Write, |
| 8 |
iter::{Copied, DoubleEndedIterator, ExactSizeIterator, FlatMap, FusedIterator}, |
| 9 |
slice::Iter, |
| 10 |
}; |
| 11 |
|
| 12 |
/// An iterator over the [`char`]s of a UTF-16 string slice |
| 13 |
/// |
| 14 |
/// This struct is created by the [`chars`][crate::Utf16Str::chars] method on |
| 15 |
/// [`Utf16Str`][crate::Utf16Str]. See its documentation for more. |
| 16 |
#[derive(Clone)] |
| 17 |
pub struct CharsUtf16<'a> { |
| 18 |
iter: DecodeUtf16<Copied<Iter<'a, u16>>>, |
| 19 |
} |
| 20 |
|
| 21 |
impl<'a> CharsUtf16<'a> { |
| 22 |
pub(super) fn new(s: &'a [u16]) -> Self { |
| 23 |
Self { |
| 24 |
iter: decode_utf16(s.iter().copied()), |
| 25 |
} |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
impl<'a> Iterator for CharsUtf16<'a> { |
| 30 |
type Item = char; |
| 31 |
|
| 32 |
#[inline] |
| 33 |
fn next(&mut self) -> Option<Self::Item> { |
| 34 |
// Utf16Str already ensures valid surrogate pairs |
| 35 |
self.iter.next().map(|r| r.unwrap()) |
| 36 |
} |
| 37 |
|
| 38 |
#[inline] |
| 39 |
fn size_hint(&self) -> (usize, Option<usize>) { |
| 40 |
self.iter.size_hint() |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
impl<'a> FusedIterator for CharsUtf16<'a> {} |
| 45 |
|
| 46 |
impl<'a> DoubleEndedIterator for CharsUtf16<'a> { |
| 47 |
#[inline] |
| 48 |
fn next_back(&mut self) -> Option<Self::Item> { |
| 49 |
self.iter.next_back().map(|r| r.unwrap()) |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
impl<'a> core::fmt::Debug for CharsUtf16<'a> { |
| 54 |
#[inline] |
| 55 |
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 56 |
debug_fmt_char_iter(self.clone(), f) |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
impl<'a> core::fmt::Display for CharsUtf16<'a> { |
| 61 |
#[inline] |
| 62 |
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 63 |
self.clone().try_for_each(|c| f.write_char(c)) |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
/// An iterator over the [`char`]s of a UTF-32 string slice |
| 68 |
/// |
| 69 |
/// This struct is created by the [`chars`][crate::Utf32Str::chars] method on |
| 70 |
/// [`Utf32Str`][crate::Utf32Str]. See its documentation for more. |
| 71 |
#[derive(Clone)] |
| 72 |
pub struct CharsUtf32<'a> { |
| 73 |
iter: DecodeUtf32<Copied<Iter<'a, u32>>>, |
| 74 |
} |
| 75 |
|
| 76 |
impl<'a> CharsUtf32<'a> { |
| 77 |
pub(super) fn new(s: &'a [u32]) -> Self { |
| 78 |
Self { |
| 79 |
iter: decode_utf32(s.iter().copied()), |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
impl<'a> Iterator for CharsUtf32<'a> { |
| 85 |
type Item = char; |
| 86 |
|
| 87 |
#[inline] |
| 88 |
fn next(&mut self) -> Option<Self::Item> { |
| 89 |
// Utf32Str already ensures valid code points |
| 90 |
self.iter.next().map(|r| r.unwrap()) |
| 91 |
} |
| 92 |
|
| 93 |
#[inline] |
| 94 |
fn size_hint(&self) -> (usize, Option<usize>) { |
| 95 |
self.iter.size_hint() |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
impl<'a> DoubleEndedIterator for CharsUtf32<'a> { |
| 100 |
#[inline] |
| 101 |
fn next_back(&mut self) -> Option<Self::Item> { |
| 102 |
// Utf32Str already ensures valid code points |
| 103 |
self.iter.next_back().map(|r| r.unwrap()) |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
impl<'a> FusedIterator for CharsUtf32<'a> {} |
| 108 |
|
| 109 |
impl<'a> ExactSizeIterator for CharsUtf32<'a> { |
| 110 |
#[inline] |
| 111 |
fn len(&self) -> usize { |
| 112 |
self.iter.len() |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
impl<'a> core::fmt::Debug for CharsUtf32<'a> { |
| 117 |
#[inline] |
| 118 |
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 119 |
debug_fmt_char_iter(self.clone(), f) |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
impl<'a> core::fmt::Display for CharsUtf32<'a> { |
| 124 |
#[inline] |
| 125 |
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 126 |
self.clone().try_for_each(|c| f.write_char(c)) |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
/// An iterator over the [`char`]s of a string slice, and their positions |
| 131 |
/// |
| 132 |
/// This struct is created by the [`char_indices`][crate::Utf16Str::char_indices] method on |
| 133 |
/// [`Utf16Str`][crate::Utf16Str]. See its documentation for more. |
| 134 |
#[derive(Debug, Clone)] |
| 135 |
pub struct CharIndicesUtf16<'a> { |
| 136 |
forward_offset: usize, |
| 137 |
back_offset: usize, |
| 138 |
iter: CharsUtf16<'a>, |
| 139 |
} |
| 140 |
|
| 141 |
impl<'a> CharIndicesUtf16<'a> { |
| 142 |
/// Returns the position of the next character, or the length of the underlying string if |
| 143 |
/// there are no more characters. |
| 144 |
#[inline] |
| 145 |
pub fn offset(&self) -> usize { |
| 146 |
self.forward_offset |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
impl<'a> CharIndicesUtf16<'a> { |
| 151 |
pub(super) fn new(s: &'a [u16]) -> Self { |
| 152 |
Self { |
| 153 |
forward_offset: 0, |
| 154 |
back_offset: s.len(), |
| 155 |
iter: CharsUtf16::new(s), |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
impl<'a> Iterator for CharIndicesUtf16<'a> { |
| 161 |
type Item = (usize, char); |
| 162 |
|
| 163 |
#[inline] |
| 164 |
fn next(&mut self) -> Option<Self::Item> { |
| 165 |
let result = self.iter.next(); |
| 166 |
if let Some(c) = result { |
| 167 |
let offset = self.forward_offset; |
| 168 |
self.forward_offset += c.len_utf16(); |
| 169 |
Some((offset, c)) |
| 170 |
} else { |
| 171 |
None |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
#[inline] |
| 176 |
fn size_hint(&self) -> (usize, Option<usize>) { |
| 177 |
self.iter.size_hint() |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
impl<'a> FusedIterator for CharIndicesUtf16<'a> {} |
| 182 |
|
| 183 |
impl<'a> DoubleEndedIterator for CharIndicesUtf16<'a> { |
| 184 |
#[inline] |
| 185 |
fn next_back(&mut self) -> Option<Self::Item> { |
| 186 |
let result = self.iter.next_back(); |
| 187 |
if let Some(c) = result { |
| 188 |
self.back_offset -= c.len_utf16(); |
| 189 |
Some((self.back_offset, c)) |
| 190 |
} else { |
| 191 |
None |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
/// An iterator over the [`char`]s of a string slice, and their positions |
| 197 |
/// |
| 198 |
/// This struct is created by the [`char_indices`][crate::Utf32Str::char_indices] method on |
| 199 |
/// [`Utf32Str`][crate::Utf32Str]. See its documentation for more. |
| 200 |
#[derive(Debug, Clone)] |
| 201 |
pub struct CharIndicesUtf32<'a> { |
| 202 |
forward_offset: usize, |
| 203 |
back_offset: usize, |
| 204 |
iter: CharsUtf32<'a>, |
| 205 |
} |
| 206 |
|
| 207 |
impl<'a> CharIndicesUtf32<'a> { |
| 208 |
/// Returns the position of the next character, or the length of the underlying string if |
| 209 |
/// there are no more characters. |
| 210 |
#[inline] |
| 211 |
pub fn offset(&self) -> usize { |
| 212 |
self.forward_offset |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
impl<'a> CharIndicesUtf32<'a> { |
| 217 |
pub(super) fn new(s: &'a [u32]) -> Self { |
| 218 |
Self { |
| 219 |
forward_offset: 0, |
| 220 |
back_offset: s.len(), |
| 221 |
iter: CharsUtf32::new(s), |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
impl<'a> Iterator for CharIndicesUtf32<'a> { |
| 227 |
type Item = (usize, char); |
| 228 |
|
| 229 |
#[inline] |
| 230 |
fn next(&mut self) -> Option<Self::Item> { |
| 231 |
let result = self.iter.next(); |
| 232 |
if let Some(c) = result { |
| 233 |
let offset = self.forward_offset; |
| 234 |
self.forward_offset += 1; |
| 235 |
Some((offset, c)) |
| 236 |
} else { |
| 237 |
None |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
#[inline] |
| 242 |
fn size_hint(&self) -> (usize, Option<usize>) { |
| 243 |
self.iter.size_hint() |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
impl<'a> FusedIterator for CharIndicesUtf32<'a> {} |
| 248 |
|
| 249 |
impl<'a> DoubleEndedIterator for CharIndicesUtf32<'a> { |
| 250 |
#[inline] |
| 251 |
fn next_back(&mut self) -> Option<Self::Item> { |
| 252 |
let result = self.iter.next_back(); |
| 253 |
if let Some(c) = result { |
| 254 |
self.back_offset -= 1; |
| 255 |
Some((self.back_offset, c)) |
| 256 |
} else { |
| 257 |
None |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
impl<'a> ExactSizeIterator for CharIndicesUtf32<'a> { |
| 263 |
#[inline] |
| 264 |
fn len(&self) -> usize { |
| 265 |
self.iter.len() |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
/// The return type of [`Utf16Str::escape_debug`][crate::Utf16Str::escape_debug]. |
| 270 |
#[derive(Debug, Clone)] |
| 271 |
pub struct EscapeDebug<I> { |
| 272 |
iter: FlatMap<I, core::char::EscapeDebug, fn(char) -> core::char::EscapeDebug>, |
| 273 |
} |
| 274 |
|
| 275 |
impl<'a> EscapeDebug<CharsUtf16<'a>> { |
| 276 |
pub(super) fn new(s: &'a [u16]) -> Self { |
| 277 |
Self { |
| 278 |
iter: CharsUtf16::new(s).flat_map(|c| c.escape_debug()), |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
impl<'a> EscapeDebug<CharsUtf32<'a>> { |
| 284 |
pub(super) fn new(s: &'a [u32]) -> Self { |
| 285 |
Self { |
| 286 |
iter: CharsUtf32::new(s).flat_map(|c| c.escape_debug()), |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
/// The return type of [`Utf16Str::escape_default`][crate::Utf16Str::escape_default]. |
| 292 |
#[derive(Debug, Clone)] |
| 293 |
pub struct EscapeDefault<I> { |
| 294 |
iter: FlatMap<I, core::char::EscapeDefault, fn(char) -> core::char::EscapeDefault>, |
| 295 |
} |
| 296 |
|
| 297 |
impl<'a> EscapeDefault<CharsUtf16<'a>> { |
| 298 |
pub(super) fn new(s: &'a [u16]) -> Self { |
| 299 |
Self { |
| 300 |
iter: CharsUtf16::new(s).flat_map(|c| c.escape_default()), |
| 301 |
} |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
impl<'a> EscapeDefault<CharsUtf32<'a>> { |
| 306 |
pub(super) fn new(s: &'a [u32]) -> Self { |
| 307 |
Self { |
| 308 |
iter: CharsUtf32::new(s).flat_map(|c| c.escape_default()), |
| 309 |
} |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
/// The return type of [`Utf16Str::escape_unicode`][crate::Utf16Str::escape_unicode]. |
| 314 |
#[derive(Debug, Clone)] |
| 315 |
pub struct EscapeUnicode<I> { |
| 316 |
iter: FlatMap<I, core::char::EscapeUnicode, fn(char) -> core::char::EscapeUnicode>, |
| 317 |
} |
| 318 |
|
| 319 |
impl<'a> EscapeUnicode<CharsUtf16<'a>> { |
| 320 |
pub(super) fn new(s: &'a [u16]) -> Self { |
| 321 |
Self { |
| 322 |
iter: CharsUtf16::new(s).flat_map(|c| c.escape_unicode()), |
| 323 |
} |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
impl<'a> EscapeUnicode<CharsUtf32<'a>> { |
| 328 |
pub(super) fn new(s: &'a [u32]) -> Self { |
| 329 |
Self { |
| 330 |
iter: CharsUtf32::new(s).flat_map(|c| c.escape_unicode()), |
| 331 |
} |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
macro_rules! escape_impls { |
| 336 |
($($name:ident),+) => {$( |
| 337 |
impl<I> core::fmt::Display for $name<I> where I: Iterator<Item = char> + Clone { |
| 338 |
#[inline] |
| 339 |
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 340 |
self.clone().try_for_each(|c| f.write_char(c)) |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
impl< I> Iterator for $name<I> where I: Iterator<Item = char> { |
| 345 |
type Item = char; |
| 346 |
|
| 347 |
#[inline] |
| 348 |
fn next(&mut self) -> Option<Self::Item> { |
| 349 |
self.iter.next() |
| 350 |
} |
| 351 |
|
| 352 |
#[inline] |
| 353 |
fn size_hint(&self) -> (usize, Option<usize>) { |
| 354 |
let (lower, upper) = self.iter.size_hint(); |
| 355 |
// Worst case, every char has to be unicode escaped as \u{NNNNNN} |
| 356 |
(lower, upper.and_then(|len| len.checked_mul(10))) |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
impl<I> FusedIterator for $name<I> where I: Iterator<Item = char> + FusedIterator {} |
| 361 |
)+} |
| 362 |
} |
| 363 |
|
| 364 |
escape_impls!(EscapeDebug, EscapeDefault, EscapeUnicode); |
| 365 |
|
| 366 |
/// An iterator over the [`u16`] code units of a UTF-16 string slice |
| 367 |
/// |
| 368 |
/// This struct is created by the [`code_units`][crate::Utf16Str::code_units] method on |
| 369 |
/// [`Utf16Str`][crate::Utf16Str]. See its documentation for more. |
| 370 |
#[derive(Debug, Clone)] |
| 371 |
pub struct CodeUnits<'a> { |
| 372 |
iter: Copied<Iter<'a, u16>>, |
| 373 |
} |
| 374 |
|
| 375 |
impl<'a> CodeUnits<'a> { |
| 376 |
pub(super) fn new(s: &'a [u16]) -> Self { |
| 377 |
Self { |
| 378 |
iter: s.iter().copied(), |
| 379 |
} |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
impl<'a> Iterator for CodeUnits<'a> { |
| 384 |
type Item = u16; |
| 385 |
|
| 386 |
#[inline] |
| 387 |
fn next(&mut self) -> Option<Self::Item> { |
| 388 |
self.iter.next() |
| 389 |
} |
| 390 |
|
| 391 |
#[inline] |
| 392 |
fn size_hint(&self) -> (usize, Option<usize>) { |
| 393 |
self.iter.size_hint() |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
impl<'a> FusedIterator for CodeUnits<'a> {} |
| 398 |
|
| 399 |
impl<'a> DoubleEndedIterator for CodeUnits<'a> { |
| 400 |
#[inline] |
| 401 |
fn next_back(&mut self) -> Option<Self::Item> { |
| 402 |
self.iter.next_back() |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
impl<'a> ExactSizeIterator for CodeUnits<'a> { |
| 407 |
#[inline] |
| 408 |
fn len(&self) -> usize { |
| 409 |
self.iter.len() |
| 410 |
} |
| 411 |
} |