|
1 | | -use std::{borrow::Borrow, cmp::Ordering, fmt, hash, iter, ops::Deref, sync::Arc}; |
| 1 | +use std::{ |
| 2 | + borrow::Borrow, |
| 3 | + cmp::{self, Ordering}, |
| 4 | + fmt, hash, iter, |
| 5 | + ops::Deref, |
| 6 | + sync::Arc, |
| 7 | +}; |
2 | 8 |
|
3 | 9 | /// A `SmolStr` is a string type that has the following properties: |
4 | 10 | /// |
@@ -368,10 +374,17 @@ impl Repr { |
368 | 374 | }; |
369 | 375 | } |
370 | 376 |
|
371 | | - let newlines = text.bytes().take_while(|&b| b == b'\n').count(); |
372 | | - if text[newlines..].bytes().all(|b| b == b' ') { |
373 | | - let spaces = len - newlines; |
374 | | - if newlines <= N_NEWLINES && spaces <= N_SPACES { |
| 377 | + if len <= N_NEWLINES + N_SPACES { |
| 378 | + let bytes = text.as_bytes(); |
| 379 | + let possible_newline_count = cmp::min(len, N_NEWLINES); |
| 380 | + let newlines = bytes[..possible_newline_count] |
| 381 | + .iter() |
| 382 | + .take_while(|&&b| b == b'\n') |
| 383 | + .count(); |
| 384 | + let possible_space_count = len - newlines; |
| 385 | + if possible_space_count <= N_SPACES && bytes[newlines..].iter().all(|&b| b == b' ') |
| 386 | + { |
| 387 | + let spaces = possible_space_count; |
375 | 388 | return Repr::Substring { newlines, spaces }; |
376 | 389 | } |
377 | 390 | } |
@@ -420,9 +433,9 @@ impl Repr { |
420 | 433 |
|
421 | 434 | #[cfg(feature = "serde")] |
422 | 435 | mod serde { |
| 436 | + use super::SmolStr; |
423 | 437 | use ::serde::de::{Deserializer, Error, Unexpected, Visitor}; |
424 | 438 | use std::fmt; |
425 | | - use super::SmolStr; |
426 | 439 |
|
427 | 440 | // https://github.com/serde-rs/serde/blob/629802f2abfd1a54a6072992888fea7ca5bc209f/serde/src/private/de.rs#L56-L125 |
428 | 441 | fn smol_str<'de: 'a, 'a, D>(deserializer: D) -> Result<SmolStr, D::Error> |
|
0 commit comments