1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME COMPONENTS --
5 -- A D A . S T R I N G S . W I D E _ S E A R C H --
11 -- Copyright (C) 1992,1993,1994 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- As a special exception, if other files instantiate generics from this --
25 -- unit, or you link this unit with other files to produce an executable, --
26 -- this unit does not by itself cause the resulting executable to be --
27 -- covered by the GNU General Public License. This exception does not --
28 -- however invalidate any other reasons why the executable file might be --
29 -- covered by the GNU Public License. --
31 -- GNAT was originally developed by the GNAT team at New York University. --
32 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
34 ------------------------------------------------------------------------------
36 with Ada
.Strings
.Wide_Maps
; use Ada
.Strings
.Wide_Maps
;
38 package body Ada
.Strings
.Wide_Search
is
40 -----------------------
41 -- Local Subprograms --
42 -----------------------
45 (Element
: Wide_Character;
46 Set
: Wide_Maps
.Wide_Character_Set
;
49 pragma Inline
(Belongs
);
50 -- Determines if the given element is in (Test = Inside) or not in
51 -- (Test = Outside) the given character set.
58 (Element
: Wide_Character;
59 Set
: Wide_Maps
.Wide_Character_Set
;
65 return Is_In
(Element
, Set
);
67 return not Is_In
(Element
, Set
);
76 (Source
: in Wide_String;
77 Pattern
: in Wide_String;
78 Mapping
: in Wide_Maps
.Wide_Character_Mapping
:= Wide_Maps
.Identity
)
89 -- Handle the case of non-identity mappings by creating a mapped
90 -- string and making a recursive call using the identity mapping
91 -- on this mapped string.
93 if Mapping
/= Wide_Maps
.Identity
then
95 Mapped_Source
: Wide_String (Source
'Range);
98 for J
in Source
'Range loop
99 Mapped_Source
(J
) := Value
(Mapping
, Source
(J
));
102 return Count
(Mapped_Source
, Pattern
);
109 while J
<= Source
'Last - (Pattern
'Length - 1) loop
110 if Source
(J
.. J
+ (Pattern
'Length - 1)) = Pattern
then
112 J
:= J
+ Pattern
'Length;
122 (Source
: in Wide_String;
123 Pattern
: in Wide_String;
124 Mapping
: in Wide_Maps
.Wide_Character_Mapping_Function
)
127 Mapped_Source
: Wide_String (Source
'Range);
130 for J
in Source
'Range loop
131 Mapped_Source
(J
) := Mapping
(Source
(J
));
134 return Count
(Mapped_Source
, Pattern
);
137 function Count
(Source
: in Wide_String;
138 Set
: in Wide_Maps
.Wide_Character_Set
)
144 for J
in Source
'Range loop
145 if Is_In
(Source
(J
), Set
) then
158 (Source
: in Wide_String;
159 Set
: in Wide_Maps
.Wide_Character_Set
;
160 Test
: in Membership
;
161 First
: out Positive;
165 for J
in Source
'Range loop
166 if Belongs
(Source
(J
), Set
, Test
) then
169 for K
in J
+ 1 .. Source
'Last loop
170 if not Belongs
(Source
(K
), Set
, Test
) then
176 -- Here if J indexes 1st char of token, and all chars
177 -- after J are in the token
184 -- Here if no token found
186 First
:= Source
'First;
195 (Source
: in Wide_String;
196 Pattern
: in Wide_String;
197 Going
: in Direction
:= Forward
;
198 Mapping
: in Wide_Maps
.Wide_Character_Mapping
:= Wide_Maps
.Identity
)
206 -- Handle the case of non-identity mappings by creating a mapped
207 -- string and making a recursive call using the identity mapping
208 -- on this mapped string.
210 if Mapping
/= Identity
then
212 Mapped_Source
: Wide_String (Source
'Range);
215 for J
in Source
'Range loop
216 Mapped_Source
(J
) := Value
(Mapping
, Source
(J
));
219 return Index
(Mapped_Source
, Pattern
, Going
);
223 if Going
= Forward
then
224 for J
in Source
'First .. Source
'Last - Pattern
'Length + 1 loop
225 if Pattern
= Source
(J
.. J
+ Pattern
'Length - 1) then
230 else -- Going = Backward
231 for J
in reverse Source
'First .. Source
'Last - Pattern
'Length + 1 loop
232 if Pattern
= Source
(J
.. J
+ Pattern
'Length - 1) then
238 -- Fall through if no match found. Note that the loops are skipped
239 -- completely in the case of the pattern being longer than the source.
249 (Source
: in Wide_String;
250 Pattern
: in Wide_String;
251 Going
: in Direction
:= Forward
;
252 Mapping
: in Wide_Maps
.Wide_Character_Mapping_Function
)
255 Mapped_Source
: Wide_String (Source
'Range);
258 for J
in Source
'Range loop
259 Mapped_Source
(J
) := Mapping
(Source
(J
));
262 return Index
(Mapped_Source
, Pattern
, Going
);
266 (Source
: in Wide_String;
267 Set
: in Wide_Maps
.Wide_Character_Set
;
268 Test
: in Membership
:= Inside
;
269 Going
: in Direction
:= Forward
)
273 if Going
= Forward
then
274 for J
in Source
'Range loop
275 if Belongs
(Source
(J
), Set
, Test
) then
280 else -- Going = Backward
281 for J
in reverse Source
'Range loop
282 if Belongs
(Source
(J
), Set
, Test
) then
288 -- Fall through if no match
293 ---------------------
294 -- Index_Non_Blank --
295 ---------------------
297 function Index_Non_Blank
298 (Source
: in Wide_String;
299 Going
: in Direction
:= Forward
)
303 if Going
= Forward
then
304 for J
in Source
'Range loop
305 if Source
(J
) /= Wide_Space
then
310 else -- Going = Backward
311 for J
in reverse Source
'Range loop
312 if Source
(J
) /= Wide_Space
then
318 -- Fall through if no match
324 end Ada
.Strings
.Wide_Search
;