Skip to content
Draft
Show file tree
Hide file tree
Changes from 4 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
9 changes: 4 additions & 5 deletions spec/std/base64_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,10 @@ describe "Base64" do
assert_prints base64_strict_encode(slice), ""
end

it "encodes static array" do
array = uninitialized StaticArray(UInt8, 5)
(0...5).each { |i| array[i] = 1_u8 }
assert_prints base64_encode(array), "AQEBAQE=\n"
assert_prints base64_strict_encode(array), "AQEBAQE="
it "encodes calling `#to_slice`" do

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this test was check that base64 can be called with StaticArray, why remove?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I interpret the intention of this spec to test that it works with a value that responds to #to_slice. StaticArray was just chosen as an example with a useful #to_slice method. This change respect that intention by replacing StaticArray with String.

I suppose we could add another spec to ensure StaticArray specifically keeps working as well. However this is only going to work as long as deprecated StaticArray#to_slice stays available. I don't think it makes sense to add an explicit overload StaticArray because passing the entire instance as argument is quite heavy and it would be easier to pass a reference, i.e. call to_unsafe_slice at the call site.

data = "\x01\x01\x01\x01\x01"
assert_prints base64_encode(data), "AQEBAQE=\n"
assert_prints base64_strict_encode(data), "AQEBAQE="
end

describe "base" do
Expand Down
3 changes: 1 addition & 2 deletions spec/std/crystal/digest/md5_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ describe Crystal::Digest::MD5 do
end

it "calculates hash of #to_slice" do
buffer = StaticArray(UInt8, 1).new(1_u8)
Crystal::Digest::MD5.hexdigest(buffer).should eq("55a54008ad1ba589aa210d2629c1df41")
Crystal::Digest::MD5.hexdigest("\x01").should eq("55a54008ad1ba589aa210d2629c1df41")
end

it "can take a block" do
Expand Down
3 changes: 1 addition & 2 deletions spec/std/digest/adler32_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ describe Digest::Adler32 do
end

it "calculates hash of #to_slice" do
buffer = StaticArray(UInt8, 1).new(1_u8)
Digest::Adler32.hexdigest(buffer).should eq("00020002")
Digest::Adler32.hexdigest("\x01").should eq("00020002")
end

it "can take a block" do
Expand Down
3 changes: 1 addition & 2 deletions spec/std/digest/crc32_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ describe Digest::CRC32 do
end

it "calculates hash of #to_slice" do
buffer = StaticArray(UInt8, 1).new(1_u8)
Digest::CRC32.hexdigest(buffer).should eq("a505df1b")
Digest::CRC32.hexdigest("\x01").should eq("a505df1b")
end

it "can take a block" do
Expand Down
3 changes: 1 addition & 2 deletions spec/std/digest/md5_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ describe Digest::MD5 do
end

it "calculates hash of #to_slice" do
buffer = StaticArray(UInt8, 1).new(1_u8)
Digest::MD5.hexdigest(buffer).should eq("55a54008ad1ba589aa210d2629c1df41")
Digest::MD5.hexdigest("\x01").should eq("55a54008ad1ba589aa210d2629c1df41")
end

it "can take a block" do
Expand Down
4 changes: 2 additions & 2 deletions spec/std/http/chunked_content_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ describe HTTP::ChunkedContent do
mem = IO::Memory.new("4\r\n123\n\r\n0\r\n\r\n")
content = HTTP::ChunkedContent.new(mem)
bytes = uninitialized UInt8[4]
bytes_read = content.read(bytes.to_slice)
bytes_read = content.read(bytes.to_unsafe_slice)
bytes_read.should eq(4)
String.new(bytes.to_slice).should eq("123\n")
String.new(bytes.to_unsafe_slice).should eq("123\n")
mem.pos.should eq(7) # only this chunk was read
end

Expand Down
2 changes: 1 addition & 1 deletion spec/std/http/web_socket_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ describe HTTP::WebSocket do

io = IO::Memory.new
ws = HTTP::WebSocket::Protocol.new(io, masked: true)
ws.send(sent_bytes.to_slice)
ws.send(sent_bytes.to_unsafe_slice)
bytes = io.to_slice
(bytes[0] & 0x0f).should eq(0x02)
end
Expand Down
8 changes: 4 additions & 4 deletions spec/std/io/hexdump_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ describe IO::Hexdump do
w.write(slice)

buf = uninitialized UInt8[101]
r.read_fully(buf.to_slice).should eq(101)
buf.to_slice.should eq(slice)
r.read_fully(buf.to_unsafe_slice).should eq(101)
buf.to_unsafe_slice.should eq(slice)

