1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . A R R A Y _ S P L I T --
9 -- Copyright (C) 2002-2013, 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
.Unchecked_Deallocation
;
34 package body GNAT
.Array_Split
is
37 new Ada
.Unchecked_Deallocation
(Slices_Indexes
, Slices_Access
);
40 new Ada
.Unchecked_Deallocation
(Separators_Indexes
, Indexes_Access
);
43 new Ada
.Unchecked_Deallocation
(Element_Sequence
, Element_Access
);
46 (Source
: Element_Sequence
;
47 Pattern
: Element_Set
) return Natural;
48 -- Returns the number of occurrences of Pattern elements in Source, 0 is
49 -- returned if no occurrence 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
)
86 S
.Source
:= new Element_Sequence
'(From);
87 Set (S, Separators, Mode);
95 (Source : Element_Sequence;
96 Pattern : Element_Set) return Natural
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);
121 Ref_Counter : Counter := S.Ref_Counter;
124 -- Ensure call is idempotent
126 S.Ref_Counter := null;
128 if Ref_Counter /= null then
129 Ref_Counter.all := Ref_Counter.all - 1;
131 if Ref_Counter.all = 0 then
144 procedure Initialize (S : in out Slice_Set) is
146 S.Ref_Counter := new Natural'(1);
155 Index
: Slice_Number
) return Slice_Separators
158 if Index
> S
.N_Slice
then
162 or else (Index
= 1 and then S
.N_Slice
= 1)
164 -- Whole string, or no separator used
166 return (Before
=> Array_End
,
170 return (Before
=> Array_End
,
171 After
=> S
.Source
(S
.Slices
(Index
).Stop
+ 1));
173 elsif Index
= S
.N_Slice
then
174 return (Before
=> S
.Source
(S
.Slices
(Index
).Start
- 1),
178 return (Before
=> S
.Source
(S
.Slices
(Index
).Start
- 1),
179 After
=> S
.Source
(S
.Slices
(Index
).Stop
+ 1));
187 function Separators
(S
: Slice_Set
) return Separators_Indexes
is
189 return S
.Indexes
.all;
197 (S
: in out Slice_Set
;
198 Separators
: Element_Sequence
;
199 Mode
: Separator_Mode
:= Single
)
202 Set
(S
, To_Set
(Separators
), Mode
);
210 (S
: in out Slice_Set
;
211 Separators
: Element_Set
;
212 Mode
: Separator_Mode
:= Single
)
214 Count_Sep
: constant Natural := Count
(S
.Source
.all, Separators
);
217 -- Free old structure
221 -- Compute all separator's indexes
223 S
.Indexes
:= new Separators_Indexes
(1 .. Count_Sep
);
224 J
:= S
.Indexes
'First;
226 for K
in S
.Source
'Range loop
227 if Is_In
(S
.Source
(K
), Separators
) then
233 -- Compute slice info for fast slice access
236 S_Info
: Slices_Indexes
(1 .. Slice_Number
(Count_Sep
) + 1);
238 Start
, Stop
: Natural;
243 Start
:= S
.Source
'First;
247 if K
> Count_Sep
then
249 -- No more separators, last slice ends at end of source string
251 Stop
:= S
.Source
'Last;
254 Stop
:= S
.Indexes
(K
) - 1;
257 -- Add slice to the table
259 S
.N_Slice
:= S
.N_Slice
+ 1;
260 S_Info
(S
.N_Slice
) := (Start
, Stop
);
262 exit when K
> Count_Sep
;
268 -- In this mode just set start to character next to the
269 -- current separator, advance the separator index.
271 Start
:= S
.Indexes
(K
) + 1;
276 -- In this mode skip separators following each other
279 Start
:= S
.Indexes
(K
) + 1;
281 exit when K
> Count_Sep
282 or else S
.Indexes
(K
) > S
.Indexes
(K
- 1) + 1;
288 S
.Slices
:= new Slices_Indexes
'(S_Info (1 .. S.N_Slice));
298 Index : Slice_Number) return Element_Sequence
304 elsif Index > S.N_Slice then
308 return S.Source (S.Slices (Index).Start .. S.Slices (Index).Stop);
316 function Slice_Count (S : Slice_Set) return Slice_Number is
321 end GNAT.Array_Split;