Skip to content
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

Added so char can be converted to int128 #12958

Merged
merged 6 commits into from
Jan 17, 2023
Merged
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: 6 additions & 0 deletions spec/std/char_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -237,31 +237,37 @@ describe "Char" do
'1'.to_i16.should eq(1i16)
'1'.to_i32.should eq(1i32)
'1'.to_i64.should eq(1i64)
'1'.to_i128.should eq(1i128)

expect_raises(ArgumentError) { 'a'.to_i8 }
expect_raises(ArgumentError) { 'a'.to_i16 }
expect_raises(ArgumentError) { 'a'.to_i32 }
expect_raises(ArgumentError) { 'a'.to_i64 }
expect_raises(ArgumentError) { 'a'.to_i128 }

'a'.to_i8?.should be_nil
'a'.to_i16?.should be_nil
'a'.to_i32?.should be_nil
'a'.to_i64?.should be_nil
'a'.to_i128?.should be_nil

'1'.to_u8.should eq(1u8)
'1'.to_u16.should eq(1u16)
'1'.to_u32.should eq(1u32)
'1'.to_u64.should eq(1u64)
'1'.to_u128.should eq(1u128)

expect_raises(ArgumentError) { 'a'.to_u8 }
expect_raises(ArgumentError) { 'a'.to_u16 }
expect_raises(ArgumentError) { 'a'.to_u32 }
expect_raises(ArgumentError) { 'a'.to_u64 }
expect_raises(ArgumentError) { 'a'.to_u128 }

'a'.to_u8?.should be_nil
'a'.to_u16?.should be_nil
'a'.to_u32?.should be_nil
'a'.to_u64?.should be_nil
'a'.to_u128?.should be_nil
end

it "does to_i with 16 base" do
Expand Down
2 changes: 1 addition & 1 deletion src/char.cr
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ struct Char
to_i?(base)
end

{% for type in %w(i8 i16 i64 u8 u16 u32 u64) %}
{% for type in %w(i8 i16 i64 i128 u8 u16 u32 u64 u128) %}
# See also: `to_i`.
def to_{{type.id}}(base : Int = 10)
to_i(base).to_{{type.id}}
Expand Down