-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Make Char no longer a subtype of Integer #8816
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,30 +7,46 @@ convert(::Type{Char}, x::Float16) = char(convert(Uint32, x)) | |
convert(::Type{Char}, x::Float32) = char(convert(Uint32, x)) | ||
convert(::Type{Char}, x::Float64) = char(convert(Uint32, x)) | ||
|
||
## char promotions ## | ||
|
||
promote_rule(::Type{Char}, ::Type{Int8}) = Int32 | ||
promote_rule(::Type{Char}, ::Type{Uint8}) = Uint32 | ||
promote_rule(::Type{Char}, ::Type{Int16}) = Int32 | ||
promote_rule(::Type{Char}, ::Type{Uint16}) = Uint32 | ||
promote_rule(::Type{Char}, ::Type{Int32}) = Int32 | ||
promote_rule(::Type{Char}, ::Type{Uint32}) = Uint32 | ||
promote_rule(::Type{Char}, ::Type{Int64}) = Int64 | ||
promote_rule(::Type{Char}, ::Type{Uint64}) = Uint64 | ||
promote_rule(::Type{Char}, ::Type{Int128}) = Int128 | ||
promote_rule(::Type{Char}, ::Type{Uint128}) = Uint128 | ||
typemax(::Type{Char}) = char(typemax(Uint32)) | ||
typemin(::Type{Char}) = char(typemin(Uint32)) | ||
|
||
## character operations & comparisons ## | ||
size(c::Char) = () | ||
size(c::Char,d) = convert(Int, d) < 1 ? throw(BoundsError()) : 1 | ||
ndims(c::Char) = 0 | ||
ndims(::Type{Char}) = 0 | ||
length(c::Char) = 1 | ||
endof(c::Char) = 1 | ||
getindex(c::Char) = c | ||
getindex(c::Char, i::Integer) = i == 1 ? c : throw(BoundsError()) | ||
getindex(c::Char, I::Integer...) = all([i == 1 for i in I]) ? c : throw(BoundsError()) | ||
getindex(c::Char, I::Real...) = getindex(c, to_index(I)...) | ||
first(c::Char) = c | ||
last(c::Char) = c | ||
|
||
start(c::Char) = false | ||
next(c::Char, state) = (c, true) | ||
done(c::Char, state) = state | ||
isempty(c::Char) = false | ||
in(x::Char, y::Char) = x == y | ||
|
||
==(x::Char, y::Char) = uint32(x) == uint32(y) | ||
==(x::Char, y::Integer) = uint32(x) == y | ||
==(x::Integer, y::Char) = x == uint32(y) | ||
|
||
< (x::Char, y::Char) = uint32(x) < uint32(y) | ||
<=(x::Char, y::Char) = uint32(x) <= uint32(y) | ||
|
||
isless(x::Char, y::Integer) = isless(uint32(x), y) | ||
isless(x::Integer, y::Char) = isless(x, uint32(y)) | ||
|
||
# numeric operations | ||
# TODO: this should be removed, but needs to be here as long as Char <: Integer | ||
+(x::Char , y::Char ) = int(x)+int(y) | ||
|
||
# ordinal operations | ||
+(x::Char , y::Integer) = reinterpret(Char, int32(x)+int32(y)) | ||
+(x::Integer, y::Char ) = y+x | ||
-(x::Char , y::Char ) = int(x)-int(y) | ||
-(x::Char , y::Integer) = reinterpret(Char, int32(x)-int32(y)) | ||
+(x::Char , y::Integer) = reinterpret(Char, int32(x) + int32(y)) | ||
+(x::Integer, y::Char) = y + x | ||
-(x::Char , y::Char) = int(x) - int(y) | ||
-(x::Char , y::Integer) = reinterpret(Char, int32(x) - int32(y)) | ||
|
||
# bitwise operations | ||
(~)(x::Char) = char(~uint32(x)) | ||
|
@@ -40,14 +56,10 @@ promote_rule(::Type{Char}, ::Type{Uint128}) = Uint128 | |
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to keep these bitwise operations for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be useful for masking. |
||
bswap(x::Char) = char(bswap(uint32(x))) | ||
|
||
<<(x::Char, y::Int32) = uint32(x) << y | ||
>>(x::Char, y::Int32) = uint32(x) >>> y | ||
>>>(x::Char, y::Int32) = uint32(x) >>> y | ||
|
||
< (x::Char, y::Char) = uint32(x) < uint32(y) | ||
<=(x::Char, y::Char) = uint32(x) <= uint32(y) | ||
|
||
## printing & showing characters ## | ||
|
||
print(io::IO, c::Char) = (write(io,c); nothing) | ||
show(io::IO, c::Char) = (print(io,'\''); print_escaped(io,utf32(c),"'"); print(io,'\'')) | ||
print(io::IO, c::Char) = (write(io, c); nothing) | ||
show(io::IO, c::Char) = begin | ||
print(io, '\'') | ||
print_escaped(io, utf32(c), "'") | ||
print(io, '\'') | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,7 @@ function countlines(io::IO, eol::Char) | |
while !eof(io) | ||
nb = readbytes!(io, a) | ||
for i=1:nb | ||
if a[i] == eol | ||
if char(a[i]) == eol | ||
preceded_by_eol = true | ||
elseif preceded_by_eol | ||
preceded_by_eol = false | ||
|
@@ -38,8 +38,6 @@ function countlines(io::IO, eol::Char) | |
nl | ||
end | ||
|
||
|
||
|
||
readdlm(input, T::Type; opts...) = readdlm(input, invalid_dlm, T, '\n'; opts...) | ||
readdlm(input, dlm::Char, T::Type; opts...) = readdlm(input, dlm, T, '\n'; opts...) | ||
|
||
|
@@ -67,11 +65,10 @@ end | |
|
||
function ascii_if_possible(sbuff::String) | ||
isa(sbuff, ASCIIString) && return sbuff | ||
|
||
asci = true | ||
d = sbuff.data | ||
for idx in 1:length(d) | ||
(d[idx] < 0x80) ? continue : (asci = false; break) | ||
(char(d[idx]) < char(0x80)) ? continue : (asci = false; break) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wouldn't have thought to do this, but now I'm wondering if I should -- what's the benefit? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, this doesn't make sense. This code is really comparing bytes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is because for UTF32 strings this is a Char / Integer comparison, and I removed those definitions some intermediate point. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see -- good to know that's what UTF32String looks like under the hood. Thanks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I got confused because this code is kind of wrong. If it's supposed to work for arbitrary Strings it shouldn't be accessing |
||
end | ||
asci ? ASCIIString(sbuff.data) : sbuff | ||
end | ||
|
@@ -168,7 +165,7 @@ function store_cell{T,S<:String}(dlmstore::DLMStore{T,S}, row::Int, col::Int, qu | |
tmp64 = dlmstore.tmp64 | ||
|
||
endpos = prevind(sbuff, nextind(sbuff,endpos)) | ||
(endpos > 0) && ('\n' == dlmstore.eol) && ('\r' == sbuff[endpos]) && (endpos = prevind(sbuff, endpos)) | ||
(endpos > 0) && ('\n' == dlmstore.eol) && ('\r' == char(sbuff[endpos])) && (endpos = prevind(sbuff, endpos)) | ||
sval = quoted ? SubString(sbuff, startpos+1, endpos-1) : SubString(sbuff, startpos, endpos) | ||
|
||
if drow > 0 | ||
|
@@ -200,7 +197,6 @@ function store_cell{T,S<:String}(dlmstore::DLMStore{T,S}, row::Int, col::Int, qu | |
((T <: Number) && dlmstore.auto) ? throw(TypeError(:store_cell, "", Any, T)) : error("file entry \"$(sval)\" cannot be converted to $T") | ||
end | ||
|
||
|
||
dlmstore.lastrow = drow | ||
dlmstore.lastcol = col | ||
else | ||
|
@@ -330,15 +326,16 @@ colval{S<:String}(sval::S, cells::Array{Any,2}, row::Int, col::Int, tmp64::Array | |
colval{T<:Char, S<:String}(sval::S, cells::Array{T,2}, row::Int, col::Int, tmp64::Array{Float64,1}) = ((length(sval) == 1) ? ((cells[row,col] = next(sval,1)[1]); false) : true) | ||
colval{S<:String}(sval::S, cells::Array, row::Int, col::Int, tmp64::Array{Float64,1}) = true | ||
|
||
|
||
dlm_parse(s::ASCIIString, eol::Char, dlm::Char, qchar::Char, cchar::Char, ign_adj_dlm::Bool, allow_quote::Bool, allow_comments::Bool, skipstart::Int, skipblanks::Bool, dh::DLMHandler) = | ||
dlm_parse(s.data, uint8(eol), uint8(dlm), uint8(qchar), uint8(cchar), ign_adj_dlm, allow_quote, allow_comments, skipstart, skipblanks, dh) | ||
dlm_parse(s::ASCIIString, eol::Char, dlm::Char, qchar::Char, cchar::Char, ign_adj_dlm::Bool, allow_quote::Bool, allow_comments::Bool, skipstart::Int, skipblanks::Bool, dh::DLMHandler) = begin | ||
dlm_parse(s.data, uint8(uint32(eol)), uint8(uint32(dlm)), uint8(uint32(qchar)), uint8(uint32(cchar)), | ||
ign_adj_dlm, allow_quote, allow_comments, skipstart, skipblanks, dh) | ||
end | ||
|
||
function dlm_parse{T,D}(dbuff::T, eol::D, dlm::D, qchar::D, cchar::D, ign_adj_dlm::Bool, allow_quote::Bool, allow_comments::Bool, skipstart::Int, skipblanks::Bool, dh::DLMHandler) | ||
all_ascii = (D <: Uint8) || (isascii(eol) && isascii(dlm) && (!allow_quote || isascii(qchar)) && (!allow_comments || isascii(cchar))) | ||
(T <: UTF8String) && all_ascii && (return dlm_parse(dbuff.data, uint8(eol), uint8(dlm), uint8(qchar), uint8(cchar), ign_adj_dlm, allow_quote, allow_comments, skipstart, skipblanks, dh)) | ||
ncols = nrows = col = 0 | ||
is_default_dlm = (dlm == invalid_dlm % D) | ||
is_default_dlm = (dlm == uint32(invalid_dlm) % D) | ||
error_str = "" | ||
# 0: begin field, 1: quoted field, 2: unquoted field, 3: second quote (could either be end of field or escape character), 4: comment, 5: skipstart | ||
state = (skipstart > 0) ? 5 : 0 | ||
|
@@ -350,16 +347,16 @@ function dlm_parse{T,D}(dbuff::T, eol::D, dlm::D, qchar::D, cchar::D, ign_adj_dl | |
was_cr = false | ||
while idx <= slen | ||
val,idx = next(dbuff, idx) | ||
if (is_eol = (val == eol)) | ||
if (is_eol = (char(val) == char(eol))) | ||
is_dlm = is_comment = is_cr = is_quote = false | ||
elseif (is_dlm = (is_default_dlm ? in(val, _default_delims) : (val == dlm))) | ||
elseif (is_dlm = (is_default_dlm ? in(char(val), _default_delims) : (char(val) == char(dlm)))) | ||
is_comment = is_cr = is_quote = false | ||
elseif (is_quote = (val == qchar)) | ||
elseif (is_quote = (char(val) == char(qchar))) | ||
is_comment = is_cr = false | ||
elseif (is_comment = (val == cchar)) | ||
elseif (is_comment = (char(val) == char(cchar))) | ||
is_cr = false | ||
else | ||
is_cr = (eol == '\n') && (val == '\r') | ||
is_cr = (char(eol) == '\n') && (char(val) == '\r') | ||
end | ||
|
||
if 2 == state # unquoted field | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -317,4 +317,3 @@ function readuntil(io::IOBuffer, delim::Uint8) | |
end | ||
A | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wasn't this supposed to be
Char
+Int
=Char
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like that's how it is implemented
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 816ee25