1 # This file is part of NIT (http://www.nitlanguage.org).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Codec for ISO8859-1 I/O
21 private class ISO88591Codec
24 redef fun char_max_size
do return 1
26 redef fun codet_size
do return 1
28 redef fun max_lookahead
do return 1
30 redef fun encode_char
(c
) do
31 var ns
= new NativeString(c
.u8char_len
)
36 redef fun add_char_to
(c
, stream
) do
37 var cp
= if c
.code_point
<= 255 then c
else '?'
42 redef fun encode_string
(s
) do
43 var ns
= new Bytes.with_capacity
(s
.byte_length
)
48 redef fun add_string_to
(s
, b
) do
62 redef fun is_valid_char
(ns
, len
) do
66 redef fun decode_char
(b
) do
67 return b
[0].to_i
.code_point
70 redef fun decode_string
(b
, len
) do
71 var buf
= new Bytes.with_capacity
(len
)
72 for i
in [0 .. len
[ do buf
.add_char
(b
[i
].to_i
.code_point
)
77 # Returns the instance of a ISO8859-1 Codec
78 fun iso88591_codec
: Codec do return once
new ISO88591Codec