Skip to content

Commit 3e9e0bd

Browse files
committed
BREAKING CHANGE: String::at and StringView::at now return UInt16
- String::at and StringView::at now return UInt16 instead of Int - These methods are now the primary interface for UTF-16 code unit access - code_unit_at is now an alias for at (previously it was the other way around) - The old Int-returning behavior is preserved in String::charcode_at (deprecated) This change aligns the API with the actual UTF-16 representation of strings and makes the type system more accurate.
1 parent 3786d58 commit 3e9e0bd

File tree

4 files changed

+18
-25
lines changed

4 files changed

+18
-25
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@ changelog should follow [Keep a Changelog](https://keepachangelog.com/en/1.1.0/)
1212

1313
#### Changed
1414

15+
- **BREAKING**: `String::at` and `StringView::at` now return `UInt16` instead of `Int` and are the primary methods for accessing UTF-16 code units
16+
- **BREAKING**: `String::code_unit_at` and `StringView::code_unit_at` are now aliases for `at` instead of being separate methods
17+
1518
#### Deprecated
1619

20+
- `String::charcode_at` (previously named `at`) is now deprecated, use `String::at` which returns `UInt16`
21+
1722
#### Removed
1823

1924
#### Fixed

builtin/intrinsics.mbt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,16 +1558,16 @@ pub fn String::length(self : String) -> Int = "%string_length"
15581558
/// * `index` : The position in the string from which to retrieve the code unit.
15591559
///
15601560
/// This method has O(1) complexity.
1561-
#alias(charcode_at)
1562-
#alias("_[_]")
15631561
#deprecated("Use `code_unit_at` instead which returns UInt16")
1564-
pub fn String::at(self : String, idx : Int) -> Int = "%string_get"
1562+
pub fn String::charcode_at(self : String, idx : Int) -> Int = "%string_get"
15651563

15661564
///|
15671565
/// Returns the UTF-16 code unit at the given index.
15681566
///
15691567
/// This method has O(1) complexity.
1570-
pub fn String::code_unit_at(self : String, idx : Int) -> UInt16 = "%string_get"
1568+
#alias("_[_]")
1569+
#alias(code_unit_at)
1570+
pub fn String::at(self : String, idx : Int) -> UInt16 = "%string_get"
15711571

15721572
///|
15731573
/// Returns the UTF-16 code unit at a given position in the string without

builtin/pkg.generated.mbti

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -683,14 +683,14 @@ pub fn Double::until(Double, Double, step? : Double, inclusive? : Bool) -> Iter[
683683
pub fn Double::upto(Double, Double, inclusive? : Bool) -> Iter[Double]
684684

685685
#alias("_[_]")
686-
#alias(charcode_at)
687-
#deprecated
688-
pub fn String::at(String, Int) -> Int
686+
#alias(code_unit_at)
687+
pub fn String::at(String, Int) -> UInt16
689688
#alias(codepoint_length, deprecated)
690689
pub fn String::char_length(String, start_offset? : Int, end_offset? : Int) -> Int
691690
pub fn String::char_length_eq(String, Int, start_offset? : Int, end_offset? : Int) -> Bool
692691
pub fn String::char_length_ge(String, Int, start_offset? : Int, end_offset? : Int) -> Bool
693-
pub fn String::code_unit_at(String, Int) -> UInt16
692+
#deprecated
693+
pub fn String::charcode_at(String, Int) -> Int
694694
#deprecated
695695
pub fn String::codepoint_at(String, Int) -> Char
696696
pub fn String::contains(String, StringView) -> Bool
@@ -955,12 +955,11 @@ pub fn BytesView::to_bytes(Self) -> Bytes
955955
pub fn BytesView::to_fixedarray(Self) -> FixedArray[Byte]
956956

957957
#alias("_[_]")
958-
#deprecated
959-
pub fn StringView::at(Self, Int) -> Int
958+
#alias(code_unit_at)
959+
pub fn StringView::at(Self, Int) -> UInt16
960960
pub fn StringView::char_length(Self) -> Int
961961
pub fn StringView::char_length_eq(Self, Int) -> Bool
962962
pub fn StringView::char_length_ge(Self, Int) -> Bool
963-
pub fn StringView::code_unit_at(Self, Int) -> UInt16
964963
pub fn StringView::contains(Self, Self) -> Bool
965964
pub fn StringView::contains_any(Self, chars~ : Self) -> Bool
966965
pub fn StringView::contains_char(Self, Char) -> Bool

builtin/stringview.mbt

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,13 @@ fn StringView::end(self : StringView) -> Int = "%stringview.end"
2727
///|
2828
fn StringView::make_view(str : String, start : Int, end : Int) -> StringView = "%stringview.make"
2929

30-
///|
31-
/// Returns the charcode(UTF-16 code unit) at the given index.
32-
///
33-
/// This method has O(1) complexity.
34-
#alias("_[_]")
35-
#deprecated("Use `code_unit_at` instead which returns UInt16")
36-
pub fn StringView::at(self : StringView, index : Int) -> Int {
37-
guard index >= 0 && index < self.length() else {
38-
abort("Index out of bounds")
39-
}
40-
self.str().unsafe_charcode_at(self.start() + index)
41-
}
42-
4330
///|
4431
/// Returns the UTF-16 code unit at the given index.
4532
///
4633
/// This method has O(1) complexity.
47-
pub fn StringView::code_unit_at(self : StringView, index : Int) -> UInt16 {
34+
#alias("_[_]")
35+
#alias(code_unit_at)
36+
pub fn StringView::at(self : StringView, index : Int) -> UInt16 {
4837
guard index >= 0 && index < self.length() else {
4938
abort("Index out of bounds")
5039
}

0 commit comments

Comments
 (0)