blob: 797e514a4c454d39fa7a7d55c0faf2ed6dcf9ed8 (
plain)
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
|
#
# tk/encodedstr.rb : Tk::EncodedString class
#
require 'tk'
###########################################
# string with Tcl's encoding
###########################################
module Tk
class EncodedString < String
Encoding = nil
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
def self.utf_backslash(str)
self.subst_utf_backslash(str)
end
def self.subst_tk_backslash(str)
TclTkLib._subst_Tcl_backslash(str)
end
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
def self.utf_to_backslash(str)
self.utf_to_backslash_sequence(str)
end
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
def self.new_with_utf_backslash(str, enc = nil)
self.new('', enc).replace(self.subst_utf_backslash(str))
end
def self.new_without_utf_backslash(str, enc = nil)
self.new('', enc).replace(str)
end
def initialize(str, enc = nil)
super(str)
# @encoding = ( enc ||
# ((self.class::Encoding)?
# self.class::Encoding : Tk.encoding_system) )
@encoding = ( enc ||
((self.class::Encoding)?
self.class::Encoding :
((Tk.encoding)? Tk.encoding : Tk.encoding_system) ) )
end
attr_reader :encoding
end
# def Tk.EncodedString(str, enc = nil)
# Tk::EncodedString.new(str, enc)
# end
##################################
class BinaryString < EncodedString
Encoding = 'binary'.freeze
end
# def Tk.BinaryString(str)
# Tk::BinaryString.new(str)
# end
##################################
class UTF8_String < EncodedString
Encoding = 'utf-8'.freeze
def self.new(str)
super(self.subst_utf_backslash(str))
end
def to_backslash_sequence
Tk::EncodedString.utf_to_backslash_sequence(self)
end
alias to_backslash to_backslash_sequence
end
# def Tk.UTF8_String(str)
# Tk::UTF8_String.new(str)
# end
end
|