forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
char.j
72 lines (60 loc) · 2.58 KB
/
char.j
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
## char conversions ##
convert(::Type{Char}, x::Bool ) = box(Char,sext32(unbox8(x)))
convert(::Type{Char}, x::Int8 ) = box(Char,sext32(unbox8(x)))
convert(::Type{Char}, x::Uint8 ) = box(Char,zext32(unbox8(x)))
convert(::Type{Char}, x::Int16 ) = box(Char,sext32(unbox16(x)))
convert(::Type{Char}, x::Uint16 ) = box(Char,zext32(unbox16(x)))
convert(::Type{Char}, x::Int32 ) = box(Char,unbox32(x))
convert(::Type{Char}, x::Uint32 ) = box(Char,unbox32(x))
convert(::Type{Char}, x::Int64 ) = box(Char,trunc32(unbox64(x)))
convert(::Type{Char}, x::Uint64 ) = box(Char,trunc32(unbox64(x)))
convert(::Type{Char}, x::Float32) = box(Char,fptoui32(unbox32(x)))
convert(::Type{Char}, x::Float64) = box(Char,fptoui32(unbox64(x)))
char(x) = convert(Char, x)
function safe_char(x)
c = char(x)
if '\ud800' <= c <= '\udfff' || '\U10ffff' < c
error("invalid Unicode code point: U+", hex(c))
end
c
end
int(x::Char) = int32(x)
uint(x::Char) = uint32(x)
float(x::Char) = float32(x)
## char promotions ##
promote_rule(::Type{Char}, ::Type{Int8}) = Int32
promote_rule(::Type{Char}, ::Type{Uint8}) = Int32
promote_rule(::Type{Char}, ::Type{Int16}) = Int32
promote_rule(::Type{Char}, ::Type{Uint16}) = Int32
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
## character operations & comparisons ##
-(x::Char) = -int32(x)
~(x::Char) = ~int32(x)
+(x::Char, y::Char) = int32(x) + int32(y)
-(x::Char, y::Char) = int32(x) - int32(y)
*(x::Char, y::Char) = int32(x) * int32(y)
div(x::Char, y::Char) = div(int32(x), int32(y))
fld(x::Char, y::Char) = div(int32(x), int32(y))
rem(x::Char, y::Char) = rem(int32(x), int32(y))
mod(x::Char, y::Char) = rem(int32(x), int32(y))
&(x::Char, y::Char) = int32(x) & int32(y)
|(x::Char, y::Char) = int32(x) | int32(y)
$(x::Char, y::Char) = int32(x) $ int32(y)
<<(x::Char, y::Int32) = int32(x) << y
>>(x::Char, y::Int32) = int32(x) >>> y
>>>(x::Char, y::Int32) = int32(x) >>> y
==(x::Char, y::Char) = uint32(x) == uint32(y)
<(x::Char, y::Char) = uint32(x) < uint32(y)
## traits ##
sizeof(::Type{Char}) = 4
## libc character class testing functions ##
for f = (:iswalnum, :iswalpha, :iswascii, :iswblank, :iswcntrl, :iswdigit,
:iswgraph, :iswhexnumber, :iswideogram, :iswlower, :iswnumber,
:iswphonogram, :iswprint, :iswpunct, :iswrune, :iswspace,
:iswspecial, :iswupper, :iswxdigit)
@eval ($f)(c::Char) = bool(ccall(dlsym(libc,$expr(:quote,f)),
Int32, (Char,), c))
end