OSDN Git Service

2007-09-26 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-wchstw.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT RUN-TIME COMPONENTS                         --
4 --                                                                          --
5 --                       S Y S T E M . W C H _ S T W                        --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2007, Free Software Foundation, Inc.         --
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 with System.WCh_Con; use System.WCh_Con;
35 with System.WCh_Cnv; use System.WCh_Cnv;
36
37 package body System.WCh_StW is
38
39    -----------------------
40    -- Local Subprograms --
41    -----------------------
42
43    procedure Get_Next_Code
44      (S  : String;
45       P  : in out Natural;
46       V  : out UTF_32_Code;
47       EM : WC_Encoding_Method);
48    --  Scans next character starting at S(P) and returns its value in V. On
49    --  exit P is updated past the last character read. Raises Constraint_Error
50    --  if the string is not well formed. Raises Constraint_Error if the code
51    --  value is greater than 16#7FFF_FFFF#. On entry P <= S'Last.
52
53    -------------------
54    -- Get_Next_Code --
55    -------------------
56
57    procedure Get_Next_Code
58      (S  : String;
59       P  : in out Natural;
60       V  : out UTF_32_Code;
61       EM : WC_Encoding_Method)
62    is
63       function In_Char return Character;
64       --  Function to return a character, bumping P, raises Constraint_Error
65       --  if P > S'Last on entry.
66
67       function Get_UTF_32 is new Char_Sequence_To_UTF_32 (In_Char);
68       --  Function to get next UFT_32 value
69
70       -------------
71       -- In_Char --
72       -------------
73
74       function In_Char return Character is
75       begin
76          if P > S'Last then
77             raise Constraint_Error
78               with "badly formed wide character code";
79          else
80             P := P + 1;
81             return S (P - 1);
82          end if;
83       end In_Char;
84
85    --  Start of processing for Get_Next_Code
86
87    begin
88       --  Check for wide character encoding
89
90       case EM is
91          when WCEM_Hex =>
92             if S (P) = ASCII.ESC then
93                V := Get_UTF_32 (In_Char, EM);
94                return;
95             end if;
96
97          when WCEM_Upper | WCEM_Shift_JIS | WCEM_EUC | WCEM_UTF8 =>
98             if S (P) >= Character'Val (16#80#) then
99                V := Get_UTF_32 (In_Char, EM);
100                return;
101             end if;
102
103          when WCEM_Brackets =>
104             if P + 2 <= S'Last
105               and then S (P) = '['
106               and then S (P + 1) = '"'
107               and then S (P + 2) /= '"'
108             then
109                V := Get_UTF_32 (In_Char, EM);
110                return;
111             end if;
112       end case;
113
114       --  If it is not a wide character code, just get it
115
116       V := Character'Pos (S (P));
117       P := P + 1;
118    end Get_Next_Code;
119
120    ---------------------------
121    -- String_To_Wide_String --
122    ---------------------------
123
124    function String_To_Wide_String
125      (S  : String;
126       EM : WC_Encoding_Method) return Wide_String
127    is
128       R  : Wide_String (1 .. S'Length);
129       RP : Natural;
130       SP : Natural;
131       V  : UTF_32_Code;
132
133    begin
134       SP := S'First;
135       RP := 0;
136       while SP <= S'Last loop
137          Get_Next_Code (S, SP, V, EM);
138
139          if V > 16#FFFF# then
140             raise Constraint_Error
141               with "out of range value for wide character";
142          end if;
143
144          RP := RP + 1;
145          R (RP) := Wide_Character'Val (V);
146       end loop;
147
148       return R (1 .. RP);
149    end String_To_Wide_String;
150
151    --------------------------------
152    -- String_To_Wide_Wide_String --
153    --------------------------------
154
155    function String_To_Wide_Wide_String
156      (S  : String;
157       EM : WC_Encoding_Method) return Wide_Wide_String
158    is
159       R  : Wide_Wide_String (1 .. S'Length);
160       RP : Natural;
161       SP : Natural;
162       V  : UTF_32_Code;
163
164    begin
165       SP := S'First;
166       RP := 0;
167       while SP <= S'Last loop
168          Get_Next_Code (S, SP, V, EM);
169          RP := RP + 1;
170          R (RP) := Wide_Wide_Character'Val (V);
171       end loop;
172
173       return R (1 .. RP);
174    end String_To_Wide_Wide_String;
175
176 end System.WCh_StW;