annotate gcc/ada/libgnat/g-decstr.ads @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents
children 84e7813d76e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 ------------------------------------------------------------------------------
kono
parents:
diff changeset
2 -- --
kono
parents:
diff changeset
3 -- GNAT RUN-TIME COMPONENTS --
kono
parents:
diff changeset
4 -- --
kono
parents:
diff changeset
5 -- G N A T . D E C O D E _ S T R I N G --
kono
parents:
diff changeset
6 -- --
kono
parents:
diff changeset
7 -- S p e c --
kono
parents:
diff changeset
8 -- --
kono
parents:
diff changeset
9 -- Copyright (C) 2007-2017, AdaCore --
kono
parents:
diff changeset
10 -- --
kono
parents:
diff changeset
11 -- GNAT is free software; you can redistribute it and/or modify it under --
kono
parents:
diff changeset
12 -- terms of the GNU General Public License as published by the Free Soft- --
kono
parents:
diff changeset
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
kono
parents:
diff changeset
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
kono
parents:
diff changeset
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
kono
parents:
diff changeset
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
kono
parents:
diff changeset
17 -- --
kono
parents:
diff changeset
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
kono
parents:
diff changeset
19 -- additional permissions described in the GCC Runtime Library Exception, --
kono
parents:
diff changeset
20 -- version 3.1, as published by the Free Software Foundation. --
kono
parents:
diff changeset
21 -- --
kono
parents:
diff changeset
22 -- You should have received a copy of the GNU General Public License and --
kono
parents:
diff changeset
23 -- a copy of the GCC Runtime Library Exception along with this program; --
kono
parents:
diff changeset
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
kono
parents:
diff changeset
25 -- <http://www.gnu.org/licenses/>. --
kono
parents:
diff changeset
26 -- --
kono
parents:
diff changeset
27 -- GNAT was originally developed by the GNAT team at New York University. --
kono
parents:
diff changeset
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
kono
parents:
diff changeset
29 -- --
kono
parents:
diff changeset
30 ------------------------------------------------------------------------------
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 -- This generic package provides utility routines for converting from an
kono
parents:
diff changeset
33 -- encoded string to a corresponding Wide_String or Wide_Wide_String value
kono
parents:
diff changeset
34 -- using a specified encoding convention, which is supplied as the generic
kono
parents:
diff changeset
35 -- parameter. UTF-8 is handled especially efficiently, and if the encoding
kono
parents:
diff changeset
36 -- method is known at compile time to be WCEM_UTF8, then the instantiation
kono
parents:
diff changeset
37 -- is specialized to handle only the UTF-8 case and exclude code for the
kono
parents:
diff changeset
38 -- other encoding methods. The package also provides positioning routines
kono
parents:
diff changeset
39 -- for skipping encoded characters in either direction, and for validating
kono
parents:
diff changeset
40 -- strings for correct encodings.
kono
parents:
diff changeset
41
kono
parents:
diff changeset
42 -- Note: this package is only about decoding sequences of 8-bit characters
kono
parents:
diff changeset
43 -- into corresponding 16-bit Wide_String or 32-bit Wide_Wide_String values.
kono
parents:
diff changeset
44 -- It knows nothing at all about the character encodings being used for the
kono
parents:
diff changeset
45 -- resulting Wide_Character and Wide_Wide_Character values. Most often this
kono
parents:
diff changeset
46 -- will be Unicode/ISO-10646 as specified by the Ada RM, but this package
kono
parents:
diff changeset
47 -- does not make any assumptions about the character coding. See also the
kono
parents:
diff changeset
48 -- packages Ada.Wide_[Wide_]Characters.Unicode for unicode specific functions.
kono
parents:
diff changeset
49
kono
parents:
diff changeset
50 -- In particular, in the case of UTF-8, all valid UTF-8 encodings, as listed
kono
parents:
diff changeset
51 -- in table 3.6 of the Unicode Standard, version 6.2.0, are recognized as
kono
parents:
diff changeset
52 -- legitimate. This includes the full range 16#0000_0000# .. 16#03FF_FFFF#.
kono
parents:
diff changeset
53 -- This includes codes in the range 16#D800# - 16#DFFF#. These codes all
kono
parents:
diff changeset
54 -- have UTF-8 encoding sequences that are well-defined (e.g. the encoding for
kono
parents:
diff changeset
55 -- 16#D800# is ED A0 80). But these codes do not correspond to defined Unicode
kono
parents:
diff changeset
56 -- characters and are thus considered to be "not well-formed" (see table 3.7
kono
parents:
diff changeset
57 -- of the Unicode Standard). If you need to exclude these codes, you must do
kono
parents:
diff changeset
58 -- that manually, e.g. use Decode_Wide_Character/Decode_Wide_String and check
kono
parents:
diff changeset
59 -- that the resulting code(s) are not in this range.
kono
parents:
diff changeset
60
kono
parents:
diff changeset
61 -- Note on the use of brackets encoding (WCEM_Brackets). The brackets encoding
kono
parents:
diff changeset
62 -- method is ambiguous in the context of this package, since there is no way
kono
parents:
diff changeset
63 -- to tell if ["1234"] is eight unencoded characters or one encoded character.
kono
parents:
diff changeset
64 -- In the context of Ada sources, any sequence starting [" must be the start
kono
parents:
diff changeset
65 -- of an encoding (since that sequence is not valid in Ada source otherwise).
kono
parents:
diff changeset
66 -- The routines in this package use the same approach. If the input string
kono
parents:
diff changeset
67 -- contains the sequence [" then this is assumed to be the start of a brackets
kono
parents:
diff changeset
68 -- encoding sequence, and if it does not match the syntax, an error is raised.
kono
parents:
diff changeset
69 -- In the case of the Prev functions, a sequence ending with "] is assumed to
kono
parents:
diff changeset
70 -- be a valid brackets sequence, and an error is raised if it is not.
kono
parents:
diff changeset
71
kono
parents:
diff changeset
72 with System.WCh_Con;
kono
parents:
diff changeset
73
kono
parents:
diff changeset
74 generic
kono
parents:
diff changeset
75 Encoding_Method : System.WCh_Con.WC_Encoding_Method;
kono
parents:
diff changeset
76
kono
parents:
diff changeset
77 package GNAT.Decode_String is
kono
parents:
diff changeset
78 pragma Pure;
kono
parents:
diff changeset
79
kono
parents:
diff changeset
80 function Decode_Wide_String (S : String) return Wide_String;
kono
parents:
diff changeset
81 pragma Inline (Decode_Wide_String);
kono
parents:
diff changeset
82 -- Decode the given String, which is encoded using the indicated coding
kono
parents:
diff changeset
83 -- method, returning the corresponding decoded Wide_String value. If S
kono
parents:
diff changeset
84 -- contains a character code that cannot be represented with the given
kono
parents:
diff changeset
85 -- encoding, then Constraint_Error is raised.
kono
parents:
diff changeset
86
kono
parents:
diff changeset
87 procedure Decode_Wide_String
kono
parents:
diff changeset
88 (S : String;
kono
parents:
diff changeset
89 Result : out Wide_String;
kono
parents:
diff changeset
90 Length : out Natural);
kono
parents:
diff changeset
91 -- Similar to the above function except that the result is stored in the
kono
parents:
diff changeset
92 -- given Wide_String variable Result, starting at Result (Result'First). On
kono
parents:
diff changeset
93 -- return, Length is set to the number of characters stored in Result. The
kono
parents:
diff changeset
94 -- caller must ensure that Result is long enough (an easy choice is to set
kono
parents:
diff changeset
95 -- the length equal to the S'Length, since decoding can never increase the
kono
parents:
diff changeset
96 -- string length). If the length of Result is insufficient Constraint_Error
kono
parents:
diff changeset
97 -- will be raised.
kono
parents:
diff changeset
98
kono
parents:
diff changeset
99 function Decode_Wide_Wide_String (S : String) return Wide_Wide_String;
kono
parents:
diff changeset
100 -- Same as above function but for Wide_Wide_String output
kono
parents:
diff changeset
101
kono
parents:
diff changeset
102 procedure Decode_Wide_Wide_String
kono
parents:
diff changeset
103 (S : String;
kono
parents:
diff changeset
104 Result : out Wide_Wide_String;
kono
parents:
diff changeset
105 Length : out Natural);
kono
parents:
diff changeset
106 -- Same as above procedure, but for Wide_Wide_String output
kono
parents:
diff changeset
107
kono
parents:
diff changeset
108 function Validate_Wide_String (S : String) return Boolean;
kono
parents:
diff changeset
109 -- This function inspects the string S to determine if it contains only
kono
parents:
diff changeset
110 -- valid encodings corresponding to Wide_Character values using the
kono
parents:
diff changeset
111 -- given encoding. If a call to Decode_Wide_String (S) would return
kono
parents:
diff changeset
112 -- without raising Constraint_Error, then Validate_Wide_String will
kono
parents:
diff changeset
113 -- return True. If the call would have raised Constraint_Error, then
kono
parents:
diff changeset
114 -- Validate_Wide_String will return False.
kono
parents:
diff changeset
115
kono
parents:
diff changeset
116 function Validate_Wide_Wide_String (S : String) return Boolean;
kono
parents:
diff changeset
117 -- Similar to Validate_Wide_String, except that it succeeds if the string
kono
parents:
diff changeset
118 -- contains only encodings corresponding to Wide_Wide_Character values.
kono
parents:
diff changeset
119
kono
parents:
diff changeset
120 procedure Decode_Wide_Character
kono
parents:
diff changeset
121 (Input : String;
kono
parents:
diff changeset
122 Ptr : in out Natural;
kono
parents:
diff changeset
123 Result : out Wide_Character);
kono
parents:
diff changeset
124 pragma Inline (Decode_Wide_Character);
kono
parents:
diff changeset
125 -- This is a lower level procedure that decodes a single character using
kono
parents:
diff changeset
126 -- the given encoding method. The encoded character is stored in Input,
kono
parents:
diff changeset
127 -- starting at Input (Ptr). The resulting output character is stored in
kono
parents:
diff changeset
128 -- Result, and on return Ptr is updated past the input character or
kono
parents:
diff changeset
129 -- encoding sequence. Constraint_Error will be raised if the input has
kono
parents:
diff changeset
130 -- has a character that cannot be represented using the given encoding,
kono
parents:
diff changeset
131 -- or if Ptr is outside the bounds of the Input string.
kono
parents:
diff changeset
132
kono
parents:
diff changeset
133 procedure Decode_Wide_Wide_Character
kono
parents:
diff changeset
134 (Input : String;
kono
parents:
diff changeset
135 Ptr : in out Natural;
kono
parents:
diff changeset
136 Result : out Wide_Wide_Character);
kono
parents:
diff changeset
137 pragma Inline (Decode_Wide_Wide_Character);
kono
parents:
diff changeset
138 -- Same as above procedure but with Wide_Wide_Character input
kono
parents:
diff changeset
139
kono
parents:
diff changeset
140 procedure Next_Wide_Character (Input : String; Ptr : in out Natural);
kono
parents:
diff changeset
141 pragma Inline (Next_Wide_Character);
kono
parents:
diff changeset
142 -- This procedure examines the input string starting at Input (Ptr), and
kono
parents:
diff changeset
143 -- advances Ptr past one character in the encoded string, so that on return
kono
parents:
diff changeset
144 -- Ptr points to the next encoded character. Constraint_Error is raised if
kono
parents:
diff changeset
145 -- an invalid encoding is encountered, or the end of the string is reached
kono
parents:
diff changeset
146 -- or if Ptr is less than String'First on entry, or if the character
kono
parents:
diff changeset
147 -- skipped is not a valid Wide_Character code.
kono
parents:
diff changeset
148
kono
parents:
diff changeset
149 procedure Prev_Wide_Character (Input : String; Ptr : in out Natural);
kono
parents:
diff changeset
150 -- This procedure is similar to Next_Encoded_Character except that it moves
kono
parents:
diff changeset
151 -- backwards in the string, so that on return, Ptr is set to point to the
kono
parents:
diff changeset
152 -- previous encoded character. Constraint_Error is raised if the start of
kono
parents:
diff changeset
153 -- the string is encountered. It is valid for Ptr to be one past the end
kono
parents:
diff changeset
154 -- of the string for this call (in which case on return it will point to
kono
parents:
diff changeset
155 -- the last encoded character).
kono
parents:
diff changeset
156 --
kono
parents:
diff changeset
157 -- Note: it is not generally possible to do this function efficiently with
kono
parents:
diff changeset
158 -- all encodings, the current implementation is only efficient for the case
kono
parents:
diff changeset
159 -- of UTF-8 (Encoding_Method = WCEM_UTF8) and Brackets (Encoding_Method =
kono
parents:
diff changeset
160 -- WCEM_Brackets). For all other encodings, we work by starting at the
kono
parents:
diff changeset
161 -- beginning of the string and moving forward till Ptr is reached, which
kono
parents:
diff changeset
162 -- is correct but slow.
kono
parents:
diff changeset
163 --
kono
parents:
diff changeset
164 -- Note: this routine assumes that the sequence prior to Ptr is correctly
kono
parents:
diff changeset
165 -- encoded, it does not have a defined behavior if this is not the case.
kono
parents:
diff changeset
166
kono
parents:
diff changeset
167 procedure Next_Wide_Wide_Character (Input : String; Ptr : in out Natural);
kono
parents:
diff changeset
168 pragma Inline (Next_Wide_Wide_Character);
kono
parents:
diff changeset
169 -- Similar to Next_Wide_Character except that codes skipped must be valid
kono
parents:
diff changeset
170 -- Wide_Wide_Character codes.
kono
parents:
diff changeset
171
kono
parents:
diff changeset
172 procedure Prev_Wide_Wide_Character (Input : String; Ptr : in out Natural);
kono
parents:
diff changeset
173 -- Similar to Prev_Wide_Character except that codes skipped must be valid
kono
parents:
diff changeset
174 -- Wide_Wide_Character codes.
kono
parents:
diff changeset
175
kono
parents:
diff changeset
176 end GNAT.Decode_String;