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

Allow mixing of integers and pointers in Crystal::System.print_error #14149

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions spec/std/crystal/system_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,9 @@ describe "Crystal::System" do
print_error_to_s("%lu,%lu,%llu,%llu", *values).should eq(values.join(','))
print_error_to_s("%lx,%lx,%llx,%llx", *values).should eq(values.join(',', &.to_s(16)))
end

it "can mix pointers and ints" do
print_error_to_s("%d,%p,%s", Pointer(UInt8).new(123), 0x456, "abc".to_unsafe.address).should eq("123,0x456,abc")
end
end
end
28 changes: 19 additions & 9 deletions src/crystal/system/print_error.cr
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,26 @@ module Crystal::System

case fmt_ptr.value
when 's'
read_arg(String | Pointer(UInt8)) do |arg|
read_arg(String, Int::Primitive, Pointer) do |arg|
yield to_string_slice(arg)
end
when 'd'
read_arg(Int::Primitive) do |arg|
read_arg(Int::Primitive, Pointer) do |arg|
to_int_slice(arg, 10, true, width) { |bytes| yield bytes }
end
when 'u'
read_arg(Int::Primitive) do |arg|
read_arg(Int::Primitive, Pointer) do |arg|
to_int_slice(arg, 10, false, width) { |bytes| yield bytes }
end
when 'x'
read_arg(Int::Primitive) do |arg|
read_arg(Int::Primitive, Pointer) do |arg|
to_int_slice(arg, 16, false, width) { |bytes| yield bytes }
end
when 'p'
read_arg(Pointer(Void)) do |arg|
read_arg(Int::Primitive, Pointer) do |arg|
# NOTE: MSVC uses `%X` rather than `0x%x`, we follow the latter on all platforms
yield "0x".to_slice
to_int_slice(arg.address, 16, false, 2) { |bytes| yield bytes }
to_int_slice(arg, 16, false, 2) { |bytes| yield bytes }
end
else
yield Slice.new(next_percent, fmt_ptr + 1 - next_percent)
Expand All @@ -85,17 +85,23 @@ module Crystal::System
end
end

private macro read_arg(type, &block)
{{ block.args[0] }} = args[arg_index].as?({{ type }})
if !{{ block.args[0] }}.nil?
private macro read_arg(*types, &block)
case {{ block.args[0] }} = args[arg_index]
{% for t in types %}
when {{ t }}
{{ block.body }}
{% end %}
else
yield "(???)".to_slice
end
arg_index += 1
end

private def self.to_string_slice(str)
if str.is_a?(Int::Primitive)
str = Pointer(UInt8).new(str)
end

if str.is_a?(UInt8*)
if str.null?
"(null)".to_slice
Expand All @@ -109,6 +115,10 @@ module Crystal::System

# simplified version of `Int#internal_to_s`
private def self.to_int_slice(num, base, signed, width, &)
if num.is_a?(Pointer)
num = num.address
end

if num == 0
yield "0".to_slice
return
Expand Down