Skip to content

feat(string): add find_by_charcode #2084

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 89 additions & 1 deletion string/methods.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ test "find" {
///|
/// Returns the offset of the first character that satisfies the given predicate.
/// If no such character is found, it returns None.
#locals(pred)
pub fn View::find_by(self : View, pred : (Char) -> Bool) -> Int? {
for i, c in self {
if pred(c) {
Expand All @@ -83,8 +84,14 @@ pub fn View::find_by(self : View, pred : (Char) -> Bool) -> Int? {
///|
/// Returns the offset of the first character that satisfies the given predicate.
/// If no such character is found, it returns None.
#locals(pred)
pub fn String::find_by(self : String, pred : (Char) -> Bool) -> Int? {
self[:].find_by(pred)
for i, c in self {
if pred(c) {
return Some(i)
}
}
None
}

///|
Expand All @@ -101,6 +108,87 @@ test "find_by" {
inspect!("😀😁😂".find_by(fn(c) { c == '😂' }), content="Some(2)")
}

///|
/// Find the index of the first charcode that satisfies the given predicate.
/// If no such charcode is found, it returns None.
#locals(pred)
pub fn View::find_by_charcode(self : View, pred : (Int) -> Bool) -> Int? {
for i in 0..<self.length() {
if pred(self.unsafe_charcode_at(i)) {
return Some(i)
}
}
None
}

///|
/// Find the index of the first charcode that satisfies the given predicate.
/// If no such charcode is found, it returns None.
#locals(pred)
pub fn String::find_by_charcode(self : String, pred : (Int) -> Bool) -> Int? {
for i in 0..<self.length() {
if pred(self.unsafe_charcode_at(i)) {
return Some(i)
}
}
None
}

///|
test "find_by_charcode" {
// Test basic character finding
inspect!("hello".find_by_charcode(Int::op_equal(_, 'o')), content="Some(4)")
inspect!("hello".find_by_charcode(Int::op_equal(_, 'l')), content="Some(2)")
inspect!("hello".find_by_charcode(Int::op_equal(_, 'z')), content="None")
inspect!(
"hello"[1:].find_by_charcode(Int::op_equal(_, 'o')),
content="Some(3)",
)
inspect!("hello"[:4].find_by_charcode(Int::op_equal(_, 'o')), content="None")
inspect!("hello".find_by_charcode(Int::op_equal(_, 'z')), content="None")

// Test empty string
inspect!("".find_by_charcode(Int::op_equal(_, 'a')), content="None")

// Test numeric characters
inspect!(
"hello".find_by_charcode(fn(c) { c >= '0' && c <= '9' }),
content="None",
)
inspect!(
"hello123".find_by_charcode(fn(c) { c >= '0' && c <= '9' }),
content="Some(5)",
)

// Test case sensitivity
inspect!(
"hello".find_by_charcode(fn(c) { c >= 'A' && c <= 'Z' }),
content="None",
)
inspect!(
"Hello".find_by_charcode(fn(c) { c >= 'A' && c <= 'Z' }),
content="Some(0)",
)

// Test non-ASCII characters
inspect!("αβγ".find_by_charcode(fn(c) { c == 'β' }), content="Some(1)")

// Test emoji and multi-byte characters
inspect!(
"😀😁😂".find_by_charcode(fn(c) { c == 0xD83D }),
content="Some(0)",
) // First code unit of 😀

// Test specific code point ranges
inspect!(
"Hello, 世界!".find_by_charcode(fn(c) { c > 127 }),
content="Some(7)",
) // First non-ASCII character

// Test with complex predicate
inspect!("a1b2c3".find_by_charcode(fn(c) { c % 2 == 0 }), content="Some(2)") // '1' has code 49 which is odd
}

///|
/// Returns the offset of the last occurrence of the given substring. If the
/// substring is not found, it returns None.
Expand Down
2 changes: 2 additions & 0 deletions string/string.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ impl StringView {
contains_char(Self, Char) -> Bool
find(Self, Self) -> Int?
find_by(Self, (Char) -> Bool) -> Int?
find_by_charcode(Self, (Int) -> Bool) -> Int?
fold[A](Self, init~ : A, (A, Char) -> A) -> A
from_array(Array[Char]) -> Self
from_iter(Iter[Char]) -> Self
Expand Down Expand Up @@ -148,6 +149,7 @@ impl String {
ends_with(String, String) -> Bool
find(String, StringView) -> Int?
find_by(String, (Char) -> Bool) -> Int?
find_by_charcode(String, (Int) -> Bool) -> Int?
fold[A](String, init~ : A, (A, Char) -> A) -> A
from_array(Array[Char]) -> String
from_iter(Iter[Char]) -> String
Expand Down