Skip to content

Commit

Permalink
Replace Slice(UInt8) with Bytes throughout the codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
Sija committed Dec 17, 2016
1 parent 45516b5 commit 056986b
Show file tree
Hide file tree
Showing 48 changed files with 144 additions and 144 deletions.
2 changes: 1 addition & 1 deletion spec/std/base64_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe "Base64" do
end

it "encodes byte slice" do
slice = Slice(UInt8).new(5) { 1_u8 }
slice = Bytes.new(5) { 1_u8 }
Base64.encode(slice).should eq("AQEBAQE=\n")
Base64.strict_encode(slice).should eq("AQEBAQE=")
end
Expand Down
4 changes: 2 additions & 2 deletions spec/std/crypto/bcrypt_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ require "spec"
require "crypto/bcrypt"

describe "Crypto::Bcrypt" do
latin1_pound_sign = String.new(Slice(UInt8).new(1, 0xa3_u8))
utf8_pound_sign = String.new(Slice(UInt8).new(2) { |i| i == 0 ? 0xc2_u8 : 0xa3_u8 })
latin1_pound_sign = String.new(Bytes.new(1, 0xa3_u8))
utf8_pound_sign = String.new(Bytes.new(2) { |i| i == 0 ? 0xc2_u8 : 0xa3_u8 })
bit8_unicode_pound_sign = "\u00A3"

