1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- G N A T . D E C O D E _ S T R I N G --
9 -- Copyright (C) 2007-2013, AdaCore --
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 -- This generic package provides utility routines for converting from an
33 -- encoded string to a corresponding Wide_String or Wide_Wide_String value
34 -- using a specified encoding convention, which is supplied as the generic
35 -- parameter. UTF-8 is handled especially efficiently, and if the encoding
36 -- method is known at compile time to be WCEM_UTF8, then the instantiation
37 -- is specialized to handle only the UTF-8 case and exclude code for the
38 -- other encoding methods. The package also provides positioning routines
39 -- for skipping encoded characters in either direction, and for validating
40 -- strings for correct encodings.
42 -- Note: this package is only about decoding sequences of 8-bit characters
43 -- into corresponding 16-bit Wide_String or 32-bit Wide_Wide_String values.
44 -- It knows nothing at all about the character encodings being used for the
45 -- resulting Wide_Character and Wide_Wide_Character values. Most often this
46 -- will be Unicode/ISO-10646 as specified by the Ada RM, but this package
47 -- does not make any assumptions about the character coding. See also the
48 -- packages Ada.Wide_[Wide_]Characters.Unicode for unicode specific functions.
50 -- In particular, in the case of UTF-8, all valid UTF-8 encodings, as listed
51 -- in table 3.6 of the Unicode Standard, version 6.2.0, are recognized as
52 -- legitimate. This includes the full range 16#0000_0000# .. 16#03FF_FFFF#.
53 -- This includes codes in the range 16#D800# - 16#DFFF#. These codes all
54 -- have UTF-8 encoding sequences that are well-defined (e.g. the encoding for
55 -- 16#D800# is ED A0 80). But these codes do not correspond to defined Unicode
56 -- characters and are thus considered to be "not well-formed" (see table 3.7
57 -- of the Unicode Standard). If you need to exclude these codes, you must do
58 -- that manually, e.g. use Decode_Wide_Character/Decode_Wide_String and check
59 -- that the resulting code(s) are not in this range.
61 -- Note on the use of brackets encoding (WCEM_Brackets). The brackets encoding
62 -- method is ambiguous in the context of this package, since there is no way
63 -- to tell if ["1234"] is eight unencoded characters or one encoded character.
64 -- In the context of Ada sources, any sequence starting [" must be the start
65 -- of an encoding (since that sequence is not valid in Ada source otherwise).
66 -- The routines in this package use the same approach. If the input string
67 -- contains the sequence [" then this is assumed to be the start of a brackets
68 -- encoding sequence, and if it does not match the syntax, an error is raised.
69 -- In the case of the Prev functions, a sequence ending with "] is assumed to
70 -- be a valid brackets sequence, and an error is raised if it is not.
75 Encoding_Method
: System
.WCh_Con
.WC_Encoding_Method
;
77 package GNAT
.Decode_String
is
80 function Decode_Wide_String
(S
: String) return Wide_String;
81 pragma Inline
(Decode_Wide_String
);
82 -- Decode the given String, which is encoded using the indicated coding
83 -- method, returning the corresponding decoded Wide_String value. If S
84 -- contains a character code that cannot be represented with the given
85 -- encoding, then Constraint_Error is raised.
87 procedure Decode_Wide_String
89 Result
: out Wide_String;
90 Length
: out Natural);
91 -- Similar to the above function except that the result is stored in the
92 -- given Wide_String variable Result, starting at Result (Result'First). On
93 -- return, Length is set to the number of characters stored in Result. The
94 -- caller must ensure that Result is long enough (an easy choice is to set
95 -- the length equal to the S'Length, since decoding can never increase the
96 -- string length). If the length of Result is insufficient Constraint_Error
99 function Decode_Wide_Wide_String
(S
: String) return Wide_Wide_String
;
100 -- Same as above function but for Wide_Wide_String output
102 procedure Decode_Wide_Wide_String
104 Result
: out Wide_Wide_String
;
105 Length
: out Natural);
106 -- Same as above procedure, but for Wide_Wide_String output
108 function Validate_Wide_String
(S
: String) return Boolean;
109 -- This function inspects the string S to determine if it contains only
110 -- valid encodings corresponding to Wide_Character values using the
111 -- given encoding. If a call to Decode_Wide_String (S) would return
112 -- without raising Constraint_Error, then Validate_Wide_String will
113 -- return True. If the call would have raised Constraint_Error, then
114 -- Validate_Wide_String will return False.
116 function Validate_Wide_Wide_String
(S
: String) return Boolean;
117 -- Similar to Validate_Wide_String, except that it succeeds if the string
118 -- contains only encodings corresponding to Wide_Wide_Character values.
120 procedure Decode_Wide_Character
122 Ptr
: in out Natural;
123 Result
: out Wide_Character);
124 pragma Inline
(Decode_Wide_Character
);
125 -- This is a lower level procedure that decodes a single character using
126 -- the given encoding method. The encoded character is stored in Input,
127 -- starting at Input (Ptr). The resulting output character is stored in
128 -- Result, and on return Ptr is updated past the input character or
129 -- encoding sequence. Constraint_Error will be raised if the input has
130 -- has a character that cannot be represented using the given encoding,
131 -- or if Ptr is outside the bounds of the Input string.
133 procedure Decode_Wide_Wide_Character
135 Ptr
: in out Natural;
136 Result
: out Wide_Wide_Character
);
137 pragma Inline
(Decode_Wide_Wide_Character
);
138 -- Same as above procedure but with Wide_Wide_Character input
140 procedure Next_Wide_Character
(Input
: String; Ptr
: in out Natural);
141 pragma Inline
(Next_Wide_Character
);
142 -- This procedure examines the input string starting at Input (Ptr), and
143 -- advances Ptr past one character in the encoded string, so that on return
144 -- Ptr points to the next encoded character. Constraint_Error is raised if
145 -- an invalid encoding is encountered, or the end of the string is reached
146 -- or if Ptr is less than String'First on entry, or if the character
147 -- skipped is not a valid Wide_Character code.
149 procedure Prev_Wide_Character
(Input
: String; Ptr
: in out Natural);
150 -- This procedure is similar to Next_Encoded_Character except that it moves
151 -- backwards in the string, so that on return, Ptr is set to point to the
152 -- previous encoded character. Constraint_Error is raised if the start of
153 -- the string is encountered. It is valid for Ptr to be one past the end
154 -- of the string for this call (in which case on return it will point to
155 -- the last encoded character).
157 -- Note: it is not generally possible to do this function efficiently with
158 -- all encodings, the current implementation is only efficient for the case
159 -- of UTF-8 (Encoding_Method = WCEM_UTF8) and Brackets (Encoding_Method =
160 -- WCEM_Brackets). For all other encodings, we work by starting at the
161 -- beginning of the string and moving forward till Ptr is reached, which
162 -- is correct but slow.
164 -- Note: this routine assumes that the sequence prior to Ptr is correctly
165 -- encoded, it does not have a defined behavior if this is not the case.
167 procedure Next_Wide_Wide_Character
(Input
: String; Ptr
: in out Natural);
168 pragma Inline
(Next_Wide_Wide_Character
);
169 -- Similar to Next_Wide_Character except that codes skipped must be valid
170 -- Wide_Wide_Character codes.
172 procedure Prev_Wide_Wide_Character
(Input
: String; Ptr
: in out Natural);
173 -- Similar to Prev_Wide_Character except that codes skipped must be valid
174 -- Wide_Wide_Character codes.
176 end GNAT
.Decode_String
;