rbs 2.0.0 → 2.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/comments.yml +34 -0
- data/.github/workflows/ruby.yml +5 -0
- data/CHANGELOG.md +82 -0
- data/README.md +6 -1
- data/Rakefile +56 -21
- data/core/array.rbs +2866 -1086
- data/core/basic_object.rbs +150 -30
- data/core/binding.rbs +33 -0
- data/core/builtin.rbs +4 -4
- data/core/class.rbs +43 -5
- data/core/comparable.rbs +57 -0
- data/core/complex.rbs +170 -4
- data/core/constants.rbs +51 -0
- data/core/deprecated.rbs +7 -0
- data/core/dir.rbs +305 -20
- data/core/encoding.rbs +1214 -77
- data/core/enumerable.rbs +2173 -234
- data/core/enumerator.rbs +448 -182
- data/core/env.rbs +448 -1
- data/core/errno.rbs +1 -10
- data/core/errors.rbs +152 -2
- data/core/exception.rbs +201 -127
- data/core/false_class.rbs +27 -0
- data/core/fiber.rbs +118 -37
- data/core/fiber_error.rbs +8 -9
- data/core/file.rbs +1060 -142
- data/core/file_test.rbs +287 -32
- data/core/float.rbs +776 -300
- data/core/gc.rbs +185 -34
- data/core/global_variables.rbs +5 -1
- data/core/hash.rbs +1582 -649
- data/core/integer.rbs +974 -204
- data/core/io/buffer.rbs +710 -0
- data/core/io/wait.rbs +29 -8
- data/core/io.rbs +2438 -417
- data/core/kernel.rbs +2319 -318
- data/core/marshal.rbs +37 -2
- data/core/match_data.rbs +123 -6
- data/core/math.rbs +126 -6
- data/core/method.rbs +226 -102
- data/core/module.rbs +421 -45
- data/core/nil_class.rbs +64 -0
- data/core/numeric.rbs +620 -142
- data/core/object.rbs +453 -81
- data/core/object_space.rbs +92 -2
- data/core/proc.rbs +482 -285
- data/core/process.rbs +443 -34
- data/core/ractor.rbs +232 -9
- data/core/random.rbs +151 -52
- data/core/range.rbs +885 -160
- data/core/rational.rbs +122 -6
- data/core/rb_config.rbs +14 -4
- data/core/refinement.rbs +44 -0
- data/core/regexp.rbs +156 -14
- data/core/ruby_vm.rbs +42 -3
- data/core/signal.rbs +78 -39
- data/core/string.rbs +2123 -567
- data/core/string_io.rbs +204 -0
- data/core/struct.rbs +283 -28
- data/core/symbol.rbs +304 -30
- data/core/thread.rbs +1288 -688
- data/core/thread_group.rbs +66 -10
- data/core/time.rbs +643 -217
- data/core/trace_point.rbs +100 -12
- data/core/true_class.rbs +24 -0
- data/core/unbound_method.rbs +73 -7
- data/core/warning.rbs +37 -12
- data/docs/CONTRIBUTING.md +40 -34
- data/docs/stdlib.md +3 -102
- data/docs/syntax.md +54 -11
- data/ext/rbs_extension/extconf.rb +1 -0
- data/ext/rbs_extension/lexer.h +5 -0
- data/ext/rbs_extension/lexstate.c +6 -0
- data/ext/rbs_extension/parser.c +85 -10
- data/ext/rbs_extension/ruby_objs.c +4 -2
- data/ext/rbs_extension/ruby_objs.h +2 -2
- data/goodcheck.yml +0 -11
- data/lib/rbs/annotate/annotations.rb +197 -0
- data/lib/rbs/annotate/formatter.rb +80 -0
- data/lib/rbs/annotate/rdoc_annotator.rb +398 -0
- data/lib/rbs/annotate/rdoc_source.rb +120 -0
- data/lib/rbs/annotate.rb +6 -0
- data/lib/rbs/ast/members.rb +21 -13
- data/lib/rbs/buffer.rb +17 -11
- data/lib/rbs/cli.rb +48 -1
- data/lib/rbs/definition_builder/method_builder.rb +28 -16
- data/lib/rbs/definition_builder.rb +6 -2
- data/lib/rbs/environment.rb +8 -4
- data/lib/rbs/location_aux.rb +12 -0
- data/lib/rbs/namespace.rb +1 -1
- data/lib/rbs/prototype/rb.rb +12 -0
- data/lib/rbs/type_alias_regularity.rb +6 -4
- data/lib/rbs/type_name.rb +1 -1
- data/lib/rbs/types.rb +1 -1
- data/lib/rbs/validator.rb +6 -0
- data/lib/rbs/version.rb +1 -1
- data/lib/rbs/writer.rb +54 -4
- data/lib/rbs.rb +0 -2
- data/schema/typeParam.json +3 -3
- data/sig/annotate/annotations.rbs +102 -0
- data/sig/annotate/formatter.rbs +24 -0
- data/sig/annotate/rdoc_annotater.rbs +82 -0
- data/sig/annotate/rdoc_source.rbs +30 -0
- data/sig/buffer.rbs +6 -2
- data/sig/cli.rbs +2 -0
- data/sig/collection/{collections.rbs → sources.rbs} +0 -0
- data/sig/location.rbs +6 -0
- data/sig/members.rbs +24 -18
- data/sig/method_builder.rbs +5 -4
- data/sig/method_types.rbs +5 -1
- data/sig/polyfill.rbs +78 -0
- data/sig/validator.rbs +3 -1
- data/sig/writer.rbs +79 -2
- data/stdlib/abbrev/0/abbrev.rbs +6 -0
- data/stdlib/abbrev/0/array.rbs +26 -0
- data/stdlib/base64/0/base64.rbs +31 -0
- data/stdlib/benchmark/0/benchmark.rbs +74 -3
- data/stdlib/bigdecimal/0/big_decimal.rbs +614 -165
- data/stdlib/bigdecimal-math/0/big_math.rbs +41 -64
- data/stdlib/cgi/0/core.rbs +649 -21
- data/stdlib/coverage/0/coverage.rbs +164 -2
- data/stdlib/csv/0/csv.rbs +2862 -398
- data/stdlib/date/0/date.rbs +483 -25
- data/stdlib/date/0/date_time.rbs +187 -12
- data/stdlib/dbm/0/dbm.rbs +152 -17
- data/stdlib/digest/0/digest.rbs +146 -0
- data/stdlib/erb/0/erb.rbs +65 -245
- data/stdlib/fiber/0/fiber.rbs +73 -91
- data/stdlib/fileutils/0/fileutils.rbs +301 -1
- data/stdlib/find/0/find.rbs +9 -0
- data/stdlib/forwardable/0/forwardable.rbs +65 -1
- data/stdlib/io-console/0/io-console.rbs +227 -15
- data/stdlib/ipaddr/0/ipaddr.rbs +161 -0
- data/stdlib/json/0/json.rbs +1147 -145
- data/stdlib/logger/0/formatter.rbs +24 -0
- data/stdlib/logger/0/log_device.rbs +64 -0
- data/stdlib/logger/0/logger.rbs +165 -13
- data/stdlib/logger/0/period.rbs +10 -0
- data/stdlib/logger/0/severity.rbs +26 -0
- data/stdlib/monitor/0/monitor.rbs +163 -0
- data/stdlib/mutex_m/0/mutex_m.rbs +35 -6
- data/stdlib/net-http/0/manifest.yaml +1 -0
- data/stdlib/net-http/0/net-http.rbs +1513 -683
- data/stdlib/nkf/0/nkf.rbs +372 -0
- data/stdlib/objspace/0/objspace.rbs +149 -90
- data/stdlib/openssl/0/openssl.rbs +8108 -71
- data/stdlib/optparse/0/optparse.rbs +487 -19
- data/stdlib/pathname/0/pathname.rbs +425 -124
- data/stdlib/prettyprint/0/prettyprint.rbs +120 -99
- data/stdlib/prime/0/integer-extension.rbs +20 -2
- data/stdlib/prime/0/prime.rbs +88 -21
- data/stdlib/pstore/0/pstore.rbs +102 -0
- data/stdlib/pty/0/pty.rbs +64 -14
- data/stdlib/resolv/0/resolv.rbs +420 -31
- data/stdlib/rubygems/0/basic_specification.rbs +4 -1
- data/stdlib/rubygems/0/config_file.rbs +33 -1
- data/stdlib/rubygems/0/dependency_installer.rbs +4 -3
- data/stdlib/rubygems/0/installer.rbs +13 -1
- data/stdlib/rubygems/0/path_support.rbs +4 -1
- data/stdlib/rubygems/0/platform.rbs +5 -1
- data/stdlib/rubygems/0/request_set.rbs +44 -2
- data/stdlib/rubygems/0/requirement.rbs +65 -2
- data/stdlib/rubygems/0/rubygems.rbs +407 -0
- data/stdlib/rubygems/0/source_list.rbs +13 -0
- data/stdlib/rubygems/0/specification.rbs +21 -1
- data/stdlib/rubygems/0/stream_ui.rbs +3 -1
- data/stdlib/rubygems/0/uninstaller.rbs +8 -1
- data/stdlib/rubygems/0/version.rbs +60 -157
- data/stdlib/securerandom/0/securerandom.rbs +44 -0
- data/stdlib/set/0/set.rbs +423 -109
- data/stdlib/shellwords/0/shellwords.rbs +55 -77
- data/stdlib/singleton/0/singleton.rbs +20 -0
- data/stdlib/socket/0/addrinfo.rbs +210 -9
- data/stdlib/socket/0/basic_socket.rbs +103 -11
- data/stdlib/socket/0/ip_socket.rbs +31 -9
- data/stdlib/socket/0/socket.rbs +586 -38
- data/stdlib/socket/0/tcp_server.rbs +22 -2
- data/stdlib/socket/0/tcp_socket.rbs +12 -1
- data/stdlib/socket/0/udp_socket.rbs +25 -2
- data/stdlib/socket/0/unix_server.rbs +22 -2
- data/stdlib/socket/0/unix_socket.rbs +45 -5
- data/stdlib/strscan/0/string_scanner.rbs +210 -9
- data/stdlib/tempfile/0/tempfile.rbs +58 -10
- data/stdlib/time/0/time.rbs +208 -116
- data/stdlib/timeout/0/timeout.rbs +10 -0
- data/stdlib/tmpdir/0/tmpdir.rbs +13 -4
- data/stdlib/tsort/0/cyclic.rbs +1 -0
- data/stdlib/tsort/0/interfaces.rbs +1 -0
- data/stdlib/tsort/0/tsort.rbs +42 -0
- data/stdlib/uri/0/common.rbs +57 -8
- data/stdlib/uri/0/file.rbs +55 -109
- data/stdlib/uri/0/ftp.rbs +6 -3
- data/stdlib/uri/0/generic.rbs +558 -329
- data/stdlib/uri/0/http.rbs +60 -114
- data/stdlib/uri/0/https.rbs +8 -102
- data/stdlib/uri/0/ldap.rbs +143 -137
- data/stdlib/uri/0/ldaps.rbs +8 -102
- data/stdlib/uri/0/mailto.rbs +3 -0
- data/stdlib/uri/0/rfc2396_parser.rbs +66 -26
- data/stdlib/uri/0/ws.rbs +6 -3
- data/stdlib/uri/0/wss.rbs +5 -3
- data/stdlib/yaml/0/dbm.rbs +151 -87
- data/stdlib/yaml/0/store.rbs +6 -0
- data/stdlib/zlib/0/zlib.rbs +90 -31
- metadata +18 -6
- data/lib/rbs/location.rb +0 -221
- data/sig/char_scanner.rbs +0 -9
data/core/encoding.rbs
CHANGED
|
@@ -1,101 +1,378 @@
|
|
|
1
|
+
# <!-- rdoc-file=encoding.c -->
|
|
2
|
+
# An Encoding instance represents a character encoding usable in Ruby. It is
|
|
3
|
+
# defined as a constant under the Encoding namespace. It has a name and
|
|
4
|
+
# optionally, aliases:
|
|
5
|
+
#
|
|
6
|
+
# Encoding::ISO_8859_1.name
|
|
7
|
+
# #=> "ISO-8859-1"
|
|
8
|
+
#
|
|
9
|
+
# Encoding::ISO_8859_1.names
|
|
10
|
+
# #=> ["ISO-8859-1", "ISO8859-1"]
|
|
11
|
+
#
|
|
12
|
+
# Ruby methods dealing with encodings return or accept Encoding instances as
|
|
13
|
+
# arguments (when a method accepts an Encoding instance as an argument, it can
|
|
14
|
+
# be passed an Encoding name or alias instead).
|
|
15
|
+
#
|
|
16
|
+
# "some string".encoding
|
|
17
|
+
# #=> #<Encoding:UTF-8>
|
|
18
|
+
#
|
|
19
|
+
# string = "some string".encode(Encoding::ISO_8859_1)
|
|
20
|
+
# #=> "some string"
|
|
21
|
+
# string.encoding
|
|
22
|
+
# #=> #<Encoding:ISO-8859-1>
|
|
23
|
+
#
|
|
24
|
+
# "some string".encode "ISO-8859-1"
|
|
25
|
+
# #=> "some string"
|
|
26
|
+
#
|
|
27
|
+
# Encoding::ASCII_8BIT is a special encoding that is usually used for a byte
|
|
28
|
+
# string, not a character string. But as the name insists, its characters in the
|
|
29
|
+
# range of ASCII are considered as ASCII characters. This is useful when you
|
|
30
|
+
# use ASCII-8BIT characters with other ASCII compatible characters.
|
|
31
|
+
#
|
|
32
|
+
# ## Changing an encoding
|
|
33
|
+
#
|
|
34
|
+
# The associated Encoding of a String can be changed in two different ways.
|
|
35
|
+
#
|
|
36
|
+
# First, it is possible to set the Encoding of a string to a new Encoding
|
|
37
|
+
# without changing the internal byte representation of the string, with
|
|
38
|
+
# String#force_encoding. This is how you can tell Ruby the correct encoding of a
|
|
39
|
+
# string.
|
|
40
|
+
#
|
|
41
|
+
# string
|
|
42
|
+
# #=> "R\xC3\xA9sum\xC3\xA9"
|
|
43
|
+
# string.encoding
|
|
44
|
+
# #=> #<Encoding:ISO-8859-1>
|
|
45
|
+
# string.force_encoding(Encoding::UTF_8)
|
|
46
|
+
# #=> "R\u00E9sum\u00E9"
|
|
47
|
+
#
|
|
48
|
+
# Second, it is possible to transcode a string, i.e. translate its internal byte
|
|
49
|
+
# representation to another encoding. Its associated encoding is also set to the
|
|
50
|
+
# other encoding. See String#encode for the various forms of transcoding, and
|
|
51
|
+
# the Encoding::Converter class for additional control over the transcoding
|
|
52
|
+
# process.
|
|
53
|
+
#
|
|
54
|
+
# string
|
|
55
|
+
# #=> "R\u00E9sum\u00E9"
|
|
56
|
+
# string.encoding
|
|
57
|
+
# #=> #<Encoding:UTF-8>
|
|
58
|
+
# string = string.encode!(Encoding::ISO_8859_1)
|
|
59
|
+
# #=> "R\xE9sum\xE9"
|
|
60
|
+
# string.encoding
|
|
61
|
+
# #=> #<Encoding::ISO-8859-1>
|
|
62
|
+
#
|
|
63
|
+
# ## Script encoding
|
|
64
|
+
#
|
|
65
|
+
# All Ruby script code has an associated Encoding which any String literal
|
|
66
|
+
# created in the source code will be associated to.
|
|
67
|
+
#
|
|
68
|
+
# The default script encoding is Encoding::UTF_8 after v2.0, but it can be
|
|
69
|
+
# changed by a magic comment on the first line of the source code file (or
|
|
70
|
+
# second line, if there is a shebang line on the first). The comment must
|
|
71
|
+
# contain the word `coding` or `encoding`, followed by a colon, space and the
|
|
72
|
+
# Encoding name or alias:
|
|
73
|
+
#
|
|
74
|
+
# # encoding: UTF-8
|
|
75
|
+
#
|
|
76
|
+
# "some string".encoding
|
|
77
|
+
# #=> #<Encoding:UTF-8>
|
|
78
|
+
#
|
|
79
|
+
# The `__ENCODING__` keyword returns the script encoding of the file which the
|
|
80
|
+
# keyword is written:
|
|
81
|
+
#
|
|
82
|
+
# # encoding: ISO-8859-1
|
|
83
|
+
#
|
|
84
|
+
# __ENCODING__
|
|
85
|
+
# #=> #<Encoding:ISO-8859-1>
|
|
86
|
+
#
|
|
87
|
+
# `ruby -K` will change the default locale encoding, but this is not
|
|
88
|
+
# recommended. Ruby source files should declare its script encoding by a magic
|
|
89
|
+
# comment even when they only depend on US-ASCII strings or regular expressions.
|
|
90
|
+
#
|
|
91
|
+
# ## Locale encoding
|
|
92
|
+
#
|
|
93
|
+
# The default encoding of the environment. Usually derived from locale.
|
|
94
|
+
#
|
|
95
|
+
# see Encoding.locale_charmap, Encoding.find('locale')
|
|
96
|
+
#
|
|
97
|
+
# ## Filesystem encoding
|
|
98
|
+
#
|
|
99
|
+
# The default encoding of strings from the filesystem of the environment. This
|
|
100
|
+
# is used for strings of file names or paths.
|
|
101
|
+
#
|
|
102
|
+
# see Encoding.find('filesystem')
|
|
103
|
+
#
|
|
104
|
+
# ## External encoding
|
|
105
|
+
#
|
|
106
|
+
# Each IO object has an external encoding which indicates the encoding that Ruby
|
|
107
|
+
# will use to read its data. By default Ruby sets the external encoding of an IO
|
|
108
|
+
# object to the default external encoding. The default external encoding is set
|
|
109
|
+
# by locale encoding or the interpreter `-E` option. Encoding.default_external
|
|
110
|
+
# returns the current value of the external encoding.
|
|
111
|
+
#
|
|
112
|
+
# ENV["LANG"]
|
|
113
|
+
# #=> "UTF-8"
|
|
114
|
+
# Encoding.default_external
|
|
115
|
+
# #=> #<Encoding:UTF-8>
|
|
116
|
+
#
|
|
117
|
+
# $ ruby -E ISO-8859-1 -e "p Encoding.default_external"
|
|
118
|
+
# #<Encoding:ISO-8859-1>
|
|
119
|
+
#
|
|
120
|
+
# $ LANG=C ruby -e 'p Encoding.default_external'
|
|
121
|
+
# #<Encoding:US-ASCII>
|
|
122
|
+
#
|
|
123
|
+
# The default external encoding may also be set through
|
|
124
|
+
# Encoding.default_external=, but you should not do this as strings created
|
|
125
|
+
# before and after the change will have inconsistent encodings. Instead use
|
|
126
|
+
# `ruby -E` to invoke ruby with the correct external encoding.
|
|
127
|
+
#
|
|
128
|
+
# When you know that the actual encoding of the data of an IO object is not the
|
|
129
|
+
# default external encoding, you can reset its external encoding with
|
|
130
|
+
# IO#set_encoding or set it at IO object creation (see IO.new options).
|
|
131
|
+
#
|
|
132
|
+
# ## Internal encoding
|
|
133
|
+
#
|
|
134
|
+
# To process the data of an IO object which has an encoding different from its
|
|
135
|
+
# external encoding, you can set its internal encoding. Ruby will use this
|
|
136
|
+
# internal encoding to transcode the data when it is read from the IO object.
|
|
137
|
+
#
|
|
138
|
+
# Conversely, when data is written to the IO object it is transcoded from the
|
|
139
|
+
# internal encoding to the external encoding of the IO object.
|
|
140
|
+
#
|
|
141
|
+
# The internal encoding of an IO object can be set with IO#set_encoding or at IO
|
|
142
|
+
# object creation (see IO.new options).
|
|
143
|
+
#
|
|
144
|
+
# The internal encoding is optional and when not set, the Ruby default internal
|
|
145
|
+
# encoding is used. If not explicitly set this default internal encoding is
|
|
146
|
+
# `nil` meaning that by default, no transcoding occurs.
|
|
147
|
+
#
|
|
148
|
+
# The default internal encoding can be set with the interpreter option `-E`.
|
|
149
|
+
# Encoding.default_internal returns the current internal encoding.
|
|
150
|
+
#
|
|
151
|
+
# $ ruby -e 'p Encoding.default_internal'
|
|
152
|
+
# nil
|
|
153
|
+
#
|
|
154
|
+
# $ ruby -E ISO-8859-1:UTF-8 -e "p [Encoding.default_external, \
|
|
155
|
+
# Encoding.default_internal]"
|
|
156
|
+
# [#<Encoding:ISO-8859-1>, #<Encoding:UTF-8>]
|
|
157
|
+
#
|
|
158
|
+
# The default internal encoding may also be set through
|
|
159
|
+
# Encoding.default_internal=, but you should not do this as strings created
|
|
160
|
+
# before and after the change will have inconsistent encodings. Instead use
|
|
161
|
+
# `ruby -E` to invoke ruby with the correct internal encoding.
|
|
162
|
+
#
|
|
163
|
+
# ## IO encoding example
|
|
164
|
+
#
|
|
165
|
+
# In the following example a UTF-8 encoded string "Ru00E9sumu00E9" is transcoded
|
|
166
|
+
# for output to ISO-8859-1 encoding, then read back in and transcoded to UTF-8:
|
|
167
|
+
#
|
|
168
|
+
# string = "R\u00E9sum\u00E9"
|
|
169
|
+
#
|
|
170
|
+
# open("transcoded.txt", "w:ISO-8859-1") do |io|
|
|
171
|
+
# io.write(string)
|
|
172
|
+
# end
|
|
173
|
+
#
|
|
174
|
+
# puts "raw text:"
|
|
175
|
+
# p File.binread("transcoded.txt")
|
|
176
|
+
# puts
|
|
177
|
+
#
|
|
178
|
+
# open("transcoded.txt", "r:ISO-8859-1:UTF-8") do |io|
|
|
179
|
+
# puts "transcoded text:"
|
|
180
|
+
# p io.read
|
|
181
|
+
# end
|
|
182
|
+
#
|
|
183
|
+
# While writing the file, the internal encoding is not specified as it is only
|
|
184
|
+
# necessary for reading. While reading the file both the internal and external
|
|
185
|
+
# encoding must be specified to obtain the correct result.
|
|
186
|
+
#
|
|
187
|
+
# $ ruby t.rb
|
|
188
|
+
# raw text:
|
|
189
|
+
# "R\xE9sum\xE9"
|
|
190
|
+
#
|
|
191
|
+
# transcoded text:
|
|
192
|
+
# "R\u00E9sum\u00E9"
|
|
193
|
+
#
|
|
1
194
|
class Encoding < Object
|
|
195
|
+
# <!--
|
|
196
|
+
# rdoc-file=encoding.c
|
|
197
|
+
# - Encoding.aliases -> {"alias1" => "orig1", "alias2" => "orig2", ...}
|
|
198
|
+
# -->
|
|
2
199
|
# Returns the hash of available encoding alias and original encoding name.
|
|
3
200
|
#
|
|
4
201
|
# Encoding.aliases
|
|
5
|
-
# #=> {"BINARY"=>"ASCII-8BIT", "ASCII"=>"US-ASCII", "ANSI_X3.4-
|
|
6
|
-
# "SJIS"=>"
|
|
202
|
+
# #=> {"BINARY"=>"ASCII-8BIT", "ASCII"=>"US-ASCII", "ANSI_X3.4-1968"=>"US-ASCII",
|
|
203
|
+
# "SJIS"=>"Windows-31J", "eucJP"=>"EUC-JP", "CP932"=>"Windows-31J"}
|
|
204
|
+
#
|
|
7
205
|
def self.aliases: () -> ::Hash[String, String]
|
|
8
206
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
#
|
|
12
|
-
#
|
|
13
|
-
#
|
|
14
|
-
# from the following locations:
|
|
207
|
+
# <!--
|
|
208
|
+
# rdoc-file=encoding.c
|
|
209
|
+
# - Encoding.compatible?(obj1, obj2) -> enc or nil
|
|
210
|
+
# -->
|
|
211
|
+
# Checks the compatibility of two objects.
|
|
15
212
|
#
|
|
16
|
-
#
|
|
213
|
+
# If the objects are both strings they are compatible when they are
|
|
214
|
+
# concatenatable. The encoding of the concatenated string will be returned if
|
|
215
|
+
# they are compatible, nil if they are not.
|
|
17
216
|
#
|
|
18
|
-
#
|
|
19
|
-
#
|
|
217
|
+
# Encoding.compatible?("\xa1".force_encoding("iso-8859-1"), "b")
|
|
218
|
+
# #=> #<Encoding:ISO-8859-1>
|
|
20
219
|
#
|
|
21
|
-
#
|
|
220
|
+
# Encoding.compatible?(
|
|
221
|
+
# "\xa1".force_encoding("iso-8859-1"),
|
|
222
|
+
# "\xa1\xa1".force_encoding("euc-jp"))
|
|
223
|
+
# #=> nil
|
|
22
224
|
#
|
|
23
|
-
#
|
|
225
|
+
# If the objects are non-strings their encodings are compatible when they have
|
|
226
|
+
# an encoding and:
|
|
227
|
+
# * Either encoding is US-ASCII compatible
|
|
228
|
+
# * One of the encodings is a 7-bit encoding
|
|
24
229
|
#
|
|
25
|
-
|
|
230
|
+
def self.compatible?: (untyped obj1, untyped obj2) -> Encoding?
|
|
231
|
+
|
|
232
|
+
# <!--
|
|
233
|
+
# rdoc-file=encoding.c
|
|
234
|
+
# - Encoding.default_external -> enc
|
|
235
|
+
# -->
|
|
236
|
+
# Returns default external encoding.
|
|
26
237
|
#
|
|
27
|
-
#
|
|
238
|
+
# The default external encoding is used by default for strings created from the
|
|
239
|
+
# following locations:
|
|
28
240
|
#
|
|
29
|
-
#
|
|
241
|
+
# * CSV
|
|
242
|
+
# * File data read from disk
|
|
243
|
+
# * SDBM
|
|
244
|
+
# * StringIO
|
|
245
|
+
# * Zlib::GzipReader
|
|
246
|
+
# * Zlib::GzipWriter
|
|
247
|
+
# * String#inspect
|
|
248
|
+
# * Regexp#inspect
|
|
30
249
|
#
|
|
31
|
-
# - [Regexp\#inspect](https://ruby-doc.org/core-2.6.3/Regexp.html#method-i-inspect)
|
|
32
250
|
#
|
|
33
251
|
# While strings created from these locations will have this encoding, the
|
|
34
|
-
# encoding may not be valid.
|
|
35
|
-
#
|
|
36
|
-
#
|
|
252
|
+
# encoding may not be valid. Be sure to check String#valid_encoding?.
|
|
253
|
+
#
|
|
254
|
+
# File data written to disk will be transcoded to the default external encoding
|
|
255
|
+
# when written, if default_internal is not nil.
|
|
37
256
|
#
|
|
38
|
-
#
|
|
39
|
-
#
|
|
257
|
+
# The default external encoding is initialized by the -E option. If -E isn't
|
|
258
|
+
# set, it is initialized to UTF-8 on Windows and the locale on other operating
|
|
259
|
+
# systems.
|
|
40
260
|
#
|
|
41
|
-
# The default external encoding is initialized by the locale or -E option.
|
|
42
261
|
def self.default_external: () -> Encoding
|
|
43
262
|
|
|
263
|
+
# <!--
|
|
264
|
+
# rdoc-file=encoding.c
|
|
265
|
+
# - Encoding.default_external = enc
|
|
266
|
+
# -->
|
|
267
|
+
# Sets default external encoding. You should not set Encoding::default_external
|
|
268
|
+
# in ruby code as strings created before changing the value may have a different
|
|
269
|
+
# encoding from strings created after the value was changed., instead you should
|
|
270
|
+
# use `ruby -E` to invoke ruby with the correct default_external.
|
|
271
|
+
#
|
|
272
|
+
# See Encoding::default_external for information on how the default external
|
|
273
|
+
# encoding is used.
|
|
274
|
+
#
|
|
44
275
|
def self.default_external=: (String arg0) -> String
|
|
45
276
|
| (Encoding arg0) -> Encoding
|
|
46
277
|
|
|
47
|
-
#
|
|
48
|
-
#
|
|
49
|
-
#
|
|
278
|
+
# <!--
|
|
279
|
+
# rdoc-file=encoding.c
|
|
280
|
+
# - Encoding.default_internal -> enc
|
|
281
|
+
# -->
|
|
282
|
+
# Returns default internal encoding. Strings will be transcoded to the default
|
|
283
|
+
# internal encoding in the following places if the default internal encoding is
|
|
284
|
+
# not nil:
|
|
50
285
|
#
|
|
51
|
-
#
|
|
286
|
+
# * CSV
|
|
287
|
+
# * Etc.sysconfdir and Etc.systmpdir
|
|
288
|
+
# * File data read from disk
|
|
289
|
+
# * File names from Dir
|
|
290
|
+
# * Integer#chr
|
|
291
|
+
# * String#inspect and Regexp#inspect
|
|
292
|
+
# * Strings returned from Readline
|
|
293
|
+
# * Strings returned from SDBM
|
|
294
|
+
# * Time#zone
|
|
295
|
+
# * Values from ENV
|
|
296
|
+
# * Values in ARGV including $PROGRAM_NAME
|
|
52
297
|
#
|
|
53
|
-
# - Etc.sysconfdir and Etc.systmpdir
|
|
54
298
|
#
|
|
55
|
-
#
|
|
56
|
-
#
|
|
299
|
+
# Additionally String#encode and String#encode! use the default internal
|
|
300
|
+
# encoding if no encoding is given.
|
|
57
301
|
#
|
|
58
|
-
#
|
|
59
|
-
#
|
|
302
|
+
# The script encoding (__ENCODING__), not default_internal, is used as the
|
|
303
|
+
# encoding of created strings.
|
|
60
304
|
#
|
|
61
|
-
#
|
|
305
|
+
# Encoding::default_internal is initialized with -E option or nil otherwise.
|
|
62
306
|
#
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
#
|
|
307
|
+
def self.default_internal: () -> Encoding?
|
|
308
|
+
|
|
309
|
+
# <!--
|
|
310
|
+
# rdoc-file=encoding.c
|
|
311
|
+
# - Encoding.default_internal = enc or nil
|
|
312
|
+
# -->
|
|
313
|
+
# Sets default internal encoding or removes default internal encoding when
|
|
314
|
+
# passed nil. You should not set Encoding::default_internal in ruby code as
|
|
315
|
+
# strings created before changing the value may have a different encoding from
|
|
316
|
+
# strings created after the change. Instead you should use `ruby -E` to invoke
|
|
317
|
+
# ruby with the correct default_internal.
|
|
66
318
|
#
|
|
67
|
-
#
|
|
319
|
+
# See Encoding::default_internal for information on how the default internal
|
|
320
|
+
# encoding is used.
|
|
68
321
|
#
|
|
69
|
-
|
|
322
|
+
def self.default_internal=: (String arg0) -> String?
|
|
323
|
+
| (Encoding arg0) -> Encoding?
|
|
324
|
+
| (nil arg0) -> nil
|
|
325
|
+
|
|
326
|
+
# <!--
|
|
327
|
+
# rdoc-file=encoding.c
|
|
328
|
+
# - Encoding.find(string) -> enc
|
|
329
|
+
# -->
|
|
330
|
+
# Search the encoding with specified *name*. *name* should be a string.
|
|
70
331
|
#
|
|
71
|
-
#
|
|
332
|
+
# Encoding.find("US-ASCII") #=> #<Encoding:US-ASCII>
|
|
72
333
|
#
|
|
73
|
-
#
|
|
334
|
+
# Names which this method accept are encoding names and aliases including
|
|
335
|
+
# following special aliases
|
|
74
336
|
#
|
|
75
|
-
#
|
|
337
|
+
# "external"
|
|
338
|
+
# : default external encoding
|
|
339
|
+
# "internal"
|
|
340
|
+
# : default internal encoding
|
|
341
|
+
# "locale"
|
|
342
|
+
# : locale encoding
|
|
343
|
+
# "filesystem"
|
|
344
|
+
# : filesystem encoding
|
|
76
345
|
#
|
|
77
|
-
# Additionally
|
|
78
|
-
# [String\#encode](https://ruby-doc.org/core-2.6.3/String.html#method-i-encode)
|
|
79
|
-
# and
|
|
80
|
-
# [String\#encode\!](https://ruby-doc.org/core-2.6.3/String.html#method-i-encode-21)
|
|
81
|
-
# use the default internal encoding if no encoding is given.
|
|
82
346
|
#
|
|
83
|
-
#
|
|
84
|
-
#
|
|
85
|
-
# ,
|
|
347
|
+
# An ArgumentError is raised when no encoding with *name*. Only
|
|
348
|
+
# `Encoding.find("internal")` however returns nil when no encoding named
|
|
349
|
+
# "internal", in other words, when Ruby has no default internal encoding.
|
|
86
350
|
#
|
|
87
|
-
# [::default\_internal](Encoding.downloaded.ruby_doc#method-c-default_internal)
|
|
88
|
-
# is initialized by the source file's internal\_encoding or -E option.
|
|
89
|
-
def self.default_internal: () -> Encoding?
|
|
90
|
-
|
|
91
|
-
def self.default_internal=: (String arg0) -> String?
|
|
92
|
-
| (Encoding arg0) -> Encoding?
|
|
93
|
-
| (nil arg0) -> nil
|
|
94
|
-
|
|
95
351
|
def self.find: (String | Encoding arg0) -> Encoding
|
|
96
352
|
|
|
353
|
+
# <!--
|
|
354
|
+
# rdoc-file=encoding.c
|
|
355
|
+
# - Encoding.list -> [enc1, enc2, ...]
|
|
356
|
+
# -->
|
|
357
|
+
# Returns the list of loaded encodings.
|
|
358
|
+
#
|
|
359
|
+
# Encoding.list
|
|
360
|
+
# #=> [#<Encoding:ASCII-8BIT>, #<Encoding:UTF-8>,
|
|
361
|
+
# #<Encoding:ISO-2022-JP (dummy)>]
|
|
362
|
+
#
|
|
363
|
+
# Encoding.find("US-ASCII")
|
|
364
|
+
# #=> #<Encoding:US-ASCII>
|
|
365
|
+
#
|
|
366
|
+
# Encoding.list
|
|
367
|
+
# #=> [#<Encoding:ASCII-8BIT>, #<Encoding:UTF-8>,
|
|
368
|
+
# #<Encoding:US-ASCII>, #<Encoding:ISO-2022-JP (dummy)>]
|
|
369
|
+
#
|
|
97
370
|
def self.list: () -> ::Array[Encoding]
|
|
98
371
|
|
|
372
|
+
# <!--
|
|
373
|
+
# rdoc-file=encoding.c
|
|
374
|
+
# - Encoding.name_list -> ["enc1", "enc2", ...]
|
|
375
|
+
# -->
|
|
99
376
|
# Returns the list of available encoding names.
|
|
100
377
|
#
|
|
101
378
|
# Encoding.name_list
|
|
@@ -103,49 +380,80 @@ class Encoding < Object
|
|
|
103
380
|
# "ISO-8859-1", "Shift_JIS", "EUC-JP",
|
|
104
381
|
# "Windows-31J",
|
|
105
382
|
# "BINARY", "CP932", "eucJP"]
|
|
383
|
+
#
|
|
106
384
|
def self.name_list: () -> ::Array[String]
|
|
107
385
|
|
|
386
|
+
# <!--
|
|
387
|
+
# rdoc-file=encoding.c
|
|
388
|
+
# - enc.ascii_compatible? -> true or false
|
|
389
|
+
# -->
|
|
108
390
|
# Returns whether ASCII-compatible or not.
|
|
109
391
|
#
|
|
110
|
-
#
|
|
111
|
-
#
|
|
112
|
-
#
|
|
113
|
-
# ```
|
|
392
|
+
# Encoding::UTF_8.ascii_compatible? #=> true
|
|
393
|
+
# Encoding::UTF_16BE.ascii_compatible? #=> false
|
|
394
|
+
#
|
|
114
395
|
def ascii_compatible?: () -> bool
|
|
115
396
|
|
|
116
|
-
#
|
|
117
|
-
#
|
|
118
|
-
#
|
|
397
|
+
# <!--
|
|
398
|
+
# rdoc-file=encoding.c
|
|
399
|
+
# - enc.dummy? -> true or false
|
|
400
|
+
# -->
|
|
401
|
+
# Returns true for dummy encodings. A dummy encoding is an encoding for which
|
|
402
|
+
# character handling is not properly implemented. It is used for stateful
|
|
403
|
+
# encodings.
|
|
404
|
+
#
|
|
405
|
+
# Encoding::ISO_2022_JP.dummy? #=> true
|
|
406
|
+
# Encoding::UTF_8.dummy? #=> false
|
|
119
407
|
#
|
|
120
|
-
# ```ruby
|
|
121
|
-
# Encoding::ISO_2022_JP.dummy? #=> true
|
|
122
|
-
# Encoding::UTF_8.dummy? #=> false
|
|
123
|
-
# ```
|
|
124
408
|
def dummy?: () -> bool
|
|
125
409
|
|
|
410
|
+
# <!--
|
|
411
|
+
# rdoc-file=encoding.c
|
|
412
|
+
# - enc.inspect -> string
|
|
413
|
+
# -->
|
|
414
|
+
# Returns a string which represents the encoding for programmers.
|
|
415
|
+
#
|
|
416
|
+
# Encoding::UTF_8.inspect #=> "#<Encoding:UTF-8>"
|
|
417
|
+
# Encoding::ISO_2022_JP.inspect #=> "#<Encoding:ISO-2022-JP (dummy)>"
|
|
418
|
+
#
|
|
126
419
|
def inspect: () -> String
|
|
127
420
|
|
|
421
|
+
# <!-- rdoc-file=encoding.c -->
|
|
128
422
|
# Returns the name of the encoding.
|
|
129
423
|
#
|
|
130
|
-
#
|
|
131
|
-
#
|
|
132
|
-
# ```
|
|
424
|
+
# Encoding::UTF_8.name #=> "UTF-8"
|
|
425
|
+
#
|
|
133
426
|
def name: () -> String
|
|
134
427
|
|
|
428
|
+
# <!--
|
|
429
|
+
# rdoc-file=encoding.c
|
|
430
|
+
# - enc.names -> array
|
|
431
|
+
# -->
|
|
135
432
|
# Returns the list of name and aliases of the encoding.
|
|
136
433
|
#
|
|
137
|
-
#
|
|
138
|
-
#
|
|
139
|
-
# ```
|
|
434
|
+
# Encoding::WINDOWS_31J.names #=> ["Windows-31J", "CP932", "csWindows31J", "SJIS", "PCK"]
|
|
435
|
+
#
|
|
140
436
|
def names: () -> ::Array[String]
|
|
141
437
|
|
|
438
|
+
# <!--
|
|
439
|
+
# rdoc-file=encoding.c
|
|
440
|
+
# - enc.replicate(name) -> encoding
|
|
441
|
+
# -->
|
|
442
|
+
# Returns a replicated encoding of *enc* whose name is *name*. The new encoding
|
|
443
|
+
# should have the same byte structure of *enc*. If *name* is used by another
|
|
444
|
+
# encoding, raise ArgumentError.
|
|
445
|
+
#
|
|
142
446
|
def replicate: (String name) -> Encoding
|
|
143
447
|
|
|
448
|
+
# <!--
|
|
449
|
+
# rdoc-file=encoding.c
|
|
450
|
+
# - enc.name -> string
|
|
451
|
+
# - enc.to_s -> string
|
|
452
|
+
# -->
|
|
144
453
|
# Returns the name of the encoding.
|
|
145
454
|
#
|
|
146
|
-
#
|
|
147
|
-
#
|
|
148
|
-
# ```
|
|
455
|
+
# Encoding::UTF_8.name #=> "UTF-8"
|
|
456
|
+
#
|
|
149
457
|
def to_s: () -> String
|
|
150
458
|
end
|
|
151
459
|
|
|
@@ -567,43 +875,872 @@ Encoding::Windows_31J: Encoding
|
|
|
567
875
|
|
|
568
876
|
Encoding::Windows_874: Encoding
|
|
569
877
|
|
|
878
|
+
# <!-- rdoc-file=transcode.c -->
|
|
879
|
+
# Encoding conversion class.
|
|
880
|
+
#
|
|
570
881
|
class Encoding::Converter < Object
|
|
882
|
+
type encoding = String | Encoding
|
|
883
|
+
type decorator = "universal_newline"
|
|
884
|
+
| "crlf_newline"
|
|
885
|
+
| "cr_newline"
|
|
886
|
+
| "xml_text_escape"
|
|
887
|
+
| "xml_attr_content_escape"
|
|
888
|
+
| "xml_attr_quote"
|
|
889
|
+
type conversion_path = Array[[encoding, encoding] | decorator]
|
|
890
|
+
type convert_result = :invalid_byte_sequence
|
|
891
|
+
| :incomplete_input
|
|
892
|
+
| :undefined_conversion
|
|
893
|
+
| :after_output
|
|
894
|
+
| :destination_buffer_full
|
|
895
|
+
| :source_buffer_empty
|
|
896
|
+
| :finished
|
|
897
|
+
|
|
898
|
+
# <!--
|
|
899
|
+
# rdoc-file=transcode.c
|
|
900
|
+
# - Encoding::Converter.asciicompat_encoding(string) -> encoding or nil
|
|
901
|
+
# - Encoding::Converter.asciicompat_encoding(encoding) -> encoding or nil
|
|
902
|
+
# -->
|
|
903
|
+
# Returns the corresponding ASCII compatible encoding.
|
|
904
|
+
#
|
|
905
|
+
# Returns nil if the argument is an ASCII compatible encoding.
|
|
906
|
+
#
|
|
907
|
+
# "corresponding ASCII compatible encoding" is an ASCII compatible encoding
|
|
908
|
+
# which can represents exactly the same characters as the given ASCII
|
|
909
|
+
# incompatible encoding. So, no conversion undefined error occurs when
|
|
910
|
+
# converting between the two encodings.
|
|
911
|
+
#
|
|
912
|
+
# Encoding::Converter.asciicompat_encoding("ISO-2022-JP") #=> #<Encoding:stateless-ISO-2022-JP>
|
|
913
|
+
# Encoding::Converter.asciicompat_encoding("UTF-16BE") #=> #<Encoding:UTF-8>
|
|
914
|
+
# Encoding::Converter.asciicompat_encoding("UTF-8") #=> nil
|
|
915
|
+
#
|
|
916
|
+
def self.asciicompat_encoding: (encoding enc) -> Encoding?
|
|
917
|
+
|
|
918
|
+
# <!--
|
|
919
|
+
# rdoc-file=transcode.c
|
|
920
|
+
# - Encoding::Converter.search_convpath(source_encoding, destination_encoding) -> ary
|
|
921
|
+
# - Encoding::Converter.search_convpath(source_encoding, destination_encoding, opt) -> ary
|
|
922
|
+
# -->
|
|
923
|
+
# Returns a conversion path.
|
|
924
|
+
#
|
|
925
|
+
# p Encoding::Converter.search_convpath("ISO-8859-1", "EUC-JP")
|
|
926
|
+
# #=> [[#<Encoding:ISO-8859-1>, #<Encoding:UTF-8>],
|
|
927
|
+
# # [#<Encoding:UTF-8>, #<Encoding:EUC-JP>]]
|
|
928
|
+
#
|
|
929
|
+
# p Encoding::Converter.search_convpath("ISO-8859-1", "EUC-JP", universal_newline: true)
|
|
930
|
+
# or
|
|
931
|
+
# p Encoding::Converter.search_convpath("ISO-8859-1", "EUC-JP", newline: :universal)
|
|
932
|
+
# #=> [[#<Encoding:ISO-8859-1>, #<Encoding:UTF-8>],
|
|
933
|
+
# # [#<Encoding:UTF-8>, #<Encoding:EUC-JP>],
|
|
934
|
+
# # "universal_newline"]
|
|
935
|
+
#
|
|
936
|
+
# p Encoding::Converter.search_convpath("ISO-8859-1", "UTF-32BE", universal_newline: true)
|
|
937
|
+
# or
|
|
938
|
+
# p Encoding::Converter.search_convpath("ISO-8859-1", "UTF-32BE", newline: :universal)
|
|
939
|
+
# #=> [[#<Encoding:ISO-8859-1>, #<Encoding:UTF-8>],
|
|
940
|
+
# # "universal_newline",
|
|
941
|
+
# # [#<Encoding:UTF-8>, #<Encoding:UTF-32BE>]]
|
|
942
|
+
#
|
|
943
|
+
def self.search_convpath: (
|
|
944
|
+
encoding source,
|
|
945
|
+
encoding destination,
|
|
946
|
+
?newline: :universal | :crlf | :cr,
|
|
947
|
+
?universal_newline: bool,
|
|
948
|
+
?crlf_newline: bool,
|
|
949
|
+
?cr_newline: bool,
|
|
950
|
+
?xml: :text | :attr
|
|
951
|
+
) -> conversion_path
|
|
952
|
+
|
|
953
|
+
public
|
|
954
|
+
|
|
955
|
+
# <!--
|
|
956
|
+
# rdoc-file=transcode.c
|
|
957
|
+
# - ec == other -> true or false
|
|
958
|
+
# -->
|
|
959
|
+
#
|
|
960
|
+
def ==: (self) -> bool
|
|
961
|
+
|
|
962
|
+
# <!--
|
|
963
|
+
# rdoc-file=transcode.c
|
|
964
|
+
# - ec.convert(source_string) -> destination_string
|
|
965
|
+
# -->
|
|
966
|
+
# Convert source_string and return destination_string.
|
|
967
|
+
#
|
|
968
|
+
# source_string is assumed as a part of source. i.e. :partial_input=>true is
|
|
969
|
+
# specified internally. finish method should be used last.
|
|
970
|
+
#
|
|
971
|
+
# ec = Encoding::Converter.new("utf-8", "euc-jp")
|
|
972
|
+
# puts ec.convert("\u3042").dump #=> "\xA4\xA2"
|
|
973
|
+
# puts ec.finish.dump #=> ""
|
|
974
|
+
#
|
|
975
|
+
# ec = Encoding::Converter.new("euc-jp", "utf-8")
|
|
976
|
+
# puts ec.convert("\xA4").dump #=> ""
|
|
977
|
+
# puts ec.convert("\xA2").dump #=> "\xE3\x81\x82"
|
|
978
|
+
# puts ec.finish.dump #=> ""
|
|
979
|
+
#
|
|
980
|
+
# ec = Encoding::Converter.new("utf-8", "iso-2022-jp")
|
|
981
|
+
# puts ec.convert("\xE3").dump #=> "".force_encoding("ISO-2022-JP")
|
|
982
|
+
# puts ec.convert("\x81").dump #=> "".force_encoding("ISO-2022-JP")
|
|
983
|
+
# puts ec.convert("\x82").dump #=> "\e$B$\"".force_encoding("ISO-2022-JP")
|
|
984
|
+
# puts ec.finish.dump #=> "\e(B".force_encoding("ISO-2022-JP")
|
|
985
|
+
#
|
|
986
|
+
# If a conversion error occur, Encoding::UndefinedConversionError or
|
|
987
|
+
# Encoding::InvalidByteSequenceError is raised. Encoding::Converter#convert
|
|
988
|
+
# doesn't supply methods to recover or restart from these exceptions. When you
|
|
989
|
+
# want to handle these conversion errors, use
|
|
990
|
+
# Encoding::Converter#primitive_convert.
|
|
991
|
+
#
|
|
992
|
+
def convert: (String source) -> String
|
|
993
|
+
|
|
994
|
+
# <!--
|
|
995
|
+
# rdoc-file=transcode.c
|
|
996
|
+
# - ec.convpath -> ary
|
|
997
|
+
# -->
|
|
998
|
+
# Returns the conversion path of ec.
|
|
999
|
+
#
|
|
1000
|
+
# The result is an array of conversions.
|
|
1001
|
+
#
|
|
1002
|
+
# ec = Encoding::Converter.new("ISO-8859-1", "EUC-JP", crlf_newline: true)
|
|
1003
|
+
# p ec.convpath
|
|
1004
|
+
# #=> [[#<Encoding:ISO-8859-1>, #<Encoding:UTF-8>],
|
|
1005
|
+
# # [#<Encoding:UTF-8>, #<Encoding:EUC-JP>],
|
|
1006
|
+
# # "crlf_newline"]
|
|
1007
|
+
#
|
|
1008
|
+
# Each element of the array is a pair of encodings or a string. A pair means an
|
|
1009
|
+
# encoding conversion. A string means a decorator.
|
|
1010
|
+
#
|
|
1011
|
+
# In the above example, [#<Encoding:ISO-8859-1>, #<Encoding:UTF-8>] means a
|
|
1012
|
+
# converter from ISO-8859-1 to UTF-8. "crlf_newline" means newline converter
|
|
1013
|
+
# from LF to CRLF.
|
|
1014
|
+
#
|
|
1015
|
+
def convpath: () -> conversion_path
|
|
1016
|
+
|
|
1017
|
+
# <!--
|
|
1018
|
+
# rdoc-file=transcode.c
|
|
1019
|
+
# - ec.destination_encoding -> encoding
|
|
1020
|
+
# -->
|
|
1021
|
+
# Returns the destination encoding as an Encoding object.
|
|
1022
|
+
#
|
|
1023
|
+
def destination_encoding: () -> Encoding
|
|
1024
|
+
|
|
1025
|
+
# <!--
|
|
1026
|
+
# rdoc-file=transcode.c
|
|
1027
|
+
# - ec.finish -> string
|
|
1028
|
+
# -->
|
|
1029
|
+
# Finishes the converter. It returns the last part of the converted string.
|
|
1030
|
+
#
|
|
1031
|
+
# ec = Encoding::Converter.new("utf-8", "iso-2022-jp")
|
|
1032
|
+
# p ec.convert("\u3042") #=> "\e$B$\""
|
|
1033
|
+
# p ec.finish #=> "\e(B"
|
|
1034
|
+
#
|
|
1035
|
+
def finish: () -> String
|
|
1036
|
+
|
|
1037
|
+
# <!--
|
|
1038
|
+
# rdoc-file=transcode.c
|
|
1039
|
+
# - ec.insert_output(string) -> nil
|
|
1040
|
+
# -->
|
|
1041
|
+
# Inserts string into the encoding converter. The string will be converted to
|
|
1042
|
+
# the destination encoding and output on later conversions.
|
|
1043
|
+
#
|
|
1044
|
+
# If the destination encoding is stateful, string is converted according to the
|
|
1045
|
+
# state and the state is updated.
|
|
1046
|
+
#
|
|
1047
|
+
# This method should be used only when a conversion error occurs.
|
|
1048
|
+
#
|
|
1049
|
+
# ec = Encoding::Converter.new("utf-8", "iso-8859-1")
|
|
1050
|
+
# src = "HIRAGANA LETTER A is \u{3042}."
|
|
1051
|
+
# dst = ""
|
|
1052
|
+
# p ec.primitive_convert(src, dst) #=> :undefined_conversion
|
|
1053
|
+
# puts "[#{dst.dump}, #{src.dump}]" #=> ["HIRAGANA LETTER A is ", "."]
|
|
1054
|
+
# ec.insert_output("<err>")
|
|
1055
|
+
# p ec.primitive_convert(src, dst) #=> :finished
|
|
1056
|
+
# puts "[#{dst.dump}, #{src.dump}]" #=> ["HIRAGANA LETTER A is <err>.", ""]
|
|
1057
|
+
#
|
|
1058
|
+
# ec = Encoding::Converter.new("utf-8", "iso-2022-jp")
|
|
1059
|
+
# src = "\u{306F 3041 3068 2661 3002}" # U+2661 is not representable in iso-2022-jp
|
|
1060
|
+
# dst = ""
|
|
1061
|
+
# p ec.primitive_convert(src, dst) #=> :undefined_conversion
|
|
1062
|
+
# puts "[#{dst.dump}, #{src.dump}]" #=> ["\e$B$O$!$H".force_encoding("ISO-2022-JP"), "\xE3\x80\x82"]
|
|
1063
|
+
# ec.insert_output "?" # state change required to output "?".
|
|
1064
|
+
# p ec.primitive_convert(src, dst) #=> :finished
|
|
1065
|
+
# puts "[#{dst.dump}, #{src.dump}]" #=> ["\e$B$O$!$H\e(B?\e$B!#\e(B".force_encoding("ISO-2022-JP"), ""]
|
|
1066
|
+
#
|
|
1067
|
+
def insert_output: (String) -> nil
|
|
1068
|
+
|
|
1069
|
+
# <!--
|
|
1070
|
+
# rdoc-file=transcode.c
|
|
1071
|
+
# - ec.inspect -> string
|
|
1072
|
+
# -->
|
|
1073
|
+
# Returns a printable version of *ec*
|
|
1074
|
+
#
|
|
1075
|
+
# ec = Encoding::Converter.new("iso-8859-1", "utf-8")
|
|
1076
|
+
# puts ec.inspect #=> #<Encoding::Converter: ISO-8859-1 to UTF-8>
|
|
1077
|
+
#
|
|
1078
|
+
def inspect: () -> String
|
|
1079
|
+
|
|
1080
|
+
# <!--
|
|
1081
|
+
# rdoc-file=transcode.c
|
|
1082
|
+
# - ec.last_error -> exception or nil
|
|
1083
|
+
# -->
|
|
1084
|
+
# Returns an exception object for the last conversion. Returns nil if the last
|
|
1085
|
+
# conversion did not produce an error.
|
|
1086
|
+
#
|
|
1087
|
+
# "error" means that Encoding::InvalidByteSequenceError and
|
|
1088
|
+
# Encoding::UndefinedConversionError for Encoding::Converter#convert and
|
|
1089
|
+
# :invalid_byte_sequence, :incomplete_input and :undefined_conversion for
|
|
1090
|
+
# Encoding::Converter#primitive_convert.
|
|
1091
|
+
#
|
|
1092
|
+
# ec = Encoding::Converter.new("utf-8", "iso-8859-1")
|
|
1093
|
+
# p ec.primitive_convert(src="\xf1abcd", dst="") #=> :invalid_byte_sequence
|
|
1094
|
+
# p ec.last_error #=> #<Encoding::InvalidByteSequenceError: "\xF1" followed by "a" on UTF-8>
|
|
1095
|
+
# p ec.primitive_convert(src, dst, nil, 1) #=> :destination_buffer_full
|
|
1096
|
+
# p ec.last_error #=> nil
|
|
1097
|
+
#
|
|
1098
|
+
def last_error: () -> Encoding::InvalidByteSequenceError?
|
|
1099
|
+
| () -> Encoding::UndefinedConversionError?
|
|
1100
|
+
|
|
1101
|
+
# <!--
|
|
1102
|
+
# rdoc-file=transcode.c
|
|
1103
|
+
# - ec.primitive_convert(source_buffer, destination_buffer) -> symbol
|
|
1104
|
+
# - ec.primitive_convert(source_buffer, destination_buffer, destination_byteoffset) -> symbol
|
|
1105
|
+
# - ec.primitive_convert(source_buffer, destination_buffer, destination_byteoffset, destination_bytesize) -> symbol
|
|
1106
|
+
# - ec.primitive_convert(source_buffer, destination_buffer, destination_byteoffset, destination_bytesize, opt) -> symbol
|
|
1107
|
+
# -->
|
|
1108
|
+
# possible opt elements:
|
|
1109
|
+
# hash form:
|
|
1110
|
+
# :partial_input => true # source buffer may be part of larger source
|
|
1111
|
+
# :after_output => true # stop conversion after output before input
|
|
1112
|
+
# integer form:
|
|
1113
|
+
# Encoding::Converter::PARTIAL_INPUT
|
|
1114
|
+
# Encoding::Converter::AFTER_OUTPUT
|
|
1115
|
+
#
|
|
1116
|
+
# possible results:
|
|
1117
|
+
# :invalid_byte_sequence
|
|
1118
|
+
# :incomplete_input
|
|
1119
|
+
# :undefined_conversion
|
|
1120
|
+
# :after_output
|
|
1121
|
+
# :destination_buffer_full
|
|
1122
|
+
# :source_buffer_empty
|
|
1123
|
+
# :finished
|
|
1124
|
+
#
|
|
1125
|
+
# primitive_convert converts source_buffer into destination_buffer.
|
|
1126
|
+
#
|
|
1127
|
+
# source_buffer should be a string or nil. nil means an empty string.
|
|
1128
|
+
#
|
|
1129
|
+
# destination_buffer should be a string.
|
|
1130
|
+
#
|
|
1131
|
+
# destination_byteoffset should be an integer or nil. nil means the end of
|
|
1132
|
+
# destination_buffer. If it is omitted, nil is assumed.
|
|
1133
|
+
#
|
|
1134
|
+
# destination_bytesize should be an integer or nil. nil means unlimited. If it
|
|
1135
|
+
# is omitted, nil is assumed.
|
|
1136
|
+
#
|
|
1137
|
+
# opt should be nil, a hash or an integer. nil means no flags. If it is omitted,
|
|
1138
|
+
# nil is assumed.
|
|
1139
|
+
#
|
|
1140
|
+
# primitive_convert converts the content of source_buffer from beginning and
|
|
1141
|
+
# store the result into destination_buffer.
|
|
1142
|
+
#
|
|
1143
|
+
# destination_byteoffset and destination_bytesize specify the region which the
|
|
1144
|
+
# converted result is stored. destination_byteoffset specifies the start
|
|
1145
|
+
# position in destination_buffer in bytes. If destination_byteoffset is nil,
|
|
1146
|
+
# destination_buffer.bytesize is used for appending the result.
|
|
1147
|
+
# destination_bytesize specifies maximum number of bytes. If
|
|
1148
|
+
# destination_bytesize is nil, destination size is unlimited. After conversion,
|
|
1149
|
+
# destination_buffer is resized to destination_byteoffset + actually produced
|
|
1150
|
+
# number of bytes. Also destination_buffer's encoding is set to
|
|
1151
|
+
# destination_encoding.
|
|
1152
|
+
#
|
|
1153
|
+
# primitive_convert drops the converted part of source_buffer. the dropped part
|
|
1154
|
+
# is converted in destination_buffer or buffered in Encoding::Converter object.
|
|
1155
|
+
#
|
|
1156
|
+
# primitive_convert stops conversion when one of following condition met.
|
|
1157
|
+
# * invalid byte sequence found in source buffer (:invalid_byte_sequence)
|
|
1158
|
+
# `primitive_errinfo` and `last_error` methods returns the detail of the
|
|
1159
|
+
# error.
|
|
1160
|
+
# * unexpected end of source buffer (:incomplete_input) this occur only when
|
|
1161
|
+
# :partial_input is not specified. `primitive_errinfo` and `last_error`
|
|
1162
|
+
# methods returns the detail of the error.
|
|
1163
|
+
# * character not representable in output encoding (:undefined_conversion)
|
|
1164
|
+
# `primitive_errinfo` and `last_error` methods returns the detail of the
|
|
1165
|
+
# error.
|
|
1166
|
+
# * after some output is generated, before input is done (:after_output) this
|
|
1167
|
+
# occur only when :after_output is specified.
|
|
1168
|
+
# * destination buffer is full (:destination_buffer_full) this occur only when
|
|
1169
|
+
# destination_bytesize is non-nil.
|
|
1170
|
+
# * source buffer is empty (:source_buffer_empty) this occur only when
|
|
1171
|
+
# :partial_input is specified.
|
|
1172
|
+
# * conversion is finished (:finished)
|
|
1173
|
+
#
|
|
1174
|
+
#
|
|
1175
|
+
# example:
|
|
1176
|
+
# ec = Encoding::Converter.new("UTF-8", "UTF-16BE")
|
|
1177
|
+
# ret = ec.primitive_convert(src="pi", dst="", nil, 100)
|
|
1178
|
+
# p [ret, src, dst] #=> [:finished, "", "\x00p\x00i"]
|
|
1179
|
+
#
|
|
1180
|
+
# ec = Encoding::Converter.new("UTF-8", "UTF-16BE")
|
|
1181
|
+
# ret = ec.primitive_convert(src="pi", dst="", nil, 1)
|
|
1182
|
+
# p [ret, src, dst] #=> [:destination_buffer_full, "i", "\x00"]
|
|
1183
|
+
# ret = ec.primitive_convert(src, dst="", nil, 1)
|
|
1184
|
+
# p [ret, src, dst] #=> [:destination_buffer_full, "", "p"]
|
|
1185
|
+
# ret = ec.primitive_convert(src, dst="", nil, 1)
|
|
1186
|
+
# p [ret, src, dst] #=> [:destination_buffer_full, "", "\x00"]
|
|
1187
|
+
# ret = ec.primitive_convert(src, dst="", nil, 1)
|
|
1188
|
+
# p [ret, src, dst] #=> [:finished, "", "i"]
|
|
1189
|
+
#
|
|
1190
|
+
def primitive_convert: (
|
|
1191
|
+
String? source,
|
|
1192
|
+
String destination,
|
|
1193
|
+
?Integer? destination_byteoffset,
|
|
1194
|
+
?Integer? destination_bytesize,
|
|
1195
|
+
?partial_input: bool,
|
|
1196
|
+
?after_output: bool
|
|
1197
|
+
) -> convert_result
|
|
1198
|
+
| (
|
|
1199
|
+
String? source,
|
|
1200
|
+
String destination,
|
|
1201
|
+
?Integer? destination_byteoffset,
|
|
1202
|
+
?Integer? destination_bytesize,
|
|
1203
|
+
?Integer opt
|
|
1204
|
+
) -> convert_result
|
|
1205
|
+
|
|
1206
|
+
# <!--
|
|
1207
|
+
# rdoc-file=transcode.c
|
|
1208
|
+
# - ec.primitive_errinfo -> array
|
|
1209
|
+
# -->
|
|
1210
|
+
# primitive_errinfo returns important information regarding the last error as a
|
|
1211
|
+
# 5-element array:
|
|
1212
|
+
#
|
|
1213
|
+
# [result, enc1, enc2, error_bytes, readagain_bytes]
|
|
1214
|
+
#
|
|
1215
|
+
# result is the last result of primitive_convert.
|
|
1216
|
+
#
|
|
1217
|
+
# Other elements are only meaningful when result is :invalid_byte_sequence,
|
|
1218
|
+
# :incomplete_input or :undefined_conversion.
|
|
1219
|
+
#
|
|
1220
|
+
# enc1 and enc2 indicate a conversion step as a pair of strings. For example, a
|
|
1221
|
+
# converter from EUC-JP to ISO-8859-1 converts a string as follows: EUC-JP ->
|
|
1222
|
+
# UTF-8 -> ISO-8859-1. So [enc1, enc2] is either ["EUC-JP", "UTF-8"] or
|
|
1223
|
+
# ["UTF-8", "ISO-8859-1"].
|
|
1224
|
+
#
|
|
1225
|
+
# error_bytes and readagain_bytes indicate the byte sequences which caused the
|
|
1226
|
+
# error. error_bytes is discarded portion. readagain_bytes is buffered portion
|
|
1227
|
+
# which is read again on next conversion.
|
|
1228
|
+
#
|
|
1229
|
+
# Example:
|
|
1230
|
+
#
|
|
1231
|
+
# # \xff is invalid as EUC-JP.
|
|
1232
|
+
# ec = Encoding::Converter.new("EUC-JP", "Shift_JIS")
|
|
1233
|
+
# ec.primitive_convert(src="\xff", dst="", nil, 10)
|
|
1234
|
+
# p ec.primitive_errinfo
|
|
1235
|
+
# #=> [:invalid_byte_sequence, "EUC-JP", "Shift_JIS", "\xFF", ""]
|
|
1236
|
+
#
|
|
1237
|
+
# # HIRAGANA LETTER A (\xa4\xa2 in EUC-JP) is not representable in ISO-8859-1.
|
|
1238
|
+
# # Since this error is occur in UTF-8 to ISO-8859-1 conversion,
|
|
1239
|
+
# # error_bytes is HIRAGANA LETTER A in UTF-8 (\xE3\x81\x82).
|
|
1240
|
+
# ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1")
|
|
1241
|
+
# ec.primitive_convert(src="\xa4\xa2", dst="", nil, 10)
|
|
1242
|
+
# p ec.primitive_errinfo
|
|
1243
|
+
# #=> [:undefined_conversion, "UTF-8", "ISO-8859-1", "\xE3\x81\x82", ""]
|
|
1244
|
+
#
|
|
1245
|
+
# # partial character is invalid
|
|
1246
|
+
# ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1")
|
|
1247
|
+
# ec.primitive_convert(src="\xa4", dst="", nil, 10)
|
|
1248
|
+
# p ec.primitive_errinfo
|
|
1249
|
+
# #=> [:incomplete_input, "EUC-JP", "UTF-8", "\xA4", ""]
|
|
1250
|
+
#
|
|
1251
|
+
# # Encoding::Converter::PARTIAL_INPUT prevents invalid errors by
|
|
1252
|
+
# # partial characters.
|
|
1253
|
+
# ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1")
|
|
1254
|
+
# ec.primitive_convert(src="\xa4", dst="", nil, 10, Encoding::Converter::PARTIAL_INPUT)
|
|
1255
|
+
# p ec.primitive_errinfo
|
|
1256
|
+
# #=> [:source_buffer_empty, nil, nil, nil, nil]
|
|
1257
|
+
#
|
|
1258
|
+
# # \xd8\x00\x00@ is invalid as UTF-16BE because
|
|
1259
|
+
# # no low surrogate after high surrogate (\xd8\x00).
|
|
1260
|
+
# # It is detected by 3rd byte (\00) which is part of next character.
|
|
1261
|
+
# # So the high surrogate (\xd8\x00) is discarded and
|
|
1262
|
+
# # the 3rd byte is read again later.
|
|
1263
|
+
# # Since the byte is buffered in ec, it is dropped from src.
|
|
1264
|
+
# ec = Encoding::Converter.new("UTF-16BE", "UTF-8")
|
|
1265
|
+
# ec.primitive_convert(src="\xd8\x00\x00@", dst="", nil, 10)
|
|
1266
|
+
# p ec.primitive_errinfo
|
|
1267
|
+
# #=> [:invalid_byte_sequence, "UTF-16BE", "UTF-8", "\xD8\x00", "\x00"]
|
|
1268
|
+
# p src
|
|
1269
|
+
# #=> "@"
|
|
1270
|
+
#
|
|
1271
|
+
# # Similar to UTF-16BE, \x00\xd8@\x00 is invalid as UTF-16LE.
|
|
1272
|
+
# # The problem is detected by 4th byte.
|
|
1273
|
+
# ec = Encoding::Converter.new("UTF-16LE", "UTF-8")
|
|
1274
|
+
# ec.primitive_convert(src="\x00\xd8@\x00", dst="", nil, 10)
|
|
1275
|
+
# p ec.primitive_errinfo
|
|
1276
|
+
# #=> [:invalid_byte_sequence, "UTF-16LE", "UTF-8", "\x00\xD8", "@\x00"]
|
|
1277
|
+
# p src
|
|
1278
|
+
# #=> ""
|
|
1279
|
+
#
|
|
1280
|
+
def primitive_errinfo: () -> [convert_result, String?, String?, String?, String?]
|
|
1281
|
+
|
|
1282
|
+
# <!--
|
|
1283
|
+
# rdoc-file=transcode.c
|
|
1284
|
+
# - ec.putback -> string
|
|
1285
|
+
# - ec.putback(max_numbytes) -> string
|
|
1286
|
+
# -->
|
|
1287
|
+
# Put back the bytes which will be converted.
|
|
1288
|
+
#
|
|
1289
|
+
# The bytes are caused by invalid_byte_sequence error. When
|
|
1290
|
+
# invalid_byte_sequence error, some bytes are discarded and some bytes are
|
|
1291
|
+
# buffered to be converted later. The latter bytes can be put back. It can be
|
|
1292
|
+
# observed by Encoding::InvalidByteSequenceError#readagain_bytes and
|
|
1293
|
+
# Encoding::Converter#primitive_errinfo.
|
|
1294
|
+
#
|
|
1295
|
+
# ec = Encoding::Converter.new("utf-16le", "iso-8859-1")
|
|
1296
|
+
# src = "\x00\xd8\x61\x00"
|
|
1297
|
+
# dst = ""
|
|
1298
|
+
# p ec.primitive_convert(src, dst) #=> :invalid_byte_sequence
|
|
1299
|
+
# p ec.primitive_errinfo #=> [:invalid_byte_sequence, "UTF-16LE", "UTF-8", "\x00\xD8", "a\x00"]
|
|
1300
|
+
# p ec.putback #=> "a\x00"
|
|
1301
|
+
# p ec.putback #=> "" # no more bytes to put back
|
|
1302
|
+
#
|
|
1303
|
+
def putback: (?Integer max_numbytes) -> String
|
|
1304
|
+
|
|
1305
|
+
# <!--
|
|
1306
|
+
# rdoc-file=transcode.c
|
|
1307
|
+
# - ec.replacement -> string
|
|
1308
|
+
# -->
|
|
1309
|
+
# Returns the replacement string.
|
|
1310
|
+
#
|
|
1311
|
+
# ec = Encoding::Converter.new("euc-jp", "us-ascii")
|
|
1312
|
+
# p ec.replacement #=> "?"
|
|
1313
|
+
#
|
|
1314
|
+
# ec = Encoding::Converter.new("euc-jp", "utf-8")
|
|
1315
|
+
# p ec.replacement #=> "\uFFFD"
|
|
1316
|
+
#
|
|
1317
|
+
def replacement: () -> String
|
|
1318
|
+
|
|
1319
|
+
# <!--
|
|
1320
|
+
# rdoc-file=transcode.c
|
|
1321
|
+
# - ec.replacement = string
|
|
1322
|
+
# -->
|
|
1323
|
+
# Sets the replacement string.
|
|
1324
|
+
#
|
|
1325
|
+
# ec = Encoding::Converter.new("utf-8", "us-ascii", :undef => :replace)
|
|
1326
|
+
# ec.replacement = "<undef>"
|
|
1327
|
+
# p ec.convert("a \u3042 b") #=> "a <undef> b"
|
|
1328
|
+
#
|
|
1329
|
+
def replacement=: (String str) -> String
|
|
1330
|
+
|
|
1331
|
+
# <!--
|
|
1332
|
+
# rdoc-file=transcode.c
|
|
1333
|
+
# - ec.source_encoding -> encoding
|
|
1334
|
+
# -->
|
|
1335
|
+
# Returns the source encoding as an Encoding object.
|
|
1336
|
+
#
|
|
1337
|
+
def source_encoding: () -> Encoding
|
|
1338
|
+
|
|
1339
|
+
private
|
|
1340
|
+
|
|
1341
|
+
# <!--
|
|
1342
|
+
# rdoc-file=transcode.c
|
|
1343
|
+
# - Encoding::Converter.new(source_encoding, destination_encoding)
|
|
1344
|
+
# - Encoding::Converter.new(source_encoding, destination_encoding, opt)
|
|
1345
|
+
# - Encoding::Converter.new(convpath)
|
|
1346
|
+
# -->
|
|
1347
|
+
# possible options elements:
|
|
1348
|
+
# hash form:
|
|
1349
|
+
# :invalid => nil # raise error on invalid byte sequence (default)
|
|
1350
|
+
# :invalid => :replace # replace invalid byte sequence
|
|
1351
|
+
# :undef => nil # raise error on undefined conversion (default)
|
|
1352
|
+
# :undef => :replace # replace undefined conversion
|
|
1353
|
+
# :replace => string # replacement string ("?" or "\uFFFD" if not specified)
|
|
1354
|
+
# :newline => :universal # decorator for converting CRLF and CR to LF
|
|
1355
|
+
# :newline => :crlf # decorator for converting LF to CRLF
|
|
1356
|
+
# :newline => :cr # decorator for converting LF to CR
|
|
1357
|
+
# :universal_newline => true # decorator for converting CRLF and CR to LF
|
|
1358
|
+
# :crlf_newline => true # decorator for converting LF to CRLF
|
|
1359
|
+
# :cr_newline => true # decorator for converting LF to CR
|
|
1360
|
+
# :xml => :text # escape as XML CharData.
|
|
1361
|
+
# :xml => :attr # escape as XML AttValue
|
|
1362
|
+
# integer form:
|
|
1363
|
+
# Encoding::Converter::INVALID_REPLACE
|
|
1364
|
+
# Encoding::Converter::UNDEF_REPLACE
|
|
1365
|
+
# Encoding::Converter::UNDEF_HEX_CHARREF
|
|
1366
|
+
# Encoding::Converter::UNIVERSAL_NEWLINE_DECORATOR
|
|
1367
|
+
# Encoding::Converter::CRLF_NEWLINE_DECORATOR
|
|
1368
|
+
# Encoding::Converter::CR_NEWLINE_DECORATOR
|
|
1369
|
+
# Encoding::Converter::XML_TEXT_DECORATOR
|
|
1370
|
+
# Encoding::Converter::XML_ATTR_CONTENT_DECORATOR
|
|
1371
|
+
# Encoding::Converter::XML_ATTR_QUOTE_DECORATOR
|
|
1372
|
+
#
|
|
1373
|
+
# Encoding::Converter.new creates an instance of Encoding::Converter.
|
|
1374
|
+
#
|
|
1375
|
+
# Source_encoding and destination_encoding should be a string or Encoding
|
|
1376
|
+
# object.
|
|
1377
|
+
#
|
|
1378
|
+
# opt should be nil, a hash or an integer.
|
|
1379
|
+
#
|
|
1380
|
+
# convpath should be an array. convpath may contain
|
|
1381
|
+
# * two-element arrays which contain encodings or encoding names, or
|
|
1382
|
+
# * strings representing decorator names.
|
|
1383
|
+
#
|
|
1384
|
+
#
|
|
1385
|
+
# Encoding::Converter.new optionally takes an option. The option should be a
|
|
1386
|
+
# hash or an integer. The option hash can contain :invalid => nil, etc. The
|
|
1387
|
+
# option integer should be logical-or of constants such as
|
|
1388
|
+
# Encoding::Converter::INVALID_REPLACE, etc.
|
|
1389
|
+
#
|
|
1390
|
+
# :invalid => nil
|
|
1391
|
+
# : Raise error on invalid byte sequence. This is a default behavior.
|
|
1392
|
+
# :invalid => :replace
|
|
1393
|
+
# : Replace invalid byte sequence by replacement string.
|
|
1394
|
+
# :undef => nil
|
|
1395
|
+
# : Raise an error if a character in source_encoding is not defined in
|
|
1396
|
+
# destination_encoding. This is a default behavior.
|
|
1397
|
+
# :undef => :replace
|
|
1398
|
+
# : Replace undefined character in destination_encoding with replacement
|
|
1399
|
+
# string.
|
|
1400
|
+
# :replace => string
|
|
1401
|
+
# : Specify the replacement string. If not specified, "uFFFD" is used for
|
|
1402
|
+
# Unicode encodings and "?" for others.
|
|
1403
|
+
# :universal_newline => true
|
|
1404
|
+
# : Convert CRLF and CR to LF.
|
|
1405
|
+
# :crlf_newline => true
|
|
1406
|
+
# : Convert LF to CRLF.
|
|
1407
|
+
# :cr_newline => true
|
|
1408
|
+
# : Convert LF to CR.
|
|
1409
|
+
# :xml => :text
|
|
1410
|
+
# : Escape as XML CharData. This form can be used as an HTML 4.0 #PCDATA.
|
|
1411
|
+
# * '&' -> '&'
|
|
1412
|
+
# * '<' -> '<'
|
|
1413
|
+
# * '>' -> '>'
|
|
1414
|
+
# * undefined characters in destination_encoding -> hexadecimal CharRef
|
|
1415
|
+
# such as &#xHH;
|
|
1416
|
+
#
|
|
1417
|
+
# :xml => :attr
|
|
1418
|
+
# : Escape as XML AttValue. The converted result is quoted as "...". This form
|
|
1419
|
+
# can be used as an HTML 4.0 attribute value.
|
|
1420
|
+
# * '&' -> '&'
|
|
1421
|
+
# * '<' -> '<'
|
|
1422
|
+
# * '>' -> '>'
|
|
1423
|
+
# * '"' -> '"'
|
|
1424
|
+
# * undefined characters in destination_encoding -> hexadecimal CharRef
|
|
1425
|
+
# such as &#xHH;
|
|
1426
|
+
#
|
|
1427
|
+
#
|
|
1428
|
+
#
|
|
1429
|
+
# Examples:
|
|
1430
|
+
# # UTF-16BE to UTF-8
|
|
1431
|
+
# ec = Encoding::Converter.new("UTF-16BE", "UTF-8")
|
|
1432
|
+
#
|
|
1433
|
+
# # Usually, decorators such as newline conversion are inserted last.
|
|
1434
|
+
# ec = Encoding::Converter.new("UTF-16BE", "UTF-8", :universal_newline => true)
|
|
1435
|
+
# p ec.convpath #=> [[#<Encoding:UTF-16BE>, #<Encoding:UTF-8>],
|
|
1436
|
+
# # "universal_newline"]
|
|
1437
|
+
#
|
|
1438
|
+
# # But, if the last encoding is ASCII incompatible,
|
|
1439
|
+
# # decorators are inserted before the last conversion.
|
|
1440
|
+
# ec = Encoding::Converter.new("UTF-8", "UTF-16BE", :crlf_newline => true)
|
|
1441
|
+
# p ec.convpath #=> ["crlf_newline",
|
|
1442
|
+
# # [#<Encoding:UTF-8>, #<Encoding:UTF-16BE>]]
|
|
1443
|
+
#
|
|
1444
|
+
# # Conversion path can be specified directly.
|
|
1445
|
+
# ec = Encoding::Converter.new(["universal_newline", ["EUC-JP", "UTF-8"], ["UTF-8", "UTF-16BE"]])
|
|
1446
|
+
# p ec.convpath #=> ["universal_newline",
|
|
1447
|
+
# # [#<Encoding:EUC-JP>, #<Encoding:UTF-8>],
|
|
1448
|
+
# # [#<Encoding:UTF-8>, #<Encoding:UTF-16BE>]]
|
|
1449
|
+
#
|
|
1450
|
+
def initialize: (encoding source, encoding destination) -> void
|
|
1451
|
+
| (encoding source, encoding destination,
|
|
1452
|
+
?invalid: :replace | nil,
|
|
1453
|
+
?undef: :replace | nil,
|
|
1454
|
+
?replace: String,
|
|
1455
|
+
?newline: :universal | :crlf | :cr,
|
|
1456
|
+
?universal_newline: bool,
|
|
1457
|
+
?crlf_newline: bool,
|
|
1458
|
+
?cr_newline: bool,
|
|
1459
|
+
?xml: :text | :attr
|
|
1460
|
+
) -> void
|
|
1461
|
+
| (encoding source, encoding destination, Integer opts) -> void
|
|
1462
|
+
| (conversion_path convpath) -> void
|
|
571
1463
|
end
|
|
572
1464
|
|
|
1465
|
+
# <!-- rdoc-file=transcode.c -->
|
|
1466
|
+
# AFTER_OUTPUT
|
|
1467
|
+
#
|
|
1468
|
+
# Stop converting after some output is complete but before all of the input was
|
|
1469
|
+
# consumed. See primitive_convert for an example.
|
|
1470
|
+
#
|
|
573
1471
|
Encoding::Converter::AFTER_OUTPUT: Integer
|
|
574
1472
|
|
|
1473
|
+
# <!-- rdoc-file=transcode.c -->
|
|
1474
|
+
# CRLF_NEWLINE_DECORATOR
|
|
1475
|
+
#
|
|
1476
|
+
# Decorator for converting LF to CRLF
|
|
1477
|
+
#
|
|
575
1478
|
Encoding::Converter::CRLF_NEWLINE_DECORATOR: Integer
|
|
576
1479
|
|
|
1480
|
+
# <!-- rdoc-file=transcode.c -->
|
|
1481
|
+
# CR_NEWLINE_DECORATOR
|
|
1482
|
+
#
|
|
1483
|
+
# Decorator for converting LF to CR
|
|
1484
|
+
#
|
|
577
1485
|
Encoding::Converter::CR_NEWLINE_DECORATOR: Integer
|
|
578
1486
|
|
|
1487
|
+
# <!-- rdoc-file=transcode.c -->
|
|
1488
|
+
# INVALID_MASK
|
|
1489
|
+
#
|
|
1490
|
+
# Mask for invalid byte sequences
|
|
1491
|
+
#
|
|
579
1492
|
Encoding::Converter::INVALID_MASK: Integer
|
|
580
1493
|
|
|
1494
|
+
# <!-- rdoc-file=transcode.c -->
|
|
1495
|
+
# INVALID_REPLACE
|
|
1496
|
+
#
|
|
1497
|
+
# Replace invalid byte sequences
|
|
1498
|
+
#
|
|
581
1499
|
Encoding::Converter::INVALID_REPLACE: Integer
|
|
582
1500
|
|
|
1501
|
+
# <!-- rdoc-file=transcode.c -->
|
|
1502
|
+
# PARTIAL_INPUT
|
|
1503
|
+
#
|
|
1504
|
+
# Indicates the source may be part of a larger string. See primitive_convert
|
|
1505
|
+
# for an example.
|
|
1506
|
+
#
|
|
583
1507
|
Encoding::Converter::PARTIAL_INPUT: Integer
|
|
584
1508
|
|
|
1509
|
+
# <!-- rdoc-file=transcode.c -->
|
|
1510
|
+
# UNDEF_HEX_CHARREF
|
|
1511
|
+
#
|
|
1512
|
+
# Replace byte sequences that are undefined in the destination encoding with an
|
|
1513
|
+
# XML hexadecimal character reference. This is valid for XML conversion.
|
|
1514
|
+
#
|
|
585
1515
|
Encoding::Converter::UNDEF_HEX_CHARREF: Integer
|
|
586
1516
|
|
|
1517
|
+
# <!-- rdoc-file=transcode.c -->
|
|
1518
|
+
# UNDEF_MASK
|
|
1519
|
+
#
|
|
1520
|
+
# Mask for a valid character in the source encoding but no related character(s)
|
|
1521
|
+
# in destination encoding.
|
|
1522
|
+
#
|
|
587
1523
|
Encoding::Converter::UNDEF_MASK: Integer
|
|
588
1524
|
|
|
1525
|
+
# <!-- rdoc-file=transcode.c -->
|
|
1526
|
+
# UNDEF_REPLACE
|
|
1527
|
+
#
|
|
1528
|
+
# Replace byte sequences that are undefined in the destination encoding.
|
|
1529
|
+
#
|
|
589
1530
|
Encoding::Converter::UNDEF_REPLACE: Integer
|
|
590
1531
|
|
|
1532
|
+
# <!-- rdoc-file=transcode.c -->
|
|
1533
|
+
# UNIVERSAL_NEWLINE_DECORATOR
|
|
1534
|
+
#
|
|
1535
|
+
# Decorator for converting CRLF and CR to LF
|
|
1536
|
+
#
|
|
591
1537
|
Encoding::Converter::UNIVERSAL_NEWLINE_DECORATOR: Integer
|
|
592
1538
|
|
|
1539
|
+
# <!-- rdoc-file=transcode.c -->
|
|
1540
|
+
# XML_ATTR_CONTENT_DECORATOR
|
|
1541
|
+
#
|
|
1542
|
+
# Escape as XML AttValue
|
|
1543
|
+
#
|
|
593
1544
|
Encoding::Converter::XML_ATTR_CONTENT_DECORATOR: Integer
|
|
594
1545
|
|
|
1546
|
+
# <!-- rdoc-file=transcode.c -->
|
|
1547
|
+
# XML_ATTR_QUOTE_DECORATOR
|
|
1548
|
+
#
|
|
1549
|
+
# Escape as XML AttValue
|
|
1550
|
+
#
|
|
595
1551
|
Encoding::Converter::XML_ATTR_QUOTE_DECORATOR: Integer
|
|
596
1552
|
|
|
1553
|
+
# <!-- rdoc-file=transcode.c -->
|
|
1554
|
+
# XML_TEXT_DECORATOR
|
|
1555
|
+
#
|
|
1556
|
+
# Escape as XML CharData
|
|
1557
|
+
#
|
|
597
1558
|
Encoding::Converter::XML_TEXT_DECORATOR: Integer
|
|
598
1559
|
|
|
1560
|
+
# <!-- rdoc-file=error.c -->
|
|
1561
|
+
# Raised by Encoding and String methods when the source encoding is incompatible
|
|
1562
|
+
# with the target encoding.
|
|
1563
|
+
#
|
|
599
1564
|
class Encoding::CompatibilityError < EncodingError
|
|
600
1565
|
end
|
|
601
1566
|
|
|
1567
|
+
# <!-- rdoc-file=transcode.c -->
|
|
1568
|
+
# Raised by transcoding methods when a named encoding does not correspond with a
|
|
1569
|
+
# known converter.
|
|
1570
|
+
#
|
|
602
1571
|
class Encoding::ConverterNotFoundError < EncodingError
|
|
603
1572
|
end
|
|
604
1573
|
|
|
1574
|
+
# <!-- rdoc-file=transcode.c -->
|
|
1575
|
+
# Raised by Encoding and String methods when the string being transcoded
|
|
1576
|
+
# contains a byte invalid for the either the source or target encoding.
|
|
1577
|
+
#
|
|
605
1578
|
class Encoding::InvalidByteSequenceError < EncodingError
|
|
1579
|
+
public
|
|
1580
|
+
|
|
1581
|
+
# <!--
|
|
1582
|
+
# rdoc-file=transcode.c
|
|
1583
|
+
# - ecerr.destination_encoding -> string
|
|
1584
|
+
# -->
|
|
1585
|
+
# Returns the destination encoding as an encoding object.
|
|
1586
|
+
#
|
|
1587
|
+
def destination_encoding: () -> Encoding
|
|
1588
|
+
|
|
1589
|
+
# <!--
|
|
1590
|
+
# rdoc-file=transcode.c
|
|
1591
|
+
# - ecerr.destination_encoding_name -> string
|
|
1592
|
+
# -->
|
|
1593
|
+
# Returns the destination encoding name as a string.
|
|
1594
|
+
#
|
|
1595
|
+
def destination_encoding_name: () -> String
|
|
1596
|
+
|
|
1597
|
+
# <!--
|
|
1598
|
+
# rdoc-file=transcode.c
|
|
1599
|
+
# - ecerr.error_bytes -> string
|
|
1600
|
+
# -->
|
|
1601
|
+
# Returns the discarded bytes when Encoding::InvalidByteSequenceError occurs.
|
|
1602
|
+
#
|
|
1603
|
+
# ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1")
|
|
1604
|
+
# begin
|
|
1605
|
+
# ec.convert("abc\xA1\xFFdef")
|
|
1606
|
+
# rescue Encoding::InvalidByteSequenceError
|
|
1607
|
+
# p $! #=> #<Encoding::InvalidByteSequenceError: "\xA1" followed by "\xFF" on EUC-JP>
|
|
1608
|
+
# puts $!.error_bytes.dump #=> "\xA1"
|
|
1609
|
+
# puts $!.readagain_bytes.dump #=> "\xFF"
|
|
1610
|
+
# end
|
|
1611
|
+
#
|
|
1612
|
+
def error_bytes: () -> String
|
|
1613
|
+
|
|
1614
|
+
# <!--
|
|
1615
|
+
# rdoc-file=transcode.c
|
|
1616
|
+
# - ecerr.incomplete_input? -> true or false
|
|
1617
|
+
# -->
|
|
1618
|
+
# Returns true if the invalid byte sequence error is caused by premature end of
|
|
1619
|
+
# string.
|
|
1620
|
+
#
|
|
1621
|
+
# ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1")
|
|
1622
|
+
#
|
|
1623
|
+
# begin
|
|
1624
|
+
# ec.convert("abc\xA1z")
|
|
1625
|
+
# rescue Encoding::InvalidByteSequenceError
|
|
1626
|
+
# p $! #=> #<Encoding::InvalidByteSequenceError: "\xA1" followed by "z" on EUC-JP>
|
|
1627
|
+
# p $!.incomplete_input? #=> false
|
|
1628
|
+
# end
|
|
1629
|
+
#
|
|
1630
|
+
# begin
|
|
1631
|
+
# ec.convert("abc\xA1")
|
|
1632
|
+
# ec.finish
|
|
1633
|
+
# rescue Encoding::InvalidByteSequenceError
|
|
1634
|
+
# p $! #=> #<Encoding::InvalidByteSequenceError: incomplete "\xA1" on EUC-JP>
|
|
1635
|
+
# p $!.incomplete_input? #=> true
|
|
1636
|
+
# end
|
|
1637
|
+
#
|
|
1638
|
+
def incomplete_input?: () -> bool
|
|
1639
|
+
|
|
1640
|
+
# <!--
|
|
1641
|
+
# rdoc-file=transcode.c
|
|
1642
|
+
# - ecerr.readagain_bytes -> string
|
|
1643
|
+
# -->
|
|
1644
|
+
# Returns the bytes to be read again when Encoding::InvalidByteSequenceError
|
|
1645
|
+
# occurs.
|
|
1646
|
+
#
|
|
1647
|
+
def readagain_bytes: () -> String
|
|
1648
|
+
|
|
1649
|
+
# <!--
|
|
1650
|
+
# rdoc-file=transcode.c
|
|
1651
|
+
# - ecerr.source_encoding -> encoding
|
|
1652
|
+
# -->
|
|
1653
|
+
# Returns the source encoding as an encoding object.
|
|
1654
|
+
#
|
|
1655
|
+
# Note that the result may not be equal to the source encoding of the encoding
|
|
1656
|
+
# converter if the conversion has multiple steps.
|
|
1657
|
+
#
|
|
1658
|
+
# ec = Encoding::Converter.new("ISO-8859-1", "EUC-JP") # ISO-8859-1 -> UTF-8 -> EUC-JP
|
|
1659
|
+
# begin
|
|
1660
|
+
# ec.convert("\xa0") # NO-BREAK SPACE, which is available in UTF-8 but not in EUC-JP.
|
|
1661
|
+
# rescue Encoding::UndefinedConversionError
|
|
1662
|
+
# p $!.source_encoding #=> #<Encoding:UTF-8>
|
|
1663
|
+
# p $!.destination_encoding #=> #<Encoding:EUC-JP>
|
|
1664
|
+
# p $!.source_encoding_name #=> "UTF-8"
|
|
1665
|
+
# p $!.destination_encoding_name #=> "EUC-JP"
|
|
1666
|
+
# end
|
|
1667
|
+
#
|
|
1668
|
+
def source_encoding: () -> Encoding
|
|
1669
|
+
|
|
1670
|
+
# <!--
|
|
1671
|
+
# rdoc-file=transcode.c
|
|
1672
|
+
# - ecerr.source_encoding_name -> string
|
|
1673
|
+
# -->
|
|
1674
|
+
# Returns the source encoding name as a string.
|
|
1675
|
+
#
|
|
1676
|
+
def source_encoding_name: () -> String
|
|
606
1677
|
end
|
|
607
1678
|
|
|
1679
|
+
# <!-- rdoc-file=transcode.c -->
|
|
1680
|
+
# Raised by Encoding and String methods when a transcoding operation fails.
|
|
1681
|
+
#
|
|
608
1682
|
class Encoding::UndefinedConversionError < EncodingError
|
|
1683
|
+
public
|
|
1684
|
+
|
|
1685
|
+
# <!--
|
|
1686
|
+
# rdoc-file=transcode.c
|
|
1687
|
+
# - ecerr.destination_encoding -> string
|
|
1688
|
+
# -->
|
|
1689
|
+
# Returns the destination encoding as an encoding object.
|
|
1690
|
+
#
|
|
1691
|
+
def destination_encoding: () -> Encoding
|
|
1692
|
+
|
|
1693
|
+
# <!--
|
|
1694
|
+
# rdoc-file=transcode.c
|
|
1695
|
+
# - ecerr.destination_encoding_name -> string
|
|
1696
|
+
# -->
|
|
1697
|
+
# Returns the destination encoding name as a string.
|
|
1698
|
+
#
|
|
1699
|
+
def destination_encoding_name: () -> String
|
|
1700
|
+
|
|
1701
|
+
# <!--
|
|
1702
|
+
# rdoc-file=transcode.c
|
|
1703
|
+
# - ecerr.error_char -> string
|
|
1704
|
+
# -->
|
|
1705
|
+
# Returns the one-character string which cause
|
|
1706
|
+
# Encoding::UndefinedConversionError.
|
|
1707
|
+
#
|
|
1708
|
+
# ec = Encoding::Converter.new("ISO-8859-1", "EUC-JP")
|
|
1709
|
+
# begin
|
|
1710
|
+
# ec.convert("\xa0")
|
|
1711
|
+
# rescue Encoding::UndefinedConversionError
|
|
1712
|
+
# puts $!.error_char.dump #=> "\xC2\xA0"
|
|
1713
|
+
# p $!.error_char.encoding #=> #<Encoding:UTF-8>
|
|
1714
|
+
# end
|
|
1715
|
+
#
|
|
1716
|
+
def error_char: () -> String
|
|
1717
|
+
|
|
1718
|
+
# <!--
|
|
1719
|
+
# rdoc-file=transcode.c
|
|
1720
|
+
# - ecerr.source_encoding -> encoding
|
|
1721
|
+
# -->
|
|
1722
|
+
# Returns the source encoding as an encoding object.
|
|
1723
|
+
#
|
|
1724
|
+
# Note that the result may not be equal to the source encoding of the encoding
|
|
1725
|
+
# converter if the conversion has multiple steps.
|
|
1726
|
+
#
|
|
1727
|
+
# ec = Encoding::Converter.new("ISO-8859-1", "EUC-JP") # ISO-8859-1 -> UTF-8 -> EUC-JP
|
|
1728
|
+
# begin
|
|
1729
|
+
# ec.convert("\xa0") # NO-BREAK SPACE, which is available in UTF-8 but not in EUC-JP.
|
|
1730
|
+
# rescue Encoding::UndefinedConversionError
|
|
1731
|
+
# p $!.source_encoding #=> #<Encoding:UTF-8>
|
|
1732
|
+
# p $!.destination_encoding #=> #<Encoding:EUC-JP>
|
|
1733
|
+
# p $!.source_encoding_name #=> "UTF-8"
|
|
1734
|
+
# p $!.destination_encoding_name #=> "EUC-JP"
|
|
1735
|
+
# end
|
|
1736
|
+
#
|
|
1737
|
+
def source_encoding: () -> Encoding
|
|
1738
|
+
|
|
1739
|
+
# <!--
|
|
1740
|
+
# rdoc-file=transcode.c
|
|
1741
|
+
# - ecerr.source_encoding_name -> string
|
|
1742
|
+
# -->
|
|
1743
|
+
# Returns the source encoding name as a string.
|
|
1744
|
+
#
|
|
1745
|
+
def source_encoding_name: () -> String
|
|
609
1746
|
end
|