vectors = [
Expand Down
8 changes: 4 additions & 4 deletions spec/std/http/server/server_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ private class RaiseErrno

include IO

def read(slice : Slice(UInt8))
def read(slice : Bytes)
Errno.value = @value
raise Errno.new "..."
end

def write(slice : Slice(UInt8)) : Nil
def write(slice : Bytes) : Nil
raise "not implemented"
end
end
Expand All @@ -25,13 +25,13 @@ private class ReverseResponseOutput
def initialize(@output : IO)
end

def write(slice : Slice(UInt8))
def write(slice : Bytes)
slice.reverse_each do |byte|
@output.write_byte(byte)
end
end

def read(slice : Slice(UInt8))
def read(slice : Bytes)
raise "Not implemented"
end

Expand Down
20 changes: 10 additions & 10 deletions spec/std/http/web_socket_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe HTTP::WebSocket do
io = IO::Memory.new(data)
ws = HTTP::WebSocket::Protocol.new(io)

buffer = Slice(UInt8).new(64)
buffer = Bytes.new(64)
result = ws.receive(buffer)
assert_text_packet result, 5, final: true
String.new(buffer[0, result.size]).should eq("Hello")
Expand All @@ -42,7 +42,7 @@ describe HTTP::WebSocket do
io = IO::Memory.new(data)
ws = HTTP::WebSocket::Protocol.new(io)

buffer = Slice(UInt8).new(3)
buffer = Bytes.new(3)

2.times do
result = ws.receive(buffer)
Expand All @@ -61,7 +61,7 @@ describe HTTP::WebSocket do
io = IO::Memory.new(data)
ws = HTTP::WebSocket::Protocol.new(io)

buffer = Slice(UInt8).new(3)
buffer = Bytes.new(3)

2.times do
result = ws.receive(buffer)
Expand All @@ -81,7 +81,7 @@ describe HTTP::WebSocket do
io = IO::Memory.new(data)
ws = HTTP::WebSocket::Protocol.new(io)

buffer = Slice(UInt8).new(10)
buffer = Bytes.new(10)

2.times do
result = ws.receive(buffer)
Expand All @@ -99,7 +99,7 @@ describe HTTP::WebSocket do
io = IO::Memory.new(data)
ws = HTTP::WebSocket::Protocol.new(io)

buffer = Slice(UInt8).new(64)
buffer = Bytes.new(64)
result = ws.receive(buffer)
assert_ping_packet result, 5, final: true
String.new(buffer[0, result.size]).should eq("Hello")
Expand All @@ -112,7 +112,7 @@ describe HTTP::WebSocket do
io = IO::Memory.new(data)
ws = HTTP::WebSocket::Protocol.new(io)

buffer = Slice(UInt8).new(64)
buffer = Bytes.new(64)

result = ws.receive(buffer)
assert_text_packet result, 3, final: false
Expand All @@ -132,23 +132,23 @@ describe HTTP::WebSocket do
io = IO::Memory.new(data)
ws = HTTP::WebSocket::Protocol.new(io)

buffer = Slice(UInt8).new(2048)
buffer = Bytes.new(2048)

result = ws.receive(buffer)
assert_text_packet result, 1023, final: true
String.new(buffer[0, 1023]).should eq("x" * 1023)
end

it "read very long packet" do
data = Slice(UInt8).new(10 + 0x010000)
data = Bytes.new(10 + 0x010000)

header = Bytes[0x82, 127_u8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x0]
data.copy_from(header)

io = IO::Memory.new(data)
ws = HTTP::WebSocket::Protocol.new(io)

buffer = Slice(UInt8).new(0x010000)
buffer = Bytes.new(0x010000)

result = ws.receive(buffer)
assert_binary_packet result, 0x010000, final: true
Expand All @@ -159,7 +159,7 @@ describe HTTP::WebSocket do
io = IO::Memory.new(data)
ws = HTTP::WebSocket::Protocol.new(io)

buffer = Slice(UInt8).new(64)
buffer = Bytes.new(64)
result = ws.receive(buffer)
assert_close_packet result, 0, final: true
end
Expand Down
8 changes: 4 additions & 4 deletions spec/std/io/buffered_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ private class BufferedWrapper
io
end

private def unbuffered_read(slice : Slice(UInt8))
private def unbuffered_read(slice : Bytes)
@called_unbuffered_read = true
@io.read(slice)
end

private def unbuffered_write(slice : Slice(UInt8))
private def unbuffered_write(slice : Bytes)
@io.write(slice)
end

Expand Down Expand Up @@ -189,7 +189,7 @@ describe "IO::Buffered" do
end
io = BufferedWrapper.new(IO::Memory.new(s))

slice = Slice(UInt8).new(9000)
slice = Bytes.new(9000)
count = io.read(slice)
count.should eq(9000)

Expand Down Expand Up @@ -283,7 +283,7 @@ describe "IO::Buffered" do
it "shouldn't call unbuffered read if reading to an empty slice" do
str = IO::Memory.new("foo")
io = BufferedWrapper.new(str)
io.read(Slice(UInt8).new(0))
io.read(Bytes.new(0))
io.called_unbuffered_read.should be_false
end

Expand Down
2 changes: 1 addition & 1 deletion spec/std/io/delimited_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ require "spec"
private class PartialReaderIO
include IO

@slice : Slice(UInt8)
@slice : Bytes

def initialize(data : String)
@slice = data.to_slice
Expand Down
4 changes: 2 additions & 2 deletions spec/std/io/hexdump_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe IO::Hexdump do
io = IO::Memory.new(ascii_table.bytesize)
r = IO::Hexdump.new(r, output: io, read: true)

slice = Slice(UInt8).new(101) { |i| i.to_u8 + 32 }
slice = Bytes.new(101) { |i| i.to_u8 + 32 }
w.write(slice)

buf = uninitialized UInt8[101]
Expand All @@ -45,7 +45,7 @@ describe IO::Hexdump do
io = IO::Memory.new(ascii_table.bytesize)
w = IO::Hexdump.new(w, output: io, write: true)

slice = Slice(UInt8).new(96) { |i| i.to_u8 + 32 }
slice = Bytes.new(96) { |i| i.to_u8 + 32 }
w.write(slice)

buf = uninitialized UInt8[96]
Expand Down
12 changes: 6 additions & 6 deletions spec/std/io/io_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ private class SimpleIOMemory
io
end

def self.new(bytes : Slice(UInt8), max_read = nil)
def self.new(bytes : Bytes, max_read = nil)
io = new(bytes.size, max_read: max_read)
io.write(bytes)
io
end

def read(slice : Slice(UInt8))
def read(slice : Bytes)
count = slice.size
count = Math.min(count, @bytesize - @pos)
if max_read = @max_read
Expand All @@ -44,7 +44,7 @@ private class SimpleIOMemory
count
end

def write(slice : Slice(UInt8))
def write(slice : Bytes)
count = slice.size
new_bytesize = bytesize + count
if new_bytesize > @capacity
Expand Down Expand Up @@ -98,7 +98,7 @@ describe IO do
it "doesn't block on first read. blocks on 2nd read" do
IO.pipe do |read, write|
write.puts "hello"
slice = Slice(UInt8).new 1024
slice = Bytes.new 1024

read.read_timeout = 1
read.read(slice).should eq(6)
Expand Down Expand Up @@ -343,7 +343,7 @@ describe IO do

it "does read_fully" do
str = SimpleIOMemory.new("hello")
slice = Slice(UInt8).new(4)
slice = Bytes.new(4)
str.read_fully(slice).should eq(4)
String.new(slice).should eq("hell")

Expand All @@ -354,7 +354,7 @@ describe IO do

it "does read_fully?" do
str = SimpleIOMemory.new("hello")
slice = Slice(UInt8).new(4)
slice = Bytes.new(4)
str.read_fully?(slice).should eq(4)
String.new(slice).should eq("hell")

Expand Down
2 changes: 1 addition & 1 deletion spec/std/secure_random_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe SecureRandom do

it "fully fills a large buffer" do
bytes = SecureRandom.random_bytes(10000)
bytes[9990, 10].should_not eq(Slice(UInt8).new(10))
bytes[9990, 10].should_not eq(Bytes.new(10))
end
end

Expand Down
6 changes: 3 additions & 3 deletions spec/std/slice_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ describe "Slice" do
end

it "does hexstring" do
slice = Slice(UInt8).new(4) { |i| i.to_u8 + 1 }
slice = Bytes.new(4) { |i| i.to_u8 + 1 }
slice.hexstring.should eq("01020304")
end

Expand All @@ -244,7 +244,7 @@ describe "Slice" do
00000050 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f pqrstuvwxyz{|}~.
EOF

slice = Slice(UInt8).new(96) { |i| i.to_u8 + 32 }
slice = Bytes.new(96) { |i| i.to_u8 + 32 }
slice.hexdump.should eq(ascii_table)

ascii_table_plus = <<-EOF
Expand All @@ -257,7 +257,7 @@ describe "Slice" do
00000060 80 81 82 83 84 .....
EOF

plus = Slice(UInt8).new(101) { |i| i.to_u8 + 32 }
plus = Bytes.new(101) { |i| i.to_u8 + 32 }
plus.hexdump.should eq(ascii_table_plus)
end

Expand Down
4 changes: 2 additions & 2 deletions spec/std/string_pool_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe StringPool do

it "gets slice" do
pool = StringPool.new
slice = Slice(UInt8).new(3, 'a'.ord.to_u8)
slice = Bytes.new(3, 'a'.ord.to_u8)

s1 = pool.get(slice)
s2 = pool.get(slice)
Expand All @@ -47,7 +47,7 @@ describe StringPool do

it "gets pointer with size" do
pool = StringPool.new
slice = Slice(UInt8).new(3, 'a'.ord.to_u8)
slice = Bytes.new(3, 'a'.ord.to_u8)

s1 = pool.get(slice.pointer(slice.size), slice.size)
s2 = pool.get(slice.pointer(slice.size), slice.size)
Expand Down
4 changes: 2 additions & 2 deletions spec/std/zlib/inflate_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module Zlib
end

str.should eq("this is a test string !!!!\n")
inflate.read(Slice(UInt8).new(10)).should eq(0)
inflate.read(Bytes.new(10)).should eq(0)
end

it "can be closed without sync" do
Expand Down Expand Up @@ -62,7 +62,7 @@ module Zlib
end
io.rewind
inflate = Inflate.new(io)
slice = Slice(UInt8).new(0)
slice = Bytes.new(0)
inflate.read(slice).should eq(0)
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/base64.cr
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ module Base64
strict_encode_to_io_internal(data, io, CHARS_SAFE, pad: true)
end

# Returns the Base64-decoded version of `data` as a *Slice(UInt8)*.
# Returns the Base64-decoded version of `data` as a *Bytes*.
# This will decode either the normal or urlsafe alphabets.
def decode(data) : Bytes
slice = data.to_slice
Expand Down
4 changes: 2 additions & 2 deletions src/bit_array.cr
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ struct BitArray
to_s(io)
end

# Returns a Slice(UInt8) able to read and write bytes from a buffer.
# Returns a `Bytes` able to read and write bytes from a buffer.
# The slice will be long enough to hold all the bits groups in bytes despite the `UInt32` internal representation.
# It's useful for reading and writing a bit array from a byte buffer directly.
def to_slice : Slice(UInt8)
def to_slice : Bytes
Slice.new(@bits.as(Pointer(UInt8)), (@size / 8.0).ceil.to_i)
end

Expand Down
10 changes: 5 additions & 5 deletions src/crypto/bcrypt.cr
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ class Crypto::Bcrypt
new(passwordb, saltb, cost)
end

getter password : Slice(UInt8)
getter salt : Slice(UInt8)
getter password : Bytes
getter salt : Bytes
getter cost : Int32

def initialize(@password : Slice(UInt8), @salt : Slice(UInt8), @cost = DEFAULT_COST)
def initialize(@password : Bytes, @salt : Bytes, @cost = DEFAULT_COST)
raise Error.new("Invalid cost") unless COST_RANGE.includes?(cost)
raise Error.new("Invalid salt size") unless salt.size == SALT_SIZE
raise Error.new("Invalid password size") unless PASSWORD_RANGE.includes?(password.size)
end

@digest : Slice(UInt8)?
@digest : Bytes?

def digest
@digest ||= hash_password
Expand Down Expand Up @@ -104,7 +104,7 @@ class Crypto::Bcrypt
end
end

ret = Slice(UInt8).new(size * 4)
ret = Bytes.new(size * 4)
j = -1

size.times do |i|
Expand Down
4 changes: 2 additions & 2 deletions src/crypto/bcrypt/base64.cr
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ module Crypto::Bcrypt::Base64
end
end

def self.decode(string, maxolen) : Slice(UInt8)
def self.decode(string, maxolen) : Bytes
off, slen, olen = 0, string.size, 0

i = -1
str = Slice(UInt8).new(maxolen)
str = Bytes.new(maxolen)

while off < slen - 1 && olen < maxolen
c1, c2 = char64(string[off]), char64(string[off + 1])
Expand Down

0 comments on commit 056986b

Please sign in to comment.