1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . A R R A Y _ S P L I T --
9 -- Copyright (C) 2002-2009, 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 -- Useful array-manipulation routines: given a set of separators, split
33 -- an array wherever the separators appear, and provide direct access
34 -- to the resulting slices.
36 with Ada
.Finalization
;
40 -- Element of the array, this must be a discrete type
42 type Element_Sequence
is array (Positive range <>) of Element
;
43 -- The array which is a sequence of element
45 type Element_Set
is private;
46 -- This type represent a set of elements. This set does not defined a
47 -- specific order of the elements. The conversion of a sequence to a
48 -- set and membership tests in the set is performed using the routines
49 -- To_Set and Is_In defined below.
51 with function To_Set
(Sequence
: Element_Sequence
) return Element_Set
;
52 -- Returns an Element_Set given an Element_Sequence. Duplicate elements
53 -- can be ignored during this conversion.
55 with function Is_In
(Item
: Element
; Set
: Element_Set
) return Boolean;
56 -- Returns True if Item is found in Set, False otherwise
58 package GNAT
.Array_Split
is
60 Index_Error
: exception;
61 -- Raised by all operations below if Index > Field_Count (S)
63 type Separator_Mode
is
65 -- In this mode the array is cut at each element in the separator
66 -- set. If two separators are contiguous the result at that position
70 -- In this mode contiguous separators are handled as a single
71 -- separator and no empty slice is created.
74 type Slice_Set
is private;
75 -- This type uses by-reference semantics. This is a set of slices as
76 -- returned by Create or Set routines below. The abstraction represents
77 -- a set of items. Each item is a part of the original string named a
78 -- Slice. It is possible to access individual slices by using the Slice
79 -- routine below. The first slice in the Set is at the position/index
80 -- 1. The total number of slices in the set is returned by Slice_Count.
84 From
: Element_Sequence
;
85 Separators
: Element_Sequence
;
86 Mode
: Separator_Mode
:= Single
);
87 -- Create a cut array object. From is the source array, and Separators
88 -- is a sequence of Element along which to split the array. The source
89 -- array is sliced at separator boundaries. The separators are not
90 -- included as part of the resulting slices.
92 -- Note that if From is terminated by a separator an extra empty element
93 -- is added to the slice set. If From only contains a separator the slice
94 -- set contains two empty elements.
98 From
: Element_Sequence
;
99 Separators
: Element_Set
;
100 Mode
: Separator_Mode
:= Single
);
101 -- Same as above but using a Element_Set
104 (S
: in out Slice_Set
;
105 Separators
: Element_Sequence
;
106 Mode
: Separator_Mode
:= Single
);
107 -- Change the set of separators. The source array will be split according
108 -- to this new set of separators.
111 (S
: in out Slice_Set
;
112 Separators
: Element_Set
;
113 Mode
: Separator_Mode
:= Single
);
114 -- Same as above but using a Element_Set
116 type Slice_Number
is new Natural;
117 -- Type used to count number of slices
119 function Slice_Count
(S
: Slice_Set
) return Slice_Number
;
120 pragma Inline
(Slice_Count
);
121 -- Returns the number of slices (fields) in S
125 Index
: Slice_Number
) return Element_Sequence
;
126 pragma Inline
(Slice
);
127 -- Returns the slice at position Index. First slice is 1. If Index is 0
128 -- the whole array is returned including the separators (this is the
129 -- original source array).
131 type Position
is (Before
, After
);
132 -- Used to designate position of separator
134 type Slice_Separators
is array (Position
) of Element
;
135 -- Separators found before and after the slice
137 Array_End
: constant Element
;
138 -- This is the separator returned for the start or the end of the array
142 Index
: Slice_Number
) return Slice_Separators
;
143 -- Returns the separators used to slice (front and back) the slice at
144 -- position Index. For slices at start and end of the original array, the
145 -- Array_End value is returned for the corresponding outer bound. In
146 -- Multiple mode only the element closest to the slice is returned.
147 -- if Index = 0, returns (Array_End, Array_End).
149 type Separators_Indexes
is array (Positive range <>) of Positive;
151 function Separators
(S
: Slice_Set
) return Separators_Indexes
;
152 -- Returns indexes of all separators used to slice original source array S
156 Array_End
: constant Element
:= Element
'First;
158 type Element_Access
is access Element_Sequence
;
160 type Counter
is access Natural;
162 type Indexes_Access
is access Separators_Indexes
;
164 type Slice_Info
is record
168 -- Starting/Ending position of a slice. This does not include separators
170 type Slices_Indexes
is array (Slice_Number
range <>) of Slice_Info
;
171 type Slices_Access
is access Slices_Indexes
;
172 -- All indexes for fast access to slices. In the Slice_Set we keep only
173 -- the original array and the indexes where each slice start and stop.
175 type Slice_Set
is new Ada
.Finalization
.Controlled
with record
176 Ref_Counter
: Counter
; -- Reference counter, by-address sem
177 Source
: Element_Access
;
178 N_Slice
: Slice_Number
:= 0; -- Number of slices found
179 Indexes
: Indexes_Access
;
180 Slices
: Slices_Access
;
183 procedure Initialize
(S
: in out Slice_Set
);
184 procedure Adjust
(S
: in out Slice_Set
);
185 procedure Finalize
(S
: in out Slice_Set
);
187 end GNAT
.Array_Split
;