781284b67a4c3226a3a386950b884ed78ec0f2b7
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 # Introduces UTF-8 as internal encoding for Strings in Nit.
18 intrude import standard
::string
33 # UTF-8 char as defined in RFC-3629, e.g. 1-4 Bytes
35 # A UTF-8 char has its bytes stored in a NativeString (char*)
36 extern class UnicodeChar `{ UTF8Char* `}
38 new(pos: Int, ns: NativeString) `{
39 UTF8Char* u
= malloc
(sizeof
(UTF8Char));
45 # Real length of the char in UTF8
47 # As per the specification :
49 # Length | UTF-8 octet sequence
51 # ---------+-------------------------------------------------
53 # 2 | 110xxxxx 10xxxxxx
54 # 3 | 1110xxxx 10xxxxxx 10xxxxxx
55 # 4 | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
56 private fun len: Int `{
60 if((nspos
& 0x80) == 0x00){ return 1;}
61 if((nspos
& 0xE0) == 0xC0){ return 2;}
62 if((nspos
& 0xF0) == 0xE0){ return 3;}
63 if((nspos
& 0xF7) == 0xF0){ return 4;}
68 # Position in containing NativeString
69 private fun pos: Int `{
73 private fun pos=(p: Int) `{recv->pos = p;`}
75 # C char* wrapping the char
76 fun ns
: NativeString `{
80 redef fun to_s
import NativeString.to_s_with_length
`{
81 int len = utf8___UnicodeChar_len___impl(recv);
82 char* r = malloc(len + 1);
84 char* src = (recv->ns + recv->pos);
86 return NativeString_to_s_with_length(r, len);
90 # A `StringIndex` is used to keep track of the position of characters in a `FlatString` object
92 # It becomes mandatory for UTF-8 strings since characters do not have a fixed size.
93 private extern class StringIndex `{ UTF8Char* `}
95 new(size: Int) `{ return malloc(size*sizeof(UTF8Char)); `}
97 # Sets the character at `index` as `item`
98 fun []=(index
: Int, item
: UnicodeChar) `{ recv[index] = *item; `}
100 # Gets the character at position `id
`
101 fun [](id: Int): UnicodeChar `{ return &recv[id]; `}
103 # Copies a part of self starting at index `my_from` of length `length` into `other`, starting at `its_from`
104 fun copy_to
(other
: StringIndex, my_from
: Int, its_from
: Int, length
: Int)`{
105 UTF8Char* myfrom = recv + my_from*(sizeof(UTF8Char));
106 UTF8Char* itsfrom = other + its_from*(sizeof(UTF8Char));
107 memcpy(itsfrom, myfrom, length);
111 redef class FlatString
113 # Index of the characters of the FlatString
114 private var index
: StringIndex
116 # Length in bytes of the string (e.g. the length of the C string)
119 private init with_infos_index
(items
: NativeString, len
: Int, index_from
: Int, index_to
: Int, index
: StringIndex, bytelen
: Int)
123 self.index_from
= index_from
124 self.index_to
= index_to
126 self.bytelen
= bytelen
133 var mylen
= self.bytelen
134 var finlen
= mylen
* i
136 var my_items
= self.items
138 var my_real_len
= length
139 var my_real_fin_len
= my_real_len
* i
141 var target_string
= calloc_string
((finlen
) + 1)
144 var new_index
= new StringIndex(my_real_fin_len
)
146 target_string
[finlen
] = '\0'
151 for iteration
in [1 .. i
] do
152 my_items
.copy_to
(target_string
, mylen
, index_from
, current_last
)
153 my_index
.copy_to
(new_index
, length
, 0, curr_index
)
154 current_last
+= mylen
157 return new FlatString.with_infos_index
(target_string
, my_real_fin_len
, 0, my_real_fin_len
-1, new_index
, finlen
)
163 redef class NativeString
165 # Creates the index for said NativeString
166 # `length` is the size of the CString (in bytes, up to the first \0)
167 # real_len is just a way to store the length (UTF-8 characters)
168 private fun make_index
(length
: Int, real_len
: Container[Int]): StringIndex import Container[Int].item
=, UnicodeChar.len
`{
171 UTF8Char* index = malloc(length*sizeof(UTF8Char));
173 UTF8Char* curr = &index[index_pos];
176 pos += UnicodeChar_len(curr);
179 Container_of_Int_item__assign(real_len, index_pos);
183 redef fun to_s
: FlatString
185 var len
= cstring_length
186 return to_s_with_length
(len
)
189 redef fun to_s_with_length
(len
: Int): FlatString
191 var real_len
= new Container[Int](0)
192 var x
= make_index
(len
, real_len
)
193 return new FlatString.with_infos_index
(self, real_len
.item
, 0, real_len
.item
- 1, x
, len
)
196 redef fun to_s_with_copy
198 var real_len
= new Container[Int](0)
199 var length
= cstring_length
200 var x
= make_index
(length
, real_len
)
201 var new_self
= calloc_string
(length
+ 1)
202 copy_to
(new_self
, length
, 0, 0)
203 return new FlatString.with_infos_index
(new_self
, real_len
.item
, 0, real_len
.item
- 1, x
, length
)