Skip to content

Commit

Permalink
lean on string slice to prevent copies
Browse files Browse the repository at this point in the history
  • Loading branch information
thatstoasty committed Sep 12, 2024
1 parent a40ba1c commit cacfb18
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
2 changes: 1 addition & 1 deletion mojoproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
29 changes: 28 additions & 1 deletion src/gojo/unicode/utf8/width.mojo
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from utils import StringSlice
from collections import InlineArray
from .table import Interval, narrow, combining, doublewidth, ambiguous, emoji, nonprint

Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/recipe.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ context:

package:
name: "gojo"
version: 0.1.6.nightly
version: 0.1.7.nightly

source:
- path: .
Expand Down
11 changes: 7 additions & 4 deletions test/test_unicode_width.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,25 @@ 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():
var ascii: String = "Hello, World!"

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():
var s: String = "🔥🔥🔥🔥"

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.")

0 comments on commit cacfb18

Please sign in to comment.