diff --git a/mojoproject.toml b/mojoproject.toml index bda1c79..0ba5e59 100644 --- a/mojoproject.toml +++ b/mojoproject.toml @@ -4,7 +4,7 @@ channels = ["conda-forge", "https://conda.modular.com/max-nightly"] description = "Experiments in porting over Golang stdlib into Mojo." name = "gojo" platforms = ["osx-arm64", "linux-64"] -version = "0.1.6.nightly" +version = "0.1.7.nightly" [tasks] tests = "bash scripts/tests.sh" diff --git a/src/gojo/unicode/utf8/width.mojo b/src/gojo/unicode/utf8/width.mojo index 5933638..8cdc421 100644 --- a/src/gojo/unicode/utf8/width.mojo +++ b/src/gojo/unicode/utf8/width.mojo @@ -1,3 +1,4 @@ +from utils import StringSlice from collections import InlineArray from .table import Interval, narrow, combining, doublewidth, ambiguous, emoji, nonprint @@ -73,7 +74,21 @@ struct Condition: """ var width = 0 for r in s: - width += self.rune_width(ord(String(r))) + width += self.rune_width(ord(r)) + return width + + fn string_width(self, s: StringSlice) -> Int: + """Return width as you can see. + + Args: + s: The string to calculate the width of. + + Returns: + The printable width of the string. + """ + var width = 0 + for r in s: + width += self.rune_width(ord(r)) return width @@ -119,6 +134,18 @@ fn string_width(s: String) -> Int: return DEFAULT_CONDITION.string_width(s) +fn string_width(s: StringSlice) -> Int: + """Return width as you can see. + + Args: + s: The string to calculate the width of. + + Returns: + The printable width of the string. + """ + return DEFAULT_CONDITION.string_width(s) + + fn rune_width(rune: UInt32) -> Int: """Return width as you can see. diff --git a/src/recipe.yaml b/src/recipe.yaml index c786ab1..d5c5e74 100644 --- a/src/recipe.yaml +++ b/src/recipe.yaml @@ -5,7 +5,7 @@ context: package: name: "gojo" - version: 0.1.6.nightly + version: 0.1.7.nightly source: - path: . diff --git a/test/test_unicode_width.mojo b/test/test_unicode_width.mojo index ae99565..c2496a1 100644 --- a/test/test_unicode_width.mojo +++ b/test/test_unicode_width.mojo @@ -5,9 +5,10 @@ import testing def test_string_width_east_asian(): var s: String = "𡨸漢𡨸漢" - testing.assert_equal(string_width(s), 8) + testing.assert_equal(string_width(s), 8, msg="The length of 𡨸漢𡨸漢 should be 8.") for r in s: - testing.assert_equal(rune_width(ord(String(r))), 2) + testing.assert_equal(rune_width(ord(r)), 2, msg="The width of each character should be 2.") + testing.assert_equal(string_width(r), 2, msg="The width of each character should be 2.") def test_string_width_ascii(): @@ -15,7 +16,8 @@ def test_string_width_ascii(): testing.assert_equal(string_width(ascii), 13) for r in ascii: - testing.assert_equal(rune_width(ord(String(r))), 1) + testing.assert_equal(rune_width(ord(r)), 1, msg="The width of each character should be 1.") + testing.assert_equal(string_width(r), 1, msg="The width of each character should be 1.") def test_string_width_emoji(): @@ -23,4 +25,5 @@ def test_string_width_emoji(): testing.assert_equal(string_width(s), 8) for r in s: - testing.assert_equal(rune_width(ord(String(r))), 2) + testing.assert_equal(rune_width(ord(r)), 2, msg="The width of each character should be 2.") + testing.assert_equal(string_width(r), 2, msg="The width of each character should be 2.")