From 178da988dcf8a58a1062a9c7517dc39c8eca8b43 Mon Sep 17 00:00:00 2001 From: Stefan Karpinski Date: Thu, 14 Dec 2017 01:19:53 -0500 Subject: [PATCH] deprecate b"..." to Vector{UInt8}("...") --- base/deprecated.jl | 8 ++++++ base/exports.jl | 1 - base/printf.jl | 4 +-- base/serialize.jl | 2 +- base/strings/io.jl | 2 -- base/strings/util.jl | 2 +- test/iobuffer.jl | 6 ++-- test/perf/shootout/fasta.jl | 4 +-- test/serialize.jl | 4 +-- test/strings/basic.jl | 56 +++++++++++++++++++------------------ test/strings/search.jl | 16 +++++------ test/strings/util.jl | 14 +++++----- test/unicode/utf8.jl | 24 ++++++++-------- 13 files changed, 75 insertions(+), 68 deletions(-) diff --git a/base/deprecated.jl b/base/deprecated.jl index 5f48ad7e237e3..b01a4f55d653c 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -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 diff --git a/base/exports.jl b/base/exports.jl index 3a374bf678963..9c9c30822d582 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -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 diff --git a/base/printf.jl b/base/printf.jl index 21ecc0cfea3ed..6b2721fbbe6f1 100644 --- a/base/printf.jl +++ b/base/printf.jl @@ -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) diff --git a/base/serialize.jl b/base/serialize.jl index 2efe10e480de6..b200a35a59a32 100644 --- a/base/serialize.jl +++ b/base/serialize.jl @@ -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 diff --git a/base/strings/io.jl b/base/strings/io.jl index e53a62ced49a1..42d6b1d2ca852 100644 --- a/base/strings/io.jl +++ b/base/strings/io.jl @@ -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 diff --git a/base/strings/util.jl b/base/strings/util.jl index 43e96fc1a8b22..4aecb5b1594c4 100644 --- a/base/strings/util.jl +++ b/base/strings/util.jl @@ -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 diff --git a/test/iobuffer.jl b/test/iobuffer.jl index 00de2463ad727..56f84cf053c65 100644 --- a/test/iobuffer.jl +++ b/test/iobuffer.jl @@ -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" @@ -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,"!") @@ -204,7 +204,7 @@ let a, @test read(io, String) == "dω" @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) diff --git a/test/perf/shootout/fasta.jl b/test/perf/shootout/fasta.jl index 2f0f233792a1e..0934de4af322b 100644 --- a/test/perf/shootout/fasta.jl +++ b/test/perf/shootout/fasta.jl @@ -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 diff --git a/test/serialize.jl b/test/serialize.jl index 770654007e41f..fc89da409ea6d 100644 --- a/test/serialize.jl +++ b/test/serialize.jl @@ -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)")) @@ -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) diff --git a/test/strings/basic.jl b/test/strings/basic.jl index 512bbd0805943..b1321c59ceed5 100644 --- a/test/strings/basic.jl +++ b/test/strings/basic.jl @@ -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 @@ -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" @@ -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] diff --git a/test/strings/search.jl b/test/strings/search.jl index c609066c4f05c..8b9a599698c49 100644 --- a/test/strings/search.jl +++ b/test/strings/search.jl @@ -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)] diff --git a/test/strings/util.jl b/test/strings/util.jl index cf8865157245a..7344698d728fe 100644 --- a/test/strings/util.jl +++ b/test/strings/util.jl @@ -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 diff --git a/test/unicode/utf8.jl b/test/unicode/utf8.jl index c65934217dfb9..dc093cd99ea8e 100644 --- a/test/unicode/utf8.jl +++ b/test/unicode/utf8.jl @@ -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 @@ -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