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

Add StringLiteral#to_utf16 #14676

Merged
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
5 changes: 5 additions & 0 deletions spec/compiler/macro/macro_methods_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,11 @@ module Crystal
assert_macro %({{"hello world".titleize}}), %("Hello World")
end

it "executes to_utf16" do
assert_macro %({{"hello".to_utf16}}), "(::Slice(::UInt16).literal(104_u16, 101_u16, 108_u16, 108_u16, 111_u16, 0_u16))[0, 5]"
assert_macro %({{"TEST 😐🐙 ±∀ の".to_utf16}}), "(::Slice(::UInt16).literal(84_u16, 69_u16, 83_u16, 84_u16, 32_u16, 55357_u16, 56848_u16, 55357_u16, 56345_u16, 32_u16, 177_u16, 8704_u16, 32_u16, 12398_u16, 0_u16))[0, 14]"
end

it "executes to_i" do
assert_macro %({{"1234".to_i}}), %(1234)
end
Expand Down
15 changes: 15 additions & 0 deletions src/compiler/crystal/macros/methods.cr
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,21 @@ module Crystal
else
raise "StringLiteral#to_i: #{@value} is not an integer"
end
when "to_utf16"
interpret_check_args do
slice = @value.to_utf16

# include the trailing zero that isn't counted in the slice but was
# generated by String#to_utf16 so the literal can be passed to C
# functions that expect a null terminated UInt16*
args = Slice(UInt16).new(slice.to_unsafe, slice.size + 1).to_a do |codepoint|
NumberLiteral.new(codepoint).as(ASTNode)
end
literal_node = Call.new(Generic.new(Path.global("Slice"), [Path.global("UInt16")] of ASTNode), "literal", args)

# but keep the trailing zero hidden in the exposed slice
Call.new(literal_node, "[]", [NumberLiteral.new("0", :i32), NumberLiteral.new(slice.size)] of ASTNode)
end
when "tr"
interpret_check_args do |first, second|
raise "first argument to StringLiteral#tr must be a string, not #{first.class_desc}" unless first.is_a?(StringLiteral)
Expand Down
Loading