Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/string_cmp_pl.rb,
lib/string_case_pl.rb

Constant Summary collapse

PL_LETTER_ORDER =
{}
UTF_8_ENCODING =
"UTF-8"
ISO_8859_2_ENCODING =
"ISO-8859-2"
WINDOWS_CP1250_ENCODING =
"Windows-1250"
PL_UTF_8_LOWER =

old up/down-case without Polish transcodings

("\xc4\x85\xc5\xbc\xc5\x9b\xc5\xba\xc4\x99" +
"\xc4\x87\xc5\x84\xc3\xb3\xc5\x82").force_encoding(UTF_8_ENCODING)
PL_UTF_8_UPPER =
("\xc4\x84\xc5\xbb\xc5\x9a\xc5\xb9\xc4\x98" +
"\xc4\x86\xc5\x83\xc3\x93\xc5\x81").force_encoding(UTF_8_ENCODING)
PL_ISO_8859_2_LOWER =
PL_UTF_8_LOWER.encode(ISO_8859_2_ENCODING)
PL_ISO_8859_2_UPPER =
PL_UTF_8_UPPER.encode(ISO_8859_2_ENCODING)
PL_WINDOWS_1250_LOWER =
PL_UTF_8_LOWER.encode(WINDOWS_CP1250_ENCODING)
PL_WINDOWS_1250_UPPER =
PL_UTF_8_UPPER.encode(WINDOWS_CP1250_ENCODING)

Instance Method Summary collapse

Instance Method Details

#<=>(other) ⇒ Object



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
# File 'lib/string_cmp_pl.rb', line 15

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

#capitalizeObject



57
58
59
60
61
62
# File 'lib/string_case_pl.rb', line 57

def capitalize
  s = self.dup
  s[0..0] = s[0..0].upcase
  s[1..-1] = s[1..-1].downcase if s[1]
  s
end

#capitalize!Object



64
65
66
67
68
# File 'lib/string_case_pl.rb', line 64

def capitalize!
  old = self.dup
  self[0..-1] = self.capitalize
  self unless old == self
end

#downcaseObject



33
34
35
36
37
# File 'lib/string_case_pl.rb', line 33

def downcase
  str = self.dup
  str.downcase!
  str
end

#downcase!Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/string_case_pl.rb', line 21

def downcase!
  case self.encoding.name
  when UTF_8_ENCODING
    self.tr!(PL_UTF_8_UPPER,PL_UTF_8_LOWER)
  when ISO_8859_2_ENCODING
    self.tr!(PL_ISO_8859_2_UPPER, PL_ISO_8859_2_LOWER)
  when WINDOWS_CP1250_ENCODING
    self.tr!(PL_WINDOWS_1250_UPPER, PL_WINDOWS_1250_LOWER)
  end
  self.old_downcase_wo_pl
end

#old_capitalize_wo_plObject



19
# File 'lib/string_case_pl.rb', line 19

alias old_capitalize_wo_pl capitalize

#old_cmp_wo_plObject



13
# File 'lib/string_cmp_pl.rb', line 13

alias old_cmp_wo_pl <=>

#old_downcase_wo_plObject



17
# File 'lib/string_case_pl.rb', line 17

alias old_downcase_wo_pl downcase!

#old_upcase_wo_plObject



18
# File 'lib/string_case_pl.rb', line 18

alias old_upcase_wo_pl upcase!

#upcaseObject



51
52
53
54
55
# File 'lib/string_case_pl.rb', line 51

def upcase
  str = self.dup
  str.upcase!
  str
end

#upcase!Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/string_case_pl.rb', line 39

def upcase!
  case self.encoding.name
  when UTF_8_ENCODING
    self.tr!(PL_UTF_8_LOWER, PL_UTF_8_UPPER)
  when ISO_8859_2_ENCODING
    self.tr!(PL_ISO_8859_2_LOWER, PL_ISO_8859_2_UPPER)
  when WINDOWS_CP1250_ENCODING
    self.tr!(PL_WINDOWS_1250_LOWER, PL_WINDOWS_1250_UPPER)
  end
  self.old_upcase_wo_pl
end