Skip to content

Commit

Permalink
deprecate b"..." to Vector{UInt8}("...")
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanKarpinski committed Dec 14, 2017
1 parent 0ab2c19 commit 178da98
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 68 deletions.
8 changes: 8 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3033,6 +3033,14 @@ end
# Associative -> AbstractDict (#25012)
@deprecate_binding Associative AbstractDict

# PR #25073
macro b_str(s)
r = repr(s)
depwarn("`b$r` is deprecated, use `Vector{UInt8}($r)` instead", Symbol("@b_str"))
:(Vector{UInt8}($(unescape_string(s))))
end
export @b_str

# END 0.7 deprecations

# BEGIN 1.0 deprecations
Expand Down
1 change: 0 additions & 1 deletion base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1154,7 +1154,6 @@ export
@cmd, # `commands`

# notation for certain types
@b_str, # byte vector
@r_str, # regex
@s_str, # regex substitution string
@v_str, # version number
Expand Down
4 changes: 2 additions & 2 deletions base/printf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -872,8 +872,8 @@ function decode_hex(d::Integer, symbols::Array{UInt8,1})
return Int32(pt), Int32(pt), neg
end

const hex_symbols = b"0123456789abcdef"
const HEX_symbols = b"0123456789ABCDEF"
const hex_symbols = Vector{UInt8}("0123456789abcdef")
const HEX_symbols = Vector{UInt8}("0123456789ABCDEF")

decode_hex(x::Integer) = decode_hex(x,hex_symbols)
decode_HEX(x::Integer) = decode_hex(x,HEX_symbols)
Expand Down
2 changes: 1 addition & 1 deletion base/serialize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ function writeheader(s::AbstractSerializer)
sizeof(Int) == 8 ? 1 :
error("unsupported word size in serializer"))
write(io, UInt8(endianness) | (UInt8(machine) << 2))
write(io, b"\x00\x00\x00") # 3 reserved bytes
write(io, [0x00,0x00,0x00]) # 3 reserved bytes
nothing
end

Expand Down
2 changes: 0 additions & 2 deletions base/strings/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,6 @@ function unescape_string(io, s::AbstractString)
end
end

macro b_str(s); :(Vector{UInt8}($(unescape_string(s)))); end

