forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathascii.jl
120 lines (107 loc) · 3.08 KB
/
ascii.jl
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
## from base/boot.jl:
#
# immutable ASCIIString <: DirectIndexString
# data::Array{Uint8,1}
# end
#
## required core functionality ##
endof(s::ASCIIString) = length(s.data)
getindex(s::ASCIIString, i::Int) = (x=s.data[i]; x < 0x80 ? char(x) : '\ufffd')
## overload methods for efficiency ##
sizeof(s::ASCIIString) = sizeof(s.data)
getindex(s::ASCIIString, r::Vector) = ASCIIString(getindex(s.data,r))
getindex(s::ASCIIString, r::UnitRange{Int}) = ASCIIString(getindex(s.data,r))
getindex(s::ASCIIString, indx::AbstractVector{Int}) = ASCIIString(s.data[indx])
search(s::ASCIIString, c::Char, i::Integer) = c < 0x80 ? search(s.data,uint8(c),i) : 0
rsearch(s::ASCIIString, c::Char, i::Integer) = c < 0x80 ? rsearch(s.data,uint8(c),i) : 0
function string(c::ASCIIString...)
if length(c) == 1
return c[1]
end
n = 0
for s in c
n += length(s.data)
end
v = Array(Uint8,n)
o = 1
for s in c
ls = length(s.data)
unsafe_copy!(v, o, s.data, 1, ls)
o += ls
end
ASCIIString(v)
end
function ucfirst(s::ASCIIString)
if length(s) > 0 && 'a' <= s[1] <= 'z'
t = ASCIIString(copy(s.data))
t.data[1] -= 32
return t
end
return s
end
function lcfirst(s::ASCIIString)
if length(s) > 0 && 'A' <= s[1] <= 'Z'
t = ASCIIString(copy(s.data))
t.data[1] += 32
return t
end
return s
end
function uppercase(s::ASCIIString)
d = s.data
for i = 1:length(d)
if 'a' <= d[i] <= 'z'
td = copy(d)
for j = i:length(td)
if 'a' <= td[j] <= 'z'
td[j] -= 32
end
end
return ASCIIString(td)
end
end
return s
end
function lowercase(s::ASCIIString)
d = s.data
for i = 1:length(d)
if 'A' <= d[i] <= 'Z'
td = copy(d)
for j = i:length(td)
if 'A' <= td[j] <= 'Z'
td[j] += 32
end
end
return ASCIIString(td)
end
end
return s
end
reverse(s::ASCIIString) = ASCIIString(reverse(s.data))
## outputing ASCII strings ##
print(io::IO, s::ASCIIString) = (write(io, s);nothing)
write(io::IO, s::ASCIIString) = write(io, s.data)
## transcoding to ASCII ##
ascii(x) = convert(ASCIIString, x)
convert(::Type{ASCIIString}, s::ASCIIString) = s
convert(::Type{ASCIIString}, s::UTF8String) = ascii(s.data)
convert(::Type{ASCIIString}, a::Array{Uint8,1}) = is_valid_ascii(a) ? ASCIIString(a) : error("invalid ASCII sequence")
function convert(::Type{ASCIIString}, a::Array{Uint8,1}, invalids_as::ASCIIString)
l = length(a)
idx = 1
iscopy = false
while idx <= l
(a[idx] < 0x80) && (idx +=1; continue)
!iscopy && (a = copy(a); iscopy = true)
endn = idx
while endn <= l
(a[endn] < 0x80) && break
endn += 1
end
(endn > idx) && (endn -= 1)
splice!(a, idx:endn, invalids_as.data)
l = length(a)
end
convert(ASCIIString, a)
end
convert(::Type{ASCIIString}, s::String) = ascii(bytestring(s))