1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . S T R I N G S . W I D E _ S E A R C H --
9 -- Copyright (C) 1992-2010, Free Software Foundation, Inc. --
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 with Ada
.Strings
.Wide_Maps
; use Ada
.Strings
.Wide_Maps
;
33 with System
; use System
;
35 package body Ada
.Strings
.Wide_Search
is
37 -----------------------
38 -- Local Subprograms --
39 -----------------------
42 (Element
: Wide_Character;
43 Set
: Wide_Maps
.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.
54 (Element
: Wide_Character;
55 Set
: Wide_Maps
.Wide_Character_Set
;
56 Test
: Membership
) return Boolean
60 return Is_In
(Element
, Set
);
62 return not Is_In
(Element
, Set
);
71 (Source
: Wide_String;
72 Pattern
: Wide_String;
73 Mapping
: Wide_Maps
.Wide_Character_Mapping
:= Wide_Maps
.Identity
)
76 PL1
: constant Integer := Pattern
'Length - 1;
91 if Mapping
'Address = Wide_Maps
.Identity
'Address then
92 while Ind
<= Source
'Last - PL1
loop
93 if Pattern
= Source
(Ind
.. Ind
+ PL1
) then
95 Ind
:= Ind
+ Pattern
'Length;
104 while Ind
<= Source
'Last - PL1
loop
106 for K
in Pattern
'Range loop
107 if Pattern
(K
) /= Value
(Mapping
, Source
(Cur
)) then
116 Ind
:= Ind
+ Pattern
'Length;
129 (Source
: Wide_String;
130 Pattern
: Wide_String;
131 Mapping
: Wide_Maps
.Wide_Character_Mapping_Function
) return Natural
133 PL1
: constant Integer := Pattern
'Length - 1;
143 -- Check for null pointer in case checks are off
145 if Mapping
= null then
146 raise Constraint_Error
;
151 while Ind
<= Source
'Last - PL1
loop
153 for K
in Pattern
'Range loop
154 if Pattern
(K
) /= Mapping
(Source
(Cur
)) then
163 Ind
:= Ind
+ Pattern
'Length;
173 (Source
: Wide_String;
174 Set
: Wide_Maps
.Wide_Character_Set
) return Natural
179 for J
in Source
'Range loop
180 if Is_In
(Source
(J
), Set
) then
193 (Source
: Wide_String;
194 Set
: Wide_Maps
.Wide_Character_Set
;
197 First
: out Positive;
201 for J
in From
.. Source
'Last loop
202 if Belongs
(Source
(J
), Set
, Test
) then
205 for K
in J
+ 1 .. Source
'Last loop
206 if not Belongs
(Source
(K
), Set
, Test
) then
212 -- Here if J indexes first char of token, and all chars after J
220 -- Here if no token found
227 (Source
: Wide_String;
228 Set
: Wide_Maps
.Wide_Character_Set
;
230 First
: out Positive;
234 for J
in Source
'Range loop
235 if Belongs
(Source
(J
), Set
, Test
) then
238 for K
in J
+ 1 .. Source
'Last loop
239 if not Belongs
(Source
(K
), Set
, Test
) then
245 -- Here if J indexes first char of token, and all chars after J
253 -- Here if no token found
255 First
:= Source
'First;
264 (Source
: Wide_String;
265 Pattern
: Wide_String;
266 Going
: Direction
:= Forward
;
267 Mapping
: Wide_Maps
.Wide_Character_Mapping
:= Wide_Maps
.Identity
)
270 PL1
: constant Integer := Pattern
'Length - 1;
274 -- Index for start of match check. This can be negative if the pattern
275 -- length is greater than the string length, which is why this variable
276 -- is Integer instead of Natural. In this case, the search loops do not
277 -- execute at all, so this Ind value is never used.
286 if Going
= Forward
then
289 -- Unmapped forward case
291 if Mapping
'Address = Wide_Maps
.Identity
'Address then
292 for J
in 1 .. Source
'Length - PL1
loop
293 if Pattern
= Source
(Ind
.. Ind
+ PL1
) then
300 -- Mapped forward case
303 for J
in 1 .. Source
'Length - PL1
loop
306 for K
in Pattern
'Range loop
307 if Pattern
(K
) /= Value
(Mapping
, Source
(Cur
)) then
324 -- Unmapped backward case
326 Ind
:= Source
'Last - PL1
;
328 if Mapping
'Address = Wide_Maps
.Identity
'Address then
329 for J
in reverse 1 .. Source
'Length - PL1
loop
330 if Pattern
= Source
(Ind
.. Ind
+ PL1
) then
337 -- Mapped backward case
340 for J
in reverse 1 .. Source
'Length - PL1
loop
343 for K
in Pattern
'Range loop
344 if Pattern
(K
) /= Value
(Mapping
, Source
(Cur
)) then
359 -- Fall through if no match found. Note that the loops are skipped
360 -- completely in the case of the pattern being longer than the source.
366 (Source
: Wide_String;
367 Pattern
: Wide_String;
368 Going
: Direction
:= Forward
;
369 Mapping
: Wide_Maps
.Wide_Character_Mapping_Function
) return Natural
371 PL1
: constant Integer := Pattern
'Length - 1;
380 -- Check for null pointer in case checks are off
382 if Mapping
= null then
383 raise Constraint_Error
;
386 -- If Pattern longer than Source it can't be found
388 if Pattern
'Length > Source
'Length then
394 if Going
= Forward
then
396 for J
in 1 .. Source
'Length - PL1
loop
399 for K
in Pattern
'Range loop
400 if Pattern
(K
) /= Mapping
.all (Source
(Cur
)) then
416 Ind
:= Source
'Last - PL1
;
417 for J
in reverse 1 .. Source
'Length - PL1
loop
420 for K
in Pattern
'Range loop
421 if Pattern
(K
) /= Mapping
.all (Source
(Cur
)) then
435 -- Fall through if no match found. Note that the loops are skipped
436 -- completely in the case of the pattern being longer than the source.
442 (Source
: Wide_String;
443 Set
: Wide_Maps
.Wide_Character_Set
;
444 Test
: Membership
:= Inside
;
445 Going
: Direction
:= Forward
) return Natural
450 if Going
= Forward
then
451 for J
in Source
'Range loop
452 if Belongs
(Source
(J
), Set
, Test
) then
460 for J
in reverse Source
'Range loop
461 if Belongs
(Source
(J
), Set
, Test
) then
467 -- Fall through if no match
473 (Source
: Wide_String;
474 Pattern
: Wide_String;
476 Going
: Direction
:= Forward
;
477 Mapping
: Wide_Maps
.Wide_Character_Mapping
:= Wide_Maps
.Identity
)
481 if Going
= Forward
then
482 if From
< Source
'First then
487 Index
(Source
(From
.. Source
'Last), Pattern
, Forward
, Mapping
);
490 if From
> Source
'Last then
495 Index
(Source
(Source
'First .. From
), Pattern
, Backward
, Mapping
);
500 (Source
: Wide_String;
501 Pattern
: Wide_String;
503 Going
: Direction
:= Forward
;
504 Mapping
: Wide_Maps
.Wide_Character_Mapping_Function
) return Natural
507 if Going
= Forward
then
508 if From
< Source
'First then
513 (Source
(From
.. Source
'Last), Pattern
, Forward
, Mapping
);
516 if From
> Source
'Last then
521 (Source
(Source
'First .. From
), Pattern
, Backward
, Mapping
);
526 (Source
: Wide_String;
527 Set
: Wide_Maps
.Wide_Character_Set
;
529 Test
: Membership
:= Inside
;
530 Going
: Direction
:= Forward
) return Natural
533 if Going
= Forward
then
534 if From
< Source
'First then
539 Index
(Source
(From
.. Source
'Last), Set
, Test
, Forward
);
542 if From
> Source
'Last then
547 Index
(Source
(Source
'First .. From
), Set
, Test
, Backward
);
551 ---------------------
552 -- Index_Non_Blank --
553 ---------------------
555 function Index_Non_Blank
556 (Source
: Wide_String;
557 Going
: Direction
:= Forward
) return Natural
560 if Going
= Forward
then
561 for J
in Source
'Range loop
562 if Source
(J
) /= Wide_Space
then
567 else -- Going = Backward
568 for J
in reverse Source
'Range loop
569 if Source
(J
) /= Wide_Space
then
575 -- Fall through if no match
580 function Index_Non_Blank
581 (Source
: Wide_String;
583 Going
: Direction
:= Forward
) return Natural
586 if Going
= Forward
then
587 if From
< Source
'First then
592 Index_Non_Blank
(Source
(From
.. Source
'Last), Forward
);
595 if From
> Source
'Last then
600 Index_Non_Blank
(Source
(Source
'First .. From
), Backward
);
604 end Ada
.Strings
.Wide_Search
;