"""
@raw_str -> String
Expand Down
2 changes: 1 addition & 1 deletion base/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ julia> hex2bytes(s)
0x30
0x39
julia> a = b"01abEF"
julia> a = Vector{UInt8}("01abEF")
6-element Array{UInt8,1}:
0x30
0x31
Expand Down
6 changes: 3 additions & 3 deletions test/iobuffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ write(io,[1,2,3])
skip(io,1)
@test write(io,UInt8(104)) === 1
skip(io,3)
@test write(io,b"apples") === 3
@test write(io,Vector{UInt8}("apples")) === 3
skip(io,71)
@test write(io,'y') === 1
@test read(io, String) == "happy"
Expand Down Expand Up @@ -190,7 +190,7 @@ end
# pr #11554
let a,
io = IOBuffer(SubString("***αhelloworldω***", 4, 16)),
io2 = IOBuffer(b"goodnightmoon", true, true)
io2 = IOBuffer(Vector{UInt8}("goodnightmoon"), true, true)

@test read(io, Char) == 'α'
@test_throws ArgumentError write(io,"!")
Expand All @@ -204,7 +204,7 @@ let a,
@test read(io, String) == ""
@test bufcontents(io) == "αhelloworldω"
@test_throws ArgumentError write(io,"!")
@test take!(io) == b"αhelloworldω"
@test take!(io) == Vector{UInt8}("αhelloworldω")
seek(io, 2)
seekend(io2)
write(io2, io)
Expand Down
4 changes: 2 additions & 2 deletions test/perf/shootout/fasta.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ const alu = string(
"AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC",
"AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA")

const iub1 = b"acgtBDHKMNRSVWY"
const iub1 = Vector{UInt8}("acgtBDHKMNRSVWY")
const iub2 = [0.27, 0.12, 0.12, 0.27, 0.02,0.02, 0.02, 0.02, 0.02, 0.02,0.02, 0.02, 0.02, 0.02, 0.02]

const homosapiens1 = b"acgt"
const homosapiens1 = Vector{UInt8}("acgt")
const homosapiens2 = [0.3029549426680, 0.1979883004921,0.1975473066391, 0.3015094502008]

const IM = 139968.0
Expand Down
4 changes: 2 additions & 2 deletions test/serialize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ end

# Module
create_serialization_stream() do s # user-defined module
mod = b"SomeModule"
mod = Vector{UInt8}("SomeModule")
modstring = String(mod)
eval(Meta.parse("module $(modstring); end"))
modtype = eval(Meta.parse("$(modstring)"))
Expand Down Expand Up @@ -505,7 +505,7 @@ let io = IOBuffer()
seekstart(io)
b = read(io)
@test b[1] == Serializer.HEADER_TAG
@test b[2:3] == b"JL"
@test b[2:3] == Vector{UInt8}("JL")
@test b[4] == Serializer.ser_version
@test (b[5] & 0x3) == (ENDIAN_BOM == 0x01020304)
@test ((b[5] & 0xc)>>2) == (sizeof(Int) == 8)
Expand Down
56 changes: 29 additions & 27 deletions test/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -316,31 +316,31 @@ end
@test isvalid(Char, val) == pass
end
for (val, pass) in (
(b"\x00", true),
(b"\x7f", true),
(b"\x80", false),
(b"\xbf", false),
(b"\xc0", false),
(b"\xff", false),
(b"\xc0\x80", false),
(b"\xc1\x80", false),
(b"\xc2\x80", true),
(b"\xc2\xc0", false),
(b"\xed\x9f\xbf", true),
(b"\xed\xa0\x80", false),
(b"\xed\xbf\xbf", false),
(b"\xee\x80\x80", true),
(b"\xef\xbf\xbf", true),
(b"\xf0\x90\x80\x80", true),
(b"\xf4\x8f\xbf\xbf", true),
(b"\xf4\x90\x80\x80", false),
(b"\xf5\x80\x80\x80", false),
(b"\ud800\udc00", false),
(b"\udbff\udfff", false),
(b"\ud800\u0100", false),
(b"\udc00\u0100", false),
(b"\udc00\ud800", false)
)
("\x00", true),
("\x7f", true),
("\x80", false),
("\xbf", false),
("\xc0", false),
("\xff", false),
("\xc0\x80", false),
("\xc1\x80", false),
("\xc2\x80", true),
("\xc2\xc0", false),
("\xed\x9f\xbf", true),
("\xed\xa0\x80", false),
("\xed\xbf\xbf", false),
("\xee\x80\x80", true),
("\xef\xbf\xbf", true),
("\xf0\x90\x80\x80", true),
("\xf4\x8f\xbf\xbf", true),
("\xf4\x90\x80\x80", false),
("\xf5\x80\x80\x80", false),
("\ud800\udc00", false),
("\udbff\udfff", false),
("\ud800\u0100", false),
("\udc00\u0100", false),
("\udc00\ud800", false),
)
@test isvalid(String, val) == pass == isvalid(String(val))
end

Expand Down Expand Up @@ -430,8 +430,8 @@ end
@test_throws ArgumentError ascii(GenericString("Hello, ∀"))
end
@testset "issue #17271: endof() doesn't throw an error even with invalid strings" begin
@test endof(String(b"\x90")) == 1
@test endof(String(b"\xce")) == 1
@test endof("\x90") == 1
@test endof("\xce") == 1
end
# issue #17624, missing getindex method for String
@test "abc"[:] == "abc"
Expand Down Expand Up @@ -652,3 +652,5 @@ end
@test ncodeunits(GenericString(s)) == n
end
end

@test Vector{UInt8}("\xcc\xdd\xee\xff\x80") == [0xcc,0xdd,0xee,0xff,0x80]
16 changes: 8 additions & 8 deletions test/strings/search.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ end
# @test_throws BoundsError rsearchindex("foo", Char[], 5)

# @test_throws ErrorException in("foobar","bar")
@test_throws BoundsError search(b"\x1\x2",0x1,0)
@test rsearchindex(b"foo",b"o",0) == 0
@test rsearchindex(SubString("",1,0),SubString("",1,0)) == 1

@test search(b"foo",'o') == 2
@test rsearch(b"foo",'o') == 3
@test search(b"foó",'ó') == 3
@test rsearch(b"foó",'ó') == 3
@test_throws BoundsError search(Vector{UInt8}("\x1\x2"), 0x1, 0)
@test rsearchindex(Vector{UInt8}("foo"), Vector{UInt8}("o"), 0) == 0
@test rsearchindex(SubString("", 1, 0), SubString("", 1, 0)) == 1

@test search(Vector{UInt8}("foo"), 'o') == 2
@test rsearch(Vector{UInt8}("foo"), 'o') == 3
@test search(Vector{UInt8}("foó"), 'ó') == 3
@test rsearch(Vector{UInt8}("foó"), 'ó') == 3

# ascii search
for str in [astr, GenericString(astr)]
Expand Down
14 changes: 7 additions & 7 deletions test/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -282,23 +282,23 @@ end
@test_throws ArgumentError hex2bytes("0123456789abcdefABCDEFGH")

@testset "Issue 23161" begin
arr = b"0123456789abcdefABCDEF"
arr = Vector{UInt8}("0123456789abcdefABCDEF")
arr1 = Vector{UInt8}(uninitialized, length(arr) >> 1)
@test hex2bytes!(arr1, arr) === arr1 # check in-place
@test "0123456789abcdefabcdef" == bytes2hex(arr1)
@test hex2bytes("0123456789abcdefABCDEF") == hex2bytes(arr)
@test_throws ArgumentError hex2bytes!(arr1, b"") # incorrect arr1 length
@test hex2bytes(b"") == UInt8[]
@test hex2bytes(view(b"012345",1:6)) == UInt8[0x01,0x23,0x45]
@test_throws ArgumentError hex2bytes!(arr1, UInt8[]) # incorrect arr1 length
@test hex2bytes(UInt8[]) == UInt8[]
@test hex2bytes(view(Vector{UInt8}("012345"),1:6)) == UInt8[0x01,0x23,0x45]
@test begin
s = view(b"012345ab",1:6)
s = view(Vector{UInt8}("012345ab"),1:6)
d = view(zeros(UInt8, 10),1:3)
hex2bytes!(d,s) == UInt8[0x01,0x23,0x45]
end
# odd size
@test_throws ArgumentError hex2bytes(b"0123456789abcdefABCDEF0")
@test_throws ArgumentError hex2bytes(Vector{UInt8}("0123456789abcdefABCDEF0"))

#non-hex characters
@test_throws ArgumentError hex2bytes(b"0123456789abcdefABCDEFGH")
@test_throws ArgumentError hex2bytes(Vector{UInt8}("0123456789abcdefABCDEFGH"))
end
end
24 changes: 12 additions & 12 deletions test/unicode/utf8.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

@testset "string indexing" begin
let str = String(b"this is a test\xed\x80")
let str = "this is a test\xed\x80"
@test next(str, 15) == (reinterpret(Char, 0xed800000), 17)
@test_throws BoundsError getindex(str, 0:3)
@test_throws BoundsError getindex(str, 17:18)
@test_throws BoundsError getindex(str, 2:17)
@test_throws BoundsError getindex(str, 16:17)
@test string(Char(0x110000)) == String(b"\xf4\x90\x80\x80")
@test string(Char(0x110000)) == "\xf4\x90\x80\x80"
end
end

Expand All @@ -17,20 +17,20 @@ end
@test reverse("abc") == "cba"
@test reverse("xyz\uff\u800\uffff\U10ffff") == "\U10ffff\uffff\u800\uffzyx"
for (s, r) in [
b"xyz\xc1" => b"\xc1zyx",
b"xyz\xd0" => b"\xd0zyx",
b"xyz\xe0" => b"\xe0zyx",
b"xyz\xed\x80" => b"\xed\x80zyx",
b"xyz\xf0" => b"\xf0zyx",
b"xyz\xf0\x80" => b"\xf0\x80zyx",
b"xyz\xf0\x80\x80" => b"\xf0\x80\x80zyx",
"xyz\xc1" => "\xc1zyx",
"xyz\xd0" => "\xd0zyx",
"xyz\xe0" => "\xe0zyx",
"xyz\xed\x80" => "\xed\x80zyx",
"xyz\xf0" => "\xf0zyx",
"xyz\xf0\x80" => "\xf0\x80zyx",
"xyz\xf0\x80\x80" => "\xf0\x80\x80zyx",
]
@test reverse(String(s)) == String(r)
@test reverse(s) == r
end
end

@testset "string convert" begin
@test String(b"this is a test\xed\x80\x80") == "this is a test\ud000"
@test "this is a test\xed\x80\x80" == "this is a test\ud000"
# Specifically check UTF-8 string whose lead byte is same as a surrogate
@test String(b"\xed\x9f\xbf") == "\ud7ff"
@test "\xed\x9f\xbf" == "\ud7ff"
end

0 comments on commit 178da98

Please sign in to comment.