3a57641905464562dd73bf15d9a5ac0c86005c3c
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);