1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME 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 --
11 -- Copyright (C) 1992-2001 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 ------------------------------------------------------------------------------
36 with Ada.Strings.Wide_Fixed;
37 with Ada.Strings.Wide_Search;
38 with Ada.Unchecked_Deallocation;
40 package body Ada.Strings.Wide_Unbounded is
49 (Left : Unbounded_Wide_String;
50 Right : Unbounded_Wide_String)
51 return Unbounded_Wide_String
53 L_Length : constant Integer := Left.Reference.all'Length;
54 R_Length : constant Integer := Right.Reference.all'Length;
55 Length : constant Integer := L_Length + R_Length;
56 Result : Unbounded_Wide_String;
59 Result.Reference := new Wide_String (1 .. Length);
60 Result.Reference.all (1 .. L_Length) := Left.Reference.all;
61 Result.Reference.all (L_Length + 1 .. Length) := Right.Reference.all;
66 (Left : Unbounded_Wide_String;
68 return Unbounded_Wide_String
70 L_Length : constant Integer := Left.Reference.all'Length;
71 Length : constant Integer := L_Length + Right'Length;
72 Result : Unbounded_Wide_String;
75 Result.Reference := new Wide_String (1 .. Length);
76 Result.Reference.all (1 .. L_Length) := Left.Reference.all;
77 Result.Reference.all (L_Length + 1 .. Length) := Right;
83 Right : Unbounded_Wide_String)
84 return Unbounded_Wide_String
86 R_Length : constant Integer := Right.Reference.all'Length;
87 Length : constant Integer := Left'Length + R_Length;
88 Result : Unbounded_Wide_String;
91 Result.Reference := new Wide_String (1 .. Length);
92 Result.Reference.all (1 .. Left'Length) := Left;
93 Result.Reference.all (Left'Length + 1 .. Length) := Right.Reference.all;
98 (Left : Unbounded_Wide_String;
99 Right : Wide_Character)
100 return Unbounded_Wide_String
102 Length : constant Integer := Left.Reference.all'Length + 1;
103 Result : Unbounded_Wide_String;
106 Result.Reference := new Wide_String (1 .. Length);
107 Result.Reference.all (1 .. Length - 1) := Left.Reference.all;
108 Result.Reference.all (Length) := Right;
113 (Left : Wide_Character;
114 Right : Unbounded_Wide_String)
115 return Unbounded_Wide_String
117 Length : constant Integer := Right.Reference.all'Length + 1;
118 Result : Unbounded_Wide_String;
121 Result.Reference := new Wide_String (1 .. Length);
122 Result.Reference.all (1) := Left;
123 Result.Reference.all (2 .. Length) := Right.Reference.all;
133 Right : Wide_Character)
134 return Unbounded_Wide_String
136 Result : Unbounded_Wide_String;
139 Result.Reference := new Wide_String (1 .. Left);
140 for J in Result.Reference'Range loop
141 Result.Reference (J) := Right;
150 return Unbounded_Wide_String
152 Result : Unbounded_Wide_String;
155 Result.Reference := new Wide_String (1 .. Left * Right'Length);
157 for J in 1 .. Left loop
159 (Right'Length * J - Right'Length + 1 .. Right'Length * J) := Right;
167 Right : Unbounded_Wide_String)
168 return Unbounded_Wide_String
170 R_Length : constant Integer := Right.Reference.all'Length;
171 Result : Unbounded_Wide_String;
174 Result.Reference := new Wide_String (1 .. Left * R_Length);
176 for I in 1 .. Left loop
177 Result.Reference.all (R_Length * I - R_Length + 1 .. R_Length * I) :=
189 (Left : in Unbounded_Wide_String;
190 Right : in Unbounded_Wide_String)
194 return Left.Reference.all < Right.Reference.all;
198 (Left : in Unbounded_Wide_String;
199 Right : in Wide_String)
203 return Left.Reference.all < Right;
207 (Left : in Wide_String;
208 Right : in Unbounded_Wide_String)
212 return Left < Right.Reference.all;
220 (Left : in Unbounded_Wide_String;
221 Right : in Unbounded_Wide_String)
225 return Left.Reference.all <= Right.Reference.all;
229 (Left : in Unbounded_Wide_String;
230 Right : in Wide_String)
234 return Left.Reference.all <= Right;
238 (Left : in Wide_String;
239 Right : in Unbounded_Wide_String)
243 return Left <= Right.Reference.all;
251 (Left : in Unbounded_Wide_String;
252 Right : in Unbounded_Wide_String)
256 return Left.Reference.all = Right.Reference.all;
260 (Left : in Unbounded_Wide_String;
261 Right : in Wide_String)
265 return Left.Reference.all = Right;
269 (Left : in Wide_String;
270 Right : in Unbounded_Wide_String)
274 return Left = Right.Reference.all;
282 (Left : in Unbounded_Wide_String;
283 Right : in Unbounded_Wide_String)
287 return Left.Reference.all > Right.Reference.all;
291 (Left : in Unbounded_Wide_String;
292 Right : in Wide_String)
296 return Left.Reference.all > Right;
300 (Left : in Wide_String;
301 Right : in Unbounded_Wide_String)
305 return Left > Right.Reference.all;
313 (Left : in Unbounded_Wide_String;
314 Right : in Unbounded_Wide_String)
318 return Left.Reference.all >= Right.Reference.all;
322 (Left : in Unbounded_Wide_String;
323 Right : in Wide_String)
327 return Left.Reference.all >= Right;
331 (Left : in Wide_String;
332 Right : in Unbounded_Wide_String)
336 return Left >= Right.Reference.all;
343 procedure Adjust (Object : in out Unbounded_Wide_String) is
345 -- Copy string, except we do not copy the statically allocated
346 -- null string, since it can never be deallocated.
348 if Object.Reference /= Null_Wide_String'Access then
349 Object.Reference := new Wide_String'(Object.Reference.all);
358 (Source : in out Unbounded_Wide_String;
359 New_Item : in Unbounded_Wide_String)
361 S_Length : constant Integer := Source.Reference.all'Length;
362 Length : constant Integer := S_Length + New_Item.Reference.all'Length;
363 Temp : Wide_String_Access := Source.Reference;
366 if Source.Reference = Null_Wide_String'Access then
367 Source := To_Unbounded_Wide_String (New_Item.Reference.all);
371 Source.Reference := new Wide_String (1 .. Length);
373 Source.Reference.all (1 .. S_Length) := Temp.all;
374 Source.Reference.all (S_Length + 1 .. Length) := New_Item.Reference.all;
379 (Source : in out Unbounded_Wide_String;
380 New_Item : in Wide_String)
382 S_Length : constant Integer := Source.Reference.all'Length;
383 Length : constant Integer := S_Length + New_Item'Length;
384 Temp : Wide_String_Access := Source.Reference;
387 if Source.Reference = Null_Wide_String'Access then
388 Source := To_Unbounded_Wide_String (New_Item);
392 Source.Reference := new Wide_String (1 .. Length);
393 Source.Reference.all (1 .. S_Length) := Temp.all;
394 Source.Reference.all (S_Length + 1 .. Length) := New_Item;
399 (Source : in out Unbounded_Wide_String;
400 New_Item : in Wide_Character)
402 S_Length : constant Integer := Source.Reference.all'Length;
403 Length : constant Integer := S_Length + 1;
404 Temp : Wide_String_Access := Source.Reference;
407 if Source.Reference = Null_Wide_String'Access then
408 Source := To_Unbounded_Wide_String ("" & New_Item);
412 Source.Reference := new Wide_String (1 .. Length);
413 Source.Reference.all (1 .. S_Length) := Temp.all;
414 Source.Reference.all (S_Length + 1) := New_Item;
423 (Source : Unbounded_Wide_String;
424 Pattern : Wide_String;
425 Mapping : Wide_Maps.Wide_Character_Mapping :=
430 return Wide_Search.Count (Source.Reference.all, Pattern, Mapping);
434 (Source : in Unbounded_Wide_String;
435 Pattern : in Wide_String;
436 Mapping : in Wide_Maps.Wide_Character_Mapping_Function)
440 return Wide_Search.Count (Source.Reference.all, Pattern, Mapping);
444 (Source : Unbounded_Wide_String;
445 Set : Wide_Maps.Wide_Character_Set)
449 return Wide_Search.Count (Source.Reference.all, Set);
457 (Source : Unbounded_Wide_String;
460 return Unbounded_Wide_String
464 To_Unbounded_Wide_String
465 (Wide_Fixed.Delete (Source.Reference.all, From, Through));
469 (Source : in out Unbounded_Wide_String;
471 Through : in Natural)
473 Temp : Wide_String_Access := Source.Reference;
475 Source := To_Unbounded_Wide_String
476 (Wide_Fixed.Delete (Temp.all, From, Through));
484 (Source : Unbounded_Wide_String;
486 return Wide_Character
489 if Index <= Source.Reference.all'Last then
490 return Source.Reference.all (Index);
492 raise Strings.Index_Error;
500 procedure Finalize (Object : in out Unbounded_Wide_String) is
501 procedure Deallocate is
502 new Ada.Unchecked_Deallocation (Wide_String, Wide_String_Access);
505 -- Note: Don't try to free statically allocated null string
507 if Object.Reference /= Null_Wide_String'Access then
508 Deallocate (Object.Reference);
509 Object.Reference := Null_Unbounded_Wide_String.Reference;
518 (Source : Unbounded_Wide_String;
519 Set : Wide_Maps.Wide_Character_Set;
520 Test : Strings.Membership;
521 First : out Positive;
525 Wide_Search.Find_Token (Source.Reference.all, Set, Test, First, Last);
532 procedure Free (X : in out Wide_String_Access) is
533 procedure Deallocate is
534 new Ada.Unchecked_Deallocation (Wide_String, Wide_String_Access);
544 (Source : Unbounded_Wide_String;
546 Pad : Wide_Character := Wide_Space)
547 return Unbounded_Wide_String
551 To_Unbounded_Wide_String
552 (Wide_Fixed.Head (Source.Reference.all, Count, Pad));
556 (Source : in out Unbounded_Wide_String;
558 Pad : in Wide_Character := Wide_Space)
561 Source := To_Unbounded_Wide_String
562 (Wide_Fixed.Head (Source.Reference.all, Count, Pad));
570 (Source : Unbounded_Wide_String;
571 Pattern : Wide_String;
572 Going : Strings.Direction := Strings.Forward;
573 Mapping : Wide_Maps.Wide_Character_Mapping :=
579 Wide_Search.Index (Source.Reference.all, Pattern, Going, Mapping);
583 (Source : in Unbounded_Wide_String;
584 Pattern : in Wide_String;
585 Going : in Direction := Forward;
586 Mapping : in Wide_Maps.Wide_Character_Mapping_Function)
591 Wide_Search.Index (Source.Reference.all, Pattern, Going, Mapping);
595 (Source : Unbounded_Wide_String;
596 Set : Wide_Maps.Wide_Character_Set;
597 Test : Strings.Membership := Strings.Inside;
598 Going : Strings.Direction := Strings.Forward)
602 return Wide_Search.Index (Source.Reference.all, Set, Test, Going);
605 function Index_Non_Blank
606 (Source : Unbounded_Wide_String;
607 Going : Strings.Direction := Strings.Forward)
611 return Wide_Search.Index_Non_Blank (Source.Reference.all, Going);
618 procedure Initialize (Object : in out Unbounded_Wide_String) is
620 Object.Reference := Null_Unbounded_Wide_String.Reference;
628 (Source : Unbounded_Wide_String;
630 New_Item : Wide_String)
631 return Unbounded_Wide_String
635 To_Unbounded_Wide_String
636 (Wide_Fixed.Insert (Source.Reference.all, Before, New_Item));
640 (Source : in out Unbounded_Wide_String;
641 Before : in Positive;
642 New_Item : in Wide_String)
645 Source := To_Unbounded_Wide_String
646 (Wide_Fixed.Insert (Source.Reference.all, Before, New_Item));
653 function Length (Source : Unbounded_Wide_String) return Natural is
655 return Source.Reference.all'Length;
663 (Source : Unbounded_Wide_String;
665 New_Item : Wide_String)
666 return Unbounded_Wide_String is
669 return To_Unbounded_Wide_String
670 (Wide_Fixed.Overwrite (Source.Reference.all, Position, New_Item));
674 (Source : in out Unbounded_Wide_String;
675 Position : in Positive;
676 New_Item : in Wide_String)
678 Temp : Wide_String_Access := Source.Reference;
680 Source := To_Unbounded_Wide_String
681 (Wide_Fixed.Overwrite (Temp.all, Position, New_Item));
684 ---------------------
685 -- Replace_Element --
686 ---------------------
688 procedure Replace_Element
689 (Source : in out Unbounded_Wide_String;
694 if Index <= Source.Reference.all'Last then
695 Source.Reference.all (Index) := By;
697 raise Strings.Index_Error;
705 function Replace_Slice
706 (Source : Unbounded_Wide_String;
710 return Unbounded_Wide_String
714 To_Unbounded_Wide_String
715 (Wide_Fixed.Replace_Slice (Source.Reference.all, Low, High, By));
718 procedure Replace_Slice
719 (Source : in out Unbounded_Wide_String;
724 Temp : Wide_String_Access := Source.Reference;
726 Source := To_Unbounded_Wide_String
727 (Wide_Fixed.Replace_Slice (Temp.all, Low, High, By));
735 (Source : Unbounded_Wide_String;
740 Length : constant Natural := Source.Reference'Length;
743 -- Note: test of High > Length is in accordance with AI95-00128
745 if Low > Length + 1 or else High > Length then
750 Result : Wide_String (1 .. High - Low + 1);
753 Result := Source.Reference.all (Low .. High);
764 (Source : Unbounded_Wide_String;
766 Pad : Wide_Character := Wide_Space)
767 return Unbounded_Wide_String is
771 To_Unbounded_Wide_String
772 (Wide_Fixed.Tail (Source.Reference.all, Count, Pad));
776 (Source : in out Unbounded_Wide_String;
778 Pad : in Wide_Character := Wide_Space)
780 Temp : Wide_String_Access := Source.Reference;
783 Source := To_Unbounded_Wide_String
784 (Wide_Fixed.Tail (Temp.all, Count, Pad));
787 ------------------------------
788 -- To_Unbounded_Wide_String --
789 ------------------------------
791 function To_Unbounded_Wide_String
792 (Source : Wide_String)
793 return Unbounded_Wide_String
795 Result : Unbounded_Wide_String;
798 Result.Reference := new Wide_String (1 .. Source'Length);
799 Result.Reference.all := Source;
801 end To_Unbounded_Wide_String;
803 function To_Unbounded_Wide_String (Length : in Natural)
804 return Unbounded_Wide_String
806 Result : Unbounded_Wide_String;
809 Result.Reference := new Wide_String (1 .. Length);
811 end To_Unbounded_Wide_String;
817 function To_Wide_String
818 (Source : Unbounded_Wide_String)
822 return Source.Reference.all;
830 (Source : Unbounded_Wide_String;
831 Mapping : Wide_Maps.Wide_Character_Mapping)
832 return Unbounded_Wide_String
836 To_Unbounded_Wide_String
837 (Wide_Fixed.Translate (Source.Reference.all, Mapping));
841 (Source : in out Unbounded_Wide_String;
842 Mapping : Wide_Maps.Wide_Character_Mapping)
845 Wide_Fixed.Translate (Source.Reference.all, Mapping);
849 (Source : in Unbounded_Wide_String;
850 Mapping : in Wide_Maps.Wide_Character_Mapping_Function)
851 return Unbounded_Wide_String
855 To_Unbounded_Wide_String
856 (Wide_Fixed.Translate (Source.Reference.all, Mapping));
860 (Source : in out Unbounded_Wide_String;
861 Mapping : in Wide_Maps.Wide_Character_Mapping_Function)
864 Wide_Fixed.Translate (Source.Reference.all, Mapping);
872 (Source : in Unbounded_Wide_String;
874 return Unbounded_Wide_String
878 To_Unbounded_Wide_String
879 (Wide_Fixed.Trim (Source.Reference.all, Side));
883 (Source : in out Unbounded_Wide_String;
886 Old : Wide_String_Access := Source.Reference;
888 Source.Reference := new Wide_String'(Wide_Fixed.Trim (Old.all, Side));
893 (Source : in Unbounded_Wide_String;
894 Left : in Wide_Maps.Wide_Character_Set;
895 Right : in Wide_Maps.Wide_Character_Set)
896 return Unbounded_Wide_String
900 To_Unbounded_Wide_String
901 (Wide_Fixed.Trim (Source.Reference.all, Left, Right));
905 (Source : in out Unbounded_Wide_String;
906 Left : in Wide_Maps.Wide_Character_Set;
907 Right : in Wide_Maps.Wide_Character_Set)
909 Old : Wide_String_Access := Source.Reference;
913 new Wide_String'(Wide_Fixed.Trim (Old.all, Left, Right));
917 end Ada.Strings.Wide_Unbounded;