Unnamed repository; edit this file 'description' to name the repository.
-rw-r--r--lib/smol_str/src/lib.rs23
1 files changed, 18 insertions, 5 deletions
diff --git a/lib/smol_str/src/lib.rs b/lib/smol_str/src/lib.rs
index 45f1cb2542..7314d270f3 100644
--- a/lib/smol_str/src/lib.rs
+++ b/lib/smol_str/src/lib.rs
@@ -1,4 +1,10 @@
-use std::{borrow::Borrow, cmp::Ordering, fmt, hash, iter, ops::Deref, sync::Arc};
+use std::{
+ borrow::Borrow,
+ cmp::{self, Ordering},
+ fmt, hash, iter,
+ ops::Deref,
+ sync::Arc,
+};
/// A `SmolStr` is a string type that has the following properties:
///
@@ -358,10 +364,17 @@ impl Repr {
};
}
- let newlines = text.bytes().take_while(|&b| b == b'\n').count();
- if text[newlines..].bytes().all(|b| b == b' ') {
- let spaces = len - newlines;
- if newlines <= N_NEWLINES && spaces <= N_SPACES {
+ if len <= N_NEWLINES + N_SPACES {
+ let bytes = text.as_bytes();
+ let possible_newline_count = cmp::min(len, N_NEWLINES);
+ let newlines = bytes[..possible_newline_count]
+ .iter()
+ .take_while(|&&b| b == b'\n')
+ .count();
+ let possible_space_count = len - newlines;
+ if possible_space_count <= N_SPACES && bytes[newlines..].iter().all(|&b| b == b' ')
+ {
+ let spaces = possible_space_count;
return Repr::Substring { newlines, spaces };
}
}