/
string_cmp_pl.rb
77 lines (73 loc) · 2.12 KB
/
string_cmp_pl.rb
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
# -*- coding: utf-8 -*-
require 'string_case_pl'
class String
PL_LETTER_ORDER = {}
("AĄBCĆDEĘFGHIJKLŁMNŃOÓPQRSŚTUVWXYZŹŻ" +
"aąbcćdeęfghijklłmnńoópqrsśtuvwxyzźż").force_encoding(UTF_8_ENCODING).
each_char.with_index do |char,index|
PL_LETTER_ORDER[char] = index
end
alias old_cmp_wo_pl <=>
def <=>(other)
if !other.is_a?(String)
old_cmp_wo_pl(other)
elsif self.encoding.name != UTF_8_ENCODING ||
other.encoding.name != UTF_8_ENCODING
case self.encoding.name
when UTF_8_ENCODING
case other.encoding.name
when ISO_8859_2_ENCODING,WINDOWS_CP1250_ENCODING
self.<=>(other.encode(UTF_8_ENCODING))
else
old_cmp_wo_pl(other)
end
when ISO_8859_2_ENCODING
case other.encoding.name
when UTF_8_ENCODING
self.encode(UTF_8_ENCODING) <=> other
when ISO_8859_2_ENCODING,WINDOWS_CP1250_ENCODING
self.encode(UTF_8_ENCODING) <=> other.encode(UTF_8_ENCODING)
else
old_cmp_wo_pl(other)
end
when WINDOWS_CP1250_ENCODING
case other.encoding.name
when UTF_8_ENCODING
self.encode(UTF_8_ENCODING) <=> other
when ISO_8859_2_ENCODING,WINDOWS_CP1250_ENCODING
self.encode(UTF_8_ENCODING) <=> other.encode(UTF_8_ENCODING)
else
old_cmp_wo_pl(other)
end
else
old_cmp_wo_pl(other)
end
else
self.each_char.with_index do |char,index|
position1 = PL_LETTER_ORDER[char]
position2 = PL_LETTER_ORDER[other[index]]
if position1 && position2
if position1 != position2
return (position1 < position2 ? -1 : 1)
end
else
if other[index]
if char.old_cmp_wo_pl(other[index]) != 0
return char.old_cmp_wo_pl(other[index])
end
else
return 1
end
end
end
# all equal, compare length
if self.length == other.length
return 0
elsif self.length < other.length
return -1
else
return 1
end
end
end
end