-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Improve error messages for invalid values of Char #2811
Conversation
|
I think I'd prefer |
The check will just be checking that the integer is in I did a couple of benchmarks, in non-release mode it's twice as slow, but in release mode there's no difference. I guess then it's worth improving this. |
The benchmark I did is: require "benchmark"
struct Int
def chr!
unless 0 <= self <= Int32::MAX
raise ArgumentError.new("#{self} out of char range")
end
chr
end
end
a = 0
Benchmark.ips do |x|
x.report("chr") do
"foo bar".each_byte do |byte|
a += byte.chr.ord
end
end
x.report("chr!") do
"foo bar".each_byte do |byte|
a += byte.chr!.ord
end
end
end
puts a Of course we'd rename |
Hmm... actually, the name is still hardcoded in the compiler:
|
You're by now more of a UTF-8 expert than me by now, but it looks like |
Mmm... on second though, never mind. We can add an "unsafe_chr" there too, and in codegen too. The Right, Do you want to send a PR for this? We'd need to check Crystal::VERSION to define Otherwise I can do it too :-) |
Please do, you have a better grasp of which |
Oh and perhaps we should extract |
The other question is if
.chr
shouldn't raise already?