-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rename Char::from_int to Char::from_int_unsafe #338
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -184,7 +184,8 @@ pub fn Double::convert_i64_u(val : Int64) -> Double = "%i64_to_f64_u" | |
|
||
pub fn Char::to_int(self : Char) -> Int = "%char_to_int" | ||
|
||
pub fn Char::from_int(val : Int) -> Char = "%char_from_int" | ||
// Converts a Int to a Char, ignoring validity. | ||
pub fn Char::from_int_unsafe(val : Int) -> Char = "%char_from_int" | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this unsafe API does not be exposed, it can be pulled in on demand, so that it is only local to its used package There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In my current implementation, it is used by |
||
pub fn Char::op_equal(self : Char, other : Char) -> Bool = "%char_eq" | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,9 +28,9 @@ test "to_string" { | |
pub fn debug_write(self : Char, buf : Buffer) -> Unit { | ||
fn to_hex_digit(i : Int) -> Char { | ||
if i < 10 { | ||
Char::from_int('0'.to_int() + i) | ||
Char::from_int('0'.to_int() + i).unwrap() | ||
} else { | ||
Char::from_int('a'.to_int() + (i - 10)) | ||
Char::from_int('a'.to_int() + (i - 10)).unwrap() | ||
} | ||
} | ||
|
||
|
@@ -61,7 +61,7 @@ pub fn debug_write(self : Char, buf : Buffer) -> Unit { | |
|
||
test "debug_write" { | ||
fn repr(chr) { | ||
let buf = Buffer::make(0) | ||
let buf = Buffer::make(4) | ||
debug_write(chr, buf) | ||
buf.to_string() | ||
} | ||
|
@@ -74,9 +74,49 @@ test "debug_write" { | |
@assertion.assert_eq(repr('\r'), "'\\r'")? | ||
@assertion.assert_eq(repr('\b'), "'\\b'")? | ||
@assertion.assert_eq(repr('\t'), "'\\t'")? | ||
@assertion.assert_eq(repr(Char::from_int(0)), "'\\x00'")? | ||
@assertion.assert_eq(repr('\x0f'), "'\\x0f'")? | ||
@assertion.assert_eq(repr(Char::from_int_unsafe(0)), "'\\x00'")? | ||
} | ||
|
||
pub fn hash(self : Char) -> Int { | ||
self.to_int() | ||
} | ||
|
||
// The lowest valid code point a char can have, '\u{0}'. | ||
pub let min : Char = '\u{0}' | ||
|
||
lijunchen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// The highest valid code point a char can have, '\u{10FFFF}'. | ||
pub let max : Char = '\u{10FFFF}' | ||
|
||
let lo_bound : Char = '\u{D7FF}' | ||
|
||
let hi_bound : Char = '\u{E000}' | ||
|
||
pub fn is_valid(self : Char) -> Bool { | ||
min <= self && self <= lo_bound || hi_bound <= self && self <= max | ||
} | ||
|
||
// Converts a Int to a Char. | ||
pub fn Char::from_int(i : Int) -> Option[Char] { | ||
let c = Char::from_int_unsafe(i) | ||
if c.is_valid() { | ||
Some(c) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
test "invalid" { | ||
inspect(Char::from_int_unsafe(0xD800).is_valid(), content="false")? | ||
inspect(Char::from_int(0xD800), content="None")? | ||
inspect(Char::from_int(-1), content="None")? | ||
} | ||
|
||
test "Unicode" { | ||
inspect(Char::from_int_unsafe(97), content="a")? | ||
inspect('中'.to_int(), content="20013")? | ||
inspect(Char::from_int(20013), content="Some(中)")? | ||
inspect(Char::from_int_unsafe(20013), content="中")? | ||
inspect('🤣'.to_int(), content="129315")? | ||
inspect(Char::from_int(129315), content="Some(🤣)")? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Guest0x0 |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,9 +102,9 @@ fn escape_to(s : String, buf : Buffer) -> Unit { | |
|
||
fn to_hex_digit(i : Int) -> Char { | ||
if i < 10 { | ||
Char::from_int('0'.to_int() + i) | ||
Char::from_int_unsafe('0'.to_int() + i) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cannot use Option::unwrap here, because of cyclic dependencies |
||
} else { | ||
Char::from_int('a'.to_int() + (i - 10)) | ||
Char::from_int_unsafe('a'.to_int() + (i - 10)) | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ fn read_char(ctx : ParseContext) -> Option[Char] { | |
if c2 >= 0xDC00 && c2 <= 0xDFFF { | ||
ctx.offset += 1 | ||
let c3 = c1.lsl(10) + c2 - 0x35fdc00 | ||
return Some(Char::from_int(c3)) | ||
return Some(Char::from_int_unsafe(c3)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe performance critical in |
||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this in perf critical place? if not, we would prefer safe API
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, but we cannot use Option::unwrap in
builtin