Skip to content

Commit

Permalink
Use Slice#unsafe_slice_of and #to_unsafe_bytes in the standard li…
Browse files Browse the repository at this point in the history
…brary and compiler (#12280)
  • Loading branch information
HertzDevil authored Jul 22, 2022
1 parent c4799c6 commit b5317ac
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion spec/std/io/memory_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe IO::Memory do
io1.write_string "abc😂".to_slice
io1.to_s io2
byte_slice = io2.to_slice
utf16_slice = Slice.new(byte_slice.to_unsafe.unsafe_as(Pointer(UInt16)), byte_slice.size // sizeof(UInt16))
utf16_slice = byte_slice.unsafe_slice_of(UInt16)

String.from_utf16(utf16_slice).should eq "abc😂"
byte_slice.should eq Bytes[0x61, 0, 0x62, 0, 0x63, 0, 0x3D, 0xD8, 0x02, 0xDE]
Expand Down
2 changes: 1 addition & 1 deletion spec/support/string.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def string_build_via_utf16(& : IO -> _)
io.set_encoding(IO::ByteFormat::SystemEndian == IO::ByteFormat::LittleEndian ? "UTF-16LE" : "UTF-16BE")
yield io
byte_slice = io.to_slice
utf16_slice = Slice.new(byte_slice.to_unsafe.unsafe_as(Pointer(UInt16)), byte_slice.size // sizeof(UInt16))
utf16_slice = byte_slice.unsafe_slice_of(UInt16)
String.from_utf16(utf16_slice)
{% end %}
end
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/compiler.cr
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ module Crystal
# TODO: Use a proper way to write encoded text to a file when that's supported.
# The first character is the BOM; it will be converted in the same endianness as the rest.
args_16 = "\ufeff#{args}".to_utf16
args_bytes = args_16.to_unsafe.as(UInt8*).to_slice(args_16.bytesize)
args_bytes = args_16.to_unsafe_bytes

args_filename = "#{output_dir}/linker_args.txt"
File.write(args_filename, args_bytes)
Expand Down
2 changes: 1 addition & 1 deletion src/crystal/hasher.cr
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ struct Crystal::Hasher
private HASH_INF_MINUS = (-314159_i64).unsafe_as(UInt64)

@@seed = uninitialized UInt64[2]
Crystal::System::Random.random_bytes(Slice.new(pointerof(@@seed).as(UInt8*), sizeof(typeof(@@seed))))
Crystal::System::Random.random_bytes(@@seed.to_slice.to_unsafe_bytes)

def initialize(@a : UInt64 = @@seed[0], @b : UInt64 = @@seed[1])
end
Expand Down
2 changes: 1 addition & 1 deletion src/crystal/system/win32/windows_registry.cr
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ module Crystal::System::WindowsRegistry
# Reads a raw value into a buffer and creates a string from it.
def self.get_string(handle : LibC::HKEY, name : Slice(UInt16))
Crystal::System.retry_wstr_buffer do |buffer, small_buf|
raw = get_raw(handle, name, Bytes.new(buffer.to_unsafe.as(UInt8*), buffer.bytesize)) || return
raw = get_raw(handle, name, buffer.to_unsafe_bytes) || return
_, length = raw

if 0 <= length <= buffer.size
Expand Down

0 comments on commit b5317ac

Please sign in to comment.