1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME COMPONENTS --
5 -- A D A . S T R I N G S . W I D E _ F I X E D --
9 -- $Revision: 1.17 $ --
11 -- Copyright (C) 1992-1997 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- As a special exception, if other files instantiate generics from this --
25 -- unit, or you link this unit with other files to produce an executable, --
26 -- this unit does not by itself cause the resulting executable to be --
27 -- covered by the GNU General Public License. This exception does not --
28 -- however invalidate any other reasons why the executable file might be --
29 -- covered by the GNU Public License. --
31 -- GNAT was originally developed by the GNAT team at New York University. --
32 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
34 ------------------------------------------------------------------------------
37 with Ada.Strings.Wide_Maps; use Ada.Strings.Wide_Maps;
38 with Ada.Strings.Wide_Search;
40 package body Ada.Strings.Wide_Fixed is
42 ------------------------
43 -- Search Subprograms --
44 ------------------------
47 (Source : in Wide_String;
48 Pattern : in Wide_String;
49 Going : in Direction := Forward;
50 Mapping : in Wide_Maps.Wide_Character_Mapping := Wide_Maps.Identity)
52 renames Ada.Strings.Wide_Search.Index;
55 (Source : in Wide_String;
56 Pattern : in Wide_String;
57 Going : in Direction := Forward;
58 Mapping : in Wide_Maps.Wide_Character_Mapping_Function)
60 renames Ada.Strings.Wide_Search.Index;
63 (Source : in Wide_String;
64 Set : in Wide_Maps.Wide_Character_Set;
65 Test : in Membership := Inside;
66 Going : in Direction := Forward)
68 renames Ada.Strings.Wide_Search.Index;
70 function Index_Non_Blank
71 (Source : in Wide_String;
72 Going : in Direction := Forward)
74 renames Ada.Strings.Wide_Search.Index_Non_Blank;
77 (Source : in Wide_String;
78 Pattern : in Wide_String;
79 Mapping : in Wide_Maps.Wide_Character_Mapping := Wide_Maps.Identity)
81 renames Ada.Strings.Wide_Search.Count;
84 (Source : in Wide_String;
85 Pattern : in Wide_String;
86 Mapping : in Wide_Maps.Wide_Character_Mapping_Function)
88 renames Ada.Strings.Wide_Search.Count;
91 (Source : in Wide_String;
92 Set : in Wide_Maps.Wide_Character_Set)
94 renames Ada.Strings.Wide_Search.Count;
97 (Source : in Wide_String;
98 Set : in Wide_Maps.Wide_Character_Set;
100 First : out Positive;
102 renames Ada.Strings.Wide_Search.Find_Token;
110 Right : in Wide_Character)
113 Result : Wide_String (1 .. Left);
116 for J in Result'Range loop
125 Right : in Wide_String)
128 Result : Wide_String (1 .. Left * Right'Length);
132 for J in 1 .. Left loop
133 Result (Ptr .. Ptr + Right'Length - 1) := Right;
134 Ptr := Ptr + Right'Length;
145 (Source : in Wide_String;
147 Through : in Natural)
151 if From not in Source'Range
152 or else Through > Source'Last
156 elsif From > Through then
161 Result : constant Wide_String :=
162 Source (Source'First .. From - 1) &
163 Source (Through + 1 .. Source'Last);
171 (Source : in out Wide_String;
173 Through : in Natural;
174 Justify : in Alignment := Left;
175 Pad : in Wide_Character := Wide_Space)
178 Move (Source => Delete (Source, From, Through),
189 (Source : in Wide_String;
191 Pad : in Wide_Character := Wide_Space)
194 Result : Wide_String (1 .. Count);
197 if Count <= Source'Length then
198 Result := Source (Source'First .. Source'First + Count - 1);
201 Result (1 .. Source'Length) := Source;
203 for J in Source'Length + 1 .. Count loop
212 (Source : in out Wide_String;
214 Justify : in Alignment := Left;
215 Pad : in Wide_Character := Ada.Strings.Wide_Space)
218 Move (Source => Head (Source, Count, Pad),
230 (Source : in Wide_String;
231 Before : in Positive;
232 New_Item : in Wide_String)
235 Result : Wide_String (1 .. Source'Length + New_Item'Length);
238 if Before < Source'First or else Before > Source'Last + 1 then
242 Result := Source (Source'First .. Before - 1) & New_Item &
243 Source (Before .. Source'Last);
248 (Source : in out Wide_String;
249 Before : in Positive;
250 New_Item : in Wide_String;
251 Drop : in Truncation := Error)
254 Move (Source => Insert (Source, Before, New_Item),
264 (Source : in Wide_String;
265 Target : out Wide_String;
266 Drop : in Truncation := Error;
267 Justify : in Alignment := Left;
268 Pad : in Wide_Character := Wide_Space)
270 Sfirst : constant Integer := Source'First;
271 Slast : constant Integer := Source'Last;
272 Slength : constant Integer := Source'Length;
274 Tfirst : constant Integer := Target'First;
275 Tlast : constant Integer := Target'Last;
276 Tlength : constant Integer := Target'Length;
278 function Is_Padding (Item : Wide_String) return Boolean;
279 -- Determinbe if all characters in Item are pad characters
281 function Is_Padding (Item : Wide_String) return Boolean is
283 for J in Item'Range loop
284 if Item (J) /= Pad then
292 -- Start of processing for Move
295 if Slength = Tlength then
298 elsif Slength > Tlength then
302 Target := Source (Slast - Tlength + 1 .. Slast);
305 Target := Source (Sfirst .. Sfirst + Tlength - 1);
310 if Is_Padding (Source (Sfirst + Tlength .. Slast)) then
312 Source (Sfirst .. Sfirst + Target'Length - 1);
318 if Is_Padding (Source (Sfirst .. Slast - Tlength)) then
319 Target := Source (Slast - Tlength + 1 .. Slast);
330 -- Source'Length < Target'Length
335 Target (Tfirst .. Tfirst + Slength - 1) := Source;
337 for J in Tfirst + Slength .. Tlast loop
342 for J in Tfirst .. Tlast - Slength loop
346 Target (Tlast - Slength + 1 .. Tlast) := Source;
350 Front_Pad : constant Integer := (Tlength - Slength) / 2;
351 Tfirst_Fpad : constant Integer := Tfirst + Front_Pad;
354 for J in Tfirst .. Tfirst_Fpad - 1 loop
358 Target (Tfirst_Fpad .. Tfirst_Fpad + Slength - 1) := Source;
360 for J in Tfirst_Fpad + Slength .. Tlast loop
373 (Source : in Wide_String;
374 Position : in Positive;
375 New_Item : in Wide_String)
379 if Position not in Source'First .. Source'Last + 1 then
383 Result_Length : Natural :=
384 Natural'Max (Source'Length,
385 Position - Source'First + New_Item'Length);
386 Result : Wide_String (1 .. Result_Length);
389 Result := Source (Source'First .. Position - 1) & New_Item &
390 Source (Position + New_Item'Length .. Source'Last);
397 (Source : in out Wide_String;
398 Position : in Positive;
399 New_Item : in Wide_String;
400 Drop : in Truncation := Right)
403 Move (Source => Overwrite (Source, Position, New_Item),
412 function Replace_Slice
413 (Source : in Wide_String;
419 Result_Length : Natural;
422 if Low > Source'Last + 1 or else High < Source'First - 1 then
426 Source'Length - Natural'Max (High - Low + 1, 0) + By'Length;
429 Result : Wide_String (1 .. Result_Length);
434 Source (Source'First .. Low - 1) & By &
435 Source (High + 1 .. Source'Last);
437 Result := Source (Source'First .. Low - 1) & By &
438 Source (Low .. Source'Last);
446 procedure Replace_Slice
447 (Source : in out Wide_String;
451 Drop : in Truncation := Error;
452 Justify : in Alignment := Left;
453 Pad : in Wide_Character := Wide_Space)
456 Move (Replace_Slice (Source, Low, High, By), Source, Drop, Justify, Pad);
464 (Source : in Wide_String;
466 Pad : in Wide_Character := Wide_Space)
469 Result : Wide_String (1 .. Count);
472 if Count < Source'Length then
473 Result := Source (Source'Last - Count + 1 .. Source'Last);
478 for J in 1 .. Count - Source'Length loop
482 Result (Count - Source'Length + 1 .. Count) := Source;
489 (Source : in out Wide_String;
491 Justify : in Alignment := Left;
492 Pad : in Wide_Character := Ada.Strings.Wide_Space)
495 Move (Source => Tail (Source, Count, Pad),
507 (Source : in Wide_String;
508 Mapping : in Wide_Maps.Wide_Character_Mapping)
511 Result : Wide_String (1 .. Source'Length);
514 for J in Source'Range loop
515 Result (J - (Source'First - 1)) := Value (Mapping, Source (J));
522 (Source : in out Wide_String;
523 Mapping : in Wide_Maps.Wide_Character_Mapping)
526 for J in Source'Range loop
527 Source (J) := Value (Mapping, Source (J));
532 (Source : in Wide_String;
533 Mapping : in Wide_Maps.Wide_Character_Mapping_Function)
536 Result : Wide_String (1 .. Source'Length);
539 for J in Source'Range loop
540 Result (J - (Source'First - 1)) := Mapping (Source (J));
547 (Source : in out Wide_String;
548 Mapping : in Wide_Maps.Wide_Character_Mapping_Function)
551 for J in Source'Range loop
552 Source (J) := Mapping (Source (J));
561 (Source : in Wide_String;
565 Low : Natural := Source'First;
566 High : Natural := Source'Last;
569 if Side = Left or else Side = Both then
570 while Low <= High and then Source (Low) = Wide_Space loop
575 if Side = Right or else Side = Both then
576 while High >= Low and then Source (High) = Wide_Space loop
586 -- At least one non-blank
590 Result : Wide_String (1 .. High - Low + 1) := Source (Low .. High);
599 (Source : in out Wide_String;
601 Justify : in Alignment := Left;
602 Pad : in Wide_Character := Wide_Space)
605 Move (Source => Trim (Source, Side),
612 (Source : in Wide_String;
613 Left : in Wide_Maps.Wide_Character_Set;
614 Right : in Wide_Maps.Wide_Character_Set)
617 Low : Natural := Source'First;
618 High : Natural := Source'Last;
621 while Low <= High and then Is_In (Source (Low), Left) loop
625 while High >= Low and then Is_In (Source (High), Right) loop
629 -- Case where source comprises only characters in the sets
635 subtype WS is Wide_String (1 .. High - Low + 1);
638 return WS (Source (Low .. High));
644 (Source : in out Wide_String;
645 Left : in Wide_Maps.Wide_Character_Set;
646 Right : in Wide_Maps.Wide_Character_Set;
647 Justify : in Alignment := Strings.Left;
648 Pad : in Wide_Character := Wide_Space)
651 Move (Source => Trim (Source, Left, Right),
657 end Ada.Strings.Wide_Fixed;