io.to_s.should eq("#{ascii_table}\n")
end
Expand All @@ -49,8 +49,8 @@ describe IO::Hexdump do
w.write(slice)

buf = uninitialized UInt8[96]
r.read_fully(buf.to_slice).should eq(96)
buf.to_slice.should eq(slice)
r.read_fully(buf.to_unsafe_slice).should eq(96)
buf.to_unsafe_slice.should eq(slice)

io.to_s.should eq("#{ascii_table}\n")
end
Expand Down
4 changes: 2 additions & 2 deletions spec/std/io/io_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -696,9 +696,9 @@ describe IO do
io.set_encoding("EUC-JP")

buffer = uninitialized UInt8[1024]
bytes_read = io.read_utf8(buffer.to_slice) # => 3
bytes_read = io.read_utf8(buffer.to_unsafe_slice) # => 3
bytes_read.should eq(3)
buffer.to_slice[0, bytes_read].to_a.should eq("好".bytes)
buffer.to_unsafe_slice[0, bytes_read].to_a.should eq("好".bytes)
end

it "raises on incomplete byte sequence" do
Expand Down
8 changes: 4 additions & 4 deletions spec/std/socket/udp_socket_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,16 @@ describe UDPSocket, tags: "network" do

buffer = uninitialized UInt8[256]

bytes_read, client_addr = server.receive(buffer.to_slice)
message = String.new(buffer.to_slice[0, bytes_read])
bytes_read, client_addr = server.receive(buffer.to_unsafe_slice)
message = String.new(buffer.to_unsafe_slice[0, bytes_read])
message.should eq("laus deo semper")

client.send("laus deo semper")

# WSA errors with WSAEMSGSIZE if the buffer is not large enough to receive the message
{% unless flag?(:win32) %}
bytes_read, client_addr = server.receive(buffer.to_slice[0, 4])
message = String.new(buffer.to_slice[0, bytes_read])
bytes_read, client_addr = server.receive(buffer.to_unsafe_slice[0, 4])
message = String.new(buffer.to_unsafe_slice[0, bytes_read])
message.should eq("laus")
{% end %}

Expand Down
2 changes: 1 addition & 1 deletion src/char.cr
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,7 @@ struct Char
chars[i] = byte
i += 1
end
io.write_string chars.to_slice[0, i]
io.write_string chars.to_unsafe_slice[0, i]
end
end

Expand Down
2 changes: 1 addition & 1 deletion src/compress/deflate/reader.cr
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class Compress::Deflate::Reader < IO
# the compressed data (for example, if the compressed stream
# is part of a zip/gzip file).
@stream.next_in = @buf.to_unsafe
@stream.avail_in = @io.read(@buf.to_slice).to_u32
@stream.avail_in = @io.read(@buf.to_unsafe_slice).to_u32
end
end

Expand Down
2 changes: 1 addition & 1 deletion src/compress/deflate/writer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class Compress::Deflate::Writer < IO
@stream.next_out = @buf.to_unsafe
@stream.avail_out = @buf.size.to_u32
LibZ.deflate(pointerof(@stream), flush) # no bad return value
@output.write(@buf.to_slice[0, @buf.size - @stream.avail_out])
@output.write(@buf.to_unsafe_slice[0, @buf.size - @stream.avail_out])
break if @stream.avail_out != 0
end
end
Expand Down
4 changes: 2 additions & 2 deletions src/compress/gzip/header.cr
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ class Compress::Gzip::Header
def initialize(first_byte : UInt8, io : IO)
header = uninitialized UInt8[10]
header[0] = first_byte
io.read_fully(header.to_slice + 1)
io.read_fully(header.to_unsafe_slice + 1)

if header[0] != ID1 || header[1] != ID2 || header[2] != DEFLATE
raise Error.new("Invalid gzip header")
end

flg = Flg.new(header[3])

seconds = IO::ByteFormat::LittleEndian.decode(Int32, header.to_slice[4, 4])
seconds = IO::ByteFormat::LittleEndian.decode(Int32, header.to_unsafe_slice[4, 4])
@modification_time = Time.unix(seconds).to_local

xfl = header[8]
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
HASH_INF_MINUS = (-314159_i64).unsafe_as(UInt64)

