Skip to content

Commit 15b1a4a

Browse files
committed
feat(lib): add string helpers
1 parent 2cf9356 commit 15b1a4a

File tree

3 files changed

+118
-3
lines changed

3 files changed

+118
-3
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "case_insensitive_string"
3-
version = "0.2.0"
3+
version = "0.2.3"
44
authors = ["Jeff Mendez <jeff@a11ywatch.com>"]
55
edition = "2021"
66
description = "A case insensitive string struct."

src/lib.rs

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ impl CaseInsensitiveString {
9898
&self.0.as_bytes()
9999
}
100100

101-
#[inline]
102101
#[cfg(not(feature = "compact"))]
102+
#[inline]
103103
pub fn inner(&self) -> &String {
104104
&self.0
105105
}
@@ -109,4 +109,119 @@ impl CaseInsensitiveString {
109109
pub fn inner(&self) -> &compact_str::CompactString {
110110
&self.0
111111
}
112+
113+
/// Appends the given [`char`] to the end of this [`CaseInsensitiveString`].
114+
///
115+
/// # Examples
116+
/// ```
117+
/// # use case_insensitive_string::CaseInsensitiveString;
118+
/// let mut s = CaseInsensitiveString::new("foo");
119+
///
120+
/// s.push('b');
121+
/// s.push('a');
122+
/// s.push('r');
123+
///
124+
/// assert_eq!(CaseInsensitiveString::from("foobar"), s);
125+
/// ```
126+
pub fn push(&mut self, ch: char) {
127+
self.push_str(ch.encode_utf8(&mut [0; 4]));
128+
}
129+
130+
/// Appends a given string slice onto the end of this [`CaseInsensitiveString`]
131+
///
132+
/// # Examples
133+
/// ```
134+
/// # use case_insensitive_string::CaseInsensitiveString;
135+
/// let mut s = CaseInsensitiveString::new("abc");
136+
///
137+
/// s.push_str("123");
138+
///
139+
/// assert_eq!(CaseInsensitiveString::new("abc123"), s);
140+
/// ```
141+
#[inline]
142+
pub fn push_str(&mut self, s: &str) {
143+
self.0.push_str(s)
144+
}
145+
146+
/// Removes a [`char`] from this [`CaseInsensitiveString`] at a byte position and returns it.
147+
///
148+
/// This is an *O*(*n*) operation, as it requires copying every element in the
149+
/// buffer.
150+
///
151+
/// # Panics
152+
///
153+
/// Panics if `idx` is larger than or equal to the [`CaseInsensitiveString`]'s length,
154+
/// or if it does not lie on a [`char`] boundary.
155+
///
156+
/// # Examples
157+
///
158+
/// ### Basic usage:
159+
///
160+
/// ```
161+
/// # use case_insensitive_string::CaseInsensitiveString;
162+
/// let mut c = CaseInsensitiveString::from("hello world");
163+
///
164+
/// assert_eq!(c.remove(0), 'h');
165+
/// assert_eq!(c, "ello world".into());
166+
///
167+
/// assert_eq!(c.remove(5), 'w');
168+
/// assert_eq!(c, "ello orld".into());
169+
/// ```
170+
///
171+
/// ### Past total length:
172+
///
173+
/// ```should_panic
174+
/// # use case_insensitive_string::CaseInsensitiveString;
175+
/// let mut c = CaseInsensitiveString::from("hello there!");
176+
/// c.remove(100);
177+
/// ```
178+
///
179+
/// ### Not on char boundary:
180+
///
181+
/// ```should_panic
182+
/// # use case_insensitive_string::CaseInsensitiveString;
183+
/// let mut c = CaseInsensitiveString::from("🦄");
184+
/// c.remove(1);
185+
/// ```
186+
#[inline]
187+
pub fn remove(&mut self, idx: usize) -> char {
188+
self.0.remove(idx)
189+
}
190+
191+
/// Returns the length of the [`CaseInsensitiveString`] in `bytes`, not [`char`]s or graphemes.
192+
///
193+
/// When using `UTF-8` encoding (which all strings in Rust do) a single character will be 1 to 4
194+
/// bytes long, therefore the return value of this method might not be what a human considers
195+
/// the length of the string.
196+
///
197+
/// # Examples
198+
/// ```
199+
/// # use case_insensitive_string::CaseInsensitiveString;
200+
/// let ascii = CaseInsensitiveString::new("hello world");
201+
/// assert_eq!(ascii.len(), 11);
202+
///
203+
/// let emoji = CaseInsensitiveString::new("👱");
204+
/// assert_eq!(emoji.len(), 4);
205+
/// ```
206+
#[inline]
207+
pub fn len(&self) -> usize {
208+
self.0.len()
209+
}
210+
211+
/// Returns `true` if the [`CaseInsensitiveString`] has a length of 0, `false` otherwise
212+
///
213+
/// # Examples
214+
/// ```
215+
/// # use case_insensitive_string::CaseInsensitiveString;
216+
/// let mut msg = CaseInsensitiveString::new("");
217+
/// assert!(msg.is_empty());
218+
///
219+
/// // add some characters
220+
/// msg.push_str("hello reader!");
221+
/// assert!(!msg.is_empty());
222+
/// ```
223+
#[inline]
224+
pub fn is_empty(&self) -> bool {
225+
self.len() == 0
226+
}
112227
}

0 commit comments

Comments
 (0)