21ea8a8d7ad9a9755807e2962c193012ba09966a
[nit.git] / lib / core / codecs / iso8859_1.nit
1 # This file is part of NIT (http://www.nitlanguage.org).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Codec for ISO8859-1 I/O
16 module iso8859_1
17
18 import codec_base
19 intrude import bytes
20
21 private class ISO88591Codec
22 super Codec
23
24 redef fun char_max_size do return 1
25
26 redef fun codet_size do return 1
27
28 redef fun max_lookahead do return 1
29
30 redef fun encode_char(c) do
31 var ns = new NativeString(c.u8char_len)
32 add_char_to(c, ns)
33 return ns
34 end
35
36 redef fun add_char_to(c, stream) do
37 var cp = if c.code_point <= 255 then c else '?'
38 stream[0] = cp.ascii
39 return 1
40 end
41
42 redef fun encode_string(s) do
43 var ns = new Bytes.with_capacity(s.byte_length)
44 add_string_to(s, ns)
45 return ns
46 end
47
48 redef fun add_string_to(s, b) do
49 var pos = 0
50 for i in s.chars do
51 var cp = i.code_point
52 if cp <= 255 then
53 b[pos] = cp.to_b
54 else
55 b[pos] = 0x3Fu8
56 end
57 pos += 1
58 end
59 return pos
60 end
61
62 redef fun is_valid_char(ns, len) do
63 return 0
64 end
65
66 redef fun decode_char(b) do
67 return b[0].to_i.code_point
68 end
69
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)
73 return buf.to_s
74 end
75 end
76
77 # Returns the instance of a ISO8859-1 Codec
78 fun iso88591_codec: Codec do return once new ISO88591Codec