@@seed = uninitialized UInt64[2]
Crystal::System::Random.random_bytes(@@seed.to_slice.to_unsafe_bytes)
Crystal::System::Random.random_bytes(@@seed.to_unsafe_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/unix.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module Crystal::System
def self.retry_with_buffer(function_name, max_buffer, &)
initial_buf = uninitialized UInt8[1024]
buf = initial_buf
buf = initial_buf.to_unsafe_slice

while (ret = yield buf.to_slice) != 0
case ret
Expand Down
2 changes: 1 addition & 1 deletion src/crystal/system/unix/getrandom.cr
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ module Crystal::System::Random

if @@getrandom_available
buf = uninitialized UInt8[1]
getrandom(buf.to_slice)
getrandom(buf.to_unsafe_slice)
buf.unsafe_as(UInt8)
elsif urandom = @@urandom
urandom.read_byte.not_nil!
Expand Down
6 changes: 3 additions & 3 deletions src/digest/digest.cr
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ abstract class Digest
raise "Digest#hexfinal(IO) can't handle digest_size over 64 bits"
end
sary = uninitialized StaticArray(UInt8, 128)
tmp = sary.to_slice[0, digest_size * 2]
tmp = sary.to_unsafe_slice[0, digest_size * 2]
hexfinal tmp
io << tmp
end
Expand All @@ -222,8 +222,8 @@ abstract class Digest
# Reads the io's data and updates the digest with it.
def update(io : IO) : self
buffer = uninitialized UInt8[IO::DEFAULT_BUFFER_SIZE]
while (read_bytes = io.read(buffer.to_slice)) > 0
self << buffer.to_slice[0, read_bytes]
while (read_bytes = io.read(buffer.to_unsafe_slice)) > 0
self << buffer.to_unsafe_slice[0, read_bytes]
end
self
end
Expand Down
2 changes: 1 addition & 1 deletion src/float/printer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module Float::Printer
end

# remove trailing zeros
buffer = str.to_slice[ptr - str.to_unsafe..]
buffer = str.to_unsafe_slice[ptr - str.to_unsafe..]
while buffer.size > 1 && buffer.unsafe_fetch(buffer.size - 1) === '0'
buffer = buffer[..-2]
decimal_exponent += 1
Expand Down
4 changes: 2 additions & 2 deletions src/http/common.cr
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,10 @@ module HTTP

def self.serialize_chunked_body(io, body)
buf = uninitialized UInt8[8192]
while (buf_length = body.read(buf.to_slice)) > 0
while (buf_length = body.read(buf.to_unsafe_slice)) > 0
buf_length.to_s(io, 16)
io << "\r\n"
io.write(buf.to_slice[0, buf_length])
io.write(buf.to_unsafe_slice[0, buf_length])
io << "\r\n"
end
io << "0\r\n\r\n"
Expand Down
12 changes: 6 additions & 6 deletions src/http/web_socket/protocol.cr
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class HTTP::WebSocket::Protocol

key = Random::DEFAULT.next_int
mask_array = key.unsafe_as(StaticArray(UInt8, 4))
@io.write mask_array.to_slice
@io.write mask_array.to_unsafe_slice

write_masked_data(data, mask_array)
end
Expand All @@ -162,7 +162,7 @@ class HTTP::WebSocket::Protocol
end

# Write the masked data
@io.write(masked_data.to_slice[0, chunk_size])
@io.write(masked_data.to_unsafe_slice[0, chunk_size])

# Discard the written data
remaining_data = remaining_data[chunk_size..]
Expand All @@ -172,14 +172,14 @@ class HTTP::WebSocket::Protocol
private def read_header
# First byte: FIN (1 bit), RSV1,2,3 (3 bits), Opcode (4 bits)
# Second byte: MASK (1 bit), Payload Length (7 bits)
@io.read_fully(@header.to_slice)
@io.read_fully(@header.to_unsafe_slice)

opcode = read_opcode
@remaining = read_size

# Read mask, if needed
if masked?
@io.read_fully(@mask.to_slice)
@io.read_fully(@mask.to_unsafe_slice)
@mask_offset = 0
end

Expand Down Expand Up @@ -308,7 +308,7 @@ class HTTP::WebSocket::Protocol
end
{% end %}

random_key = Base64.strict_encode(StaticArray(UInt8, 16).new { rand(256).to_u8 })
random_key = Base64.strict_encode(StaticArray(UInt8, 16).new { rand(256).to_u8 }.to_unsafe_slice)

headers["Host"] = "#{host}:#{port}"
headers["Connection"] = "Upgrade"
Expand Down Expand Up @@ -356,7 +356,7 @@ class HTTP::WebSocket::Protocol
{% if flag?(:without_openssl) %}
::Crystal::Digest::SHA1.base64digest(key + GUID)
{% else %}
Base64.strict_encode(OpenSSL::SHA1.hash(key + GUID))
Base64.strict_encode(OpenSSL::SHA1.hash(key + GUID).to_unsafe_slice)
{% end %}
end
end
16 changes: 8 additions & 8 deletions src/io.cr
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ abstract class IO
def skip(bytes_count : Int) : Nil
buffer = uninitialized UInt8[DEFAULT_BUFFER_SIZE]
while bytes_count > 0
read_count = read(buffer.to_slice[0, Math.min(bytes_count, buffer.size)])
read_count = read(buffer.to_unsafe_slice[0, Math.min(bytes_count, buffer.size)])
raise IO::EOFError.new if read_count == 0

bytes_count -= read_count
Expand All @@ -880,7 +880,7 @@ abstract class IO
# are no more bytes.
def skip_to_end : Nil
buffer = uninitialized UInt8[DEFAULT_BUFFER_SIZE]
while read(buffer.to_slice) > 0
while read(buffer.to_unsafe_slice) > 0
end
end

Expand Down Expand Up @@ -1173,8 +1173,8 @@ abstract class IO
def self.copy(src, dst) : Int64
buffer = uninitialized UInt8[DEFAULT_BUFFER_SIZE]
count = 0_i64
while (len = src.read(buffer.to_slice).to_i32) > 0
dst.write buffer.to_slice[0, len]
while (len = src.read(buffer.to_unsafe_slice).to_i32) > 0
dst.write buffer.to_unsafe_slice[0, len]
count &+= len
end
count
Expand All @@ -1197,8 +1197,8 @@ abstract class IO

buffer = uninitialized UInt8[DEFAULT_BUFFER_SIZE]
remaining = limit
while (len = src.read(buffer.to_slice[0, Math.min(buffer.size, Math.max(remaining, 0))])) > 0
dst.write buffer.to_slice[0, len]
while (len = src.read(buffer.to_unsafe_slice[0, Math.min(buffer.size, Math.max(remaining, 0))])) > 0
dst.write buffer.to_unsafe_slice[0, len]
remaining &-= len
end
limit - remaining
Expand All @@ -1218,8 +1218,8 @@ abstract class IO
buf2 = uninitialized UInt8[1024]

while true
read1 = stream1.read(buf1.to_slice)
read2 = stream2.read_fully?(buf2.to_slice[0, read1])
read1 = stream1.read(buf1.to_unsafe_slice)
read2 = stream2.read_fully?(buf2.to_unsafe_slice[0, read1])
return false unless read2

return false if buf1.to_unsafe.memcmp(buf2.to_unsafe, read1) != 0
Expand Down
6 changes: 3 additions & 3 deletions src/io/byte_format.cr
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,18 @@ module IO::ByteFormat
def self.encode(int : {{type.id}}, io : IO)
buffer = int.unsafe_as(StaticArray(UInt8, {{bytesize}}))
buffer.reverse! unless SystemEndian == self
io.write(buffer.to_slice)
io.write(buffer.to_unsafe_slice)
end

def self.encode(int : {{type.id}}, bytes : Bytes)
buffer = int.unsafe_as(StaticArray(UInt8, {{bytesize}}))
buffer.reverse! unless SystemEndian == self
buffer.to_slice.copy_to(bytes)
buffer.to_unsafe_slice.copy_to(bytes)
end

def self.decode(int : {{type.id}}.class, io : IO)
buffer = uninitialized UInt8[{{bytesize}}]
io.read_fully(buffer.to_slice)
io.read_fully(buffer.to_unsafe_slice)
buffer.reverse! unless SystemEndian == self
buffer.unsafe_as({{type.id}})
end
Expand Down
2 changes: 1 addition & 1 deletion src/io/encoding.cr
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class IO
if err == Crystal::Iconv::ERROR
@iconv.handle_invalid(pointerof(inbuf_ptr), pointerof(inbytesleft))
end
io.write(outbuf.to_slice[0, outbuf.size - outbytesleft])
io.write(outbuf.to_unsafe_slice[0, outbuf.size - outbytesleft])
end
end

Expand Down
4 changes: 2 additions & 2 deletions src/openssl/ssl/hostname_validation.cr
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ module OpenSSL::SSL::HostnameValidation
case data.size
when 4
if v4_fields = ::Socket::IPAddress.parse_v4_fields?(hostname)
return Result::MatchFound if v4_fields.to_slice == data
return Result::MatchFound if v4_fields.to_unsafe_slice == data
end
when 16
if v6_fields = ::Socket::IPAddress.parse_v6_fields?(hostname)
{% if IO::ByteFormat::NetworkEndian != IO::ByteFormat::SystemEndian %}
v6_fields.map! &.byte_swap
{% end %}
return Result::MatchFound if v6_fields.to_slice.to_unsafe_bytes == data
return Result::MatchFound if v6_fields.to_unsafe_slice.to_unsafe_bytes == data
end
else
# not a length we expect
Expand Down
Loading