Skip to content

Commit 2f00aeb

Browse files
authored
[ISSUE #86]✨Add character-based methods to CheetahString: starts_with_char, ends_with_char, and contains_char (#88)
1 parent cf3a2be commit 2f00aeb

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

examples/char_methods.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use cheetah_string::CheetahString;
2+
3+
fn main() {
4+
let s = CheetahString::from("hello world");
5+
6+
// Use starts_with_char method to check characters
7+
println!("starts_with_char('h'): {}", s.starts_with_char('h'));
8+
println!("starts_with_char('+'): {}", s.starts_with_char('+'));
9+
10+
// Use ends_with_char method to check characters
11+
println!("ends_with_char('d'): {}", s.ends_with_char('d'));
12+
println!("ends_with_char('+'): {}", s.ends_with_char('+'));
13+
14+
// Use contains_char method to check characters
15+
println!("contains_char('o'): {}", s.contains_char('o'));
16+
println!("contains_char('+'): {}", s.contains_char('+'));
17+
18+
// Original methods still work with strings
19+
println!("starts_with(\"hello\"): {}", s.starts_with("hello"));
20+
println!("ends_with(\"world\"): {}", s.ends_with("world"));
21+
println!("contains(\"llo\"): {}", s.contains("llo"));
22+
}

src/cheetah_string.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,22 @@ impl CheetahString {
468468
self.as_str().starts_with(pat.as_ref())
469469
}
470470

471+
/// Returns `true` if the string starts with the given character.
472+
///
473+
/// # Examples
474+
///
475+
/// ```
476+
/// use cheetah_string::CheetahString;
477+
///
478+
/// let s = CheetahString::from("hello world");
479+
/// assert!(s.starts_with_char('h'));
480+
/// assert!(!s.starts_with_char('w'));
481+
/// ```
482+
#[inline]
483+
pub fn starts_with_char(&self, pat: char) -> bool {
484+
self.as_str().starts_with(pat)
485+
}
486+
471487
/// Returns `true` if the string ends with the given pattern.
472488
///
473489
/// # Examples
@@ -484,6 +500,22 @@ impl CheetahString {
484500
self.as_str().ends_with(pat.as_ref())
485501
}
486502

503+
/// Returns `true` if the string ends with the given character.
504+
///
505+
/// # Examples
506+
///
507+
/// ```
508+
/// use cheetah_string::CheetahString;
509+
///
510+
/// let s = CheetahString::from("hello world");
511+
/// assert!(s.ends_with_char('d'));
512+
/// assert!(!s.ends_with_char('h'));
513+
/// ```
514+
#[inline]
515+
pub fn ends_with_char(&self, pat: char) -> bool {
516+
self.as_str().ends_with(pat)
517+
}
518+
487519
/// Returns `true` if the string contains the given pattern.
488520
///
489521
/// # Examples
@@ -500,6 +532,22 @@ impl CheetahString {
500532
self.as_str().contains(pat.as_ref())
501533
}
502534

535+
/// Returns `true` if the string contains the given character.
536+
///
537+
/// # Examples
538+
///
539+
/// ```
540+
/// use cheetah_string::CheetahString;
541+
///
542+
/// let s = CheetahString::from("hello world");
543+
/// assert!(s.contains_char('o'));
544+
/// assert!(!s.contains_char('x'));
545+
/// ```
546+
#[inline]
547+
pub fn contains_char(&self, pat: char) -> bool {
548+
self.as_str().contains(pat)
549+
}
550+
503551
/// Returns the byte index of the first occurrence of the pattern, or `None` if not found.
504552
///
505553
/// # Examples

0 commit comments

Comments
 (0)