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

Deprecate String#unsafe_byte_at #10559

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
2 changes: 1 addition & 1 deletion src/io.cr
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ abstract class IO

# One byte: use gets(Char)
if delimiter.bytesize == 1
return gets(delimiter.unsafe_byte_at(0).unsafe_chr, chomp: chomp)
return gets(delimiter.to_unsafe[0].unsafe_chr, chomp: chomp)
end

# One char: use gets(Char)
Expand Down
17 changes: 9 additions & 8 deletions src/string.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,7 @@ class String
end

# Returns the byte at the given *index* without bounds checking.
@[Deprecated("Use `to_unsafe[index]` instead.")]
def unsafe_byte_at(index : Int) : UInt8
to_unsafe[index]
end
Expand All @@ -1242,7 +1243,7 @@ class String
if single_byte_optimizable? && (options.none? || options.ascii?)
return String.new(bytesize) do |buffer|
bytesize.times do |i|
buffer[i] = unsafe_byte_at(i).unsafe_chr.downcase.ord.to_u8
buffer[i] = to_unsafe[i].unsafe_chr.downcase.ord.to_u8
end
{@bytesize, @length}
end
Expand Down Expand Up @@ -1277,7 +1278,7 @@ class String
if single_byte_optimizable? && (options.none? || options.ascii?)
return String.new(bytesize) do |buffer|
bytesize.times do |i|
buffer[i] = unsafe_byte_at(i).unsafe_chr.upcase.ord.to_u8
buffer[i] = to_unsafe[i].unsafe_chr.upcase.ord.to_u8
end
{@bytesize, @length}
end
Expand Down Expand Up @@ -1314,9 +1315,9 @@ class String
return String.new(bytesize) do |buffer|
bytesize.times do |i|
byte = if i.zero?
unsafe_byte_at(i).unsafe_chr.upcase.ord.to_u8
to_unsafe[i].unsafe_chr.upcase.ord.to_u8
else
unsafe_byte_at(i).unsafe_chr.downcase.ord.to_u8
to_unsafe[i].unsafe_chr.downcase.ord.to_u8
end

buffer[i] = byte
Expand Down Expand Up @@ -1361,7 +1362,7 @@ class String

return String.new(bytesize) do |buffer|
bytesize.times do |i|
char = unsafe_byte_at(i).unsafe_chr
char = to_unsafe[i].unsafe_chr
replaced_char = upcase_next ? char.upcase : char.downcase
buffer[i] = replaced_char.ord.to_u8
upcase_next = char.whitespace?
Expand Down Expand Up @@ -2017,7 +2018,7 @@ class String
return delete(from) if to.empty?

if from.bytesize == 1
return gsub(from.unsafe_byte_at(0).unsafe_chr, to[0])
return gsub(from.to_unsafe[0].unsafe_chr, to[0])
end

multi = nil
Expand Down Expand Up @@ -2435,7 +2436,7 @@ class String
# ```
def gsub(char : Char, replacement)
if replacement.is_a?(String) && replacement.bytesize == 1
return gsub(char, replacement.unsafe_byte_at(0).unsafe_chr)
return gsub(char, replacement.to_unsafe[0].unsafe_chr)
end

if includes?(char)
Expand Down Expand Up @@ -2545,7 +2546,7 @@ class String
# ```
def gsub(string : String, replacement)
if string.bytesize == 1
gsub(string.unsafe_byte_at(0).unsafe_chr, replacement)
gsub(string.to_unsafe[0].unsafe_chr, replacement)
else
gsub(string) { replacement }
end
Expand Down
2 changes: 1 addition & 1 deletion src/string/utf16.cr
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class String
if i == bytesize
0_u16
else
unsafe_byte_at(i).to_u16
to_unsafe[i].to_u16
end
end
return slice[0, bytesize]
Expand Down
6 changes: 3 additions & 3 deletions src/uri/encoding.cr
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ class URI
i = 0
bytesize = string.bytesize
while i < bytesize
byte = string.unsafe_byte_at(i)
byte = string.to_unsafe[i]
char = byte.unsafe_chr
i = decode_one(string, bytesize, i, byte, char, io, plus_to_space) { |byte| yield byte }
end
Expand Down Expand Up @@ -244,15 +244,15 @@ class URI

if char == '%' && i < bytesize - 2
i += 1
first = string.unsafe_byte_at(i)
first = string.to_unsafe[i]
first_num = first.unsafe_chr.to_i? 16
unless first_num
io.write_byte byte
return i
end

i += 1
second = string.unsafe_byte_at(i)
second = string.to_unsafe[i]
second_num = second.unsafe_chr.to_i? 16
unless second_num
io.write_byte byte
Expand Down
2 changes: 1 addition & 1 deletion src/uri/params.cr
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class URI
first_equal = true
bytesize = query.bytesize
while i < bytesize
byte = query.unsafe_byte_at(i)
byte = query.to_unsafe[i]
char = byte.unsafe_chr

case char
Expand Down