a-stwiun.adb 29.4 KB
Newer Older
Richard Kenner committed
1 2
------------------------------------------------------------------------------
--                                                                          --
3
--                         GNAT RUN-TIME COMPONENTS                         --
Richard Kenner committed
4 5 6 7 8
--                                                                          --
--           A D A . S T R I N G S . W I D E _ U N B O U N D E D            --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9
--          Copyright (C) 1992-2012, Free Software Foundation, Inc.         --
Richard Kenner committed
10 11 12
--                                                                          --
-- GNAT is free software;  you can  redistribute it  and/or modify it under --
-- 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- --
Richard Kenner committed
14 15
-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 17 18 19 20 21 22 23 24 25
-- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
--                                                                          --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception,   --
-- version 3.1, as published by the Free Software Foundation.               --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
Richard Kenner committed
26 27
--                                                                          --
-- GNAT was originally developed  by the GNAT team at  New York University. --
28
-- Extensive contributions were provided by Ada Core Technologies Inc.      --
Richard Kenner committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
--                                                                          --
------------------------------------------------------------------------------

with Ada.Strings.Wide_Fixed;
with Ada.Strings.Wide_Search;
with Ada.Unchecked_Deallocation;

package body Ada.Strings.Wide_Unbounded is

   use Ada.Finalization;

   ---------
   -- "&" --
   ---------

   function "&"
     (Left  : Unbounded_Wide_String;
Robert Dewar committed
46
      Right : Unbounded_Wide_String) return Unbounded_Wide_String
Richard Kenner committed
47
   is
48 49
      L_Length : constant Natural := Left.Last;
      R_Length : constant Natural := Right.Last;
Richard Kenner committed
50 51 52
      Result   : Unbounded_Wide_String;

   begin
53 54 55 56 57 58 59 60 61
      Result.Last := L_Length + R_Length;

      Result.Reference := new Wide_String (1 .. Result.Last);

      Result.Reference (1 .. L_Length) :=
        Left.Reference (1 .. Left.Last);
      Result.Reference (L_Length + 1 .. Result.Last) :=
        Right.Reference (1 .. Right.Last);

Richard Kenner committed
62 63 64 65 66
      return Result;
   end "&";

   function "&"
     (Left  : Unbounded_Wide_String;
Robert Dewar committed
67
      Right : Wide_String) return Unbounded_Wide_String
Richard Kenner committed
68
   is
69
      L_Length : constant Natural := Left.Last;
Richard Kenner committed
70 71 72
      Result   : Unbounded_Wide_String;

   begin
73 74 75 76 77 78 79
      Result.Last := L_Length + Right'Length;

      Result.Reference := new Wide_String (1 .. Result.Last);

      Result.Reference (1 .. L_Length) := Left.Reference (1 .. Left.Last);
      Result.Reference (L_Length + 1 .. Result.Last) := Right;

Richard Kenner committed
80 81 82 83 84
      return Result;
   end "&";

   function "&"
     (Left  : Wide_String;
Robert Dewar committed
85
      Right : Unbounded_Wide_String) return Unbounded_Wide_String
Richard Kenner committed
86
   is
87
      R_Length : constant Natural := Right.Last;
Richard Kenner committed
88 89 90
      Result   : Unbounded_Wide_String;

   begin
