1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . S T R I N G S . W I D E _ F I X E D --
9 -- Copyright (C) 1992-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 with Ada
.Strings
.Wide_Wide_Maps
; use Ada
.Strings
.Wide_Wide_Maps
;
33 with Ada
.Strings
.Wide_Wide_Search
;
35 package body Ada
.Strings
.Wide_Wide_Fixed
is
37 ------------------------
38 -- Search Subprograms --
39 ------------------------
42 (Source
: Wide_Wide_String
;
43 Pattern
: Wide_Wide_String
;
44 Going
: Direction
:= Forward
;
45 Mapping
: Wide_Wide_Maps
.Wide_Wide_Character_Mapping
:=
46 Wide_Wide_Maps
.Identity
)
48 renames Ada
.Strings
.Wide_Wide_Search
.Index
;
51 (Source
: Wide_Wide_String
;
52 Pattern
: Wide_Wide_String
;
53 Going
: Direction
:= Forward
;
54 Mapping
: Wide_Wide_Maps
.Wide_Wide_Character_Mapping_Function
)
56 renames Ada
.Strings
.Wide_Wide_Search
.Index
;
59 (Source
: Wide_Wide_String
;
60 Set
: Wide_Wide_Maps
.Wide_Wide_Character_Set
;
61 Test
: Membership
:= Inside
;
62 Going
: Direction
:= Forward
) return Natural
63 renames Ada
.Strings
.Wide_Wide_Search
.Index
;
66 (Source
: Wide_Wide_String
;
67 Pattern
: Wide_Wide_String
;
69 Going
: Direction
:= Forward
;
70 Mapping
: Wide_Wide_Maps
.Wide_Wide_Character_Mapping
:=
71 Wide_Wide_Maps
.Identity
)
73 renames Ada
.Strings
.Wide_Wide_Search
.Index
;
76 (Source
: Wide_Wide_String
;
77 Pattern
: Wide_Wide_String
;
79 Going
: Direction
:= Forward
;
80 Mapping
: Wide_Wide_Maps
.Wide_Wide_Character_Mapping_Function
)
82 renames Ada
.Strings
.Wide_Wide_Search
.Index
;
85 (Source
: Wide_Wide_String
;
86 Set
: Wide_Wide_Maps
.Wide_Wide_Character_Set
;
88 Test
: Membership
:= Inside
;
89 Going
: Direction
:= Forward
) return Natural
90 renames Ada
.Strings
.Wide_Wide_Search
.Index
;
92 function Index_Non_Blank
93 (Source
: Wide_Wide_String
;
94 Going
: Direction
:= Forward
) return Natural
95 renames Ada
.Strings
.Wide_Wide_Search
.Index_Non_Blank
;
97 function Index_Non_Blank
98 (Source
: Wide_Wide_String
;
100 Going
: Direction
:= Forward
) return Natural
101 renames Ada
.Strings
.Wide_Wide_Search
.Index_Non_Blank
;
104 (Source
: Wide_Wide_String
;
105 Pattern
: Wide_Wide_String
;
106 Mapping
: Wide_Wide_Maps
.Wide_Wide_Character_Mapping
:=
107 Wide_Wide_Maps
.Identity
)
109 renames Ada
.Strings
.Wide_Wide_Search
.Count
;
112 (Source
: Wide_Wide_String
;
113 Pattern
: Wide_Wide_String
;
114 Mapping
: Wide_Wide_Maps
.Wide_Wide_Character_Mapping_Function
)
116 renames Ada
.Strings
.Wide_Wide_Search
.Count
;
119 (Source
: Wide_Wide_String
;
120 Set
: Wide_Wide_Maps
.Wide_Wide_Character_Set
) return Natural
121 renames Ada
.Strings
.Wide_Wide_Search
.Count
;
124 (Source
: Wide_Wide_String
;
125 Set
: Wide_Wide_Maps
.Wide_Wide_Character_Set
;
127 First
: out Positive;
129 renames Ada
.Strings
.Wide_Wide_Search
.Find_Token
;
137 Right
: Wide_Wide_Character
) return Wide_Wide_String
139 Result
: Wide_Wide_String
(1 .. Left
);
142 for J
in Result
'Range loop
151 Right
: Wide_Wide_String
) return Wide_Wide_String
153 Result
: Wide_Wide_String
(1 .. Left
* Right
'Length);
157 for J
in 1 .. Left
loop
158 Result
(Ptr
.. Ptr
+ Right
'Length - 1) := Right
;
159 Ptr
:= Ptr
+ Right
'Length;
170 (Source
: Wide_Wide_String
;
172 Through
: Natural) return Wide_Wide_String
175 if From
not in Source
'Range
176 or else Through
> Source
'Last
180 elsif From
> Through
then
185 Len
: constant Integer := Source
'Length - (Through
- From
+ 1);
186 Result
: constant Wide_Wide_String
187 (Source
'First .. Source
'First + Len
- 1) :=
188 Source
(Source
'First .. From
- 1) &
189 Source
(Through
+ 1 .. Source
'Last);
197 (Source
: in out Wide_Wide_String
;
200 Justify
: Alignment
:= Left
;
201 Pad
: Wide_Wide_Character
:= Wide_Wide_Space
)
204 Move
(Source
=> Delete
(Source
, From
, Through
),
215 (Source
: Wide_Wide_String
;
217 Pad
: Wide_Wide_Character
:= Wide_Wide_Space
) return Wide_Wide_String
219 Result
: Wide_Wide_String
(1 .. Count
);
222 if Count
<= Source
'Length then
223 Result
:= Source
(Source
'First .. Source
'First + Count
- 1);
226 Result
(1 .. Source
'Length) := Source
;
228 for J
in Source
'Length + 1 .. Count
loop
237 (Source
: in out Wide_Wide_String
;
239 Justify
: Alignment
:= Left
;
240 Pad
: Wide_Wide_Character
:= Ada
.Strings
.Wide_Wide_Space
)
243 Move
(Source
=> Head
(Source
, Count
, Pad
),
255 (Source
: Wide_Wide_String
;
257 New_Item
: Wide_Wide_String
) return Wide_Wide_String
259 Result
: Wide_Wide_String
(1 .. Source
'Length + New_Item
'Length);
262 if Before
< Source
'First or else Before
> Source
'Last + 1 then
266 Result
:= Source
(Source
'First .. Before
- 1) & New_Item
&
267 Source
(Before
.. Source
'Last);
272 (Source
: in out Wide_Wide_String
;
274 New_Item
: Wide_Wide_String
;
275 Drop
: Truncation
:= Error
)
278 Move
(Source
=> Insert
(Source
, Before
, New_Item
),
288 (Source
: Wide_Wide_String
;
289 Target
: out Wide_Wide_String
;
290 Drop
: Truncation
:= Error
;
291 Justify
: Alignment
:= Left
;
292 Pad
: Wide_Wide_Character
:= Wide_Wide_Space
)
294 Sfirst
: constant Integer := Source
'First;
295 Slast
: constant Integer := Source
'Last;
296 Slength
: constant Integer := Source
'Length;
298 Tfirst
: constant Integer := Target
'First;
299 Tlast
: constant Integer := Target
'Last;
300 Tlength
: constant Integer := Target
'Length;
302 function Is_Padding
(Item
: Wide_Wide_String
) return Boolean;
303 -- Determinbe if all characters in Item are pad characters
305 function Is_Padding
(Item
: Wide_Wide_String
) return Boolean is
307 for J
in Item
'Range loop
308 if Item
(J
) /= Pad
then
316 -- Start of processing for Move
319 if Slength
= Tlength
then
322 elsif Slength
> Tlength
then
326 Target
:= Source
(Slast
- Tlength
+ 1 .. Slast
);
329 Target
:= Source
(Sfirst
.. Sfirst
+ Tlength
- 1);
334 if Is_Padding
(Source
(Sfirst
+ Tlength
.. Slast
)) then
336 Source
(Sfirst
.. Sfirst
+ Target
'Length - 1);
342 if Is_Padding
(Source
(Sfirst
.. Slast
- Tlength
)) then
343 Target
:= Source
(Slast
- Tlength
+ 1 .. Slast
);
354 -- Source'Length < Target'Length
359 Target
(Tfirst
.. Tfirst
+ Slength
- 1) := Source
;
361 for J
in Tfirst
+ Slength
.. Tlast
loop
366 for J
in Tfirst
.. Tlast
- Slength
loop
370 Target
(Tlast
- Slength
+ 1 .. Tlast
) := Source
;
374 Front_Pad
: constant Integer := (Tlength
- Slength
) / 2;
375 Tfirst_Fpad
: constant Integer := Tfirst
+ Front_Pad
;
378 for J
in Tfirst
.. Tfirst_Fpad
- 1 loop
382 Target
(Tfirst_Fpad
.. Tfirst_Fpad
+ Slength
- 1) := Source
;
384 for J
in Tfirst_Fpad
+ Slength
.. Tlast
loop
397 (Source
: Wide_Wide_String
;
399 New_Item
: Wide_Wide_String
) return Wide_Wide_String
402 if Position
not in Source
'First .. Source
'Last + 1 then
406 Result_Length
: constant Natural :=
409 Position
- Source
'First + New_Item
'Length);
411 Result
: Wide_Wide_String
(1 .. Result_Length
);
414 Result
:= Source
(Source
'First .. Position
- 1) & New_Item
&
415 Source
(Position
+ New_Item
'Length .. Source
'Last);
422 (Source
: in out Wide_Wide_String
;
424 New_Item
: Wide_Wide_String
;
425 Drop
: Truncation
:= Right
)
428 Move
(Source
=> Overwrite
(Source
, Position
, New_Item
),
437 function Replace_Slice
438 (Source
: Wide_Wide_String
;
441 By
: Wide_Wide_String
) return Wide_Wide_String
443 Result_Length
: Natural;
446 if Low
> Source
'Last + 1 or else High
< Source
'First - 1 then
450 Source
'Length - Natural'Max (High
- Low
+ 1, 0) + By
'Length;
453 Result
: Wide_Wide_String
(1 .. Result_Length
);
458 Source
(Source
'First .. Low
- 1) & By
&
459 Source
(High
+ 1 .. Source
'Last);
461 Result
:= Source
(Source
'First .. Low
- 1) & By
&
462 Source
(Low
.. Source
'Last);
470 procedure Replace_Slice
471 (Source
: in out Wide_Wide_String
;
474 By
: Wide_Wide_String
;
475 Drop
: Truncation
:= Error
;
476 Justify
: Alignment
:= Left
;
477 Pad
: Wide_Wide_Character
:= Wide_Wide_Space
)
480 Move
(Replace_Slice
(Source
, Low
, High
, By
), Source
, Drop
, Justify
, Pad
);
488 (Source
: Wide_Wide_String
;
490 Pad
: Wide_Wide_Character
:= Wide_Wide_Space
) return Wide_Wide_String
492 Result
: Wide_Wide_String
(1 .. Count
);
495 if Count
< Source
'Length then
496 Result
:= Source
(Source
'Last - Count
+ 1 .. Source
'Last);
501 for J
in 1 .. Count
- Source
'Length loop
505 Result
(Count
- Source
'Length + 1 .. Count
) := Source
;
512 (Source
: in out Wide_Wide_String
;
514 Justify
: Alignment
:= Left
;
515 Pad
: Wide_Wide_Character
:= Ada
.Strings
.Wide_Wide_Space
)
518 Move
(Source
=> Tail
(Source
, Count
, Pad
),
530 (Source
: Wide_Wide_String
;
531 Mapping
: Wide_Wide_Maps
.Wide_Wide_Character_Mapping
)
532 return Wide_Wide_String
534 Result
: Wide_Wide_String
(1 .. Source
'Length);
537 for J
in Source
'Range loop
538 Result
(J
- (Source
'First - 1)) := Value
(Mapping
, Source
(J
));
545 (Source
: in out Wide_Wide_String
;
546 Mapping
: Wide_Wide_Maps
.Wide_Wide_Character_Mapping
)
549 for J
in Source
'Range loop
550 Source
(J
) := Value
(Mapping
, Source
(J
));
555 (Source
: Wide_Wide_String
;
556 Mapping
: Wide_Wide_Maps
.Wide_Wide_Character_Mapping_Function
)
557 return Wide_Wide_String
559 Result
: Wide_Wide_String
(1 .. Source
'Length);
562 for J
in Source
'Range loop
563 Result
(J
- (Source
'First - 1)) := Mapping
(Source
(J
));
570 (Source
: in out Wide_Wide_String
;
571 Mapping
: Wide_Wide_Maps
.Wide_Wide_Character_Mapping_Function
)
574 for J
in Source
'Range loop
575 Source
(J
) := Mapping
(Source
(J
));
584 (Source
: Wide_Wide_String
;
585 Side
: Trim_End
) return Wide_Wide_String
587 Low
: Natural := Source
'First;
588 High
: Natural := Source
'Last;
591 if Side
= Left
or else Side
= Both
then
592 while Low
<= High
and then Source
(Low
) = Wide_Wide_Space
loop
597 if Side
= Right
or else Side
= Both
then
598 while High
>= Low
and then Source
(High
) = Wide_Wide_Space
loop
608 -- At least one non-blank
612 Result
: constant Wide_Wide_String
(1 .. High
- Low
+ 1) :=
613 Source
(Low
.. High
);
622 (Source
: in out Wide_Wide_String
;
624 Justify
: Alignment
:= Left
;
625 Pad
: Wide_Wide_Character
:= Wide_Wide_Space
)
628 Move
(Source
=> Trim
(Source
, Side
),
635 (Source
: Wide_Wide_String
;
636 Left
: Wide_Wide_Maps
.Wide_Wide_Character_Set
;
637 Right
: Wide_Wide_Maps
.Wide_Wide_Character_Set
) return Wide_Wide_String
639 Low
: Natural := Source
'First;
640 High
: Natural := Source
'Last;
643 while Low
<= High
and then Is_In
(Source
(Low
), Left
) loop
647 while High
>= Low
and then Is_In
(Source
(High
), Right
) loop
651 -- Case where source comprises only characters in the sets
657 subtype WS
is Wide_Wide_String
(1 .. High
- Low
+ 1);
660 return WS
(Source
(Low
.. High
));
666 (Source
: in out Wide_Wide_String
;
667 Left
: Wide_Wide_Maps
.Wide_Wide_Character_Set
;
668 Right
: Wide_Wide_Maps
.Wide_Wide_Character_Set
;
669 Justify
: Alignment
:= Strings
.Left
;
670 Pad
: Wide_Wide_Character
:= Wide_Wide_Space
)
673 Move
(Source
=> Trim
(Source
, Left
, Right
),
679 end Ada
.Strings
.Wide_Wide_Fixed
;