Skip to content
This repository was archived by the owner on Feb 14, 2025. It is now read-only.

Commit cacfb18

Browse files
committed
lean on string slice to prevent copies
1 parent a40ba1c commit cacfb18

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

mojoproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ channels = ["conda-forge", "https://conda.modular.com/max-nightly"]
44
description = "Experiments in porting over Golang stdlib into Mojo."
55
name = "gojo"
66
platforms = ["osx-arm64", "linux-64"]
7-
version = "0.1.6.nightly"
7+
version = "0.1.7.nightly"
88

99
[tasks]
1010
tests = "bash scripts/tests.sh"

src/gojo/unicode/utf8/width.mojo

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from utils import StringSlice
12
from collections import InlineArray
23
from .table import Interval, narrow, combining, doublewidth, ambiguous, emoji, nonprint
34

@@ -73,7 +74,21 @@ struct Condition:
7374
"""
7475
var width = 0
7576
for r in s:
76-
width += self.rune_width(ord(String(r)))
77+
width += self.rune_width(ord(r))
78+
return width
79+
80+
fn string_width(self, s: StringSlice) -> Int:
81+
"""Return width as you can see.
82+
83+
Args:
84+
s: The string to calculate the width of.
85+
86+
Returns:
87+
The printable width of the string.
88+
"""
89+
var width = 0
90+
for r in s:
91+
width += self.rune_width(ord(r))
7792
return width
7893

7994

@@ -119,6 +134,18 @@ fn string_width(s: String) -> Int:
119134
return DEFAULT_CONDITION.string_width(s)
120135

121136

137+
fn string_width(s: StringSlice) -> Int:
138+
"""Return width as you can see.
139+
140+
Args:
141+
s: The string to calculate the width of.
142+
143+
Returns:
144+
The printable width of the string.
145+
"""
146+
return DEFAULT_CONDITION.string_width(s)
147+
148+
122149
fn rune_width(rune: UInt32) -> Int:
123150
"""Return width as you can see.
124151

src/recipe.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ context:
55

66
package:
77
name: "gojo"
8-
version: 0.1.6.nightly
8+
version: 0.1.7.nightly
99

1010
source:
1111
- path: .

test/test_unicode_width.mojo

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,25 @@ import testing
55
def test_string_width_east_asian():
66
var s: String = "𡨸漢𡨸漢"
77

8-
testing.assert_equal(string_width(s), 8)
8+
testing.assert_equal(string_width(s), 8, msg="The length of 𡨸漢𡨸漢 should be 8.")
99
for r in s:
10-
testing.assert_equal(rune_width(ord(String(r))), 2)
10+
testing.assert_equal(rune_width(ord(r)), 2, msg="The width of each character should be 2.")
11+
testing.assert_equal(string_width(r), 2, msg="The width of each character should be 2.")
1112

1213

1314
def test_string_width_ascii():
1415
var ascii: String = "Hello, World!"
1516

1617
testing.assert_equal(string_width(ascii), 13)
1718
for r in ascii:
18-
testing.assert_equal(rune_width(ord(String(r))), 1)
19+
testing.assert_equal(rune_width(ord(r)), 1, msg="The width of each character should be 1.")
20+
testing.assert_equal(string_width(r), 1, msg="The width of each character should be 1.")
1921

2022

2123
def test_string_width_emoji():
2224
var s: String = "🔥🔥🔥🔥"
2325

2426
testing.assert_equal(string_width(s), 8)
2527
for r in s:
26-
testing.assert_equal(rune_width(ord(String(r))), 2)
28+
testing.assert_equal(rune_width(ord(r)), 2, msg="The width of each character should be 2.")
29+
testing.assert_equal(string_width(r), 2, msg="The width of each character should be 2.")

0 commit comments

Comments
 (0)