Merge from mainline (160224:163495).
[official-gcc/graphite-test-results.git] / gcc / ada / a-strunb-shared.ads
blobb4b7c62275930de451d05d8c4a9475ae97bd70d6
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- A D A . S T R I N G S . U N B O U N D E D --
6 -- --
7 -- S p e c --
8 -- --
9 -- Copyright (C) 1992-2010, Free Software Foundation, Inc. --
10 -- --
11 -- This specification is derived from the Ada Reference Manual for use with --
12 -- GNAT. The copyright notice above, and the license provisions that follow --
13 -- apply solely to the contents of the part following the private keyword. --
14 -- --
15 -- GNAT is free software; you can redistribute it and/or modify it under --
16 -- terms of the GNU General Public License as published by the Free Soft- --
17 -- ware Foundation; either version 2, or (at your option) any later ver- --
18 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
19 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
20 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
21 -- for more details. You should have received a copy of the GNU General --
22 -- Public License distributed with GNAT; see file COPYING. If not, write --
23 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
24 -- Boston, MA 02110-1301, USA. --
25 -- --
26 -- As a special exception, if other files instantiate generics from this --
27 -- unit, or you link this unit with other files to produce an executable, --
28 -- this unit does not by itself cause the resulting executable to be --
29 -- covered by the GNU General Public License. This exception does not --
30 -- however invalidate any other reasons why the executable file might be --
31 -- covered by the GNU Public License. --
32 -- --
33 -- GNAT was originally developed by the GNAT team at New York University. --
34 -- Extensive contributions were provided by Ada Core Technologies Inc. --
35 -- --
36 ------------------------------------------------------------------------------
38 -- This package provides an implementation of Ada.Strings.Unbounded that uses
39 -- reference counts to implement copy on modification (rather than copy on
40 -- assignment). This is significantly more efficient on many targets.
42 -- This version is supported on:
43 -- - all Alpha platforms
44 -- - all ia64 platforms
45 -- - all PowerPC platforms
46 -- - all SPARC V9 platforms
47 -- - all x86_64 platforms
49 -- This package uses several techniques to increase speed:
51 -- - Implicit sharing or copy-on-write. An Unbounded_String contains only
52 -- the reference to the data which is shared between several instances.
53 -- The shared data is reallocated only when its value is changed and
54 -- the object mutation can't be used or it is unefficient to use it.
56 -- - Object mutation. Shared data object can be reused without memory
57 -- reallocation when all of the following requirements are met:
58 -- - shared data object is no longer used by anyone else.
59 -- - the size is sufficient to store new value.
60 -- - the gap after reuse is less then a defined threashold.
62 -- - Memory preallocation. Most of used memory allocation algorithms
63 -- allign allocated segments on the some boundary, thus some amount of
64 -- additional memory can be preallocated without any impact. Such
65 -- preallocated memory can used later by Append/Insert operations
66 -- without reallocation.
68 -- Reference counting uses GCC builtin atomic operations, which allows to
69 -- safely share internal data between Ada tasks. Nevertheless, this not
70 -- make objects of Unbounded_String thread-safe, so each instance can't be
71 -- accessed by several tasks simulatenously.
73 with Ada.Strings.Maps;
74 private with Ada.Finalization;
75 private with Interfaces;
77 package Ada.Strings.Unbounded is
78 pragma Preelaborate;
80 type Unbounded_String is private;
81 pragma Preelaborable_Initialization (Unbounded_String);
83 Null_Unbounded_String : constant Unbounded_String;
85 function Length (Source : Unbounded_String) return Natural;
87 type String_Access is access all String;
89 procedure Free (X : in out String_Access);
91 --------------------------------------------------------
92 -- Conversion, Concatenation, and Selection Functions --
93 --------------------------------------------------------
95 function To_Unbounded_String
96 (Source : String) return Unbounded_String;
98 function To_Unbounded_String
99 (Length : Natural) return Unbounded_String;
101 function To_String (Source : Unbounded_String) return String;
103 procedure Set_Unbounded_String
104 (Target : out Unbounded_String;
105 Source : String);
106 pragma Ada_05 (Set_Unbounded_String);
108 procedure Append
109 (Source : in out Unbounded_String;
110 New_Item : Unbounded_String);
112 procedure Append
113 (Source : in out Unbounded_String;
114 New_Item : String);
116 procedure Append
117 (Source : in out Unbounded_String;
118 New_Item : Character);
120 function "&"
121 (Left : Unbounded_String;
122 Right : Unbounded_String) return Unbounded_String;
124 function "&"
125 (Left : Unbounded_String;
126 Right : String) return Unbounded_String;
128 function "&"
129 (Left : String;
130 Right : Unbounded_String) return Unbounded_String;
132 function "&"
133 (Left : Unbounded_String;
134 Right : Character) return Unbounded_String;
136 function "&"
137 (Left : Character;
138 Right : Unbounded_String) return Unbounded_String;
140 function Element
141 (Source : Unbounded_String;
142 Index : Positive) return Character;
144 procedure Replace_Element
145 (Source : in out Unbounded_String;
146 Index : Positive;
147 By : Character);
149 function Slice
150 (Source : Unbounded_String;
151 Low : Positive;
152 High : Natural) return String;
154 function Unbounded_Slice
155 (Source : Unbounded_String;
156 Low : Positive;
157 High : Natural) return Unbounded_String;
158 pragma Ada_05 (Unbounded_Slice);
160 procedure Unbounded_Slice
161 (Source : Unbounded_String;
162 Target : out Unbounded_String;
163 Low : Positive;
164 High : Natural);
165 pragma Ada_05 (Unbounded_Slice);
167 function "="
168 (Left : Unbounded_String;
169 Right : Unbounded_String) return Boolean;
171 function "="
172 (Left : Unbounded_String;
173 Right : String) return Boolean;
175 function "="
176 (Left : String;
177 Right : Unbounded_String) return Boolean;
179 function "<"
180 (Left : Unbounded_String;
181 Right : Unbounded_String) return Boolean;
183 function "<"
184 (Left : Unbounded_String;
185 Right : String) return Boolean;
187 function "<"
188 (Left : String;
189 Right : Unbounded_String) return Boolean;
191 function "<="
192 (Left : Unbounded_String;
193 Right : Unbounded_String) return Boolean;
195 function "<="
196 (Left : Unbounded_String;
197 Right : String) return Boolean;
199 function "<="
200 (Left : String;
201 Right : Unbounded_String) return Boolean;
203 function ">"
204 (Left : Unbounded_String;
205 Right : Unbounded_String) return Boolean;
207 function ">"
208 (Left : Unbounded_String;
209 Right : String) return Boolean;
211 function ">"
212 (Left : String;
213 Right : Unbounded_String) return Boolean;
215 function ">="
216 (Left : Unbounded_String;
217 Right : Unbounded_String) return Boolean;
219 function ">="
220 (Left : Unbounded_String;
221 Right : String) return Boolean;
223 function ">="
224 (Left : String;
225 Right : Unbounded_String) return Boolean;
227 ------------------------
228 -- Search Subprograms --
229 ------------------------
231 function Index
232 (Source : Unbounded_String;
233 Pattern : String;
234 Going : Direction := Forward;
235 Mapping : Maps.Character_Mapping := Maps.Identity) return Natural;
237 function Index
238 (Source : Unbounded_String;
239 Pattern : String;
240 Going : Direction := Forward;
241 Mapping : Maps.Character_Mapping_Function) return Natural;
243 function Index
244 (Source : Unbounded_String;
245 Set : Maps.Character_Set;
246 Test : Membership := Inside;
247 Going : Direction := Forward) return Natural;
249 function Index
250 (Source : Unbounded_String;
251 Pattern : String;
252 From : Positive;
253 Going : Direction := Forward;
254 Mapping : Maps.Character_Mapping := Maps.Identity) return Natural;
255 pragma Ada_05 (Index);
257 function Index
258 (Source : Unbounded_String;
259 Pattern : String;
260 From : Positive;
261 Going : Direction := Forward;
262 Mapping : Maps.Character_Mapping_Function) return Natural;
263 pragma Ada_05 (Index);
265 function Index
266 (Source : Unbounded_String;
267 Set : Maps.Character_Set;
268 From : Positive;
269 Test : Membership := Inside;
270 Going : Direction := Forward) return Natural;
271 pragma Ada_05 (Index);
273 function Index_Non_Blank
274 (Source : Unbounded_String;
275 Going : Direction := Forward) return Natural;
277 function Index_Non_Blank
278 (Source : Unbounded_String;
279 From : Positive;
280 Going : Direction := Forward) return Natural;
281 pragma Ada_05 (Index_Non_Blank);
283 function Count
284 (Source : Unbounded_String;
285 Pattern : String;
286 Mapping : Maps.Character_Mapping := Maps.Identity) return Natural;
288 function Count
289 (Source : Unbounded_String;
290 Pattern : String;
291 Mapping : Maps.Character_Mapping_Function) return Natural;
293 function Count
294 (Source : Unbounded_String;
295 Set : Maps.Character_Set) return Natural;
297 procedure Find_Token
298 (Source : Unbounded_String;
299 Set : Maps.Character_Set;
300 Test : Membership;
301 First : out Positive;
302 Last : out Natural);
304 ------------------------------------
305 -- String Translation Subprograms --
306 ------------------------------------
308 function Translate
309 (Source : Unbounded_String;
310 Mapping : Maps.Character_Mapping) return Unbounded_String;
312 procedure Translate
313 (Source : in out Unbounded_String;
314 Mapping : Maps.Character_Mapping);
316 function Translate
317 (Source : Unbounded_String;
318 Mapping : Maps.Character_Mapping_Function) return Unbounded_String;
320 procedure Translate
321 (Source : in out Unbounded_String;
322 Mapping : Maps.Character_Mapping_Function);
324 ---------------------------------------
325 -- String Transformation Subprograms --
326 ---------------------------------------
328 function Replace_Slice
329 (Source : Unbounded_String;
330 Low : Positive;
331 High : Natural;
332 By : String) return Unbounded_String;
334 procedure Replace_Slice
335 (Source : in out Unbounded_String;
336 Low : Positive;
337 High : Natural;
338 By : String);
340 function Insert
341 (Source : Unbounded_String;
342 Before : Positive;
343 New_Item : String) return Unbounded_String;
345 procedure Insert
346 (Source : in out Unbounded_String;
347 Before : Positive;
348 New_Item : String);
350 function Overwrite
351 (Source : Unbounded_String;
352 Position : Positive;
353 New_Item : String) return Unbounded_String;
355 procedure Overwrite
356 (Source : in out Unbounded_String;
357 Position : Positive;
358 New_Item : String);
360 function Delete
361 (Source : Unbounded_String;
362 From : Positive;
363 Through : Natural) return Unbounded_String;
365 procedure Delete
366 (Source : in out Unbounded_String;
367 From : Positive;
368 Through : Natural);
370 function Trim
371 (Source : Unbounded_String;
372 Side : Trim_End) return Unbounded_String;
374 procedure Trim
375 (Source : in out Unbounded_String;
376 Side : Trim_End);
378 function Trim
379 (Source : Unbounded_String;
380 Left : Maps.Character_Set;
381 Right : Maps.Character_Set) return Unbounded_String;
383 procedure Trim
384 (Source : in out Unbounded_String;
385 Left : Maps.Character_Set;
386 Right : Maps.Character_Set);
388 function Head
389 (Source : Unbounded_String;
390 Count : Natural;
391 Pad : Character := Space) return Unbounded_String;
393 procedure Head
394 (Source : in out Unbounded_String;
395 Count : Natural;
396 Pad : Character := Space);
398 function Tail
399 (Source : Unbounded_String;
400 Count : Natural;
401 Pad : Character := Space) return Unbounded_String;
403 procedure Tail
404 (Source : in out Unbounded_String;
405 Count : Natural;
406 Pad : Character := Space);
408 function "*"
409 (Left : Natural;
410 Right : Character) return Unbounded_String;
412 function "*"
413 (Left : Natural;
414 Right : String) return Unbounded_String;
416 function "*"
417 (Left : Natural;
418 Right : Unbounded_String) return Unbounded_String;
420 private
421 pragma Inline (Length);
423 package AF renames Ada.Finalization;
425 type Shared_String (Max_Length : Natural) is limited record
426 Counter : aliased Interfaces.Unsigned_32 := 1;
427 -- Reference counter
429 Last : Natural := 0;
430 Data : String (1 .. Max_Length);
431 -- Last is the index of last significant element of the Data. All
432 -- elements with larger indexes are currently insignificant.
433 end record;
435 type Shared_String_Access is access all Shared_String;
437 procedure Reference (Item : not null Shared_String_Access);
438 -- Increment reference counter
440 procedure Unreference (Item : not null Shared_String_Access);
441 -- Decrement reference counter, deallocate Item when counter goes to zero
443 function Can_Be_Reused
444 (Item : Shared_String_Access;
445 Length : Natural) return Boolean;
446 -- Returns True if Shared_String can be reused. There are two criteria when
447 -- Shared_String can be reused: its reference counter must be one (thus
448 -- Shared_String is owned exclusively) and its size is sufficient to
449 -- store string with specified length effectively.
451 function Allocate (Max_Length : Natural) return Shared_String_Access;
452 -- Allocates new Shared_String with at least specified maximum length.
453 -- Actual maximum length of the allocated Shared_String can be sligtly
454 -- greater. Returns reference to Empty_Shared_String when requested length
455 -- is zero.
457 Empty_Shared_String : aliased Shared_String (0);
459 function To_Unbounded (S : String) return Unbounded_String
460 renames To_Unbounded_String;
461 -- This renames are here only to be used in the pragma Stream_Convert
463 type Unbounded_String is new AF.Controlled with record
464 Reference : Shared_String_Access := Empty_Shared_String'Access;
465 end record;
467 pragma Stream_Convert (Unbounded_String, To_Unbounded, To_String);
468 -- Provide stream routines without dragging in Ada.Streams
470 pragma Finalize_Storage_Only (Unbounded_String);
471 -- Finalization is required only for freeing storage
473 overriding procedure Initialize (Object : in out Unbounded_String);
474 overriding procedure Adjust (Object : in out Unbounded_String);
475 overriding procedure Finalize (Object : in out Unbounded_String);
477 Null_Unbounded_String : constant Unbounded_String :=
478 (AF.Controlled with
479 Reference => Empty_Shared_String'Access);
481 end Ada.Strings.Unbounded;