1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- ADA.STRINGS.UTF_ENCODING.STRINGS --
9 -- Copyright (C) 2010-2012, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 package body Ada
.Strings
.UTF_Encoding
.Strings
is
39 -- Decode UTF-8/UTF-16BE/UTF-16LE input to String
43 Input_Scheme
: Encoding_Scheme
) return String
46 if Input_Scheme
= UTF_8
then
49 return Decode
(To_UTF_16
(Item
, Input_Scheme
));
53 -- Decode UTF-8 input to String
55 function Decode
(Item
: UTF_8_String
) return String is
56 Result
: String (1 .. Item
'Length);
57 -- Result string (worst case is same length as input)
60 -- Length of result stored so far
68 procedure Get_Continuation
;
69 -- Reads a continuation byte of the form 10xxxxxx, shifts R left
70 -- by 6 bits, and or's in the xxxxxx to the low order 6 bits. On
71 -- return Ptr is incremented. Raises exception if continuation
72 -- byte does not exist or is invalid.
74 ----------------------
75 -- Get_Continuation --
76 ----------------------
78 procedure Get_Continuation
is
80 if Iptr
> Item
'Last then
81 Raise_Encoding_Error
(Iptr
- 1);
84 C
:= To_Unsigned_8
(Item
(Iptr
));
87 if C
not in 2#
10_000000#
.. 2#
10_111111#
then
88 Raise_Encoding_Error
(Iptr
- 1);
90 R
:= Shift_Left
(R
, 6) or Unsigned_16
(C
and 2#
00_111111#
);
95 -- Start of processing for Decode
103 and then Item
(Iptr
.. Iptr
+ 2) = BOM_8
109 elsif Item
'Length >= 2
110 and then (Item
(Iptr
.. Iptr
+ 1) = BOM_16BE
112 Item
(Iptr
.. Iptr
+ 1) = BOM_16LE
)
114 Raise_Encoding_Error
(Iptr
);
117 while Iptr
<= Item
'Last loop
118 C
:= To_Unsigned_8
(Item
(Iptr
));
121 -- Codes in the range 16#00# - 16#7F# are represented as
125 R
:= Unsigned_16
(C
);
127 -- No initial code can be of the form 10xxxxxx. Such codes are used
128 -- only for continuations.
130 elsif C
<= 2#
10_111111#
then
131 Raise_Encoding_Error
(Iptr
- 1);
133 -- Codes in the range 16#80# - 16#7FF# are represented as
136 elsif C
<= 2#
110_11111#
then
137 R
:= Unsigned_16
(C
and 2#
000_11111#
);
140 -- Codes in the range 16#800# - 16#FFFF# are represented as
141 -- 1110yyyy 10yyyyxx 10xxxxxx
143 -- Such codes are out of range for type Character
145 -- Codes in the range 16#10000# - 16#10FFFF# are represented as
146 -- 11110zzz 10zzyyyy 10yyyyxx 10xxxxxx
148 -- Such codes are out of range for Wide_String output
150 -- Thus all remaining cases raise Encoding_Error
153 Raise_Encoding_Error
(Iptr
- 1);
157 Result
(Len
) := Character'Val (R
);
160 return Result
(1 .. Len
);
163 -- Decode UTF-16 input to String
165 function Decode
(Item
: UTF_16_Wide_String
) return String is
166 Result
: String (1 .. Item
'Length);
167 -- Result is same length as input (possibly minus 1 if BOM present)
173 -- Index of next Item element
178 -- Skip UTF-16 BOM at start
182 if Item
'Length > 0 and then Item
(Iptr
) = BOM_16
(1) then
186 -- Loop through input characters
188 while Iptr
<= Item
'Last loop
189 C
:= To_Unsigned_16
(Item
(Iptr
));
192 -- Codes in the range 16#0000#..16#00FF# represent their own value
194 if C
<= 16#
00FF#
then
196 Result
(Len
) := Character'Val (C
);
198 -- All other codes are invalid, either they are invalid UTF-16
199 -- encoding sequences, or they represent values that are out of
200 -- range for type Character.
203 Raise_Encoding_Error
(Iptr
- 1);
207 return Result
(1 .. Len
);
214 -- Encode String in UTF-8, UTF-16BE or UTF-16LE
218 Output_Scheme
: Encoding_Scheme
;
219 Output_BOM
: Boolean := False) return UTF_String
224 if Output_Scheme
= UTF_8
then
225 return Encode
(Item
, Output_BOM
);
227 -- Case of UTF_16LE or UTF_16BE, use UTF-16 intermediary
230 return From_UTF_16
(UTF_16_Wide_String
'(Encode (Item)),
231 Output_Scheme, Output_BOM);
235 -- Encode String in UTF-8
239 Output_BOM : Boolean := False) return UTF_8_String
241 Result : UTF_8_String (1 .. 3 * Item'Length + 3);
242 -- Worst case is three bytes per input byte + space for BOM
245 -- Number of output codes stored in Result
248 -- Single input character
250 procedure Store (C : Unsigned_8);
251 pragma Inline (Store);
252 -- Store one output code, C is in the range 0 .. 255
258 procedure Store (C : Unsigned_8) is
261 Result (Len) := Character'Val (C);
264 -- Start of processing for UTF8_Encode
267 -- Output BOM if required
270 Result (1 .. 3) := BOM_8;
276 -- Loop through characters of input
278 for J in Item'Range loop
279 C := To_Unsigned_8 (Item (J));
281 -- Codes in the range 16#00# - 16#7F# are represented as
287 -- Codes in the range 16#80# - 16#7FF# are represented as
290 -- For type character of course, the limit is 16#FF# in any case
293 Store (2#110_00000# or Shift_Right (C, 6));
294 Store (2#10_000000# or (C and 2#00_111111#));
298 return Result (1 .. Len);
301 -- Encode String in UTF-16
305 Output_BOM : Boolean := False) return UTF_16_Wide_String
307 Result : UTF_16_Wide_String
308 (1 .. Item'Length + Boolean'Pos (Output_BOM));
309 -- Output is same length as input + possible BOM
312 -- Length of output string
317 -- Output BOM if required
320 Result (1) := BOM_16 (1);
326 -- Loop through input characters encoding them
328 for Iptr in Item'Range loop
329 C := To_Unsigned_8 (Item (Iptr));
331 -- Codes in the range 16#0000#..16#00FF# are output unchanged. This
332 -- includes all possible cases of Character values.
335 Result (Len) := Wide_Character'Val (C);
341 end Ada.Strings.UTF_Encoding.Strings;