-
Notifications
You must be signed in to change notification settings - Fork 3
/
caseless_strings.jl
53 lines (42 loc) · 1.91 KB
/
caseless_strings.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
# **Caseless strings
#
# We need caseless strings for the dictionaries, in order to allow
# caseless comparisons.
#
#
export CaselessString
"""
A string which ignores case
"""
struct CaselessString <: AbstractString
actual_string::String
end
Base.:(==)(a::CaselessString,b::CaselessString) = lowercase(a)==lowercase(b)
Base.:(==)(a::SubString{CaselessString},b::SubString{CaselessString}) = lowercase(a)==lowercase(b)
# == CaselessString == #
Base.lowercase(a::CaselessString) = lowercase(a.actual_string)
Base.uppercase(a::CaselessString) = uppercase(a.actual_string)
Base.:(==)(a::CaselessString,b::AbstractString) = lowercase(a) == lowercase(b)
Base.:(==)(a::AbstractString,b::CaselessString) = lowercase(a) == lowercase(b)
Base.:(==)(a::SubString{CaselessString},b::AbstractString) = lowercase(a) == lowercase(b)
Base.:(==)(a::SubString{CaselessString},b::CaselessString) = lowercase(a) == lowercase(b)
Base.:(==)(a::AbstractString,b::SubString{CaselessString}) = lowercase(a) == lowercase(b)
Base.:(==)(a::CaselessString,b::SubString{CaselessString}) = lowercase(a) == lowercase(b)
Base.iterate(c::CaselessString) = iterate(c.actual_string)
Base.iterate(c::CaselessString,s::Integer) = iterate(c.actual_string,s)
Base.ncodeunits(c::CaselessString) = ncodeunits(c.actual_string)
Base.isvalid(c::CaselessString,i::Integer) = isvalid(c.actual_string,i)
Base.codeunit(c::CaselessString) = codeunit(c.actual_string)
Base.show(io::IO,c::CaselessString) = show(io,c.actual_string)
#== A caseless string should match both upper and lower case ==#
Base.getindex(d::Dict{String,V} where V,key::Union{CaselessString,SubString{CaselessString}}) = begin
for (k,v) in d
if lowercase(k) == lowercase(key)
return v
end
end
KeyError("$key not found")
end
Base.haskey(d::Dict{CaselessString,V} where V,key) = lowercase(key) in keys(d)
#
Base.hash(c::CaselessString,h::UInt) = hash(lowercase(c.actual_string),h)