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
19 intrude import standard
::file
34 # UTF-8 char as defined in RFC-3629, e.g. 1-4 Bytes
36 # A UTF-8 char has its bytes stored in a NativeString (char*)
37 extern class UnicodeChar `{ UTF8Char* `}
39 new(pos: Int, ns: NativeString) `{
40 UTF8Char* u
= malloc
(sizeof
(UTF8Char));
46 # Real length of the char in UTF8
48 # As per the specification :
50 # Length | UTF-8 octet sequence
52 # ---------+-------------------------------------------------
54 # 2 | 110xxxxx 10xxxxxx
55 # 3 | 1110xxxx 10xxxxxx 10xxxxxx
56 # 4 | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
57 private fun len: Int `{
61 if((nspos
& 0x80) == 0x00){ return 1;}
62 if((nspos
& 0xE0) == 0xC0){ return 2;}
63 if((nspos
& 0xF0) == 0xE0){ return 3;}
64 if((nspos
& 0xF7) == 0xF0){ return 4;}
69 # Position in containing NativeString
70 private fun pos: Int `{
74 private fun pos=(p: Int) `{recv->pos = p;`}
76 # C char* wrapping the char
77 fun ns
: NativeString `{
81 # Returns the Unicode code point representing the character
83 # Note : A unicode character might not be a visible glyph, but it will be used to determine canonical equivalence
84 fun code_point
: Int import UnicodeChar.len
`{
85 switch(UnicodeChar_len(recv)){
87 return (long)(0x7F & (unsigned char)recv->ns[recv->pos]);
89 return 0 | ((0x1F & (unsigned char)recv->ns[recv->pos]) << 6) | (0x3F & (unsigned char)recv->ns[recv->pos+1]);
91 return 0 | ((0x0F & (unsigned char)recv->ns[recv->pos]) << 12) |
92 ((0x3F & (unsigned char)recv->ns[recv->pos+1]) << 6) |
93 (0x3F & (unsigned char)recv->ns[recv->pos+2]);
95 return 0 | ((0x07 & (unsigned char)recv->ns[recv->pos]) << 18) |
96 ((0x3F & (unsigned char)recv->ns[recv->pos+1]) << 12) |
97 ((0x3F & (unsigned char)recv->ns[recv->pos+2]) << 6) |
98 (0x3F & (unsigned char)recv->ns[recv->pos+3]);
102 # Returns an upper-case version of self
104 # NOTE : Works only on ASCII chars
105 # TODO : Support unicode for to_upper
106 fun to_upper
: UnicodeChar import UnicodeChar.code_point
`{
107 int cp = UnicodeChar_code_point(recv);
108 if(cp < 97 || cp > 122){ return recv; }
109 char* ns = malloc(2);
111 char c = recv->ns[recv->pos];
113 UTF8Char* ret = malloc(sizeof(UTF8Char));
119 # Returns an lower-case version of self
121 # NOTE : Works only on ASCII chars
122 # TODO : Support unicode for to_upper
123 fun to_lower
: UnicodeChar import UnicodeChar.code_point
`{
124 int cp = UnicodeChar_code_point(recv);
125 if(cp < 65 || cp > 90){ return recv; }
126 char* ns = malloc(2);
128 char c = recv->ns[recv->pos];
130 UTF8Char* ret = malloc(sizeof(UTF8Char));
139 if len
!= 1 then return false
140 if code_point
== o
.ascii
then return true
141 else if o
isa UnicodeChar then
142 if len
!= o
.len
then return false
143 if code_point
== o
.code_point
then return true
148 redef fun output
import UnicodeChar.code_point
`{
149 switch(UnicodeChar_len(recv)){
151 printf("%c", recv->ns[recv->pos]);
154 printf("%c%c", recv->ns[recv->pos], recv->ns[recv->pos + 1]);
157 printf("%c%c%c", recv->ns[recv->pos], recv->ns[recv->pos + 1], recv->ns[recv->pos + 2]);
160 printf("%c%c%c%c", recv->ns[recv->pos], recv->ns[recv->pos + 1], recv->ns[recv->pos + 2], recv->ns[recv->pos + 3]);
165 redef fun to_s
import NativeString.to_s_with_length
`{
166 int len = utf8___UnicodeChar_len___impl(recv);
167 char* r = malloc(len + 1);
169 char* src = (recv->ns + recv->pos);
171 return NativeString_to_s_with_length(r, len);
175 # A `StringIndex` is used to keep track of the position of characters in a `FlatString` object
177 # It becomes mandatory for UTF-8 strings since characters do not have a fixed size.
178 private extern class StringIndex `{ UTF8Char* `}
180 new(size: Int) `{ return malloc(size*sizeof(UTF8Char)); `}
182 # Sets the character at `index` as `item`
183 fun []=(index
: Int, item
: UnicodeChar) `{ recv[index] = *item; `}
185 # Gets the character at position `id
`
186 fun [](id: Int): UnicodeChar `{ return &recv[id]; `}
188 # Copies a part of self starting at index `my_from` of length `length` into `other`, starting at `its_from`
189 fun copy_to
(other
: StringIndex, my_from
: Int, its_from
: Int, length
: Int)`{
190 UTF8Char* myfrom = recv + my_from*(sizeof(UTF8Char));
191 UTF8Char* itsfrom = other + its_from*(sizeof(UTF8Char));
192 memcpy(itsfrom, myfrom, length);
196 redef class FlatString
198 # Index of the characters of the FlatString
199 private var index
: StringIndex
201 # Length in bytes of the string (e.g. the length of the C string)
204 private init with_infos_index
(items
: NativeString, len
: Int, index_from
: Int, index_to
: Int, index
: StringIndex, bytelen
: Int)
208 self.index_from
= index_from
209 self.index_to
= index_to
211 self.bytelen
= bytelen
216 if real_items
!= null then return real_items
.as(not null)
217 var new_items
= calloc_string
(bytelen
+ 1)
218 self.items
.copy_to
(new_items
, bytelen
, index
[index_from
].pos
, 0)
219 new_items
[bytelen
] = '\0'
220 self.real_items
= new_items
224 redef fun substring
(from
, count
)
230 if count
< 0 then count
= 0
234 if count
== 0 then return empty
236 var real_from
= index_from
+ from
237 var real_to
= real_from
+ count
- 1
239 if real_to
> index_to
then real_to
= index_to
241 var sub_bytelen
= (index
[real_to
].pos
- index
[from
].pos
) + index
[from
].len
243 return new FlatString.with_infos_index
(items
, count
, real_from
, real_to
, index
, sub_bytelen
)
248 var native
= calloc_string
(self.bytelen
+ 1)
249 var length
= self.length
250 var index
= self.index
254 var new_index
= new StringIndex(length
)
255 var pos_index
= length
258 var uchar_len
= uchar
.len
260 new_index
[pos_index
] = new UnicodeChar(ipos
, native
)
262 items
.copy_to
(native
, uchar_len
, pos
, ipos
)
266 return new FlatString.with_infos_index
(native
, length
, 0, length-1
, new_index
, bytelen
)
273 var mylen
= self.bytelen
274 var finlen
= mylen
* i
276 var my_items
= self.items
278 var my_real_len
= length
279 var my_real_fin_len
= my_real_len
* i
281 var target_string
= calloc_string
((finlen
) + 1)
284 var new_index
= new StringIndex(my_real_fin_len
)
286 target_string
[finlen
] = '\0'
291 for iteration
in [1 .. i
] do
292 my_items
.copy_to
(target_string
, mylen
, index_from
, current_last
)
293 my_index
.copy_to
(new_index
, length
, 0, curr_index
)
294 current_last
+= mylen
297 return new FlatString.with_infos_index
(target_string
, my_real_fin_len
, 0, my_real_fin_len
-1, new_index
, finlen
)
303 var outstr
= calloc_string
(self.bytelen
+ 1)
306 var index
= self.index
309 var items
= self.items
312 var u
= index
[ipos
].to_upper
313 u
.ns
.copy_to
(outstr
, u
.len
, u
.pos
, out_index
)
318 outstr
[self.bytelen
] = '\0'
320 return outstr
.to_s_with_length
(self.bytelen
)
325 var outstr
= calloc_string
(self.bytelen
+ 1)
328 var index
= self.index
331 var items
= self.items
334 var u
= index
[ipos
].to_lower
335 u
.ns
.copy_to
(outstr
, u
.len
, u
.pos
, out_index
)
340 outstr
[self.bytelen
] = '\0'
342 return outstr
.to_s_with_length
(self.bytelen
)
347 var i
= self.index_from
348 var imax
= self.index_to
357 redef class NativeString
359 # Creates the index for said NativeString
360 # `length` is the size of the CString (in bytes, up to the first \0)
361 # real_len is just a way to store the length (UTF-8 characters)
362 private fun make_index
(length
: Int, real_len
: Container[Int]): StringIndex import Container[Int].item
=, UnicodeChar.len
`{
365 UTF8Char* index = malloc(length*sizeof(UTF8Char));
367 UTF8Char* curr = &index[index_pos];
370 pos += UnicodeChar_len(curr);
373 Container_of_Int_item__assign(real_len, index_pos);
377 redef fun to_s
: FlatString
379 var len
= cstring_length
380 return to_s_with_length
(len
)
383 redef fun to_s_with_length
(len
: Int): FlatString
385 var real_len
= new Container[Int](0)
386 var x
= make_index
(len
, real_len
)
387 return new FlatString.with_infos_index
(self, real_len
.item
, 0, real_len
.item
- 1, x
, len
)
390 redef fun to_s_with_copy
392 var real_len
= new Container[Int](0)
393 var length
= cstring_length
394 var x
= make_index
(length
, real_len
)
395 var new_self
= calloc_string
(length
+ 1)
396 copy_to
(new_self
, length
, 0, 0)
397 return new FlatString.with_infos_index
(new_self
, real_len
.item
, 0, real_len
.item
- 1, x
, length
)
405 if s
isa FlatText then
406 if s
isa FlatString then
407 write_native
(s
.to_cstring
, s
.bytelen
)
409 write_native
(s
.to_cstring
, s
.length
)
411 else for i
in s
.substrings
do write_native
(i
.to_cstring
, i
.length
)