Skip to content

Commit

Permalink
Merge pull request #259 from moonbitlang/hongbo/string-to-array
Browse files Browse the repository at this point in the history
String to array #256
  • Loading branch information
bobzhang committed Apr 14, 2024
2 parents 18071d4 + 5c0c1ec commit d7da7cc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

3 changes: 2 additions & 1 deletion string/moon.pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"moonbitlang/core/builtin",
"moonbitlang/core/assertion",
"moonbitlang/core/coverage",
"moonbitlang/core/bytes"
"moonbitlang/core/bytes",
"moonbitlang/core/char"
]
}
20 changes: 20 additions & 0 deletions string/string.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,23 @@ test "Buffer::to_bytes" {
buffer.write_string("中文")
@assertion.assert_eq(buffer.to_bytes().to_string(), "中文")?
}

/// Converts the String into an array of Chars.
pub fn to_array(self : String) -> Array[Char] {
let len = self.length()
let rv = Array::make(len, '\x00')
for i = 0; i < len; i = i + 1 {
rv[i] = self[i]
}
rv
}

test "to_array" {
let a = "你好!mbt".to_array()
inspect(a[0], ~content="你")?
inspect(a[1], ~content="好")?
inspect(a[2], ~content="!")?
inspect(a[3], ~content="m")?
inspect(a[4], ~content="b")?
inspect(a[5], ~content="t")?
}

0 comments on commit d7da7cc

Please sign in to comment.