Skip to content

Commit

Permalink
fix: string
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-jerry-ye committed Apr 3, 2024
1 parent 21c27f2 commit f7e119f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 78 deletions.
4 changes: 1 addition & 3 deletions examples/string/lib/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
{
"name": "lib"
}
{}
39 changes: 13 additions & 26 deletions examples/string/lib/string.mbt
Original file line number Diff line number Diff line change
@@ -1,38 +1,27 @@
pub fn split(s : String, sep : Char) -> (Array[String], Int) {
pub fn split(s : String, sep : Char) -> @vec.Vec[String] {
let mut index = 0
let mut result : Array[String] = Array::make(5, "")
let mut cur = 0
let result = @vec.Vec::new()
let it = iter(s)
while it.index != it.length {
let c = match it.next() {
Some(c) => c
None => {return (result, cur)}
None => return result
}
if c == sep {
let sub = match sub_string(s, index, it.index - 1) {
Some(sub) => sub
None => {return (result, cur)}
}
result.set(cur, sub)
cur = cur + 1
if cur >= result.length() {
let new_result = Array::make(result.length() * 2, "")
let mut tmp_cur = 0
while tmp_cur < result.length() {
new_result.set(tmp_cur, result[tmp_cur])
tmp_cur = tmp_cur + 1
}
result = new_result
None => return result
}
result.push(sub)
index = it.index
}
}
let sub = match sub_string(s, index, it.length) {
Some(sub) => sub
None => {return (result, cur)}
None => return result
}
result.set(cur, sub)
(result, cur)
result.push(sub)
result
}

pub fn sub_string(s : String, start : Int, end : Int) -> Option[String] {
Expand All @@ -44,7 +33,7 @@ pub fn sub_string(s : String, start : Int, end : Int) -> Option[String] {
while it.index != it.length {
let c = match it.next() {
Some(c) => c
None => {return None}
None => return None
}
if it.index - 1 >= start && it.index - 1 < end {
result = result + c.to_string()
Expand All @@ -65,8 +54,8 @@ pub fn trim(s : String, removed : Char) -> String {
}
if start <= end {
match sub_string(s, start, end + 1) {
Some(sub) => {return sub}
None => {return s}
Some(sub) => return sub
None => return s
}
}
s
Expand All @@ -79,8 +68,7 @@ pub fn index_of(s : String, pattern : String) -> Option[Int] {
let l = s.length()
while s_index < l {
if s[s_index] == pattern[p_index] {
while p_index < pattern.length() &&
s_index < l && s[s_index] == pattern[p_index] {
while p_index < pattern.length() && s_index < l && s[s_index] == pattern[p_index] {
s_index = s_index + 1
p_index = p_index + 1
}
Expand All @@ -105,8 +93,7 @@ pub fn last_index_of(s : String, pattern : String) -> Option[Int] {
let l = s.length()
while s_index < l {
if s[s_index] == pattern[p_index] {
while p_index < pattern.length() &&
s_index < l && s[s_index] == pattern[p_index] {
while p_index < pattern.length() && s_index < l && s[s_index] == pattern[p_index] {
s_index = s_index + 1
p_index = p_index + 1
}
Expand Down
15 changes: 15 additions & 0 deletions examples/string/lib/string_test.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
test {
let arr = split("a.b.c.e.f.g.h.i.j.k.l.m.n", '.')
@assertion.assert_eq(
arr,
@vec.Vec::["a", "b", "c", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n"],
)?
inspect(sub_string("Hello World", 3, 7), ~content="Some(lo W)")?
("Hello World" |> index_of("Wo") |> @assertion.assert_eq(Some(6)))?
("Hello World World Hello" |> last_index_of("Wo") |> @assertion.assert_eq(Some(12)))?
@assertion.assert_true(contains("Hello World", "Wo"))?
let arr = to_char_array("HelloWorld")
@assertion.assert_eq(arr, ['H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd'])?
@assertion.assert_eq(char_at("abcde", 4), Some('e'))?
@assertion.assert_eq(trim(" abcde ", ' '), "abcde")?
}
43 changes: 0 additions & 43 deletions examples/string/main/main.mbt

This file was deleted.

6 changes: 0 additions & 6 deletions examples/string/main/moon.pkg.json

This file was deleted.

0 comments on commit f7e119f

Please sign in to comment.