1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . A R R A Y _ S P I T --
9 -- Copyright (C) 2002-2003 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
.Unchecked_Deallocation
;
36 package body GNAT
.Array_Split
is
39 new Ada
.Unchecked_Deallocation
(Slices_Indexes
, Slices_Access
);
42 new Ada
.Unchecked_Deallocation
(Separators_Indexes
, Indexes_Access
);
45 (Source
: Element_Sequence
;
46 Pattern
: Element_Set
)
48 -- Returns the number of occurences of Pattern elements in Source, 0 is
49 -- returned if no occurence is found in Source.
55 procedure Adjust
(S
: in out Slice_Set
) is
57 S
.Ref_Counter
.all := S
.Ref_Counter
.all + 1;
66 From
: Element_Sequence
;
67 Separators
: Element_Sequence
;
68 Mode
: Separator_Mode
:= Single
)
71 Create
(S
, From
, To_Set
(Separators
), Mode
);
80 From
: Element_Sequence
;
81 Separators
: Element_Set
;
82 Mode
: Separator_Mode
:= Single
)
85 S
.Source
:= new Element_Sequence
'(From);
86 Set (S, Separators, Mode);
94 (Source : Element_Sequence;
95 Pattern : Element_Set)
100 for K in Source'Range loop
101 if Is_In (Source (K), Pattern) then
113 procedure Finalize (S : in out Slice_Set) is
116 new Ada.Unchecked_Deallocation (Element_Sequence, Element_Access);
119 new Ada.Unchecked_Deallocation (Natural, Counter);
122 S.Ref_Counter.all := S.Ref_Counter.all - 1;
124 if S.Ref_Counter.all = 0 then
128 Free (S.Ref_Counter);
136 procedure Initialize (S : in out Slice_Set) is
138 S.Ref_Counter := new Natural'(1);
147 Index
: Slice_Number
)
148 return Slice_Separators
151 if Index
> S
.N_Slice
then
155 or else (Index
= 1 and then S
.N_Slice
= 1)
157 -- Whole string, or no separator used.
159 return (Before
=> Array_End
,
163 return (Before
=> Array_End
,
164 After
=> S
.Source
(S
.Slices
(Index
).Stop
+ 1));
166 elsif Index
= S
.N_Slice
then
167 return (Before
=> S
.Source
(S
.Slices
(Index
).Start
- 1),
171 return (Before
=> S
.Source
(S
.Slices
(Index
).Start
- 1),
172 After
=> S
.Source
(S
.Slices
(Index
).Stop
+ 1));
180 function Separators
(S
: Slice_Set
) return Separators_Indexes
is
182 return S
.Indexes
.all;
190 (S
: in out Slice_Set
;
191 Separators
: Element_Sequence
;
192 Mode
: Separator_Mode
:= Single
)
195 Set
(S
, To_Set
(Separators
), Mode
);
203 (S
: in out Slice_Set
;
204 Separators
: Element_Set
;
205 Mode
: Separator_Mode
:= Single
)
207 Count_Sep
: constant Natural := Count
(S
.Source
.all, Separators
);
210 -- Free old structure
214 -- Compute all separator's indexes
216 S
.Indexes
:= new Separators_Indexes
(1 .. Count_Sep
);
217 J
:= S
.Indexes
'First;
219 for K
in S
.Source
'Range loop
220 if Is_In
(S
.Source
(K
), Separators
) then
226 -- Compute slice info for fast slice access
229 S_Info
: Slices_Indexes
(1 .. Slice_Number
(Count_Sep
) + 1);
231 Start
, Stop
: Natural;
236 Start
:= S
.Source
'First;
240 if K
> Count_Sep
then
241 -- No more separator, last slice end at the end of the source
243 Stop
:= S
.Source
'Last;
245 Stop
:= S
.Indexes
(K
) - 1;
248 -- Add slice to the table
250 S
.N_Slice
:= S
.N_Slice
+ 1;
251 S_Info
(S
.N_Slice
) := (Start
, Stop
);
253 exit when K
> Count_Sep
;
258 -- In this mode just set start to character next to the
259 -- current separator, advance the separator index.
260 Start
:= S
.Indexes
(K
) + 1;
264 -- In this mode skip separators following each others
266 Start
:= S
.Indexes
(K
) + 1;
268 exit when K
> Count_Sep
269 or else S
.Indexes
(K
) > S
.Indexes
(K
- 1) + 1;
275 S
.Slices
:= new Slices_Indexes
'(S_Info (1 .. S.N_Slice));
285 Index : Slice_Number)
286 return Element_Sequence
292 elsif Index > S.N_Slice then
296 return S.Source (S.Slices (Index).Start .. S.Slices (Index).Stop);
304 function Slice_Count (S : Slice_Set) return Slice_Number is
309 end GNAT.Array_Split;