Skip to content

Commit 65c4975

Browse files
Yu-zhbobzhang
authored andcommitted
refactor(string): deprecate String::op_get and some other methods
1 parent 646e15a commit 65c4975

14 files changed

+70
-90
lines changed

Diff for: builtin/bigint_nonjs.mbt

+16-13
Original file line numberDiff line numberDiff line change
@@ -1042,20 +1042,23 @@ pub fn BigInt::from_string(input : String) -> BigInt {
10421042
if len == 0 {
10431043
abort("empty string")
10441044
}
1045-
let sign : Sign = if input[0] == '-' { Negative } else { Positive }
1045+
let sign : Sign = if Char::from_int(input.unsafe_charcode_at(0)) == '-' {
1046+
Negative
1047+
} else {
1048+
Positive
1049+
}
10461050
let mut b_len = (
10471051
(len.to_double() / decimal_ratio).to_unchecked_int() + 1 + radix_bit_len
10481052
) /
10491053
radix_bit_len +
10501054
1
10511055
let b = FixedArray::make(b_len, 0U)
1052-
for i = (match sign {
1053-
Negative => 1
1054-
Positive => 0
1055-
})
1056-
i < input.length()
1057-
i = i + 1 {
1058-
let x = input[i].to_int() - 48 // ASCII value of '0'
1056+
for
1057+
i in (match sign {
1058+
Negative => 1
1059+
Positive => 0
1060+
})..<input.length() {
1061+
let x = input.unsafe_charcode_at(i) - 48 // ASCII value of '0'
10591062
if x < 0 || x > 9 {
10601063
abort("invalid character")
10611064
}
@@ -1108,8 +1111,7 @@ pub fn BigInt::from_string(input : String) -> BigInt {
11081111
/// ```
11091112
pub fn BigInt::from_hex(input : String) -> BigInt {
11101113
// WARN: this implementation assumes that `radix_bit_len` is a multiple of 4.
1111-
fn char_from_hex(c : Char) -> UInt {
1112-
let x = c.to_int()
1114+
fn char_from_hex(x : Int) -> UInt {
11131115
if x >= 48 && x <= 57 {
11141116
// ASCII value of '0'
11151117
(x - 48).reinterpret_as_uint()
@@ -1128,7 +1130,7 @@ pub fn BigInt::from_hex(input : String) -> BigInt {
11281130
if len == 0 {
11291131
abort("empty string")
11301132
}
1131-
let (sign, number_len) = if input[0] == '-' {
1133+
let (sign, number_len) = if Char::from_int(input.unsafe_charcode_at(0)) == '-' {
11321134
(Negative, len - 1)
11331135
} else {
11341136
(Positive, len)
@@ -1141,13 +1143,14 @@ pub fn BigInt::from_hex(input : String) -> BigInt {
11411143
if mod != 0 {
11421144
let start = len - quotient * nb_char - mod
11431145
for i in 0..<mod {
1144-
b[b_len - 1] = (b[b_len - 1] << 4) | char_from_hex(input[start + i])
1146+
b[b_len - 1] = (b[b_len - 1] << 4) |
1147+
char_from_hex(input.unsafe_charcode_at(start + i))
11451148
}
11461149
}
11471150
for i in 0..<quotient {
11481151
let start = len - (i + 1) * nb_char
11491152
for j in 0..<nb_char {
1150-
b[i] = (b[i] << 4) | char_from_hex(input[start + j])
1153+
b[i] = (b[i] << 4) | char_from_hex(input.unsafe_charcode_at(start + j))
11511154
}
11521155
}
11531156
while b[b_len - 1] == 0 && b_len > 1 {

Diff for: builtin/bytes.mbt

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ pub fn FixedArray::blit_from_string(
192192
guard length >= 0 && s1 >= 0 && e1 < len1 && s2 >= 0 && e2 < len2
193193
let end_str_offset = str_offset + length
194194
for i = str_offset, j = bytes_offset; i < end_str_offset; i = i + 1, j = j + 2 {
195-
let c = str[i].to_uint()
195+
let c = str.unsafe_charcode_at(i).reinterpret_as_uint()
196196
self[j] = (c & 0xff).to_byte()
197197
self[j + 1] = (c >> 8).to_byte()
198198
}

Diff for: builtin/hasher.mbt

+1-1
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ pub fn Hasher::combine_bytes(self : Hasher, value : Bytes) -> Unit {
363363
/// ```
364364
pub fn Hasher::combine_string(self : Hasher, value : String) -> Unit {
365365
for i in 0..<value.length() {
366-
self.combine_uint(value[i].to_uint())
366+
self.combine_uint(value.unsafe_charcode_at(i).reinterpret_as_uint())
367367
}
368368
}
369369

Diff for: builtin/intrinsics.mbt

+4-20
Original file line numberDiff line numberDiff line change
@@ -1745,25 +1745,7 @@ pub fn FixedArray::make[T](len : Int, init : T) -> FixedArray[T] = "%fixedarray.
17451745
pub fn String::length(self : String) -> Int = "%string_length"
17461746

17471747
///|
1748-
/// Returns the length of the string in UTF-16 code units. This corresponds to
1749-
/// the number of 16-bit elements needed to store the string's contents.
1750-
///
1751-
/// Parameters:
1752-
///
1753-
/// * `string` : The string whose length is to be determined.
1754-
///
1755-
/// Returns the number of UTF-16 code units in the string. Note that this may not
1756-
/// equal the number of Unicode characters (code points) in the string, as some
1757-
/// characters (like emojis) require two UTF-16 code units.
1758-
///
1759-
/// Example:
1760-
///
1761-
/// ```moonbit
1762-
/// test "String::charcode_length" {
1763-
/// let s = "Hello🤣"
1764-
/// inspect!(s.charcode_length(), content="7") // 5 ASCII chars + 2 surrogate pairs for emoji
1765-
/// }
1766-
/// ```
1748+
#deprecated("use `length` instead")
17671749
pub fn String::charcode_length(self : String) -> Int = "%string_length"
17681750

17691751
///|
@@ -1796,6 +1778,7 @@ pub fn String::charcode_length(self : String) -> Int = "%string_length"
17961778
/// ```
17971779
///
17981780
/// @alert unsafe "Panic if index is out of bounds"
1781+
#deprecated("use `charcode_at` instead")
17991782
pub fn String::op_get(self : String, idx : Int) -> Char = "%string_get"
18001783

18011784
///|
@@ -1826,6 +1809,7 @@ pub fn String::op_get(self : String, idx : Int) -> Char = "%string_get"
18261809
/// ignore(s.get(5)) // Index equals length
18271810
/// }
18281811
/// ```
1812+
#deprecated("use `charcode_at` instead")
18291813
pub fn String::get(self : String, idx : Int) -> Char = "%string_get"
18301814

18311815
///|
@@ -1852,7 +1836,7 @@ pub fn String::get(self : String, idx : Int) -> Char = "%string_get"
18521836
/// ```
18531837
///
18541838
/// @alert unsafe "Panic if index is out of bounds."
1855-
pub fn String::unsafe_charcode_at(self : String, idx : Int) -> Int = "%string_get"
1839+
pub fn String::unsafe_charcode_at(self : String, idx : Int) -> Int = "%string.unsafe_get"
18561840

18571841
///|
18581842
/// Concatenates two strings, creating a new string that contains all characters

Diff for: builtin/output.mbt

+10-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ fn Int64::output(self : Int64, logger : &Logger, radix~ : Int = 10) -> Unit {
3131
if num2 != 0L {
3232
write_digits(num2)
3333
}
34-
logger.write_char(ALPHABET[abs(num % radix).to_int()])
34+
logger.write_char(
35+
Char::from_int(ALPHABET.charcode_at(abs(num % radix).to_int())),
36+
)
3537
}
3638

3739
write_digits(abs(self))
@@ -55,7 +57,7 @@ fn Int::output(self : Int, logger : &Logger, radix~ : Int = 10) -> Unit {
5557
if num2 != 0 {
5658
write_digits(num2)
5759
}
58-
logger.write_char(ALPHABET[abs(num % radix)])
60+
logger.write_char(Char::from_int(ALPHABET.charcode_at(abs(num % radix))))
5961
}
6062

6163
write_digits(abs(self))
@@ -69,7 +71,9 @@ fn UInt::output(self : UInt, logger : &Logger, radix~ : Int = 10) -> Unit {
6971
if num2 != 0U {
7072
write_digits(num2)
7173
}
72-
logger.write_char(ALPHABET[(num % radix).reinterpret_as_int()])
74+
logger.write_char(
75+
Char::from_int(ALPHABET.charcode_at((num % radix).reinterpret_as_int())),
76+
)
7377
}
7478

7579
write_digits(self)
@@ -83,7 +87,9 @@ fn UInt64::output(self : UInt64, logger : &Logger, radix~ : Int = 10) -> Unit {
8387
if num2 != 0UL {
8488
write_digits(num2)
8589
}
86-
logger.write_char(ALPHABET[(num % radix).to_int()])
90+
logger.write_char(
91+
Char::from_int(ALPHABET.charcode_at((num % radix).to_int())),
92+
)
8793
}
8894

8995
write_digits(self)

Diff for: builtin/show.mbt

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ pub impl Show for String with output(self, logger) {
103103
}
104104

105105
for i in 0..<self.length() {
106-
let c = self[i]
106+
let c = self.unsafe_charcode_at(i) |> Char::from_int
107107
match c {
108108
'"' | '\\' => {
109109
flush_segment(i)

Diff for: builtin/string.mbt

+3-20
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,7 @@ fn code_point_of_surrogate_pair(leading : Int, trailing : Int) -> Char {
7676
/// # Panics
7777
///
7878
/// @alert unsafe "Panics if the index is out of bounds."
79-
pub fn String::charcode_at(self : String, index : Int) -> Int {
80-
guard index >= 0 && index < self.length() else {
81-
abort("index out of bounds")
82-
}
83-
self.unsafe_charcode_at(index)
84-
}
79+
pub fn String::charcode_at(self : String, index : Int) -> Int = "%string_get"
8580
8681
///|
8782
/// Returns the Unicode code point at the given index.
@@ -103,7 +98,7 @@ pub fn String::charcode_at(self : String, index : Int) -> Int {
10398
/// - The index is out of bounds
10499
/// - The string contains an invalid surrogate pair
105100
pub fn String::codepoint_at(self : String, index : Int) -> Char {
106-
let charcode_len = self.charcode_length()
101+
let charcode_len = self.length()
107102
guard index >= 0 && index < charcode_len else { abort("index out of bounds") }
108103
for char_count = 0, utf16_offset = 0
109104
char_count < charcode_len && utf16_offset < index
@@ -150,7 +145,7 @@ pub fn String::codepoint_at(self : String, index : Int) -> Char {
150145
/// inspect!(s.charcode_length(), content = "7"); // 5 ASCII chars + 2 surrogate pairs
151146
/// ```
152147
pub fn String::codepoint_length(self : String) -> Int {
153-
let charcode_len = self.charcode_length()
148+
let charcode_len = self.length()
154149
for char_count = 0, len = 0
155150
char_count < charcode_len
156151
char_count = char_count + 1, len = len + 1 {
@@ -168,18 +163,6 @@ pub fn String::codepoint_length(self : String) -> Int {
168163
}
169164
}
170165
171-
///|
172-
// planned op_get method for String
173-
// pub fn String::op_get(self : String, index : Int) -> Char {
174-
// codepoint_at(self, index)
175-
// }
176-
177-
///|
178-
// planned length method for String
179-
// pub fn String::length(self : String) -> Int {
180-
// codepoint_length(self)
181-
//}
182-
183166
///|
184167
/// @intrinsic %string.substring
185168
fn unsafe_substring(str : String, start : Int, end : Int) -> String {

Diff for: coverage/coverage.mbt

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ pub fn end() -> Unit {
110110
let str = sbuf._[i]
111111
let mut start = 0
112112
for j in 0..<str.length() {
113-
if str[j] == '\n' {
113+
if Char::from_int(str.unsafe_charcode_at(j)) == '\n' {
114114
line_buf.write_substring(str, start, j)
115115
println(line_buf.to_string())
116116
line_buf.reset()

Diff for: json/lex_misc.mbt

+6-7
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,19 @@
1515
///|
1616
fn ParseContext::read_char(ctx : ParseContext) -> Char? {
1717
if ctx.offset < ctx.end_offset {
18-
let c = ctx.input[ctx.offset]
18+
let c1 = ctx.input.unsafe_charcode_at(ctx.offset)
1919
ctx.offset += 1
20-
let c1 = c.to_int()
2120
if c1 >= 0xD800 && c1 <= 0xDBFF {
2221
if ctx.offset < ctx.end_offset {
23-
let c2 = ctx.input[ctx.offset].to_int()
22+
let c2 = ctx.input.unsafe_charcode_at(ctx.offset)
2423
if c2 >= 0xDC00 && c2 <= 0xDFFF {
2524
ctx.offset += 1
2625
let c3 = (c1 << 10) + c2 - 0x35fdc00
2726
return Some(Char::from_int(c3))
2827
}
2928
}
3029
}
31-
Some(c)
30+
Some(Char::from_int(c1))
3231
} else {
3332
None
3433
}
@@ -46,7 +45,7 @@ const SURROGATE_HIGH_CHAR = 0xDFFF
4645
/// otherwise raise an error, when it is an error, the position is unspecified.
4746
fn ParseContext::expect_char(ctx : ParseContext, c : Char) -> Unit!ParseError {
4847
guard ctx.offset < ctx.end_offset else { raise InvalidEof }
49-
let c1 = ctx.input[ctx.offset].to_int()
48+
let c1 = ctx.input.unsafe_charcode_at(ctx.offset)
5049
ctx.offset += 1
5150
let c0 = c.to_int()
5251
if c0 < 0xFFFF {
@@ -62,7 +61,7 @@ fn ParseContext::expect_char(ctx : ParseContext, c : Char) -> Unit!ParseError {
6261
ctx.offset < ctx.end_offset else {
6362
ctx.invalid_char!(shift=-1)
6463
}
65-
let c2 = ctx.input[ctx.offset].to_int()
64+
let c2 = ctx.input.unsafe_charcode_at(ctx.offset)
6665
let c3 = (c1 << 10) + c2 - 0x35fdc00
6766
if c3 != c0 {
6867
ctx.invalid_char!(shift=-1)
@@ -80,7 +79,7 @@ fn ParseContext::expect_ascii_char(
8079
c : Byte
8180
) -> Unit!ParseError {
8281
guard ctx.offset < ctx.end_offset else { raise InvalidEof }
83-
let c1 = ctx.input[ctx.offset].to_int()
82+
let c1 = ctx.input.unsafe_charcode_at(ctx.offset)
8483
ctx.offset += 1
8584
if c.to_int() != c1 {
8685
ctx.invalid_char!(shift=-1)

Diff for: json/utils.mbt

+5-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn offset_to_position(input : String, offset : Int) -> Position {
1717
let mut line = 1
1818
let mut column = 0
1919
for i in 0..<offset {
20-
let c = input[i]
20+
let c = input.unsafe_charcode_at(i) |> Char::from_int
2121
if c == '\n' {
2222
line += 1
2323
column = 0
@@ -35,5 +35,8 @@ fn ParseContext::invalid_char[T](
3535
) -> T!ParseError {
3636
let offset = ctx.offset + shift
3737
// FIXME: this should check the surrogate pair
38-
raise InvalidChar(offset_to_position(ctx.input, offset), ctx.input[offset])
38+
raise InvalidChar(
39+
offset_to_position(ctx.input, offset),
40+
ctx.input.unsafe_charcode_at(offset) |> Char::from_int,
41+
)
3942
}

Diff for: strconv/decimal.mbt

+1-1
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ fn new_digits(self : Decimal, s : Int) -> Int {
500500
less = true
501501
break
502502
}
503-
let d = cheat_num[i].to_int() - '0'.to_int()
503+
let d = cheat_num.unsafe_charcode_at(i) - '0'.to_int()
504504
if self.digits[i].to_int() != d {
505505
less = self.digits[i].to_int() < d
506506
break

Diff for: strconv/string_slice.mbt

+3-3
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ fn step(self : StringSlice, step : Int) -> StringSlice? {
5959
let mut step = step
6060
let mut start = c.start
6161
while start < c.end && step > 0 {
62-
if c.string[start] != '_' {
62+
if Char::from_int(c.string.unsafe_charcode_at(start)) != '_' {
6363
step -= 1
6464
}
6565
start += 1
@@ -80,7 +80,7 @@ fn step_1_unchecked(self : StringSlice) -> StringSlice {
8080
///|
8181
/// Unsafe: make sure index is in bounds
8282
fn op_get(self : StringSlice, index : Int) -> Char {
83-
self.string[self.start + index]
83+
self.string.unsafe_charcode_at(self.start + index) |> Char::from_int
8484
}
8585

8686
///|
@@ -106,7 +106,7 @@ fn StringSlice::to_string(self : StringSlice) -> String {
106106
fn prefix_eq_ignore_case(self : StringSlice, s2 : String) -> Bool {
107107
for i = 0; i < s2.length() && i < self.length(); i = i + 1 {
108108
let c1 = self[i]
109-
let c2 = s2[i]
109+
let c2 = s2.unsafe_charcode_at(i) |> Char::from_int
110110
if lower(c1) != lower(c2) {
111111
return false
112112
}

0 commit comments

Comments
 (0)