OSDN Git Service

2010-10-08 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / g-decstr.ads
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT RUN-TIME COMPONENTS                         --
4 --                                                                          --
5 --                    G N A T . D E C O D E _ S T R I N G                   --
6 --                                                                          --
7 --                                 S p e c                                  --
8 --                                                                          --
9 --                       Copyright (C) 2007, AdaCore                        --
10 --                                                                          --
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 2,  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.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, USA.                                              --
21 --                                                                          --
22 -- As a special exception,  if other files  instantiate  generics from this --
23 -- unit, or you link  this unit with other files  to produce an executable, --
24 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
25 -- covered  by the  GNU  General  Public  License.  This exception does not --
26 -- however invalidate  any other reasons why  the executable file  might be --
27 -- covered by the  GNU Public License.                                      --
28 --                                                                          --
29 -- GNAT was originally developed  by the GNAT team at  New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 --  This generic package provides utility routines for converting from an
35 --  encoded string to a corresponding Wide_String or Wide_Wide_String value
36 --  using a specified encoding convention, which is supplied as the generic
37 --  parameter. UTF-8 is handled especially efficiently, and if the encoding
38 --  method is known at compile time to be WCEM_UTF8, then the instantiation
39 --  is specialized to handle only the UTF-8 case and exclude code for the
40 --  other encoding methods. The package also provides positioning routines
41 --  for skipping encoded characters in either direction, and for validating
42 --  strings for correct encodings.
43
44 --  Note: this package is only about decoding sequences of 8-bit characters
45 --  into corresponding 16-bit Wide_String or 32-bit Wide_Wide_String values.
46 --  It knows nothing at all about the character encodings being used for the
47 --  resulting Wide_Character and Wide_Wide_Character values. Most often this
48 --  will be Unicode/ISO-10646 as specified by the Ada RM, but this package
49 --  does not make any assumptions about the character coding. See also the
50 --  packages Ada.Wide_[Wide_]Characters.Unicode for unicode specific functions.
51
52 --  Note on the use of brackets encoding (WCEM_Brackets). The brackets encoding
53 --  method is ambiguous in the context of this package, since there is no way
54 --  to tell if ["1234"] is eight unencoded characters or one encoded character.
55 --  In the context of Ada sources, any sequence starting [" must be the start
56 --  of an encoding (since that sequence is not valid in Ada source otherwise).
57 --  The routines in this package use the same approach. If the input string
58 --  contains the sequence [" then this is assumed to be the start of a brackets
59 --  encoding sequence, and if it does not match the syntax, an error is raised.
60 --  In the case of the Prev functions, a sequence ending with "] is assumed to
61 --  be a valid brackets sequence, and an error is raised if it is not.
62
63 with System.WCh_Con;
64
65 generic
66    Encoding_Method : System.WCh_Con.WC_Encoding_Method;
67
68 package GNAT.Decode_String is
69    pragma Pure;
70
71    function Decode_Wide_String (S : String) return Wide_String;
72    pragma Inline (Decode_Wide_String);
73    --  Decode the given String, which is encoded using the indicated coding
74    --  method, returning the corresponding decoded Wide_String value. If S
75    --  contains a character code that cannot be represented with the given
76    --  encoding, then Constraint_Error is raised.
77
78    procedure Decode_Wide_String
79      (S      : String;
80       Result : out Wide_String;
81       Length : out Natural);
82    --  Similar to the above function except that the result is stored in the
83    --  given Wide_String variable Result, starting at Result (Result'First). On
84    --  return, Length is set to the number of characters stored in Result. The
85    --  caller must ensure that Result is long enough (an easy choice is to set
86    --  the length equal to the S'Length, since decoding can never increase the
87    --  string length). If the length of Result is insufficient Constraint_Error
88    --  will be raised.
89
90    function Decode_Wide_Wide_String (S : String) return Wide_Wide_String;
91    pragma Inline (Decode_Wide_Wide_String);
92    --  Same as above function but for Wide_Wide_String output
93
94    procedure Decode_Wide_Wide_String
95      (S      : String;
96       Result : out Wide_Wide_String;
97       Length : out Natural);
98    --  Same as above procedure, but for Wide_Wide_String output
99
100    function Validate_Wide_String (S : String) return Boolean;
101    --  This function inspects the string S to determine if it contains only
102    --  valid encodings corresponding to Wide_Character values using the
103    --  given encoding. If a call to Decode_Wide_String (S) would return
104    --  without raising Constraint_Error, then Validate_Wide_String will
105    --  return True. If the call would have raised Constraint_Error, then
106    --  Validate_Wide_String will return False.
107
108    function Validate_Wide_Wide_String (S : String) return Boolean;
109    --  Similar to Validate_Wide_String, except that it succeeds if the string
110    --  contains only encodings corresponding to Wide_Wide_Character values.
111
112    procedure Decode_Wide_Character
113      (Input  : String;
114       Ptr    : in out Natural;
115       Result : out Wide_Character);
116    pragma Inline (Decode_Wide_Character);
117    --  This is a lower level procedure that decodes a single character using
118    --  the given encoding method. The encoded character is stored in Input,
119    --  starting at Input (Ptr). The resulting output character is stored in
120    --  Result, and on return Ptr is updated past the input character or
121    --  encoding sequence. Constraint_Error will be raised if the input has
122    --  has a character that cannot be represented using the given encoding,
123    --  or if Ptr is outside the bounds of the Input string.
124
125    procedure Decode_Wide_Wide_Character
126      (Input  : String;
127       Ptr    : in out Natural;
128       Result : out Wide_Wide_Character);
129    --  Same as above procedure but with Wide_Wide_Character input
130
131    procedure Next_Wide_Character (Input : String; Ptr : in out Natural);
132    --  This procedure examines the input string starting at Input (Ptr), and
133    --  advances Ptr past one character in the encoded string, so that on return
134    --  Ptr points to the next encoded character. Constraint_Error is raised if
135    --  an invalid encoding is encountered, or the end of the string is reached
136    --  or if Ptr is less than String'First on entry, or if the character
137    --  skipped is not a valid Wide_Character code. This call may be more
138    --  efficient than calling Decode_Wide_Character and discarding the result.
139
140    procedure Prev_Wide_Character (Input : String; Ptr : in out Natural);
141    --  This procedure is similar to Next_Encoded_Character except that it moves
142    --  backwards in the string, so that on return, Ptr is set to point to the
143    --  previous encoded character. Constraint_Error is raised if the start of
144    --  the string is encountered. It is valid for Ptr to be one past the end
145    --  of the string for this call (in which case on return it will point to
146    --  the last encoded character).
147    --
148    --  Note: it is not generally possible to do this function efficiently with
149    --  all encodings, the current implementation is only efficient for the case
150    --  of UTF-8 (Encoding_Method = WCEM_UTF8) and Brackets (Encoding_Method =
151    --  WCEM_Brackets). For all other encodings, we work by starting at the
152    --  beginning of the string and moving forward till Ptr is reached, which
153    --  is correct but slow.
154
155    procedure Next_Wide_Wide_Character (Input : String; Ptr : in out Natural);
156    --  Similar to Next_Wide_Character except that codes skipped must be valid
157    --  Wide_Wide_Character codes.
158
159    procedure Prev_Wide_Wide_Character (Input : String; Ptr : in out Natural);
160    --  Similar to Prev_Wide_Character except that codes skipped must be valid
161    --  Wide_Wide_Character codes.
162
163 end GNAT.Decode_String;