91 92 93 94 95 96 97 98
      Result.Last := Left'Length + R_Length;

      Result.Reference := new Wide_String (1 .. Result.Last);

      Result.Reference (1 .. Left'Length) := Left;
      Result.Reference (Left'Length + 1 .. Result.Last) :=
        Right.Reference (1 .. Right.Last);

Richard Kenner committed
99 100 101 102 103
      return Result;
   end "&";

   function "&"
     (Left  : Unbounded_Wide_String;
Robert Dewar committed
104
      Right : Wide_Character) return Unbounded_Wide_String
Richard Kenner committed
105 106 107 108
   is
      Result : Unbounded_Wide_String;

   begin
109 110 111 112 113 114 115 116
      Result.Last := Left.Last + 1;

      Result.Reference := new Wide_String (1 .. Result.Last);

      Result.Reference (1 .. Result.Last - 1) :=
        Left.Reference (1 .. Left.Last);
      Result.Reference (Result.Last) := Right;

Richard Kenner committed
117 118 119 120 121
      return Result;
   end "&";

   function "&"
     (Left  : Wide_Character;
Robert Dewar committed
122
      Right : Unbounded_Wide_String) return Unbounded_Wide_String
Richard Kenner committed
123 124 125 126
   is
      Result : Unbounded_Wide_String;

   begin
127 128 129 130 131 132
      Result.Last := Right.Last + 1;

      Result.Reference := new Wide_String (1 .. Result.Last);
      Result.Reference (1) := Left;
      Result.Reference (2 .. Result.Last) :=
        Right.Reference (1 .. Right.Last);
Richard Kenner committed
133 134 135 136 137 138 139 140 141
      return Result;
   end "&";

   ---------
   -- "*" --
   ---------

   function "*"
     (Left  : Natural;
Robert Dewar committed
142
      Right : Wide_Character) return Unbounded_Wide_String
Richard Kenner committed
143 144 145 146
   is
      Result : Unbounded_Wide_String;

   begin
147
      Result.Last   := Left;
148

Richard Kenner committed
149 150 151 152 153 154 155 156 157
      Result.Reference := new Wide_String (1 .. Left);
      for J in Result.Reference'Range loop
         Result.Reference (J) := Right;
      end loop;

      return Result;
   end "*";

   function "*"
Robert Dewar committed
158 159
     (Left  : Natural;
      Right : Wide_String) return Unbounded_Wide_String
Richard Kenner committed
160
   is
161 162
      Len    : constant Natural := Right'Length;
      K      : Positive;
Richard Kenner committed
163 164 165
      Result : Unbounded_Wide_String;

   begin
166 167 168
      Result.Last := Left * Len;

      Result.Reference := new Wide_String (1 .. Result.Last);
Richard Kenner committed
169

170
      K := 1;
Richard Kenner committed
171
      for J in 1 .. Left loop
172 173
         Result.Reference (K .. K + Len - 1) := Right;
         K := K + Len;
Richard Kenner committed
174 175 176 177 178 179 180
      end loop;

      return Result;
   end "*";

   function "*"
     (Left  : Natural;
Robert Dewar committed
181
      Right : Unbounded_Wide_String) return Unbounded_Wide_String
Richard Kenner committed
182
   is
183 184
      Len    : constant Natural := Right.Last;
      K      : Positive;
185
      Result : Unbounded_Wide_String;
Richard Kenner committed
186 187

   begin
188 189 190
      Result.Last := Left * Len;

      Result.Reference := new Wide_String (1 .. Result.Last);
Richard Kenner committed
191

192
      K := 1;
193
      for J in 1 .. Left loop
194 195 196
         Result.Reference (K .. K + Len - 1) :=
           Right.Reference (1 .. Right.Last);
         K := K + Len;
Richard Kenner committed
197 198 199 200 201 202 203 204 205 206
      end loop;

      return Result;
   end "*";

   ---------
   -- "<" --
   ---------

   function "<"
207
     (Left  : Unbounded_Wide_String;
Robert Dewar committed
208
      Right : Unbounded_Wide_String) return Boolean
Richard Kenner committed
209 210
   is
   begin
211 212
      return
        Left.Reference (1 .. Left.Last) < Right.Reference (1 .. Right.Last);
Richard Kenner committed
213 214 215
   end "<";

   function "<"
216
     (Left  : Unbounded_Wide_String;
Robert Dewar committed
217
      Right : Wide_String) return Boolean
Richard Kenner committed
218 219
   is
   begin
220
      return Left.Reference (1 .. Left.Last) < Right;
Richard Kenner committed
221 222 223
   end "<";

   function "<"
224
     (Left  : Wide_String;
Robert Dewar committed
225
      Right : Unbounded_Wide_String) return Boolean
Richard Kenner committed
226 227
   is
   begin
228
      return Left < Right.Reference (1 .. Right.Last);
Richard Kenner committed
229 230 231 232 233 234 235
   end "<";

   ----------
   -- "<=" --
   ----------

   function "<="
236
     (Left  : Unbounded_Wide_String;
Robert Dewar committed
237
      Right : Unbounded_Wide_String) return Boolean
Richard Kenner committed
238 239
   is
   begin
240 241
      return
        Left.Reference (1 .. Left.Last) <= Right.Reference (1 .. Right.Last);
Richard Kenner committed
242 243 244
   end "<=";

   function "<="
245
     (Left  : Unbounded_Wide_String;
Robert Dewar committed
246
      Right : Wide_String) return Boolean
Richard Kenner committed
247 248
   is
   begin
249
      return Left.Reference (1 .. Left.Last) <= Right;
Richard Kenner committed
250 251 252
   end "<=";

   function "<="
253
     (Left  : Wide_String;
Robert Dewar committed
254
      Right : Unbounded_Wide_String) return Boolean
Richard Kenner committed
255 256
   is
   begin
257
      return Left <= Right.Reference (1 .. Right.Last);
Richard Kenner committed
258 259 260 261 262 263 264
   end "<=";

   ---------
   -- "=" --
   ---------

   function "="
265
     (Left  : Unbounded_Wide_String;
Robert Dewar committed
266
      Right : Unbounded_Wide_String) return Boolean
Richard Kenner committed
267 268
   is
   begin
269 270
      return
        Left.Reference (1 .. Left.Last) = Right.Reference (1 .. Right.Last);
Richard Kenner committed
271 272 273
   end "=";

   function "="
274
     (Left  : Unbounded_Wide_String;
Robert Dewar committed
275
      Right : Wide_String) return Boolean
Richard Kenner committed
276 277
   is
   begin
278
      return Left.Reference (1 .. Left.Last) = Right;
Richard Kenner committed
279 280 281
   end "=";

   function "="
282
     (Left  : Wide_String;
Robert Dewar committed
283
      Right : Unbounded_Wide_String) return Boolean
Richard Kenner committed
284 285
   is
   begin
286
      return Left = Right.Reference (1 .. Right.Last);
Richard Kenner committed
287 288 289 290 291 292 293
   end "=";

   ---------
   -- ">" --
   ---------

   function ">"
294
     (Left  : Unbounded_Wide_String;
Robert Dewar committed
295
      Right : Unbounded_Wide_String) return Boolean
Richard Kenner committed
296 297
   is
   begin
298 299
      return
        Left.Reference (1 .. Left.Last) > Right.Reference (1 .. Right.Last);
Richard Kenner committed
300 301 302
   end ">";

   function ">"
303
     (Left  : Unbounded_Wide_String;
Robert Dewar committed
304
      Right : Wide_String) return Boolean
Richard Kenner committed
305 306
   is
   begin
307
      return Left.Reference (1 .. Left.Last) > Right;
Richard Kenner committed
308 309 310
   end ">";

   function ">"
311
     (Left  : Wide_String;
Robert Dewar committed
312
      Right : Unbounded_Wide_String) return Boolean
Richard Kenner committed
313 314
   is
   begin
315
      return Left > Right.Reference (1 .. Right.Last);
Richard Kenner committed
316 317 318 319 320 321 322
   end ">";

   ----------
   -- ">=" --
   ----------

   function ">="
323
     (Left  : Unbounded_Wide_String;
Robert Dewar committed
324
      Right : Unbounded_Wide_String) return Boolean
Richard Kenner committed
325 326
   is
   begin
327 328
      return
        Left.Reference (1 .. Left.Last) >= Right.Reference (1 .. Right.Last);
Richard Kenner committed
329 330 331
   end ">=";

   function ">="
332
     (Left  : Unbounded_Wide_String;
Robert Dewar committed
333
      Right : Wide_String) return Boolean
Richard Kenner committed
334 335
   is
   begin
336
      return Left.Reference (1 .. Left.Last) >= Right;
Richard Kenner committed
337 338 339
   end ">=";

   function ">="
340
     (Left  : Wide_String;
Robert Dewar committed
341
      Right : Unbounded_Wide_String) return Boolean
Richard Kenner committed
342 343
   is
   begin
344
      return Left >= Right.Reference (1 .. Right.Last);
Richard Kenner committed
345 346 347 348 349 350 351 352
   end ">=";

   ------------
   -- Adjust --
   ------------

   procedure Adjust (Object : in out Unbounded_Wide_String) is
   begin
353 354 355
      --  Copy string, except we do not copy the statically allocated null
      --  string, since it can never be deallocated. Note that we do not copy
      --  extra string room here to avoid dragging unused allocated memory.
Richard Kenner committed
356 357

      if Object.Reference /= Null_Wide_String'Access then
358 359
         Object.Reference :=
           new Wide_String'(Object.Reference (1 .. Object.Last));
Richard Kenner committed
360 361 362 363 364 365 366 367 368
      end if;
   end Adjust;

   ------------
   -- Append --
   ------------

   procedure Append
     (Source   : in out Unbounded_Wide_String;
369
      New_Item : Unbounded_Wide_String)
Richard Kenner committed
370 371
   is
   begin
372 373 374 375
      Realloc_For_Chunk (Source, New_Item.Last);
      Source.Reference (Source.Last + 1 .. Source.Last + New_Item.Last) :=
        New_Item.Reference (1 .. New_Item.Last);
      Source.Last := Source.Last + New_Item.Last;
Richard Kenner committed
376 377 378 379
   end Append;

   procedure Append
     (Source   : in out Unbounded_Wide_String;
380
      New_Item : Wide_String)
Richard Kenner committed
381 382
   is
   begin
383 384 385 386
      Realloc_For_Chunk (Source, New_Item'Length);
      Source.Reference (Source.Last + 1 .. Source.Last + New_Item'Length) :=
        New_Item;
      Source.Last := Source.Last + New_Item'Length;
Richard Kenner committed
387 388 389 390
   end Append;

   procedure Append
     (Source   : in out Unbounded_Wide_String;
391
      New_Item : Wide_Character)
Richard Kenner committed
392 393
   is
   begin
394 395 396
      Realloc_For_Chunk (Source, 1);
      Source.Reference (Source.Last + 1) := New_Item;
      Source.Last := Source.Last + 1;
Richard Kenner committed
397 398 399 400 401 402 403
   end Append;

   -----------
   -- Count --
   -----------

   function Count
Robert Dewar committed
404 405
     (Source  : Unbounded_Wide_String;
      Pattern : Wide_String;
406
      Mapping : Wide_Maps.Wide_Character_Mapping := Wide_Maps.Identity)
Robert Dewar committed
407
      return Natural
Richard Kenner committed
408 409
   is
   begin
410 411 412
      return
        Wide_Search.Count
          (Source.Reference (1 .. Source.Last), Pattern, Mapping);
Richard Kenner committed
413 414 415
   end Count;

   function Count
Robert Dewar committed
416 417 418
     (Source  : Unbounded_Wide_String;
      Pattern : Wide_String;
      Mapping : Wide_Maps.Wide_Character_Mapping_Function) return Natural
Richard Kenner committed
419 420
   is
   begin
421 422 423
      return
        Wide_Search.Count
          (Source.Reference (1 .. Source.Last), Pattern, Mapping);
Richard Kenner committed
424 425 426
   end Count;

   function Count
Robert Dewar committed
427 428
     (Source : Unbounded_Wide_String;
      Set    : Wide_Maps.Wide_Character_Set) return Natural
Richard Kenner committed
429 430
   is
   begin
431 432 433
      return
        Wide_Search.Count
        (Source.Reference (1 .. Source.Last), Set);
Richard Kenner committed
434 435 436 437 438 439 440 441 442
   end Count;

   ------------
   -- Delete --
   ------------

   function Delete
     (Source  : Unbounded_Wide_String;
      From    : Positive;
Robert Dewar committed
443
      Through : Natural) return Unbounded_Wide_String
Richard Kenner committed
444 445
   is
   begin
446 447 448 449
      return
        To_Unbounded_Wide_String
          (Wide_Fixed.Delete
             (Source.Reference (1 .. Source.Last), From, Through));
Richard Kenner committed
450 451 452 453
   end Delete;

   procedure Delete
     (Source  : in out Unbounded_Wide_String;
454 455
      From    : Positive;
      Through : Natural)
Richard Kenner committed
456 457
   is
   begin
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
      if From > Through then
         null;

      elsif From < Source.Reference'First or else Through > Source.Last then
         raise Index_Error;

      else
         declare
            Len : constant Natural := Through - From + 1;

         begin
            Source.Reference (From .. Source.Last - Len) :=
              Source.Reference (Through + 1 .. Source.Last);
            Source.Last := Source.Last - Len;
         end;
      end if;
Richard Kenner committed
474 475 476 477 478 479 480 481
   end Delete;

   -------------
   -- Element --
   -------------

   function Element
     (Source : Unbounded_Wide_String;
Robert Dewar committed
482
      Index  : Positive) return Wide_Character
Richard Kenner committed
483 484
   is
   begin
485 486
      if Index <= Source.Last then
         return Source.Reference (Index);
Richard Kenner committed
487 488 489 490 491 492 493 494 495 496 497
      else
         raise Strings.Index_Error;
      end if;
   end Element;

   --------------
   -- Finalize --
   --------------

   procedure Finalize (Object : in out Unbounded_Wide_String) is
      procedure Deallocate is
498
         new Ada.Unchecked_Deallocation (Wide_String, Wide_String_Access);
Richard Kenner committed
499 500 501 502 503 504 505

   begin
      --  Note: Don't try to free statically allocated null string

      if Object.Reference /= Null_Wide_String'Access then
         Deallocate (Object.Reference);
         Object.Reference := Null_Unbounded_Wide_String.Reference;
506
         Object.Last := 0;
Richard Kenner committed
507 508 509 510 511 512 513 514 515 516
      end if;
   end Finalize;

   ----------------
   -- Find_Token --
   ----------------

   procedure Find_Token
     (Source : Unbounded_Wide_String;
      Set    : Wide_Maps.Wide_Character_Set;
Arnaud Charlet committed
517 518 519 520 521 522 523 524 525 526 527 528 529
      From   : Positive;
      Test   : Strings.Membership;
      First  : out Positive;
      Last   : out Natural)
   is
   begin
      Wide_Search.Find_Token
        (Source.Reference (From .. Source.Last), Set, Test, First, Last);
   end Find_Token;

   procedure Find_Token
     (Source : Unbounded_Wide_String;
      Set    : Wide_Maps.Wide_Character_Set;
Richard Kenner committed
530 531 532 533 534
      Test   : Strings.Membership;
      First  : out Positive;
      Last   : out Natural)
   is
   begin
535 536
      Wide_Search.Find_Token
        (Source.Reference (1 .. Source.Last), Set, Test, First, Last);
Richard Kenner committed
537 538 539 540 541 542 543 544 545
   end Find_Token;

   ----------
   -- Free --
   ----------

   procedure Free (X : in out Wide_String_Access) is
      procedure Deallocate is
         new Ada.Unchecked_Deallocation (Wide_String, Wide_String_Access);
546

Richard Kenner committed
547
   begin
548 549 550 551 552
      --  Note: Do not try to free statically allocated null string

      if X /= Null_Unbounded_Wide_String.Reference then
         Deallocate (X);
      end if;
Richard Kenner committed
553 554 555 556 557 558 559 560 561
   end Free;

   ----------
   -- Head --
   ----------

   function Head
     (Source : Unbounded_Wide_String;
      Count  : Natural;
Robert Dewar committed
562
      Pad    : Wide_Character := Wide_Space) return Unbounded_Wide_String
Richard Kenner committed
563 564
   is
   begin
565 566
      return To_Unbounded_Wide_String
        (Wide_Fixed.Head (Source.Reference (1 .. Source.Last), Count, Pad));
Richard Kenner committed
567 568 569 570
   end Head;

   procedure Head
     (Source : in out Unbounded_Wide_String;
571 572
      Count  : Natural;
      Pad    : Wide_Character := Wide_Space)
Richard Kenner committed
573
   is
574
      Old : Wide_String_Access := Source.Reference;
Richard Kenner committed
575
   begin
576 577 578
      Source.Reference :=
        new Wide_String'
          (Wide_Fixed.Head (Source.Reference (1 .. Source.Last), Count, Pad));
579 580
      Source.Last := Source.Reference'Length;
      Free (Old);
Richard Kenner committed
581 582 583 584 585 586 587
   end Head;

   -----------
   -- Index --
   -----------

   function Index
Robert Dewar committed
588 589 590
     (Source  : Unbounded_Wide_String;
      Pattern : Wide_String;
      Going   : Strings.Direction := Strings.Forward;
591 592
      Mapping : Wide_Maps.Wide_Character_Mapping := Wide_Maps.Identity)
      return Natural
Richard Kenner committed
593 594
   is
   begin
595 596 597
      return
        Wide_Search.Index
          (Source.Reference (1 .. Source.Last), Pattern, Going, Mapping);
Richard Kenner committed
598 599 600
   end Index;

   function Index
Robert Dewar committed
601 602 603 604
     (Source  : Unbounded_Wide_String;
      Pattern : Wide_String;
      Going   : Direction := Forward;
      Mapping : Wide_Maps.Wide_Character_Mapping_Function) return Natural
Richard Kenner committed
605 606
   is
   begin
607 608 609
      return
        Wide_Search.Index
          (Source.Reference (1 .. Source.Last), Pattern, Going, Mapping);
Richard Kenner committed
610 611 612 613 614 615
   end Index;

   function Index
     (Source : Unbounded_Wide_String;
      Set    : Wide_Maps.Wide_Character_Set;
      Test   : Strings.Membership := Strings.Inside;
Robert Dewar committed
616
      Going  : Strings.Direction  := Strings.Forward) return Natural
Richard Kenner committed
617 618
   is
   begin
619 620
      return Wide_Search.Index
        (Source.Reference (1 .. Source.Last), Set, Test, Going);
Richard Kenner committed
621 622
   end Index;

623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
   function Index
     (Source  : Unbounded_Wide_String;
      Pattern : Wide_String;
      From    : Positive;
      Going   : Direction := Forward;
      Mapping : Wide_Maps.Wide_Character_Mapping := Wide_Maps.Identity)
      return Natural
   is
   begin
      return
        Wide_Search.Index
          (Source.Reference (1 .. Source.Last), Pattern, From, Going, Mapping);
   end Index;

   function Index
     (Source  : Unbounded_Wide_String;
      Pattern : Wide_String;
      From    : Positive;
      Going   : Direction := Forward;
      Mapping : Wide_Maps.Wide_Character_Mapping_Function) return Natural
   is
   begin
      return
        Wide_Search.Index
          (Source.Reference (1 .. Source.Last), Pattern, From, Going, Mapping);
   end Index;

   function Index
     (Source  : Unbounded_Wide_String;
      Set     : Wide_Maps.Wide_Character_Set;
      From    : Positive;
      Test    : Membership := Inside;
      Going   : Direction := Forward) return Natural
   is
   begin
      return
        Wide_Search.Index
          (Source.Reference (1 .. Source.Last), Set, From, Test, Going);
   end Index;

Richard Kenner committed
663 664
   function Index_Non_Blank
     (Source : Unbounded_Wide_String;
Robert Dewar committed
665
      Going  : Strings.Direction := Strings.Forward) return Natural
Richard Kenner committed
666 667
   is
   begin
668 669 670 671 672 673 674 675 676 677 678 679 680 681
      return
        Wide_Search.Index_Non_Blank
          (Source.Reference (1 .. Source.Last), Going);
   end Index_Non_Blank;

   function Index_Non_Blank
     (Source : Unbounded_Wide_String;
      From   : Positive;
      Going  : Direction := Forward) return Natural
   is
   begin
      return
        Wide_Search.Index_Non_Blank
          (Source.Reference (1 .. Source.Last), From, Going);
Richard Kenner committed
682 683 684 685 686 687 688 689 690
   end Index_Non_Blank;

   ----------------
   -- Initialize --
   ----------------

   procedure Initialize (Object : in out Unbounded_Wide_String) is
   begin
      Object.Reference := Null_Unbounded_Wide_String.Reference;
691
      Object.Last      := 0;
Richard Kenner committed
692 693 694 695 696 697 698 699 700
   end Initialize;

   ------------
   -- Insert --
   ------------

   function Insert
     (Source   : Unbounded_Wide_String;
      Before   : Positive;
Robert Dewar committed
701
      New_Item : Wide_String) return Unbounded_Wide_String
Richard Kenner committed
702 703
   is
   begin
704 705 706 707
      return
        To_Unbounded_Wide_String
          (Wide_Fixed.Insert
             (Source.Reference (1 .. Source.Last), Before, New_Item));
Richard Kenner committed
708 709 710 711
   end Insert;

   procedure Insert
     (Source   : in out Unbounded_Wide_String;
712 713
      Before   : Positive;
      New_Item : Wide_String)
Richard Kenner committed
714 715
   is
   begin
716 717 718 719
      if Before not in Source.Reference'First .. Source.Last + 1 then
         raise Index_Error;
      end if;

720
      Realloc_For_Chunk (Source, New_Item'Length);
721 722 723 724 725 726 727

      Source.Reference
        (Before +  New_Item'Length .. Source.Last + New_Item'Length) :=
           Source.Reference (Before .. Source.Last);

      Source.Reference (Before .. Before + New_Item'Length - 1) := New_Item;
      Source.Last := Source.Last + New_Item'Length;
Richard Kenner committed
728 729 730 731 732 733 734 735
   end Insert;

   ------------
   -- Length --
   ------------

   function Length (Source : Unbounded_Wide_String) return Natural is
   begin
736
      return Source.Last;
Richard Kenner committed
737 738 739 740 741 742 743
   end Length;

   ---------------
   -- Overwrite --
   ---------------

   function Overwrite
Robert Dewar committed
744 745 746 747
     (Source   : Unbounded_Wide_String;
      Position : Positive;
      New_Item : Wide_String) return Unbounded_Wide_String
   is
Richard Kenner committed
748
   begin
749 750 751 752
      return
        To_Unbounded_Wide_String
          (Wide_Fixed.Overwrite
            (Source.Reference (1 .. Source.Last), Position, New_Item));
Richard Kenner committed
753 754 755 756
   end Overwrite;

   procedure Overwrite
     (Source    : in out Unbounded_Wide_String;
757 758
      Position  : Positive;
      New_Item  : Wide_String)
Richard Kenner committed
759
   is
760
      NL : constant Natural := New_Item'Length;
Richard Kenner committed
761
   begin
762 763 764 765 766 767 768 769 770 771 772 773 774
      if Position <= Source.Last - NL + 1 then
         Source.Reference (Position .. Position + NL - 1) := New_Item;
      else
         declare
            Old : Wide_String_Access := Source.Reference;
         begin
            Source.Reference := new Wide_String'
              (Wide_Fixed.Overwrite
                (Source.Reference (1 .. Source.Last), Position, New_Item));
            Source.Last := Source.Reference'Length;
            Free (Old);
         end;
      end if;
Richard Kenner committed
775 776
   end Overwrite;

777 778 779 780 781 782 783 784
   -----------------------
   -- Realloc_For_Chunk --
   -----------------------

   procedure Realloc_For_Chunk
     (Source     : in out Unbounded_Wide_String;
      Chunk_Size : Natural)
   is
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
      Growth_Factor : constant := 32;
      --  The growth factor controls how much extra space is allocated when
      --  we have to increase the size of an allocated unbounded string. By
      --  allocating extra space, we avoid the need to reallocate on every
      --  append, particularly important when a string is built up by repeated
      --  append operations of small pieces. This is expressed as a factor so
      --  32 means add 1/32 of the length of the string as growth space.

      Min_Mul_Alloc : constant := Standard'Maximum_Alignment;
      --  Allocation will be done by a multiple of Min_Mul_Alloc This causes
      --  no memory loss as most (all?) malloc implementations are obliged to
      --  align the returned memory on the maximum alignment as malloc does not
      --  know the target alignment.

      S_Length : constant Natural := Source.Reference'Length;
800 801 802 803

   begin
      if Chunk_Size > S_Length - Source.Last then
         declare
804
            New_Size : constant Positive :=
805
              S_Length + Chunk_Size + (S_Length / Growth_Factor);
806 807

            New_Rounded_Up_Size : constant Positive :=
808
              ((New_Size - 1) / Min_Mul_Alloc + 1) * Min_Mul_Alloc;
809 810

            Tmp : constant Wide_String_Access :=
811
              new Wide_String (1 .. New_Rounded_Up_Size);
812

813 814 815 816 817 818 819 820
         begin
            Tmp (1 .. Source.Last) := Source.Reference (1 .. Source.Last);
            Free (Source.Reference);
            Source.Reference := Tmp;
         end;
      end if;
   end Realloc_For_Chunk;

Richard Kenner committed
821 822 823 824 825 826 827 828 829 830
   ---------------------
   -- Replace_Element --
   ---------------------

   procedure Replace_Element
     (Source : in out Unbounded_Wide_String;
      Index  : Positive;
      By     : Wide_Character)
   is
   begin
831 832
      if Index <= Source.Last then
         Source.Reference (Index) := By;
Richard Kenner committed
833 834 835 836 837 838 839 840 841 842
      else
         raise Strings.Index_Error;
      end if;
   end Replace_Element;

   -------------------
   -- Replace_Slice --
   -------------------

   function Replace_Slice
Robert Dewar committed
843 844 845 846
     (Source : Unbounded_Wide_String;
      Low    : Positive;
      High   : Natural;
      By     : Wide_String) return Unbounded_Wide_String
Richard Kenner committed
847 848
   is
   begin
849
      return To_Unbounded_Wide_String
850 851
        (Wide_Fixed.Replace_Slice
           (Source.Reference (1 .. Source.Last), Low, High, By));
Richard Kenner committed
852 853 854
   end Replace_Slice;

   procedure Replace_Slice
855 856 857 858
     (Source : in out Unbounded_Wide_String;
      Low    : Positive;
      High   : Natural;
      By     : Wide_String)
Richard Kenner committed
859
   is
860
      Old : Wide_String_Access := Source.Reference;
Richard Kenner committed
861
   begin
862 863 864 865 866
      Source.Reference := new Wide_String'
        (Wide_Fixed.Replace_Slice
           (Source.Reference (1 .. Source.Last), Low, High, By));
      Source.Last := Source.Reference'Length;
      Free (Old);
Richard Kenner committed
867 868
   end Replace_Slice;

869 870 871 872 873 874 875 876 877 878 879 880 881 882
   -------------------------------
   -- Set_Unbounded_Wide_String --
   -------------------------------

   procedure Set_Unbounded_Wide_String
     (Target : out Unbounded_Wide_String;
      Source : Wide_String)
   is
   begin
      Target.Last          := Source'Length;
      Target.Reference     := new Wide_String (1 .. Source'Length);
      Target.Reference.all := Source;
   end Set_Unbounded_Wide_String;

Richard Kenner committed
883 884 885 886 887 888 889
   -----------
   -- Slice --
   -----------

   function Slice
     (Source : Unbounded_Wide_String;
      Low    : Positive;
Robert Dewar committed
890
      High   : Natural) return Wide_String
Richard Kenner committed
891 892 893 894
   is
   begin
      --  Note: test of High > Length is in accordance with AI95-00128

895
      if Low > Source.Last + 1 or else High > Source.Last then
Richard Kenner committed
896 897
         raise Index_Error;
      else
898
         return Source.Reference (Low .. High);
Richard Kenner committed
899 900 901 902 903 904 905 906 907 908
      end if;
   end Slice;

   ----------
   -- Tail --
   ----------

   function Tail
     (Source : Unbounded_Wide_String;
      Count  : Natural;
909
      Pad    : Wide_Character := Wide_Space) return Unbounded_Wide_String is
Richard Kenner committed
910
   begin
911 912
      return To_Unbounded_Wide_String
        (Wide_Fixed.Tail (Source.Reference (1 .. Source.Last), Count, Pad));
Richard Kenner committed
913 914 915 916
   end Tail;

   procedure Tail
     (Source : in out Unbounded_Wide_String;
917 918
      Count  : Natural;
      Pad    : Wide_Character := Wide_Space)
Richard Kenner committed
919
   is
920
      Old : Wide_String_Access := Source.Reference;
Richard Kenner committed
921
   begin
922 923 924 925
      Source.Reference := new Wide_String'
        (Wide_Fixed.Tail (Source.Reference (1 .. Source.Last), Count, Pad));
      Source.Last := Source.Reference'Length;
      Free (Old);
Richard Kenner committed
926 927 928 929 930 931 932
   end Tail;

   ------------------------------
   -- To_Unbounded_Wide_String --
   ------------------------------

   function To_Unbounded_Wide_String
933 934
     (Source : Wide_String)
      return Unbounded_Wide_String
Richard Kenner committed
935 936 937
   is
      Result : Unbounded_Wide_String;
   begin
938 939
      Result.Last          := Source'Length;
      Result.Reference     := new Wide_String (1 .. Source'Length);
Richard Kenner committed
940 941 942 943
      Result.Reference.all := Source;
      return Result;
   end To_Unbounded_Wide_String;

Robert Dewar committed
944 945
   function To_Unbounded_Wide_String
     (Length : Natural) return Unbounded_Wide_String
Richard Kenner committed
946 947 948
   is
      Result : Unbounded_Wide_String;
   begin
949
      Result.Last      := Length;
Richard Kenner committed
950 951 952 953
      Result.Reference := new Wide_String (1 .. Length);
      return Result;
   end To_Unbounded_Wide_String;

954
   -------------------
Richard Kenner committed
955 956 957 958
   -- To_Wide_String --
   --------------------

   function To_Wide_String
959 960
     (Source : Unbounded_Wide_String)
      return Wide_String
Richard Kenner committed
961 962
   is
   begin
963
      return Source.Reference (1 .. Source.Last);
Richard Kenner committed
964 965 966 967 968 969 970 971
   end To_Wide_String;

   ---------------
   -- Translate --
   ---------------

   function Translate
     (Source  : Unbounded_Wide_String;
972 973
      Mapping : Wide_Maps.Wide_Character_Mapping)
      return Unbounded_Wide_String
Richard Kenner committed
974 975
   is
   begin
976 977 978 979
      return
        To_Unbounded_Wide_String
          (Wide_Fixed.Translate
             (Source.Reference (1 .. Source.Last), Mapping));
Richard Kenner committed
980 981 982 983 984 985 986
   end Translate;

   procedure Translate
     (Source  : in out Unbounded_Wide_String;
      Mapping : Wide_Maps.Wide_Character_Mapping)
   is
   begin
987
      Wide_Fixed.Translate (Source.Reference (1 .. Source.Last), Mapping);
Richard Kenner committed
988 989 990
   end Translate;

   function Translate
991 992
     (Source  : Unbounded_Wide_String;
      Mapping : Wide_Maps.Wide_Character_Mapping_Function)
Robert Dewar committed
993
      return Unbounded_Wide_String
Richard Kenner committed
994 995
   is
   begin
996 997 998 999
      return
        To_Unbounded_Wide_String
          (Wide_Fixed.Translate
            (Source.Reference (1 .. Source.Last), Mapping));
Richard Kenner committed
1000 1001 1002 1003
   end Translate;

   procedure Translate
     (Source  : in out Unbounded_Wide_String;
1004
      Mapping : Wide_Maps.Wide_Character_Mapping_Function)
Richard Kenner committed
1005 1006
   is
   begin
1007
      Wide_Fixed.Translate (Source.Reference (1 .. Source.Last), Mapping);
Richard Kenner committed
1008 1009 1010 1011 1012 1013 1014
   end Translate;

   ----------
   -- Trim --
   ----------

   function Trim
1015
     (Source : Unbounded_Wide_String;
Robert Dewar committed
1016
      Side   : Trim_End) return Unbounded_Wide_String
Richard Kenner committed
1017 1018
   is
   begin
1019 1020 1021
      return
        To_Unbounded_Wide_String
          (Wide_Fixed.Trim (Source.Reference (1 .. Source.Last), Side));
Richard Kenner committed
1022 1023 1024 1025
   end Trim;

   procedure Trim
     (Source : in out Unbounded_Wide_String;
1026
      Side   : Trim_End)
Richard Kenner committed
1027 1028 1029
   is
      Old : Wide_String_Access := Source.Reference;
   begin
1030 1031 1032
      Source.Reference :=
        new Wide_String'
          (Wide_Fixed.Trim (Source.Reference (1 .. Source.Last), Side));
1033
      Source.Last      := Source.Reference'Length;
Richard Kenner committed
1034 1035 1036 1037
      Free (Old);
   end Trim;

   function Trim
1038 1039
     (Source : Unbounded_Wide_String;
      Left   : Wide_Maps.Wide_Character_Set;
1040 1041
      Right  : Wide_Maps.Wide_Character_Set)
      return Unbounded_Wide_String
Richard Kenner committed
1042 1043
   is
   begin
1044 1045 1046 1047
      return
        To_Unbounded_Wide_String
          (Wide_Fixed.Trim
             (Source.Reference (1 .. Source.Last), Left, Right));
Richard Kenner committed
1048 1049 1050 1051
   end Trim;

   procedure Trim
     (Source : in out Unbounded_Wide_String;
1052 1053
      Left   : Wide_Maps.Wide_Character_Set;
      Right  : Wide_Maps.Wide_Character_Set)
Richard Kenner committed
1054 1055 1056
   is
      Old : Wide_String_Access := Source.Reference;
   begin
1057 1058 1059 1060
      Source.Reference :=
        new Wide_String'
          (Wide_Fixed.Trim
             (Source.Reference (1 .. Source.Last), Left, Right));
1061
      Source.Last      := Source.Reference'Length;
Richard Kenner committed
1062 1063 1064
      Free (Old);
   end Trim;

1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
   ---------------------
   -- Unbounded_Slice --
   ---------------------

   function Unbounded_Slice
     (Source : Unbounded_Wide_String;
      Low    : Positive;
      High   : Natural) return Unbounded_Wide_String
   is
   begin
      if Low > Source.Last + 1 or else High > Source.Last then
         raise Index_Error;
      else
         return To_Unbounded_Wide_String (Source.Reference.all (Low .. High));
      end if;
   end Unbounded_Slice;

   procedure Unbounded_Slice
     (Source : Unbounded_Wide_String;
      Target : out Unbounded_Wide_String;
      Low    : Positive;
      High   : Natural)
   is
   begin
      if Low > Source.Last + 1 or else High > Source.Last then
         raise Index_Error;
      else
         Target :=
           To_Unbounded_Wide_String (Source.Reference.all (Low .. High));
      end if;
   end Unbounded_Slice;

Richard Kenner committed
1097
end Ada.Strings.Wide_Unbounded;