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 --
9 -- Copyright (C) 1992,1993,1994 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 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
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. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 with Ada
.Strings
.Wide_Maps
; use Ada
.Strings
.Wide_Maps
;
36 package body Ada
.Strings
.Wide_Search
is
38 -----------------------
39 -- Local Subprograms --
40 -----------------------
43 (Element
: Wide_Character;
44 Set
: Wide_Maps
.Wide_Character_Set
;
47 pragma Inline
(Belongs
);
48 -- Determines if the given element is in (Test = Inside) or not in
49 -- (Test = Outside) the given character set.
56 (Element
: Wide_Character;
57 Set
: Wide_Maps
.Wide_Character_Set
;
63 return Is_In
(Element
, Set
);
65 return not Is_In
(Element
, Set
);
74 (Source
: in Wide_String;
75 Pattern
: in Wide_String;
76 Mapping
: in Wide_Maps
.Wide_Character_Mapping
:= Wide_Maps
.Identity
)
87 -- Handle the case of non-identity mappings by creating a mapped
88 -- string and making a recursive call using the identity mapping
89 -- on this mapped string.
91 if Mapping
/= Wide_Maps
.Identity
then
93 Mapped_Source
: Wide_String (Source
'Range);
96 for J
in Source
'Range loop
97 Mapped_Source
(J
) := Value
(Mapping
, Source
(J
));
100 return Count
(Mapped_Source
, Pattern
);
107 while J
<= Source
'Last - (Pattern
'Length - 1) loop
108 if Source
(J
.. J
+ (Pattern
'Length - 1)) = Pattern
then
110 J
:= J
+ Pattern
'Length;
120 (Source
: in Wide_String;
121 Pattern
: in Wide_String;
122 Mapping
: in Wide_Maps
.Wide_Character_Mapping_Function
)
125 Mapped_Source
: Wide_String (Source
'Range);
128 for J
in Source
'Range loop
129 Mapped_Source
(J
) := Mapping
(Source
(J
));
132 return Count
(Mapped_Source
, Pattern
);
135 function Count
(Source
: in Wide_String;
136 Set
: in Wide_Maps
.Wide_Character_Set
)
142 for J
in Source
'Range loop
143 if Is_In
(Source
(J
), Set
) then
156 (Source
: in Wide_String;
157 Set
: in Wide_Maps
.Wide_Character_Set
;
158 Test
: in Membership
;
159 First
: out Positive;
163 for J
in Source
'Range loop
164 if Belongs
(Source
(J
), Set
, Test
) then
167 for K
in J
+ 1 .. Source
'Last loop
168 if not Belongs
(Source
(K
), Set
, Test
) then
174 -- Here if J indexes 1st char of token, and all chars
175 -- after J are in the token
182 -- Here if no token found
184 First
:= Source
'First;
193 (Source
: in Wide_String;
194 Pattern
: in Wide_String;
195 Going
: in Direction
:= Forward
;
196 Mapping
: in Wide_Maps
.Wide_Character_Mapping
:= Wide_Maps
.Identity
)
204 -- Handle the case of non-identity mappings by creating a mapped
205 -- string and making a recursive call using the identity mapping
206 -- on this mapped string.
208 if Mapping
/= Identity
then
210 Mapped_Source
: Wide_String (Source
'Range);
213 for J
in Source
'Range loop
214 Mapped_Source
(J
) := Value
(Mapping
, Source
(J
));
217 return Index
(Mapped_Source
, Pattern
, Going
);
221 if Going
= Forward
then
222 for J
in Source
'First .. Source
'Last - Pattern
'Length + 1 loop
223 if Pattern
= Source
(J
.. J
+ Pattern
'Length - 1) then
228 else -- Going = Backward
229 for J
in reverse Source
'First .. Source
'Last - Pattern
'Length + 1 loop
230 if Pattern
= Source
(J
.. J
+ Pattern
'Length - 1) then
236 -- Fall through if no match found. Note that the loops are skipped
237 -- completely in the case of the pattern being longer than the source.
247 (Source
: in Wide_String;
248 Pattern
: in Wide_String;
249 Going
: in Direction
:= Forward
;
250 Mapping
: in Wide_Maps
.Wide_Character_Mapping_Function
)
253 Mapped_Source
: Wide_String (Source
'Range);
256 for J
in Source
'Range loop
257 Mapped_Source
(J
) := Mapping
(Source
(J
));
260 return Index
(Mapped_Source
, Pattern
, Going
);
264 (Source
: in Wide_String;
265 Set
: in Wide_Maps
.Wide_Character_Set
;
266 Test
: in Membership
:= Inside
;
267 Going
: in Direction
:= Forward
)
271 if Going
= Forward
then
272 for J
in Source
'Range loop
273 if Belongs
(Source
(J
), Set
, Test
) then
278 else -- Going = Backward
279 for J
in reverse Source
'Range loop
280 if Belongs
(Source
(J
), Set
, Test
) then
286 -- Fall through if no match
291 ---------------------
292 -- Index_Non_Blank --
293 ---------------------
295 function Index_Non_Blank
296 (Source
: in Wide_String;
297 Going
: in Direction
:= Forward
)
301 if Going
= Forward
then
302 for J
in Source
'Range loop
303 if Source
(J
) /= Wide_Space
then
308 else -- Going = Backward
309 for J
in reverse Source
'Range loop
310 if Source
(J
) /= Wide_Space
then
316 -- Fall through if no match
322 end Ada
.Strings
.Wide_Search
;