From f7e119f6c8a47bc4ef0c1625596b440e8fa7cbbe Mon Sep 17 00:00:00 2001 From: zihang Date: Wed, 3 Apr 2024 13:42:38 +0800 Subject: [PATCH] fix: string --- examples/string/lib/moon.pkg.json | 4 +-- examples/string/lib/string.mbt | 39 +++++++++----------------- examples/string/lib/string_test.mbt | 15 ++++++++++ examples/string/main/main.mbt | 43 ----------------------------- examples/string/main/moon.pkg.json | 6 ---- 5 files changed, 29 insertions(+), 78 deletions(-) create mode 100644 examples/string/lib/string_test.mbt delete mode 100644 examples/string/main/main.mbt delete mode 100644 examples/string/main/moon.pkg.json diff --git a/examples/string/lib/moon.pkg.json b/examples/string/lib/moon.pkg.json index 47b3a8cb..9e26dfee 100644 --- a/examples/string/lib/moon.pkg.json +++ b/examples/string/lib/moon.pkg.json @@ -1,3 +1 @@ -{ - "name": "lib" -} \ No newline at end of file +{} \ No newline at end of file diff --git a/examples/string/lib/string.mbt b/examples/string/lib/string.mbt index 9a853c98..b16676d9 100644 --- a/examples/string/lib/string.mbt +++ b/examples/string/lib/string.mbt @@ -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] { @@ -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() @@ -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 @@ -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 } @@ -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 } diff --git a/examples/string/lib/string_test.mbt b/examples/string/lib/string_test.mbt new file mode 100644 index 00000000..883befc5 --- /dev/null +++ b/examples/string/lib/string_test.mbt @@ -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")? +} diff --git a/examples/string/main/main.mbt b/examples/string/main/main.mbt deleted file mode 100644 index defc7ac3..00000000 --- a/examples/string/main/main.mbt +++ /dev/null @@ -1,43 +0,0 @@ -fn init { - let (arr, len) = @lib.split("a.b.c.e.f.g.h.i.j.k.l.m.n", '.') - let mut index = 0 - // let l = arr.length() - while index < len { - println(arr[index]) - index = index + 1 - } - - match @lib.sub_string("Hello World", 3, 7) { - Some(v) => println(v) - None => println("Not found") - } - - match @lib.index_of("Hello World", "Wo") { - Some(v) => println(v) - None => println("Not found") - } - - match @lib.last_index_of("Hello World World Hello", "Wo") { - Some(v) => println(v) - None => println("Not found") - } - - match @lib.contains("Hello World", "Wo") { - true => println("Contains") - false => println("Not found") - } - - let arr = @lib.to_char_array("HelloWorld") - let mut index = 0 - let l = arr.length() - while index < l { - println(arr[index]) - index = index + 1 - } - - match @lib.char_at("abcde", 4) { - Some(v) => println(v) - None => println("Not found") - } - println(@lib.trim(" abcde ", ' ')) -} diff --git a/examples/string/main/moon.pkg.json b/examples/string/main/moon.pkg.json deleted file mode 100644 index ebf7dc82..00000000 --- a/examples/string/main/moon.pkg.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "name": "main", - "import": { - "string/lib": "" - } -} \ No newline at end of file