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 # Returns the Unicode code point representing the character
82 # Note : A unicode character might not be a visible glyph, but it will be used to determine canonical equivalence
83 fun code_point
: Int import UnicodeChar.len
`{
84 switch(UnicodeChar_len(recv)){
86 return (long)(0x7F & (unsigned char)recv->ns[recv->pos]);
88 return 0 | ((0x1F & (unsigned char)recv->ns[recv->pos]) << 6) | (0x3F & (unsigned char)recv->ns[recv->pos+1]);
90 return 0 | ((0x0F & (unsigned char)recv->ns[recv->pos]) << 12) |
91 ((0x3F & (unsigned char)recv->ns[recv->pos+1]) << 6) |
92 (0x3F & (unsigned char)recv->ns[recv->pos+2]);
94 return 0 | ((0x07 & (unsigned char)recv->ns[recv->pos]) << 18) |
95 ((0x3F & (unsigned char)recv->ns[recv->pos+1]) << 12) |
96 ((0x3F & (unsigned char)recv->ns[recv->pos+2]) << 6) |
97 (0x3F & (unsigned char)recv->ns[recv->pos+3]);
101 redef fun to_s
import NativeString.to_s_with_length
`{
102 int len = utf8___UnicodeChar_len___impl(recv);
103 char* r = malloc(len + 1);
105 char* src = (recv->ns + recv->pos);
107 return NativeString_to_s_with_length(r, len);
111 # A `StringIndex` is used to keep track of the position of characters in a `FlatString` object
113 # It becomes mandatory for UTF-8 strings since characters do not have a fixed size.
114 private extern class StringIndex `{ UTF8Char* `}
116 new(size: Int) `{ return malloc(size*sizeof(UTF8Char)); `}
118 # Sets the character at `index` as `item`
119 fun []=(index
: Int, item
: UnicodeChar) `{ recv[index] = *item; `}
121 # Gets the character at position `id
`
122 fun [](id: Int): UnicodeChar `{ return &recv[id]; `}
124 # Copies a part of self starting at index `my_from` of length `length` into `other`, starting at `its_from`
125 fun copy_to
(other
: StringIndex, my_from
: Int, its_from
: Int, length
: Int)`{
126 UTF8Char* myfrom = recv + my_from*(sizeof(UTF8Char));
127 UTF8Char* itsfrom = other + its_from*(sizeof(UTF8Char));
128 memcpy(itsfrom, myfrom, length);
132 redef class FlatString
134 # Index of the characters of the FlatString
135 private var index
: StringIndex
137 # Length in bytes of the string (e.g. the length of the C string)
140 private init with_infos_index
(items
: NativeString, len
: Int, index_from
: Int, index_to
: Int, index
: StringIndex, bytelen
: Int)
144 self.index_from
= index_from
145 self.index_to
= index_to
147 self.bytelen
= bytelen
154 var mylen
= self.bytelen
155 var finlen
= mylen
* i
157 var my_items
= self.items
159 var my_real_len
= length
160 var my_real_fin_len
= my_real_len
* i
162 var target_string
= calloc_string
((finlen
) + 1)
165 var new_index
= new StringIndex(my_real_fin_len
)
167 target_string
[finlen
] = '\0'
172 for iteration
in [1 .. i
] do
173 my_items
.copy_to
(target_string
, mylen
, index_from
, current_last
)
174 my_index
.copy_to
(new_index
, length
, 0, curr_index
)
175 current_last
+= mylen
178 return new FlatString.with_infos_index
(target_string
, my_real_fin_len
, 0, my_real_fin_len
-1, new_index
, finlen
)
184 redef class NativeString
186 # Creates the index for said NativeString
187 # `length` is the size of the CString (in bytes, up to the first \0)
188 # real_len is just a way to store the length (UTF-8 characters)
189 private fun make_index
(length
: Int, real_len
: Container[Int]): StringIndex import Container[Int].item
=, UnicodeChar.len
`{
192 UTF8Char* index = malloc(length*sizeof(UTF8Char));
194 UTF8Char* curr = &index[index_pos];
197 pos += UnicodeChar_len(curr);
200 Container_of_Int_item__assign(real_len, index_pos);
204 redef fun to_s
: FlatString
206 var len
= cstring_length
207 return to_s_with_length
(len
)
210 redef fun to_s_with_length
(len
: Int): FlatString
212 var real_len
= new Container[Int](0)
213 var x
= make_index
(len
, real_len
)
214 return new FlatString.with_infos_index
(self, real_len
.item
, 0, real_len
.item
- 1, x
, len
)
217 redef fun to_s_with_copy
219 var real_len
= new Container[Int](0)
220 var length
= cstring_length
221 var x
= make_index
(length
, real_len
)
222 var new_self
= calloc_string
(length
+ 1)
223 copy_to
(new_self
, length
, 0, 0)
224 return new FlatString.with_infos_index
(new_self
, real_len
.item
, 0, real_len
.item
- 1, x
, length
)