Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions bigint/bigint_nonjs.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,7 @@ pub fn BigInt::from_string(input : String) -> BigInt {
if x < 0 || x > 9 {
abort("invalid character")
}
let mut carry = x.reinterpret_as_uint().to_uint64()
let mut carry = x.to_uint64()
for j in 0..<b_len {
carry += b[j].to_uint64() * 10
b[j] = (carry % radix).to_uint()
Expand Down Expand Up @@ -1043,13 +1043,13 @@ pub fn BigInt::from_string(input : String) -> BigInt {
/// ```
pub fn BigInt::from_hex(input : String) -> BigInt {
// WARN: this implementation assumes that `radix_bit_len` is a multiple of 4.
fn char_from_hex(x : Int) -> UInt {
fn char_from_hex(x : UInt16) -> UInt {
(match x {
'0'..='9' => x - '0'
'A'..='F' => x + (10 - 'A')
'a'..='f' => x + (10 - 'a')
_ => abort("invalid character")
}).reinterpret_as_uint()
}).to_uint()
}

let len = input.length()
Expand Down
4 changes: 2 additions & 2 deletions buffer/buffer.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ pub fn Buffer::write_string_utf16le(buf : Self, string : @string.View) -> Unit {
let len = string.length()
buf.grow_if_necessary(buf.len + len * 2)
for i = 0, j = buf.len; i < len; i = i + 1, j = j + 2 {
let c = string.unsafe_charcode_at(i).reinterpret_as_uint()
let c = string.unsafe_charcode_at(i).to_uint()
buf.data[j] = (c & 0xff).to_byte()
buf.data[j + 1] = (c >> 8).to_byte()
}
Expand All @@ -701,7 +701,7 @@ pub fn Buffer::write_string_utf16be(buf : Self, string : @string.View) -> Unit {
let len = string.length()
buf.grow_if_necessary(buf.len + len * 2)
for i = 0, j = buf.len; i < len; i = i + 1, j = j + 2 {
let c = string.unsafe_charcode_at(i).reinterpret_as_uint()
let c = string.unsafe_charcode_at(i).to_uint()
buf.data[j + 1] = (c & 0xff).to_byte()
buf.data[j] = (c >> 8).to_byte()
}
Expand Down
2 changes: 1 addition & 1 deletion builtin/bytes.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ pub fn FixedArray::blit_from_string(
guard length >= 0 && s1 >= 0 && e1 < len1 && s2 >= 0 && e2 < len2
let end_str_offset = str_offset + length
for i = str_offset, j = bytes_offset; i < end_str_offset; i = i + 1, j = j + 2 {
let c = str.unsafe_charcode_at(i).reinterpret_as_uint()
let c = str.unsafe_charcode_at(i)
self[j] = (c & 0xff).to_byte()
self[j + 1] = (c >> 8).to_byte()
}
Expand Down
2 changes: 1 addition & 1 deletion builtin/deprecated.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ pub fn String::codepoint_length(
///|
#deprecated("Use `s[i]` instead")
pub fn String::charcode_at(self : String, index : Int) -> Int {
self[index]
self[index].to_int()
}

///|
Expand Down
2 changes: 1 addition & 1 deletion builtin/hasher.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ pub fn Hasher::combine_bytes(self : Hasher, value : Bytes) -> Unit {
/// ```
pub fn Hasher::combine_string(self : Hasher, value : String) -> Unit {
for i in 0..<value.length() {
self.combine_uint(value.unsafe_charcode_at(i).reinterpret_as_uint())
self.combine_uint(value.unsafe_charcode_at(i).to_uint())
}
}

Expand Down
7 changes: 5 additions & 2 deletions builtin/intrinsics.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -1320,6 +1320,9 @@ pub fn Int::to_byte(self : Int) -> Byte = "%i32_to_byte"
///|
pub fn Int::unsafe_to_char(self : Int) -> Char = "%char_from_int"

///|
pub fn UInt16::unsafe_to_char(self : UInt16) -> Char = "%char_from_int"

///|
pub fn Int::to_char(self : Int) -> Char? {
if self is (0..=0xD7FF) || self is (0xE000..=0x10FFFF) {
Expand Down Expand Up @@ -1536,7 +1539,7 @@ pub fn String::charcode_length(self : String) -> Int = "%string_length"
/// * `index` : The position in the string from which to retrieve the code unit.
///
/// This method has O(1) complexity.
pub fn String::op_get(self : String, idx : Int) -> Int = "%string_get"
pub fn String::op_get(self : String, idx : Int) -> UInt16 = "%string_get"

///|
/// Returns the UTF-16 code unit at a given position in the string without
Expand All @@ -1563,7 +1566,7 @@ pub fn String::op_get(self : String, idx : Int) -> Int = "%string_get"
/// ```
/// TODO: rename to `unsafe_get`
#internal(unsafe, "Panic if index is out of bounds.")
pub fn String::unsafe_charcode_at(self : String, idx : Int) -> Int = "%string.unsafe_get"
pub fn String::unsafe_charcode_at(self : String, idx : Int) -> UInt16 = "%string.unsafe_get"

///|
/// Concatenates two strings, creating a new string that contains all characters
Expand Down
22 changes: 20 additions & 2 deletions builtin/pkg.generated.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -460,10 +460,16 @@ fn UInt::trunc_double(Double) -> UInt
#deprecated
fn UInt::upto(UInt, UInt, inclusive? : Bool) -> Iter[UInt]

fn UInt16::is_leading_surrogate(UInt16) -> Bool
fn UInt16::is_surrogate(UInt16) -> Bool
fn UInt16::is_trailing_surrogate(UInt16) -> Bool
fn UInt16::to_byte(UInt16) -> Byte
fn UInt16::to_int(UInt16) -> Int
fn UInt16::to_int64(UInt16) -> Int64
fn UInt16::to_string(UInt16, radix? : Int) -> String
fn UInt16::to_uint(UInt16) -> UInt
fn UInt16::to_uint64(UInt16) -> UInt64
fn UInt16::unsafe_to_char(UInt16) -> Char

fn UInt64::clz(UInt64) -> Int
fn UInt64::ctz(UInt64) -> Int
Expand Down Expand Up @@ -531,13 +537,13 @@ fn String::codepoint_length(String, start_offset? : Int, end_offset? : Int) -> I
fn String::escape(String) -> String
fn String::length(String) -> Int
fn String::make(Int, Char) -> String
fn String::op_get(String, Int) -> Int
fn String::op_get(String, Int) -> UInt16
#deprecated
fn String::substring(String, start? : Int, end? : Int) -> String
fn String::to_string(String) -> String
#deprecated
fn String::unsafe_char_at(String, Int) -> Char
fn String::unsafe_charcode_at(String, Int) -> Int
fn String::unsafe_charcode_at(String, Int) -> UInt16
fn String::unsafe_substring(String, start~ : Int, end~ : Int) -> String

fn[X : Show] Option::to_string(X?) -> String
Expand Down Expand Up @@ -592,6 +598,7 @@ impl Add for Byte
impl Add for Int
impl Add for Int64
impl Add for UInt
impl Add for UInt16
impl Add for UInt64
impl Add for Float
impl Add for Double
Expand All @@ -602,20 +609,23 @@ pub(open) trait BitAnd {
}
impl BitAnd for Byte
impl BitAnd for Int64
impl BitAnd for UInt16
impl BitAnd for UInt64

pub(open) trait BitOr {
lor(Self, Self) -> Self
}
impl BitOr for Byte
impl BitOr for Int64
impl BitOr for UInt16
impl BitOr for UInt64

pub(open) trait BitXOr {
lxor(Self, Self) -> Self
}
impl BitXOr for Byte
impl BitXOr for Int64
impl BitXOr for UInt16
impl BitXOr for UInt64

pub(open) trait Compare : Eq {
Expand All @@ -627,6 +637,7 @@ impl Compare for Char
impl Compare for Int
impl Compare for Int64
impl Compare for UInt
impl Compare for UInt16
impl Compare for UInt64
impl Compare for Float
impl Compare for Double
Expand Down Expand Up @@ -667,6 +678,7 @@ impl Div for Byte
impl Div for Int
impl Div for Int64
impl Div for UInt
impl Div for UInt16
impl Div for UInt64
impl Div for Float
impl Div for Double
Expand All @@ -682,6 +694,7 @@ impl Eq for Char
impl Eq for Int
impl Eq for Int64
impl Eq for UInt
impl Eq for UInt16
impl Eq for UInt64
impl Eq for Float
impl Eq for Double
Expand Down Expand Up @@ -737,6 +750,7 @@ impl Mod for Byte
impl Mod for Int
impl Mod for Int64
impl Mod for UInt
impl Mod for UInt16
impl Mod for UInt64

pub(open) trait Mul {
Expand All @@ -747,6 +761,7 @@ impl Mul for Byte
impl Mul for Int
impl Mul for Int64
impl Mul for UInt
impl Mul for UInt16
impl Mul for UInt64
impl Mul for Float
impl Mul for Double
Expand All @@ -768,6 +783,7 @@ impl Shl for Byte
impl Shl for Int
impl Shl for Int64
impl Shl for UInt
impl Shl for UInt16
impl Shl for UInt64

pub(open) trait Show {
Expand Down Expand Up @@ -813,6 +829,7 @@ impl Shr for Byte
impl Shr for Int
impl Shr for Int64
impl Shr for UInt
impl Shr for UInt16
impl Shr for UInt64

pub(open) trait Sub {
Expand All @@ -823,6 +840,7 @@ impl Sub for Byte
impl Sub for Int
impl Sub for Int64
impl Sub for UInt
impl Sub for UInt16
impl Sub for UInt64
impl Sub for Float
impl Sub for Double
Expand Down
10 changes: 6 additions & 4 deletions builtin/show.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub impl Show for UInt16 with output(self, logger) {
}

///|
fn to_hex_digit(i : Int) -> Char {
fn to_hex_digit(i : UInt16) -> Char {
if i < 10 {
(i + '0').unsafe_to_char()
} else {
Expand All @@ -73,12 +73,14 @@ fn to_hex_digit(i : Int) -> Char {
///|
test "to_hex_digit" {
for i in 0..<10 {
guard to_hex_digit(i) == ('0'.to_int() + i).unsafe_to_char() else {
let i = i.to_uint16()
guard to_hex_digit(i) == (i + '0').unsafe_to_char() else {
fail("to_hex_digit(\{i}) does not match")
}
}
for i in 10..<16 {
guard to_hex_digit(i) == ('a'.to_int() + (i - 10)).unsafe_to_char() else {
let i = i.to_uint16()
guard to_hex_digit(i) == (i + 'a' - 10).unsafe_to_char() else {
fail("to_hex_digit(\{i}) does not match")
}
}
Expand All @@ -88,7 +90,7 @@ test "to_hex_digit" {
pub impl Show for Bytes with output(self, logger) {
logger.write_string("b\"")
for b in self {
let byte = b.to_int()
let byte = b.to_uint16()
logger
..write_string("\\x")
..write_char(to_hex_digit(byte / 16))
Expand Down
4 changes: 3 additions & 1 deletion builtin/string.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ pub fn String::make(length : Int, value : Char) -> String {
}

///|
fn code_point_of_surrogate_pair(leading : Int, trailing : Int) -> Char {
fn code_point_of_surrogate_pair(leading : UInt16, trailing : UInt16) -> Char {
let leading = leading.to_int()
let trailing = trailing.to_int()
((leading - 0xD800) * 0x400 + trailing - 0xDC00 + 0x10000).unsafe_to_char()
}

Expand Down
Loading
Loading