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-2017, 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 -- RM 2005 A.4.3 (68/1) specifies that an exception must be raised if
256 -- Source'First is not positive and is assigned to First. Formulation
257 -- is slightly different in RM 2012, but the intent seems similar, so
258 -- we check explicitly for that condition.
260 if Source
'First not in Positive then
261 raise Constraint_Error
;
264 First
:= Source
'First;
274 (Source
: Wide_String;
275 Pattern
: Wide_String;
276 Going
: Direction
:= Forward
;
277 Mapping
: Wide_Maps
.Wide_Character_Mapping
:= Wide_Maps
.Identity
)
280 PL1
: constant Integer := Pattern
'Length - 1;
284 -- Index for start of match check. This can be negative if the pattern
285 -- length is greater than the string length, which is why this variable
286 -- is Integer instead of Natural. In this case, the search loops do not
287 -- execute at all, so this Ind value is never used.
296 if Going
= Forward
then
299 -- Unmapped forward case
301 if Mapping
'Address = Wide_Maps
.Identity
'Address then
302 for J
in 1 .. Source
'Length - PL1
loop
303 if Pattern
= Source
(Ind
.. Ind
+ PL1
) then
310 -- Mapped forward case
313 for J
in 1 .. Source
'Length - PL1
loop
316 for K
in Pattern
'Range loop
317 if Pattern
(K
) /= Value
(Mapping
, Source
(Cur
)) then
334 -- Unmapped backward case
336 Ind
:= Source
'Last - PL1
;
338 if Mapping
'Address = Wide_Maps
.Identity
'Address then
339 for J
in reverse 1 .. Source
'Length - PL1
loop
340 if Pattern
= Source
(Ind
.. Ind
+ PL1
) then
347 -- Mapped backward case
350 for J
in reverse 1 .. Source
'Length - PL1
loop
353 for K
in Pattern
'Range loop
354 if Pattern
(K
) /= Value
(Mapping
, Source
(Cur
)) then
369 -- Fall through if no match found. Note that the loops are skipped
370 -- completely in the case of the pattern being longer than the source.
376 (Source
: Wide_String;
377 Pattern
: Wide_String;
378 Going
: Direction
:= Forward
;
379 Mapping
: Wide_Maps
.Wide_Character_Mapping_Function
) return Natural
381 PL1
: constant Integer := Pattern
'Length - 1;
390 -- Check for null pointer in case checks are off
392 if Mapping
= null then
393 raise Constraint_Error
;
396 -- If Pattern longer than Source it can't be found
398 if Pattern
'Length > Source
'Length then
404 if Going
= Forward
then
406 for J
in 1 .. Source
'Length - PL1
loop
409 for K
in Pattern
'Range loop
410 if Pattern
(K
) /= Mapping
.all (Source
(Cur
)) then
426 Ind
:= Source
'Last - PL1
;
427 for J
in reverse 1 .. Source
'Length - PL1
loop
430 for K
in Pattern
'Range loop
431 if Pattern
(K
) /= Mapping
.all (Source
(Cur
)) then
445 -- Fall through if no match found. Note that the loops are skipped
446 -- completely in the case of the pattern being longer than the source.
452 (Source
: Wide_String;
453 Set
: Wide_Maps
.Wide_Character_Set
;
454 Test
: Membership
:= Inside
;
455 Going
: Direction
:= Forward
) return Natural
460 if Going
= Forward
then
461 for J
in Source
'Range loop
462 if Belongs
(Source
(J
), Set
, Test
) then
470 for J
in reverse Source
'Range loop
471 if Belongs
(Source
(J
), Set
, Test
) then
477 -- Fall through if no match
483 (Source
: Wide_String;
484 Pattern
: Wide_String;
486 Going
: Direction
:= Forward
;
487 Mapping
: Wide_Maps
.Wide_Character_Mapping
:= Wide_Maps
.Identity
)
491 if Going
= Forward
then
492 if From
< Source
'First then
497 Index
(Source
(From
.. Source
'Last), Pattern
, Forward
, Mapping
);
500 if From
> Source
'Last then
505 Index
(Source
(Source
'First .. From
), Pattern
, Backward
, Mapping
);
510 (Source
: Wide_String;
511 Pattern
: Wide_String;
513 Going
: Direction
:= Forward
;
514 Mapping
: Wide_Maps
.Wide_Character_Mapping_Function
) return Natural
517 if Going
= Forward
then
518 if From
< Source
'First then
523 (Source
(From
.. Source
'Last), Pattern
, Forward
, Mapping
);
526 if From
> Source
'Last then
531 (Source
(Source
'First .. From
), Pattern
, Backward
, Mapping
);
536 (Source
: Wide_String;
537 Set
: Wide_Maps
.Wide_Character_Set
;
539 Test
: Membership
:= Inside
;
540 Going
: Direction
:= Forward
) return Natural
543 if Going
= Forward
then
544 if From
< Source
'First then
549 Index
(Source
(From
.. Source
'Last), Set
, Test
, Forward
);
552 if From
> Source
'Last then
557 Index
(Source
(Source
'First .. From
), Set
, Test
, Backward
);
561 ---------------------
562 -- Index_Non_Blank --
563 ---------------------
565 function Index_Non_Blank
566 (Source
: Wide_String;
567 Going
: Direction
:= Forward
) return Natural
570 if Going
= Forward
then
571 for J
in Source
'Range loop
572 if Source
(J
) /= Wide_Space
then
577 else -- Going = Backward
578 for J
in reverse Source
'Range loop
579 if Source
(J
) /= Wide_Space
then
585 -- Fall through if no match
590 function Index_Non_Blank
591 (Source
: Wide_String;
593 Going
: Direction
:= Forward
) return Natural
596 if Going
= Forward
then
597 if From
< Source
'First then
602 Index_Non_Blank
(Source
(From
.. Source
'Last), Forward
);
605 if From
> Source
'Last then
610 Index_Non_Blank
(Source
(Source
'First .. From
), Backward
);
614 end Ada
.Strings
.Wide_Search
;