OSDN Git Service

2009-07-07 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / a-stzsea.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT RUN-TIME COMPONENTS                         --
4 --                                                                          --
5 --         A D A . S T R I N G S . W I D E _ W I D E _ S E A R C H          --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2009, 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 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.                                     --
17 --                                                                          --
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.               --
21 --                                                                          --
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/>.                                          --
26 --                                                                          --
27 -- GNAT was originally developed  by the GNAT team at  New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
29 --                                                                          --
30 ------------------------------------------------------------------------------
31
32 with Ada.Strings.Wide_Wide_Maps; use Ada.Strings.Wide_Wide_Maps;
33 with System; use System;
34
35 package body Ada.Strings.Wide_Wide_Search is
36
37    -----------------------
38    -- Local Subprograms --
39    -----------------------
40
41    function Belongs
42      (Element : Wide_Wide_Character;
43       Set     : Wide_Wide_Maps.Wide_Wide_Character_Set;
44       Test    : Membership) return Boolean;
45    pragma Inline (Belongs);
46    --  Determines if the given element is in (Test = Inside) or not in
47    --  (Test = Outside) the given character set.
48
49    -------------
50    -- Belongs --
51    -------------
52
53    function Belongs
54      (Element : Wide_Wide_Character;
55       Set     : Wide_Wide_Maps.Wide_Wide_Character_Set;
56       Test    : Membership) return Boolean
57    is
58    begin
59       if Test = Inside then
60          return Is_In (Element, Set);
61       else
62          return not Is_In (Element, Set);
63       end if;
64    end Belongs;
65
66    -----------
67    -- Count --
68    -----------
69
70    function Count
71      (Source  : Wide_Wide_String;
72       Pattern : Wide_Wide_String;
73       Mapping : Wide_Wide_Maps.Wide_Wide_Character_Mapping :=
74                   Wide_Wide_Maps.Identity)
75       return Natural
76    is
77       PL1 : constant Integer := Pattern'Length - 1;
78       Num : Natural;
79       Ind : Natural;
80       Cur : Natural;
81
82    begin
83       if Pattern = "" then
84          raise Pattern_Error;
85       end if;
86
87       Num := 0;
88       Ind := Source'First;
89
90       --  Unmapped case
91
92       if Mapping'Address = Wide_Wide_Maps.Identity'Address then
93          while Ind <= Source'Last - PL1 loop
94             if Pattern = Source (Ind .. Ind + PL1) then
95                Num := Num + 1;
96                Ind := Ind + Pattern'Length;
97             else
98                Ind := Ind + 1;
99             end if;
100          end loop;
101
102       --  Mapped case
103
104       else
105          while Ind <= Source'Last - PL1 loop
106             Cur := Ind;
107             for K in Pattern'Range loop
108                if Pattern (K) /= Value (Mapping, Source (Cur)) then
109                   Ind := Ind + 1;
110                   goto Cont;
111                else
112                   Cur := Cur + 1;
113                end if;
114             end loop;
115
116             Num := Num + 1;
117             Ind := Ind + Pattern'Length;
118
119          <<Cont>>
120             null;
121          end loop;
122       end if;
123
124       --  Return result
125
126       return Num;
127    end Count;
128
129    function Count
130      (Source  : Wide_Wide_String;
131       Pattern : Wide_Wide_String;
132       Mapping : Wide_Wide_Maps.Wide_Wide_Character_Mapping_Function)
133       return Natural
134    is
135       PL1 : constant Integer := Pattern'Length - 1;
136       Num : Natural;
137       Ind : Natural;
138       Cur : Natural;
139
140    begin
141       if Pattern = "" then
142          raise Pattern_Error;
143       end if;
144
145       --  Check for null pointer in case checks are off
146
147       if Mapping = null then
148          raise Constraint_Error;
149       end if;
150
151       Num := 0;
152       Ind := Source'First;
153       while Ind <= Source'Last - PL1 loop
154          Cur := Ind;
155          for K in Pattern'Range loop
156             if Pattern (K) /= Mapping (Source (Cur)) then
157                Ind := Ind + 1;
158                goto Cont;
159             else
160                Cur := Cur + 1;
161             end if;
162          end loop;
163
164          Num := Num + 1;
165          Ind := Ind + Pattern'Length;
166
167       <<Cont>>
168          null;
169       end loop;
170
171       return Num;
172    end Count;
173
174    function Count
175      (Source : Wide_Wide_String;
176       Set : Wide_Wide_Maps.Wide_Wide_Character_Set) return Natural
177    is
178       N : Natural := 0;
179
180    begin
181       for J in Source'Range loop
182          if Is_In (Source (J), Set) then
183             N := N + 1;
184          end if;
185       end loop;
186
187       return N;
188    end Count;
189
190    ----------------
191    -- Find_Token --
192    ----------------
193
194    procedure Find_Token
195      (Source : Wide_Wide_String;
196       Set    : Wide_Wide_Maps.Wide_Wide_Character_Set;
197       Test   : Membership;
198       First  : out Positive;
199       Last   : out Natural)
200    is
201    begin
202       for J in Source'Range loop
203          if Belongs (Source (J), Set, Test) then
204             First := J;
205
206             for K in J + 1 .. Source'Last loop
207                if not Belongs (Source (K), Set, Test) then
208                   Last := K - 1;
209                   return;
210                end if;
211             end loop;
212
213             --  Here if J indexes first char of token, and all chars after J
214             --  are in the token.
215
216             Last := Source'Last;
217             return;
218          end if;
219       end loop;
220
221       --  Here if no token found
222
223       First := Source'First;
224       Last  := 0;
225    end Find_Token;
226
227    -----------
228    -- Index --
229    -----------
230
231    function Index
232      (Source  : Wide_Wide_String;
233       Pattern : Wide_Wide_String;
234       Going   : Direction := Forward;
235       Mapping : Wide_Wide_Maps.Wide_Wide_Character_Mapping :=
236                   Wide_Wide_Maps.Identity)
237       return Natural
238    is
239       PL1 : constant Integer := Pattern'Length - 1;
240       Ind : Natural;
241       Cur : Natural;
242
243    begin
244       if Pattern = "" then
245          raise Pattern_Error;
246       end if;
247
248       --  If Pattern longer than Source it can't be found
249
250       if Pattern'Length > Source'Length then
251          return 0;
252       end if;
253
254       --  Forwards case
255
256       if Going = Forward then
257          Ind := Source'First;
258
259          --  Unmapped forward case
260
261          if Mapping'Address = Wide_Wide_Maps.Identity'Address then
262             for J in 1 .. Source'Length - PL1 loop
263                if Pattern = Source (Ind .. Ind + PL1) then
264                   return Ind;
265                else
266                   Ind := Ind + 1;
267                end if;
268             end loop;
269
270          --  Mapped forward case
271
272          else
273             for J in 1 .. Source'Length - PL1 loop
274                Cur := Ind;
275
276                for K in Pattern'Range loop
277                   if Pattern (K) /= Value (Mapping, Source (Cur)) then
278                      goto Cont1;
279                   else
280                      Cur := Cur + 1;
281                   end if;
282                end loop;
283
284                return Ind;
285
286             <<Cont1>>
287                Ind := Ind + 1;
288             end loop;
289          end if;
290
291       --  Backwards case
292
293       else
294          --  Unmapped backward case
295
296          Ind := Source'Last - PL1;
297
298          if Mapping'Address = Wide_Wide_Maps.Identity'Address then
299             for J in reverse 1 .. Source'Length - PL1 loop
300                if Pattern = Source (Ind .. Ind + PL1) then
301                   return Ind;
302                else
303                   Ind := Ind - 1;
304                end if;
305             end loop;
306
307          --  Mapped backward case
308
309          else
310             for J in reverse 1 .. Source'Length - PL1 loop
311                Cur := Ind;
312
313                for K in Pattern'Range loop
314                   if Pattern (K) /= Value (Mapping, Source (Cur)) then
315                      goto Cont2;
316                   else
317                      Cur := Cur + 1;
318                   end if;
319                end loop;
320
321                return Ind;
322
323             <<Cont2>>
324                Ind := Ind - 1;
325             end loop;
326          end if;
327       end if;
328
329       --  Fall through if no match found. Note that the loops are skipped
330       --  completely in the case of the pattern being longer than the source.
331
332       return 0;
333    end Index;
334
335    function Index
336      (Source  : Wide_Wide_String;
337       Pattern : Wide_Wide_String;
338       Going   : Direction := Forward;
339       Mapping : Wide_Wide_Maps.Wide_Wide_Character_Mapping_Function)
340       return Natural
341    is
342       PL1 : constant Integer := Pattern'Length - 1;
343       Ind : Natural;
344       Cur : Natural;
345
346    begin
347       if Pattern = "" then
348          raise Pattern_Error;
349       end if;
350
351       --  Check for null pointer in case checks are off
352
353       if Mapping = null then
354          raise Constraint_Error;
355       end if;
356
357       --  If Pattern longer than Source it can't be found
358
359       if Pattern'Length > Source'Length then
360          return 0;
361       end if;
362
363       --  Forwards case
364
365       if Going = Forward then
366          Ind := Source'First;
367          for J in 1 .. Source'Length - PL1 loop
368             Cur := Ind;
369
370             for K in Pattern'Range loop
371                if Pattern (K) /= Mapping.all (Source (Cur)) then
372                   goto Cont1;
373                else
374                   Cur := Cur + 1;
375                end if;
376             end loop;
377
378             return Ind;
379
380          <<Cont1>>
381             Ind := Ind + 1;
382          end loop;
383
384       --  Backwards case
385
386       else
387          Ind := Source'Last - PL1;
388          for J in reverse 1 .. Source'Length - PL1 loop
389             Cur := Ind;
390
391             for K in Pattern'Range loop
392                if Pattern (K) /= Mapping.all (Source (Cur)) then
393                   goto Cont2;
394                else
395                   Cur := Cur + 1;
396                end if;
397             end loop;
398
399             return Ind;
400
401          <<Cont2>>
402             Ind := Ind - 1;
403          end loop;
404       end if;
405
406       --  Fall through if no match found. Note that the loops are skipped
407       --  completely in the case of the pattern being longer than the source.
408
409       return 0;
410    end Index;
411
412    function Index
413      (Source : Wide_Wide_String;
414       Set    : Wide_Wide_Maps.Wide_Wide_Character_Set;
415       Test   : Membership := Inside;
416       Going  : Direction  := Forward) return Natural
417    is
418    begin
419       --  Forwards case
420
421       if Going = Forward then
422          for J in Source'Range loop
423             if Belongs (Source (J), Set, Test) then
424                return J;
425             end if;
426          end loop;
427
428       --  Backwards case
429
430       else
431          for J in reverse Source'Range loop
432             if Belongs (Source (J), Set, Test) then
433                return J;
434             end if;
435          end loop;
436       end if;
437
438       --  Fall through if no match
439
440       return 0;
441    end Index;
442
443    function Index
444      (Source  : Wide_Wide_String;
445       Pattern : Wide_Wide_String;
446       From    : Positive;
447       Going   : Direction := Forward;
448       Mapping : Wide_Wide_Maps.Wide_Wide_Character_Mapping :=
449                   Wide_Wide_Maps.Identity)
450       return Natural
451    is
452    begin
453       if Going = Forward then
454          if From < Source'First then
455             raise Index_Error;
456          end if;
457
458          return
459            Index (Source (From .. Source'Last), Pattern, Forward, Mapping);
460
461       else
462          if From > Source'Last then
463             raise Index_Error;
464          end if;
465
466          return
467            Index (Source (Source'First .. From), Pattern, Backward, Mapping);
468       end if;
469    end Index;
470
471    function Index
472      (Source  : Wide_Wide_String;
473       Pattern : Wide_Wide_String;
474       From    : Positive;
475       Going   : Direction := Forward;
476       Mapping : Wide_Wide_Maps.Wide_Wide_Character_Mapping_Function)
477       return Natural
478    is
479    begin
480       if Going = Forward then
481          if From < Source'First then
482             raise Index_Error;
483          end if;
484
485          return Index
486            (Source (From .. Source'Last), Pattern, Forward, Mapping);
487
488       else
489          if From > Source'Last then
490             raise Index_Error;
491          end if;
492
493          return Index
494            (Source (Source'First .. From), Pattern, Backward, Mapping);
495       end if;
496    end Index;
497
498    function Index
499      (Source  : Wide_Wide_String;
500       Set     : Wide_Wide_Maps.Wide_Wide_Character_Set;
501       From    : Positive;
502       Test    : Membership := Inside;
503       Going   : Direction := Forward) return Natural
504    is
505    begin
506       if Going = Forward then
507          if From < Source'First then
508             raise Index_Error;
509          end if;
510
511          return
512            Index (Source (From .. Source'Last), Set, Test, Forward);
513
514       else
515          if From > Source'Last then
516             raise Index_Error;
517          end if;
518
519          return
520            Index (Source (Source'First .. From), Set, Test, Backward);
521       end if;
522    end Index;
523
524    ---------------------
525    -- Index_Non_Blank --
526    ---------------------
527
528    function Index_Non_Blank
529      (Source : Wide_Wide_String;
530       Going  : Direction := Forward) return Natural
531    is
532    begin
533       if Going = Forward then
534          for J in Source'Range loop
535             if Source (J) /= Wide_Wide_Space then
536                return J;
537             end if;
538          end loop;
539
540       else -- Going = Backward
541          for J in reverse Source'Range loop
542             if Source (J) /= Wide_Wide_Space then
543                return J;
544             end if;
545          end loop;
546       end if;
547
548       --  Fall through if no match
549
550       return 0;
551    end Index_Non_Blank;
552
553    function Index_Non_Blank
554      (Source : Wide_Wide_String;
555       From   : Positive;
556       Going  : Direction := Forward) return Natural
557    is
558    begin
559       if Going = Forward then
560          if From < Source'First then
561             raise Index_Error;
562          end if;
563
564          return
565            Index_Non_Blank (Source (From .. Source'Last), Forward);
566
567       else
568          if From > Source'Last then
569             raise Index_Error;
570          end if;
571
572          return
573            Index_Non_Blank (Source (Source'First .. From), Backward);
574       end if;
575    end Index_Non_Blank;
576
577 end Ada.Strings.Wide_Wide_Search;