Skip to content

Commit

Permalink
Merge pull request #14578 from omus/char_add
Browse files Browse the repository at this point in the history
Fixes unintentional promotion to Any with Char/Int math
  • Loading branch information
tkelman committed Jan 7, 2016
2 parents c1faf7f + 9841953 commit bef2b08
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions base/char.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ isless(x::Integer, y::Char) = isless(x, UInt32(y))
+(x::Char, y::Integer) = Char(Int32(x) + Int32(y))
+(x::Integer, y::Char) = y + x

Base.promote_op{I<:Integer}(::Base.SubFun, ::Type{Char}, ::Type{I}) = Char
Base.promote_op{I<:Integer}(::Base.AddFun, ::Type{Char}, ::Type{I}) = Char
Base.promote_op{I<:Integer}(::Base.AddFun, ::Type{I}, ::Type{Char}) = Char

bswap(x::Char) = Char(bswap(UInt32(x)))

print(io::IO, c::Char) = (write(io, c); nothing)
Expand Down
22 changes: 22 additions & 0 deletions test/char.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
# should be swapped
@test bswap('\U10200') == '\U20100'

@test 'b' + 1 == 'c'
@test typeof('b' + 1) == Char
@test 1 + 'b' == 'c'
@test typeof(1 + 'b') == Char
@test 'b' - 1 == 'a'
@test typeof('b' - 1) == Char

let

numberchars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
Expand Down Expand Up @@ -171,3 +178,18 @@ end #end of let block

@test convert(Signed, 'A') === Int32(65)
@test convert(Unsigned, 'A') === UInt32(65)

# issue #14573
let
array = ['a', 'b', 'c'] + [1, 2, 3]
@test array == ['b', 'd', 'f']
@test eltype(array) == Char

array = [1, 2, 3] + ['a', 'b', 'c']
@test array == ['b', 'd', 'f']
@test eltype(array) == Char

array = ['a', 'b', 'c'] - [0, 1, 2]
@test array == ['a', 'a', 'a']
@test eltype(array) == Char
end

0 comments on commit bef2b08

Please sign in to comment.