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-2012, 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
;
128 First
: out Positive;
130 renames Ada
.Strings
.Wide_Wide_Search
.Find_Token
;
133 (Source
: Wide_Wide_String
;
134 Set
: Wide_Wide_Maps
.Wide_Wide_Character_Set
;
136 First
: out Positive;
138 renames Ada
.Strings
.Wide_Wide_Search
.Find_Token
;
146 Right
: Wide_Wide_Character
) return Wide_Wide_String
148 Result
: Wide_Wide_String
(1 .. Left
);
151 for J
in Result
'Range loop
160 Right
: Wide_Wide_String
) return Wide_Wide_String
162 Result
: Wide_Wide_String
(1 .. Left
* Right
'Length);
166 for J
in 1 .. Left
loop
167 Result
(Ptr
.. Ptr
+ Right
'Length - 1) := Right
;
168 Ptr
:= Ptr
+ Right
'Length;
179 (Source
: Wide_Wide_String
;
181 Through
: Natural) return Wide_Wide_String
184 if From
not in Source
'Range
185 or else Through
> Source
'Last
189 elsif From
> Through
then
194 Len
: constant Integer := Source
'Length - (Through
- From
+ 1);
195 Result
: constant Wide_Wide_String
196 (Source
'First .. Source
'First + Len
- 1) :=
197 Source
(Source
'First .. From
- 1) &
198 Source
(Through
+ 1 .. Source
'Last);
206 (Source
: in out Wide_Wide_String
;
209 Justify
: Alignment
:= Left
;
210 Pad
: Wide_Wide_Character
:= Wide_Wide_Space
)
213 Move
(Source
=> Delete
(Source
, From
, Through
),
224 (Source
: Wide_Wide_String
;
226 Pad
: Wide_Wide_Character
:= Wide_Wide_Space
) return Wide_Wide_String
228 Result
: Wide_Wide_String
(1 .. Count
);
231 if Count
<= Source
'Length then
232 Result
:= Source
(Source
'First .. Source
'First + Count
- 1);
235 Result
(1 .. Source
'Length) := Source
;
237 for J
in Source
'Length + 1 .. Count
loop
246 (Source
: in out Wide_Wide_String
;
248 Justify
: Alignment
:= Left
;
249 Pad
: Wide_Wide_Character
:= Ada
.Strings
.Wide_Wide_Space
)
252 Move
(Source
=> Head
(Source
, Count
, Pad
),
264 (Source
: Wide_Wide_String
;
266 New_Item
: Wide_Wide_String
) return Wide_Wide_String
268 Result
: Wide_Wide_String
(1 .. Source
'Length + New_Item
'Length);
271 if Before
< Source
'First or else Before
> Source
'Last + 1 then
275 Result
:= Source
(Source
'First .. Before
- 1) & New_Item
&
276 Source
(Before
.. Source
'Last);
281 (Source
: in out Wide_Wide_String
;
283 New_Item
: Wide_Wide_String
;
284 Drop
: Truncation
:= Error
)
287 Move
(Source
=> Insert
(Source
, Before
, New_Item
),
297 (Source
: Wide_Wide_String
;
298 Target
: out Wide_Wide_String
;
299 Drop
: Truncation
:= Error
;
300 Justify
: Alignment
:= Left
;
301 Pad
: Wide_Wide_Character
:= Wide_Wide_Space
)
303 Sfirst
: constant Integer := Source
'First;
304 Slast
: constant Integer := Source
'Last;
305 Slength
: constant Integer := Source
'Length;
307 Tfirst
: constant Integer := Target
'First;
308 Tlast
: constant Integer := Target
'Last;
309 Tlength
: constant Integer := Target
'Length;
311 function Is_Padding
(Item
: Wide_Wide_String
) return Boolean;
312 -- Determinbe if all characters in Item are pad characters
314 function Is_Padding
(Item
: Wide_Wide_String
) return Boolean is
316 for J
in Item
'Range loop
317 if Item
(J
) /= Pad
then
325 -- Start of processing for Move
328 if Slength
= Tlength
then
331 elsif Slength
> Tlength
then
335 Target
:= Source
(Slast
- Tlength
+ 1 .. Slast
);
338 Target
:= Source
(Sfirst
.. Sfirst
+ Tlength
- 1);
343 if Is_Padding
(Source
(Sfirst
+ Tlength
.. Slast
)) then
345 Source
(Sfirst
.. Sfirst
+ Target
'Length - 1);
351 if Is_Padding
(Source
(Sfirst
.. Slast
- Tlength
)) then
352 Target
:= Source
(Slast
- Tlength
+ 1 .. Slast
);
363 -- Source'Length < Target'Length
368 Target
(Tfirst
.. Tfirst
+ Slength
- 1) := Source
;
370 for J
in Tfirst
+ Slength
.. Tlast
loop
375 for J
in Tfirst
.. Tlast
- Slength
loop
379 Target
(Tlast
- Slength
+ 1 .. Tlast
) := Source
;
383 Front_Pad
: constant Integer := (Tlength
- Slength
) / 2;
384 Tfirst_Fpad
: constant Integer := Tfirst
+ Front_Pad
;
387 for J
in Tfirst
.. Tfirst_Fpad
- 1 loop
391 Target
(Tfirst_Fpad
.. Tfirst_Fpad
+ Slength
- 1) := Source
;
393 for J
in Tfirst_Fpad
+ Slength
.. Tlast
loop
406 (Source
: Wide_Wide_String
;
408 New_Item
: Wide_Wide_String
) return Wide_Wide_String
411 if Position
not in Source
'First .. Source
'Last + 1 then
415 Result_Length
: constant Natural :=
418 Position
- Source
'First + New_Item
'Length);
420 Result
: Wide_Wide_String
(1 .. Result_Length
);
423 Result
:= Source
(Source
'First .. Position
- 1) & New_Item
&
424 Source
(Position
+ New_Item
'Length .. Source
'Last);
431 (Source
: in out Wide_Wide_String
;
433 New_Item
: Wide_Wide_String
;
434 Drop
: Truncation
:= Right
)
437 Move
(Source
=> Overwrite
(Source
, Position
, New_Item
),
446 function Replace_Slice
447 (Source
: Wide_Wide_String
;
450 By
: Wide_Wide_String
) return Wide_Wide_String
453 if Low
> Source
'Last + 1 or else High
< Source
'First - 1 then
459 Front_Len
: constant Integer :=
460 Integer'Max (0, Low
- Source
'First);
461 -- Length of prefix of Source copied to result
463 Back_Len
: constant Integer :=
464 Integer'Max (0, Source
'Last - High
);
465 -- Length of suffix of Source copied to result
467 Result_Length
: constant Integer :=
468 Front_Len
+ By
'Length + Back_Len
;
471 Result
: Wide_Wide_String
(1 .. Result_Length
);
474 Result
(1 .. Front_Len
) := Source
(Source
'First .. Low
- 1);
475 Result
(Front_Len
+ 1 .. Front_Len
+ By
'Length) := By
;
476 Result
(Front_Len
+ By
'Length + 1 .. Result
'Length) :=
477 Source
(High
+ 1 .. Source
'Last);
482 return Insert
(Source
, Before
=> Low
, New_Item
=> By
);
486 procedure Replace_Slice
487 (Source
: in out Wide_Wide_String
;
490 By
: Wide_Wide_String
;
491 Drop
: Truncation
:= Error
;
492 Justify
: Alignment
:= Left
;
493 Pad
: Wide_Wide_Character
:= Wide_Wide_Space
)
496 Move
(Replace_Slice
(Source
, Low
, High
, By
), Source
, Drop
, Justify
, Pad
);
504 (Source
: Wide_Wide_String
;
506 Pad
: Wide_Wide_Character
:= Wide_Wide_Space
) return Wide_Wide_String
508 Result
: Wide_Wide_String
(1 .. Count
);
511 if Count
< Source
'Length then
512 Result
:= Source
(Source
'Last - Count
+ 1 .. Source
'Last);
517 for J
in 1 .. Count
- Source
'Length loop
521 Result
(Count
- Source
'Length + 1 .. Count
) := Source
;
528 (Source
: in out Wide_Wide_String
;
530 Justify
: Alignment
:= Left
;
531 Pad
: Wide_Wide_Character
:= Ada
.Strings
.Wide_Wide_Space
)
534 Move
(Source
=> Tail
(Source
, Count
, Pad
),
546 (Source
: Wide_Wide_String
;
547 Mapping
: Wide_Wide_Maps
.Wide_Wide_Character_Mapping
)
548 return Wide_Wide_String
550 Result
: Wide_Wide_String
(1 .. Source
'Length);
553 for J
in Source
'Range loop
554 Result
(J
- (Source
'First - 1)) := Value
(Mapping
, Source
(J
));
561 (Source
: in out Wide_Wide_String
;
562 Mapping
: Wide_Wide_Maps
.Wide_Wide_Character_Mapping
)
565 for J
in Source
'Range loop
566 Source
(J
) := Value
(Mapping
, Source
(J
));
571 (Source
: Wide_Wide_String
;
572 Mapping
: Wide_Wide_Maps
.Wide_Wide_Character_Mapping_Function
)
573 return Wide_Wide_String
575 Result
: Wide_Wide_String
(1 .. Source
'Length);
578 for J
in Source
'Range loop
579 Result
(J
- (Source
'First - 1)) := Mapping
(Source
(J
));
586 (Source
: in out Wide_Wide_String
;
587 Mapping
: Wide_Wide_Maps
.Wide_Wide_Character_Mapping_Function
)
590 for J
in Source
'Range loop
591 Source
(J
) := Mapping
(Source
(J
));
600 (Source
: Wide_Wide_String
;
601 Side
: Trim_End
) return Wide_Wide_String
603 Low
: Natural := Source
'First;
604 High
: Natural := Source
'Last;
607 if Side
= Left
or else Side
= Both
then
608 while Low
<= High
and then Source
(Low
) = Wide_Wide_Space
loop
613 if Side
= Right
or else Side
= Both
then
614 while High
>= Low
and then Source
(High
) = Wide_Wide_Space
loop
624 -- At least one non-blank
628 Result
: constant Wide_Wide_String
(1 .. High
- Low
+ 1) :=
629 Source
(Low
.. High
);
638 (Source
: in out Wide_Wide_String
;
640 Justify
: Alignment
:= Left
;
641 Pad
: Wide_Wide_Character
:= Wide_Wide_Space
)
644 Move
(Source
=> Trim
(Source
, Side
),
651 (Source
: Wide_Wide_String
;
652 Left
: Wide_Wide_Maps
.Wide_Wide_Character_Set
;
653 Right
: Wide_Wide_Maps
.Wide_Wide_Character_Set
) return Wide_Wide_String
655 Low
: Natural := Source
'First;
656 High
: Natural := Source
'Last;
659 while Low
<= High
and then Is_In
(Source
(Low
), Left
) loop
663 while High
>= Low
and then Is_In
(Source
(High
), Right
) loop
667 -- Case where source comprises only characters in the sets
673 subtype WS
is Wide_Wide_String
(1 .. High
- Low
+ 1);
676 return WS
(Source
(Low
.. High
));
682 (Source
: in out Wide_Wide_String
;
683 Left
: Wide_Wide_Maps
.Wide_Wide_Character_Set
;
684 Right
: Wide_Wide_Maps
.Wide_Wide_Character_Set
;
685 Justify
: Alignment
:= Strings
.Left
;
686 Pad
: Wide_Wide_Character
:= Wide_Wide_Space
)
689 Move
(Source
=> Trim
(Source
, Left
, Right
),
695 end Ada
.Strings
.Wide_Wide_Fixed
;