Class: Tk::EncodedString

Inherits:
String
  • Object
show all
Defined in:
lib/tk/encodedstr.rb

Direct Known Subclasses

BinaryString, UTF8_String

Constant Summary collapse

Encoding =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str, enc = nil) ⇒ EncodedString

Returns a new instance of EncodedString.


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/tk/encodedstr.rb', line 69

def initialize(str, enc = nil)
  super(str)
  # @encoding = ( enc ||
  #              ((self.class::Encoding)?
  #                  self.class::Encoding : Tk.encoding_system) )
  enc ||= (self.class::Encoding)?
                     self.class::Encoding :
                     ((Tk.encoding)? Tk.encoding : Tk.encoding_system)
  if TkCore::WITH_ENCODING
    unless encobj = Tk::Encoding::ENCODING_TABLE.get_obj(enc)
      fail ArgumentError, "unsupported Tk encoding '#{enc}'"
    end
    self.force_encoding(encobj)
  else
    @encoding = enc
  end
end

Class Method Details

.new_with_utf_backslash(str, enc = nil) ⇒ Object


61
62
63
# File 'lib/tk/encodedstr.rb', line 61

def self.new_with_utf_backslash(str, enc = nil)
  self.new('', enc).replace(self.subst_utf_backslash(str))
end

.new_without_utf_backslash(str, enc = nil) ⇒ Object


65
66
67
# File 'lib/tk/encodedstr.rb', line 65

def self.new_without_utf_backslash(str, enc = nil)
  self.new('', enc).replace(str)
end

.subst_tk_backslash(str) ⇒ Object


22
23
24
# File 'lib/tk/encodedstr.rb', line 22

def self.subst_tk_backslash(str)
  TclTkLib._subst_Tcl_backslash(str)
end

.subst_utf_backslash(str) ⇒ Object


14
15
16
17
# File 'lib/tk/encodedstr.rb', line 14

def self.subst_utf_backslash(str)
  # str.gsub(/\\u([0-9A-Fa-f]{1,4})/){[$1.hex].pack('U')}
  TclTkLib._subst_UTF_backslash(str)
end

.to_backslash_sequence(str) ⇒ Object


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/tk/encodedstr.rb', line 39

def self.to_backslash_sequence(str)
  str.unpack('U*').collect{|c|
    if c <= 0x1F  # control character
      case c
      when 0x07; '\a'
      when 0x08; '\b'
      when 0x09; '\t'
      when 0x0a; '\n'
      when 0x0b; '\v'
      when 0x0c; '\f'
      when 0x0d; '\r'
      else
        format('\x%02X', c)
      end
    elsif c <= 0xFF  # ascii character
      c.chr
    else
      format('\u%X', c)
    end
  }.join('')
end

.utf_backslash(str) ⇒ Object


18
19
20
# File 'lib/tk/encodedstr.rb', line 18

def self.utf_backslash(str)
  self.subst_utf_backslash(str)
end

.utf_to_backslash(str) ⇒ Object


35
36
37
# File 'lib/tk/encodedstr.rb', line 35

def self.utf_to_backslash(str)
  self.utf_to_backslash_sequence(str)
end

.utf_to_backslash_sequence(str) ⇒ Object


26
27
28
29
30
31
32
33
34
# File 'lib/tk/encodedstr.rb', line 26

def self.utf_to_backslash_sequence(str)
  str.unpack('U*').collect{|c|
    if c <= 0xFF  # ascii character
      c.chr
    else
      format('\u%X', c)
    end
  }.join('')
end

Instance Method Details

#__encodingObject


89
# File 'lib/tk/encodedstr.rb', line 89

alias __encoding   encoding

#__instance_evalObject


104
# File 'lib/tk/encodedstr.rb', line 104

alias __instance_eval         instance_eval

#__instance_variable_getObject

wrapper methods for compatibility


102
# File 'lib/tk/encodedstr.rb', line 102

alias __instance_variable_get instance_variable_get

#__instance_variable_setObject


103
# File 'lib/tk/encodedstr.rb', line 103

alias __instance_variable_set instance_variable_set

#__instance_variablesObject


105
# File 'lib/tk/encodedstr.rb', line 105

alias __instance_variables    instance_variables

#encodingObject Also known as: encoding_obj


90
91
92
# File 'lib/tk/encodedstr.rb', line 90

def encoding
  Tk::Encoding::ENCODING_TABLE.get_name(super())
end

#instance_eval(*args, &b) ⇒ Object


128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/tk/encodedstr.rb', line 128

def instance_eval(*args, &b)
  old_enc = @encoding = self.encoding

  ret = super(*args, &b)

  if @encoding
    if @encoding != old_enc
      # modified by user
      self.force_encoding(@encoding)
    end
    remove_instance_variable(:@encoding)
  else
    begin
      remove_instance_variable(:@encoding)
      # user sets to nil -> use current default
      self.force_encoding(Tk.encoding)
    rescue NameError
      # removed by user -> ignore, because user don't use @encoding
    end
  end
  ret
end

#instance_variable_get(key) ⇒ Object


107
108
109
110
111
112
113
# File 'lib/tk/encodedstr.rb', line 107

def instance_variable_get(key)
  if (key.to_s == '@encoding')
    self.encoding
  else
    super(key)
  end
end

#instance_variable_set(key, value) ⇒ Object


115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/tk/encodedstr.rb', line 115

def instance_variable_set(key, value)
  if (key.to_s == '@encoding')
    if value
      self.force_encoding(value)
    else
      self.force_encoding(Tk::Encoding::UNKNOWN)
    end
    value
  else
    super(key, value)
  end
end

#instance_variablesObject


152
153
154
155
156
# File 'lib/tk/encodedstr.rb', line 152

def instance_variables
  ret = super()
  ret << :@encoding  # fake !!
  ret
end