1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . S T R I N G S . W I D E _ U N B O U N D E D --
9 -- Copyright (C) 1992-2014, Free Software Foundation, Inc. --
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. --
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 3, 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. --
22 -- As a special exception under Section 7 of GPL version 3, you are granted --
23 -- additional permissions described in the GCC Runtime Library Exception, --
24 -- version 3.1, as published by the Free Software Foundation. --
26 -- You should have received a copy of the GNU General Public License and --
27 -- a copy of the GCC Runtime Library Exception along with this program; --
28 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
29 -- <http://www.gnu.org/licenses/>. --
31 -- GNAT was originally developed by the GNAT team at New York University. --
32 -- Extensive contributions were provided by Ada Core Technologies Inc. --
34 ------------------------------------------------------------------------------
36 -- This version is supported on:
37 -- - all Alpha platforms
38 -- - all ia64 platforms
39 -- - all PowerPC platforms
40 -- - all SPARC V9 platforms
41 -- - all x86 platforms
42 -- - all x86_64 platforms
44 with Ada
.Strings
.Wide_Maps
;
45 private with Ada
.Finalization
;
46 private with System
.Atomic_Counters
;
48 package Ada
.Strings
.Wide_Unbounded
is
51 type Unbounded_Wide_String
is private;
52 pragma Preelaborable_Initialization
(Unbounded_Wide_String
);
54 Null_Unbounded_Wide_String
: constant Unbounded_Wide_String
;
56 function Length
(Source
: Unbounded_Wide_String
) return Natural;
58 type Wide_String_Access
is access all Wide_String;
60 procedure Free
(X
: in out Wide_String_Access
);
62 --------------------------------------------------------
63 -- Conversion, Concatenation, and Selection Functions --
64 --------------------------------------------------------
66 function To_Unbounded_Wide_String
67 (Source
: Wide_String) return Unbounded_Wide_String
;
69 function To_Unbounded_Wide_String
70 (Length
: Natural) return Unbounded_Wide_String
;
72 function To_Wide_String
73 (Source
: Unbounded_Wide_String
) return Wide_String;
75 procedure Set_Unbounded_Wide_String
76 (Target
: out Unbounded_Wide_String
;
77 Source
: Wide_String);
78 pragma Ada_05
(Set_Unbounded_Wide_String
);
81 (Source
: in out Unbounded_Wide_String
;
82 New_Item
: Unbounded_Wide_String
);
85 (Source
: in out Unbounded_Wide_String
;
86 New_Item
: Wide_String);
89 (Source
: in out Unbounded_Wide_String
;
90 New_Item
: Wide_Character);
93 (Left
: Unbounded_Wide_String
;
94 Right
: Unbounded_Wide_String
) return Unbounded_Wide_String
;
97 (Left
: Unbounded_Wide_String
;
98 Right
: Wide_String) return Unbounded_Wide_String
;
102 Right
: Unbounded_Wide_String
) return Unbounded_Wide_String
;
105 (Left
: Unbounded_Wide_String
;
106 Right
: Wide_Character) return Unbounded_Wide_String
;
109 (Left
: Wide_Character;
110 Right
: Unbounded_Wide_String
) return Unbounded_Wide_String
;
113 (Source
: Unbounded_Wide_String
;
114 Index
: Positive) return Wide_Character;
116 procedure Replace_Element
117 (Source
: in out Unbounded_Wide_String
;
119 By
: Wide_Character);
122 (Source
: Unbounded_Wide_String
;
124 High
: Natural) return Wide_String;
126 function Unbounded_Slice
127 (Source
: Unbounded_Wide_String
;
129 High
: Natural) return Unbounded_Wide_String
;
130 pragma Ada_05
(Unbounded_Slice
);
132 procedure Unbounded_Slice
133 (Source
: Unbounded_Wide_String
;
134 Target
: out Unbounded_Wide_String
;
137 pragma Ada_05
(Unbounded_Slice
);
140 (Left
: Unbounded_Wide_String
;
141 Right
: Unbounded_Wide_String
) return Boolean;
144 (Left
: Unbounded_Wide_String
;
145 Right
: Wide_String) return Boolean;
149 Right
: Unbounded_Wide_String
) return Boolean;
152 (Left
: Unbounded_Wide_String
;
153 Right
: Unbounded_Wide_String
) return Boolean;
156 (Left
: Unbounded_Wide_String
;
157 Right
: Wide_String) return Boolean;
161 Right
: Unbounded_Wide_String
) return Boolean;
164 (Left
: Unbounded_Wide_String
;
165 Right
: Unbounded_Wide_String
) return Boolean;
168 (Left
: Unbounded_Wide_String
;
169 Right
: Wide_String) return Boolean;
173 Right
: Unbounded_Wide_String
) return Boolean;
176 (Left
: Unbounded_Wide_String
;
177 Right
: Unbounded_Wide_String
) return Boolean;
180 (Left
: Unbounded_Wide_String
;
181 Right
: Wide_String) return Boolean;
185 Right
: Unbounded_Wide_String
) return Boolean;
188 (Left
: Unbounded_Wide_String
;
189 Right
: Unbounded_Wide_String
) return Boolean;
192 (Left
: Unbounded_Wide_String
;
193 Right
: Wide_String) return Boolean;
197 Right
: Unbounded_Wide_String
) return Boolean;
199 ------------------------
200 -- Search Subprograms --
201 ------------------------
204 (Source
: Unbounded_Wide_String
;
205 Pattern
: Wide_String;
206 Going
: Direction
:= Forward
;
207 Mapping
: Wide_Maps
.Wide_Character_Mapping
:= Wide_Maps
.Identity
)
211 (Source
: Unbounded_Wide_String
;
212 Pattern
: Wide_String;
213 Going
: Direction
:= Forward
;
214 Mapping
: Wide_Maps
.Wide_Character_Mapping_Function
) return Natural;
217 (Source
: Unbounded_Wide_String
;
218 Set
: Wide_Maps
.Wide_Character_Set
;
219 Test
: Membership
:= Inside
;
220 Going
: Direction
:= Forward
) return Natural;
223 (Source
: Unbounded_Wide_String
;
224 Pattern
: Wide_String;
226 Going
: Direction
:= Forward
;
227 Mapping
: Wide_Maps
.Wide_Character_Mapping
:= Wide_Maps
.Identity
)
229 pragma Ada_05
(Index
);
232 (Source
: Unbounded_Wide_String
;
233 Pattern
: Wide_String;
235 Going
: Direction
:= Forward
;
236 Mapping
: Wide_Maps
.Wide_Character_Mapping_Function
) return Natural;
237 pragma Ada_05
(Index
);
240 (Source
: Unbounded_Wide_String
;
241 Set
: Wide_Maps
.Wide_Character_Set
;
243 Test
: Membership
:= Inside
;
244 Going
: Direction
:= Forward
) return Natural;
245 pragma Ada_05
(Index
);
247 function Index_Non_Blank
248 (Source
: Unbounded_Wide_String
;
249 Going
: Direction
:= Forward
) return Natural;
251 function Index_Non_Blank
252 (Source
: Unbounded_Wide_String
;
254 Going
: Direction
:= Forward
) return Natural;
255 pragma Ada_05
(Index_Non_Blank
);
258 (Source
: Unbounded_Wide_String
;
259 Pattern
: Wide_String;
260 Mapping
: Wide_Maps
.Wide_Character_Mapping
:= Wide_Maps
.Identity
)
264 (Source
: Unbounded_Wide_String
;
265 Pattern
: Wide_String;
266 Mapping
: Wide_Maps
.Wide_Character_Mapping_Function
) return Natural;
269 (Source
: Unbounded_Wide_String
;
270 Set
: Wide_Maps
.Wide_Character_Set
) return Natural;
273 (Source
: Unbounded_Wide_String
;
274 Set
: Wide_Maps
.Wide_Character_Set
;
277 First
: out Positive;
279 pragma Ada_2012
(Find_Token
);
282 (Source
: Unbounded_Wide_String
;
283 Set
: Wide_Maps
.Wide_Character_Set
;
285 First
: out Positive;
288 ------------------------------------
289 -- String Translation Subprograms --
290 ------------------------------------
293 (Source
: Unbounded_Wide_String
;
294 Mapping
: Wide_Maps
.Wide_Character_Mapping
)
295 return Unbounded_Wide_String
;
298 (Source
: in out Unbounded_Wide_String
;
299 Mapping
: Wide_Maps
.Wide_Character_Mapping
);
302 (Source
: Unbounded_Wide_String
;
303 Mapping
: Wide_Maps
.Wide_Character_Mapping_Function
)
304 return Unbounded_Wide_String
;
307 (Source
: in out Unbounded_Wide_String
;
308 Mapping
: Wide_Maps
.Wide_Character_Mapping_Function
);
310 ---------------------------------------
311 -- String Transformation Subprograms --
312 ---------------------------------------
314 function Replace_Slice
315 (Source
: Unbounded_Wide_String
;
318 By
: Wide_String) return Unbounded_Wide_String
;
320 procedure Replace_Slice
321 (Source
: in out Unbounded_Wide_String
;
327 (Source
: Unbounded_Wide_String
;
329 New_Item
: Wide_String) return Unbounded_Wide_String
;
332 (Source
: in out Unbounded_Wide_String
;
334 New_Item
: Wide_String);
337 (Source
: Unbounded_Wide_String
;
339 New_Item
: Wide_String) return Unbounded_Wide_String
;
342 (Source
: in out Unbounded_Wide_String
;
344 New_Item
: Wide_String);
347 (Source
: Unbounded_Wide_String
;
349 Through
: Natural) return Unbounded_Wide_String
;
352 (Source
: in out Unbounded_Wide_String
;
357 (Source
: Unbounded_Wide_String
;
358 Side
: Trim_End
) return Unbounded_Wide_String
;
361 (Source
: in out Unbounded_Wide_String
;
365 (Source
: Unbounded_Wide_String
;
366 Left
: Wide_Maps
.Wide_Character_Set
;
367 Right
: Wide_Maps
.Wide_Character_Set
) return Unbounded_Wide_String
;
370 (Source
: in out Unbounded_Wide_String
;
371 Left
: Wide_Maps
.Wide_Character_Set
;
372 Right
: Wide_Maps
.Wide_Character_Set
);
375 (Source
: Unbounded_Wide_String
;
377 Pad
: Wide_Character := Wide_Space
) return Unbounded_Wide_String
;
380 (Source
: in out Unbounded_Wide_String
;
382 Pad
: Wide_Character := Wide_Space
);
385 (Source
: Unbounded_Wide_String
;
387 Pad
: Wide_Character := Wide_Space
) return Unbounded_Wide_String
;
390 (Source
: in out Unbounded_Wide_String
;
392 Pad
: Wide_Character := Wide_Space
);
396 Right
: Wide_Character) return Unbounded_Wide_String
;
400 Right
: Wide_String) return Unbounded_Wide_String
;
404 Right
: Unbounded_Wide_String
) return Unbounded_Wide_String
;
407 pragma Inline
(Length
);
409 package AF
renames Ada
.Finalization
;
411 type Shared_Wide_String
(Max_Length
: Natural) is limited record
412 Counter
: System
.Atomic_Counters
.Atomic_Counter
;
416 Data
: Wide_String (1 .. Max_Length
);
417 -- Last is the index of last significant element of the Data. All
418 -- elements with larger indices are just an extra room.
421 type Shared_Wide_String_Access
is access all Shared_Wide_String
;
423 procedure Reference
(Item
: not null Shared_Wide_String_Access
);
424 -- Increment reference counter.
426 procedure Unreference
(Item
: not null Shared_Wide_String_Access
);
427 -- Decrement reference counter. Deallocate Item when ref counter is zero
429 function Can_Be_Reused
430 (Item
: Shared_Wide_String_Access
;
431 Length
: Natural) return Boolean;
432 -- Returns True if Shared_Wide_String can be reused. There are two criteria
433 -- when Shared_Wide_String can be reused: its reference counter must be one
434 -- (thus Shared_Wide_String is owned exclusively) and its size is
435 -- sufficient to store string with specified length effectively.
437 function Allocate
(Max_Length
: Natural) return Shared_Wide_String_Access
;
438 -- Allocates new Shared_Wide_String with at least specified maximum length.
439 -- Actual maximum length of the allocated Shared_Wide_String can be
440 -- slightly greater. Returns reference to Empty_Shared_Wide_String when
441 -- requested length is zero.
443 Empty_Shared_Wide_String
: aliased Shared_Wide_String
(0);
445 function To_Unbounded
(S
: Wide_String) return Unbounded_Wide_String
446 renames To_Unbounded_Wide_String
;
447 -- This renames are here only to be used in the pragma Stream_Convert
449 type Unbounded_Wide_String
is new AF
.Controlled
with record
450 Reference
: Shared_Wide_String_Access
:= Empty_Shared_Wide_String
'Access;
453 -- The Unbounded_Wide_String uses several techniques to increase speed of
456 -- - implicit sharing or copy-on-write. Unbounded_Wide_String contains
457 -- only the reference to the data which is shared between several
458 -- instances. The shared data is reallocated only when its value is
459 -- changed and the object mutation can't be used or it is inefficient to
462 -- - object mutation. Shared data object can be reused without memory
463 -- reallocation when all of the following requirements are meat:
464 -- - shared data object don't used anywhere longer;
465 -- - its size is sufficient to store new value;
466 -- - the gap after reuse is less than some threshold.
468 -- - memory preallocation. Most of used memory allocation algorithms
469 -- aligns allocated segment on the some boundary, thus some amount of
470 -- additional memory can be preallocated without any impact. Such
471 -- preallocated memory can used later by Append/Insert operations
472 -- without reallocation.
474 -- Reference counting uses GCC builtin atomic operations, which allows safe
475 -- sharing of internal data between Ada tasks. Nevertheless, this does not
476 -- make objects of Unbounded_String thread-safe: an instance cannot be
477 -- accessed by several tasks simultaneously.
479 pragma Stream_Convert
(Unbounded_Wide_String
, To_Unbounded
, To_Wide_String
);
480 -- Provide stream routines without dragging in Ada.Streams
482 pragma Finalize_Storage_Only
(Unbounded_Wide_String
);
483 -- Finalization is required only for freeing storage
485 overriding
procedure Initialize
(Object
: in out Unbounded_Wide_String
);
486 overriding
procedure Adjust
(Object
: in out Unbounded_Wide_String
);
487 overriding
procedure Finalize
(Object
: in out Unbounded_Wide_String
);
489 Null_Unbounded_Wide_String
: constant Unbounded_Wide_String
:=
492 Empty_Shared_Wide_String
'Access);
494 end Ada
.Strings
.Wide_Unbounded
;