⚲
Project
General
Profile
Sign in
Register
Home
Projects
Help
Search
:
Ruby master
All Projects
Ruby
»
Ruby master
Overview
Activity
Roadmap
Issues
Repository
Like
Download (1.08 KB)
Bug #3432
» encoding.rb
Eregon (Benoit Daloze)
, 06/12/2010 07:54 PM
# encoding: utf-8
require
"iconv"
Encoding
.
list
.
each
{
|
e
|
s
=
"UTF-8"
# source encoding
e
=
e
.
to_s
# ok: "Windows-1252" # "macRoman" # "ISO-8859-1" # \\nok "UTF-8-MAC"
#puts "-"*15
#puts e
word
=
"étè"
begin
# forward conversion
rub
=
word
.
encode
(
e
,
s
)
# Ruby conversion
ico
=
Iconv
.
iconv
(
e
,
s
,
word
).
first
# Iconv conversion
puts
"Not the same encoding"
if
rub
.
encoding
!=
ico
.
encoding
puts
"Not the same bytes"
if
rub
.
bytes
.
to_a
!=
ico
.
bytes
.
to_a
# backward conversion (from forward conversion)
r2
=
rub
.
encode
(
s
,
e
)
i2
=
Iconv
.
iconv
(
s
,
e
,
ico
).
first
puts
"Backward conversion: not the same encoding"
if
r2
.
encoding
!=
i2
.
encoding
puts
"
#{
e
}
Backward conversion: not the same bytes:
\n\t
#{
r2
.
bytes
.
to_a
}
!=
#{
i2
.
bytes
.
to_a
}
"
if
r2
.
bytes
.
to_a
!=
i2
.
bytes
.
to_a
puts
"
#{
e
}
Backward conversion: not the same chars:
\n\t
#{
r2
.
chars
.
to_a
}
!=
#{
i2
.
chars
.
to_a
}
"
if
r2
.
chars
.
to_a
!=
i2
.
chars
.
to_a
rescue
Encoding
::
UndefinedConversionError
,
Iconv
::
InvalidEncoding
,
Encoding
::
ConverterNotFoundError
#puts "Not compatible encoding: #{e}"
end
#puts
}
(1-1/1)
Loading...