Skip to content

Commit

Permalink
Unicode string iter (#48)
Browse files Browse the repository at this point in the history
* update string iter to not copy strings

* add rune width

* add missing tests
  • Loading branch information
thatstoasty authored Jul 7, 2024
1 parent bf7e1d0 commit 7cd2dfe
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
23 changes: 23 additions & 0 deletions tests/test_unicode_string.mojo
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from gojo.unicode import UnicodeString
from tests.wrapper import MojoTest


fn test_unicode_string():
var test = MojoTest("Testing unicode.UnicodeString")
var s = UnicodeString("𡨸漢𡨸漢")
test.assert_equal(s.bytecount(), 14)
test.assert_equal(len(s), 4)

var i = 0
var results = List[String]("𡨸", "", "𡨸", "")
for c in s:
test.assert_equal(String(c), results[i])
i += 1

test.assert_equal(String(s[:1]), "𡨸")
test.assert_equal(String(s[:2]), "𡨸漢")
# test.assert_equal(String(s[:-1]), "𡨸漢𡨸漢")


fn main():
test_unicode_string()
20 changes: 20 additions & 0 deletions tests/test_unicode_width.mojo
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from gojo.unicode import string_width, rune_width, UnicodeString
from tests.wrapper import MojoTest


fn test_string_width():
var test = MojoTest("Testing unicode.string_width and unicode.rune_width")
var ascii = "Hello, World!"
var s: String = "𡨸漢𡨸漢"
test.assert_equal(string_width(s), 8)
test.assert_equal(string_width(ascii), 13)

for r in UnicodeString(s):
test.assert_equal(rune_width(ord(String(r))), 2)

for r in UnicodeString(ascii):
test.assert_equal(rune_width(ord(String(r))), 1)


fn main():
test_string_width()

0 comments on commit 7cd2dfe

Please sign in to comment.