adacl_5.15.1_e7c1515b/src/adacl-wide_strings.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
--------------------------------------------------------------- {{{1 ----------
--: Copyright © 2003 … 2023 Martin Krischik «krischik@users.sourceforge.net»
------------------------------------------------------------------------------
--: This library is free software; you can redistribute it and/or modify it
--: under the terms of the GNU Library General Public License as published by
--: the Free Software Foundation; either version 2 of the License, or (at your
--: option) any later version.
--:
--: This library is distributed in the hope that it will be useful, but
--: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
--: or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
--: License for more details.
--:
--: You should have received a copy of the GNU Library General Public License
--: along with this library; if not, write to the Free Software Foundation,
--: Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
--------------------------------------------------------------- }}}1 ----------

pragma License (Modified_Gpl);
pragma Ada_2022;

with Ada.Strings.Wide_Fixed;
with Ada.Integer_Wide_Text_IO;

---
--  @summary
--
--  @description
--  No, I have not created some new Wide_String class - yet. Just a few string tools.
--
package body AdaCL.Wide_Strings is

   ---------------------------------------------------------------------------

   package Maps renames Ada.Strings.Wide_Maps;
   package Strings renames Ada.Strings;
   package Unbounded renames Ada.Strings.Wide_Unbounded;
   package Fixed renames Ada.Strings.Wide_Fixed;

   ---------------------------------------------------------------------------

   use type Unbounded.Unbounded_Wide_String;

   ---
   --  Searches for all occurences of text "Search" and Inserts text "Insert" after the found text but only when
   --  "Insert" is not allready there.
   --
   --: @param Source     Wide_String to be changed
   --: @param Search     Wide_String we look for
   --: @param New_Item   Wide_String we want to insert
   --: @param Mapping    Search mapping
   procedure Append_All
      (Source   : in out Unbounded.Unbounded_Wide_String;
       Search   : in     Wide_String;
       New_Item : in     Wide_String;
       Mapping  : in     Maps.Wide_Character_Mapping := Maps.Identity)
   is
      Count : Natural := Natural'First;
   begin
      Append_All
         (Source   => Source,
          Search   => Search,
          New_Item => New_Item,
          Mapping  => Mapping,
          Count    => Count);
   end Append_All;

   ---
   --  Searches for all occurences of text "Search" and Inserts text "Insert" after the found text but only when
   --  "Insert" is not allready there.
   --
   --: @param Source    Wide_String to be changed
   --: @param Search    Wide_String we look for
   --: @param New_Item  Wide_String we want to insert
   --: @param Mapping   Search mapping
   --: @param Count     Count of replaces done
   procedure Append_All
      (Source   : in out Unbounded.Unbounded_Wide_String;
       Search   : in     Wide_String;
       New_Item : in     Wide_String;
       Mapping  : in     Maps.Wide_Character_Mapping := Maps.Identity;
       Count    :    out Natural)
   is
      --  Offset from which we start. 0 means search from the first character
      --
      Offset : Natural := Natural'First;
      --  Lenght of the full string
      --
      Len : constant Natural := Unbounded.Length (Source);
   begin
      --
      --  nothing found yet
      --
      Count := Natural'First;

      Search_Next :
      loop
         --
         --  Last Found Item was at the end of the Wide_String
         --
         exit Search_Next when Len = 0 or else Offset >= Len;

         Next_Sub_String :
         declare
            --
            --  We slice from the Offset on to the end. One might be suprised to learn that Sub_String'First might not
            --  be 1
            --
            Sub_String : constant Wide_String := Unbounded.Slice
                  (Source => Source,
                   Low    => Offset + 1,
                   High   => Len);
            --
            --  We search for Pattern
            --
            Low : constant Natural := Fixed.Index
                  (Source  => Sub_String,
                   Pattern => Search,
                   Going   => Strings.Forward,
                   Mapping => Mapping);
            High : constant Natural := Low + Search'Length;
         begin
            --: Ada.Text_IO.Put_Line ("New_Item'Length   = " & Natural'Image (New_Item'Length));
            --: Ada.Text_IO.Put_Line ("Sub_String'Length = " & Natural'Image (Sub_String'Length));
            --: Ada.Text_IO.Put_Line ("High              = " & Natural'Image (High));
            --: Ada.Text_IO.Put_Line ("High - NI'Len-1   = " & Natural'Image (High - New_Item'Length));
            --: Ada.Text_IO.Put_Line ("High - 1          = " & Natural'Image (High - 1));

            --  Exit Loop when we haven't found anything
            --
            exit Search_Next when Low = 0;

            if New_Item'Length > Sub_String'Last - High + 1
               or else New_Item /= Sub_String (High .. High + New_Item'Length - 1)
            then
               --
               --  We insert one character after the end of the found string when the new text does not allready
               --  follow. This can of course only happen when there are enouch characters behind the found string
               --  to contain the new text.
               --
               Unbounded.Insert
                  (Source   => Source,
                   Before   => High + Offset + 1 - Sub_String'First,
                   New_Item => New_Item);
               --
               --  Found one.
               --
               Count := Natural'Succ (Count);
            end if;
            --
            --  We set the offset to the end of the found string.
            --
            Offset := Offset + High;
         end Next_Sub_String;
      end loop Search_Next;
   end Append_All;

   ---
   --  Searches for first occurence of text "Search" and Inserts text "Insert" after the found text but only when
   --  "Insert" is not allready there.
   --
   --: @param Source    Wide_String to be changed
   --: @param Search    Wide_String we look for
   --: @param New_Item  Wide_String we want to insert
   --: @param Mapping   Search mapping
   procedure Append_First
      (Source   : in out Unbounded.Unbounded_Wide_String;
       Search   : in     Wide_String;
       New_Item : in     Wide_String;
       Mapping  : in     Maps.Wide_Character_Mapping := Maps.Identity)
   is
      Found : Boolean := Boolean'First;
   begin
      Append_First
         (Source   => Source,
          Search   => Search,
          New_Item => New_Item,
          Mapping  => Mapping,
          Found    => Found);
   end Append_First;

   ---
   --  Searches for first occurence of text "Search" and Inserts text "Insert" after the found text but only when
   --  "Insert" is not allready there.
   --
   --: @param Source    Wide_String to be changed
   --: @param Search    Wide_String we look for
   --: @param New_Item  Wide_String we want to insert
   --: @param Mapping   Search mapping
   --: @param Found     Count of replaces done
   procedure Append_First
      (Source   : in out Unbounded.Unbounded_Wide_String;
       Search   : in     Wide_String;
       New_Item : in     Wide_String;
       Mapping  : in     Maps.Wide_Character_Mapping := Maps.Identity;
       Found    :    out Boolean)
   is
      Low : constant Natural := Unbounded.Index
            (Source  => Source,
             Pattern => Search,
             Going   => Strings.Forward,
             Mapping => Mapping);
      High : constant Natural := Low + Search'Length;
      Len  : constant Natural := Unbounded.Length (Source);
   begin
      if Low = 0 then
         Found := False;
      elsif New_Item'Length >= Len - High or else New_Item /= Unbounded.Slice
               (Source => Source,
                Low    => High,
                High   => High + New_Item'Length - 1)
      then
         Unbounded.Insert
            (Source   => Source,
             Before   => High,
             New_Item => New_Item);
         Found := True;
      else
         Found := False;
      end if;
   end Append_First;

   ---
   --  Searches for last occurence of text "Search" and Inserts text "Insert" after the found text but only when
   --  "Insert" is not allready there.
   --
   --: @param Source    Wide_String to be changed
   --: @param Search    Wide_String we look for
   --: @param New_Item  Wide_String we want to insert
   --: @param Mapping   Search mapping
   procedure Append_Last
      (Source   : in out Unbounded.Unbounded_Wide_String;
       Search   : in     Wide_String;
       New_Item : in     Wide_String;
       Mapping  : in     Maps.Wide_Character_Mapping := Maps.Identity)
   is
      Found : Boolean := Boolean'First;
   begin
      Append_Last
         (Source   => Source,
          Search   => Search,
          New_Item => New_Item,
          Mapping  => Mapping,
          Found    => Found);
   end Append_Last;

   ---
   --  Searches for last occurence of text "Search" and Inserts text "Insert" after the found text but only when
   --  "Insert" is not allready there.
   --
   --: @param Source    Wide_String to be changed
   --: @param Search    Wide_String we look for
   --: @param New_Item  Wide_String we want to insert
   --: @param Mapping   Search mapping
   --: @param Found     Count of replaces done
   procedure Append_Last
      (Source   : in out Unbounded.Unbounded_Wide_String;
       Search   : in     Wide_String;
       New_Item : in     Wide_String;
       Mapping  : in     Maps.Wide_Character_Mapping := Maps.Identity;
       Found    :    out Boolean)
   is
      use Ada.Strings.Wide_Unbounded;

      Low : constant Natural := Index
            (Source  => Source,
             Pattern => Search,
             Going   => Strings.Backward,
             Mapping => Mapping);
      High : constant Natural := Low + Search'Length;
      Len  : constant Natural := Length (Source);
   begin
      if Low = 0 then
         Found := False;
      elsif New_Item'Length >= Len - High or else New_Item /= Slice
               (Source => Source,
                Low    => High,
                High   => High + New_Item'Length - 1)
      then
         Insert
            (Source   => Source,
             Before   => High,
             New_Item => New_Item);
         Found := True;
      else
         Found := False;
      end if;
   end Append_Last;

   ---
   --  Replace all Search with Replace
   --
   --: @param Source   Wide_String to be changed
   --: @param Search   Wide_String we look for
   --: @param Replace  Wide_String we want to have
   --: @param Mapping  Search mapping
   procedure Change_All
      (Source  : in out Unbounded.Unbounded_Wide_String;
       Search  : in     Wide_String;
       Replace : in     Wide_String;
       Mapping : in     Maps.Wide_Character_Mapping := Maps.Identity)
   is
      Count : Natural := Natural'First;
   begin
      Change_All
         (Source  => Source,
          Search  => Search,
          Replace => Replace,
          Mapping => Mapping,
          Count   => Count);
   end Change_All;

   ---
   --  Replace all Search with Replace and Count how often it was done.
   --
   --: @param Source   Wide_String to be changed
   --: @param Search   Wide_String we look for
   --: @param Replace  Wide_String we want to have
   --: @param Mapping  Search mapping
   --: @param Count    Count of replaces done
   procedure Change_All
      (Source  : in out Unbounded.Unbounded_Wide_String;
       Search  : in     Wide_String;
       Replace : in     Wide_String;
       Mapping : in     Maps.Wide_Character_Mapping := Maps.Identity;
       Count   :    out Natural)
   is
   begin
      Count := Natural'First;

      SearchAll :
      loop
         ReplaceOne :
         declare
            Found : Boolean;
         begin
            Change_First
               (Source  => Source,
                Search  => Search,
                Replace => Replace,
                Mapping => Mapping,
                Found   => Found);

            exit SearchAll when not Found;

            Count := Natural'Succ (Count);
         end ReplaceOne;
      end loop SearchAll;
   end Change_All;

   ---
   --  Replace First Search with Replace and return success flag.
   --
   --: @param Source   Wide_String to be changed
   --: @param Search   Wide_String we look for
   --: @param Replace  Wide_String we want to have
   --: @param Mapping  Search mapping
   --: @param Found    Count of replaces done
   procedure Change_First
      (Source  : in out Unbounded.Unbounded_Wide_String;
       Search  : in     Wide_String;
       Replace : in     Wide_String;
       Mapping : in     Maps.Wide_Character_Mapping := Maps.Identity;
       Found   :    out Boolean)
   is
      use Ada.Strings.Wide_Unbounded;

      Low : constant Natural := Index
            (Source  => Source,
             Pattern => Search,
             Going   => Strings.Forward,
             Mapping => Mapping);
   begin
      if Low /= 0 then
         ReplaceOne :
         declare
            High : constant Natural := Low + Search'Length - 1;
         begin
            Replace_Slice
               (Source => Source,
                Low    => Low,
                High   => High,
                By     => Replace);
         end ReplaceOne;
         Found := True;
      else
         Found := False;
      end if;
   end Change_First;

   ---
   --  Replace all Search with Replace.
   --
   --: @param Source   Wide_String to be changed
   --: @param Search   Wide_String we look for
   --: @param Replace  Wide_String we want to have
   --: @param Mapping  Search mapping
   procedure Change_First
      (Source  : in out Unbounded.Unbounded_Wide_String;
       Search  : in     Wide_String;
       Replace : in     Wide_String;
       Mapping : in     Maps.Wide_Character_Mapping := Maps.Identity)
   is
      Found : Boolean := False;
   begin
      Change_First
         (Source  => Source,
          Search  => Search,
          Replace => Replace,
          Mapping => Mapping,
          Found   => Found);
   end Change_First;

   ---
   --  Replace Last Search with Replace
   --
   --: @param Source   Search mapping
   --: @param Search   Wide_String we want to have
   --: @param Replace  Wide_String we look for
   --: @param Mapping  Wide_String to be changed
   procedure Change_Last
      (Source  : in out Unbounded.Unbounded_Wide_String;
       Search  : in     Wide_String;
       Replace : in     Wide_String;
       Mapping : in     Maps.Wide_Character_Mapping := Maps.Identity)
   is
      Found : Boolean := False;
   begin
      Change_First
         (Source  => Source,
          Search  => Search,
          Replace => Replace,
          Mapping => Mapping,
          Found   => Found);
   end Change_Last;

   ---
   --  Replace Last Search with Replace and return success flag.
   --
   --: @param Source   Wide_String to be changed
   --: @param Search   Wide_String we look for
   --: @param Replace  Wide_String we want to have
   --: @param Mapping  Search mapping
   --: @param Found    Count of replaces done
   procedure Change_Last
      (Source  : in out Unbounded.Unbounded_Wide_String;
       Search  : in     Wide_String;
       Replace : in     Wide_String;
       Mapping : in     Maps.Wide_Character_Mapping := Maps.Identity;
       Found   :    out Boolean)
   is
      use Ada.Strings.Wide_Unbounded;

      Low : constant Natural := Index
            (Source  => Source,
             Pattern => Search,
             Going   => Strings.Backward,
             Mapping => Mapping);
   begin
      if Low /= 0 then
         ReplaceOne :
         declare
            High : constant Natural := Low + Search'Length - 1;
         begin
            Replace_Slice
               (Source => Source,
                Low    => Low,
                High   => High,
                By     => Replace);
         end ReplaceOne;
         Found := True;
      else
         Found := False;
      end if;
   end Change_Last;

   ---
   --  Return the end-of-field position in Data after "Starting_Index", assuming that fields are separated by the
   --  Field_Separator. If there's no Field_Separator, return the end of the Data.
   --
   --: @param Source            Wide_String to search in
   --: @param Field_Separator   Field seperator.
   --: @param Starting_At       Start search at.
   function Field_End
      (Source          : in Wide_String;
       Field_Separator : in Wide_Character;
       Starting_At     :    Positive)
       return Natural
   is
   begin
      for I in Starting_At .. Source'Last loop
         if Source (I) = Field_Separator then
            return I - 1;
         end if;
      end loop;
      return Source'Last;
   end Field_End;

   ---
   --  Hash function for booch components.
   --
   --: @param Key     Wide_String to calculate a hash value form
   --: @return  hash value
   function Hash (Key : Wide_String) return Natural is
      Retval : Natural          := Natural'First;
      Len    : constant Natural := Key'Length;
   begin
      if Len /= 0 then
         Retval :=
            (Wide_Character'Pos (Key (Key'First)) + Wide_Character'Pos (Key (Len)) +
             Wide_Character'Pos (Key (Len / 2)));
      end if;
      return Retval;
   end Hash;

   ---
   --  Hash function for booch components.
   --
   --: @param Key     Wide_String to calculate a hash value form
   --: @return  hash value
   function Hash (Key : Wide_String) return Ada.Containers.Hash_Type is
      use type Ada.Containers.Hash_Type;

      Retval : Ada.Containers.Hash_Type := Ada.Containers.Hash_Type'First;
      Len    : constant Natural         := Key'Length;
   begin
      if Len /= 0 then
         Retval :=
            (Wide_Character'Pos (Key (Key'First)) + Wide_Character'Pos (Key (Len)) +
             Wide_Character'Pos (Key (Len / 2)));
      end if;
      return Retval;
   end Hash;

   ---
   --  Hash function for booch components.
   --
   --: @param Key     Wide_String to calculate a hash value form
   --: @return  hash value
   function Hash (Key : Unbounded.Unbounded_Wide_String) return Natural is
      use Ada.Strings.Wide_Unbounded;

      Retval : Natural          := Natural'First;
      Len    : constant Natural := Length (Key);
   begin
      if Len /= 0 then
         Retval :=
            (Wide_Character'Pos (Element (Key, 1)) + Wide_Character'Pos (Element (Key, Len)) +
             Wide_Character'Pos (Element (Key, Len / 2)));
      end if;
      return Retval;
   end Hash;

   ---
   --
   --: @param Key     Wide_String to calculate a hash value form
   --: @return  hash value
   function Hash (Key : Unbounded.Unbounded_Wide_String) return Ada.Containers.Hash_Type is
      use type Ada.Containers.Hash_Type;
      use Ada.Strings.Wide_Unbounded;

      Retval : Ada.Containers.Hash_Type := Ada.Containers.Hash_Type'First;
      Len    : constant Natural         := Length (Key);
   begin
      if Len /= 0 then
         Retval :=
            (Wide_Character'Pos (Element (Key, 1)) + Wide_Character'Pos (Element (Key, Len)) +
             Wide_Character'Pos (Element (Key, Len / 2)));
      end if;
      return Retval;
   end Hash;

   ---
   --  Searches for all occurences of text "Search" and Inserts text "Insert" bevore when "Insert" is there.
   --
   --: @param Source    Wide_String to be changed
   --: @param Search    Wide_String we look for
   --: @param New_Item  Wide_String we want to insert
   --: @param Mapping   Search mapping
   procedure Insert_All
      (Source   : in out Unbounded.Unbounded_Wide_String;
       Search   : in     Wide_String;
       New_Item : in     Wide_String;
       Mapping  : in     Maps.Wide_Character_Mapping := Maps.Identity)
   is
      Count : Natural := Natural'First;
   begin
      Insert_All
         (Source   => Source,
          Search   => Search,
          New_Item => New_Item,
          Mapping  => Mapping,
          Count    => Count);
   end Insert_All;

   ---
   --  Searches for all occurences of text "Search" and Inserts text "Insert" bevore when "Insert" is there.
   --
   --: @param Source    Wide_String to be changed
   --: @param Search    Wide_String we look for
   --: @param New_Item  Wide_String we want to insert
   --: @param Mapping   Search mapping
   --: @param Count     Count of replaces done
   procedure Insert_All
      (Source   : in out Unbounded.Unbounded_Wide_String;
       Search   : in     Wide_String;
       New_Item : in     Wide_String;
       Mapping  : in     Maps.Wide_Character_Mapping := Maps.Identity;
       Count    :    out Natural)
   is
      use Ada.Strings.Wide_Unbounded;

      --
      --  Offset from which we start. 0 means search from the first character
      --
      Offset : Natural := Natural'First;
      --
      --  Lenght of the full string
      --
      Len : constant Natural := Length (Source);
   begin
      --
      --  nothing found yet
      --
      Count := Natural'First;

      Search_Next :
      loop
         --
         --  Last Found Item was at the end of the Wide_String
         --
         exit Search_Next when Len = 0 or else Offset >= Len;

         Next_Sub_String :
         declare
            --
            --  We slice from the Offset on to the end. Here I have learned an important new Ada lessons: Strings don't
            --  have a 'First of 1.
            --
            Sub_String : constant Wide_String := Slice
                  (Source => Source,
                   Low    => Offset + 1,
                   High   => Len);
            --
            --  We search for Pattern
            --
            High : constant Natural := Fixed.Index
                  (Source  => Sub_String,
                   Pattern => Search,
                   Going   => Strings.Forward,
                   Mapping => Mapping);
         begin
            --
            --  Exit Loop when we havn't found anything
            --
            exit Search_Next when High = 0;

            --: Ada.Text_IO.Put_Line ("New_Item'Length   = " & Natural'Image (New_Item'Length));
            --: Ada.Text_IO.Put_Line ("Sub_String'Length = " & Natural'Image (Sub_String'Length));
            --: Ada.Text_IO.Put_Line ("High              = " & Natural'Image (High));
            --: Ada.Text_IO.Put_Line ("High - NI'Len-1   = " & Natural'Image (High - New_Item'Length));
            --: Ada.Text_IO.Put_Line ("High - 1          = " & Natural'Image (High - 1));

            if New_Item'Length > High - Sub_String'First
               or else New_Item /= Sub_String (High - New_Item'Length .. High - 1)
            then
               --
               --  We insert one character bevore the beginning of the found string when the new text does not allready
               --  follow. This can of course only happen when there are enouch characters behind the found string to
               --  contain the new text.
               --
               Insert
                  (Source   => Source,
                   Before   => High + Offset - Sub_String'First + 1,
                   New_Item => New_Item);
               --
               --  Next Element
               --
               Count := Natural'Succ (Count);
            end if;
            --
            --  We set the offset to the end of the found string.
            --
            Offset := Offset + High + Search'Length;
         end Next_Sub_String;
      end loop Search_Next;
   end Insert_All;

   ---
   --  Searches for first occurence of text "Search" and Inserts text "Insert" bevore when "Insert" is there.
   --
   --: @param Source    Wide_String to be changed
   --: @param Search    Wide_String we look for
   --: @param New_Item  Wide_String we want to insert
   --: @param Mapping   Search mapping
   procedure Insert_First
      (Source   : in out Unbounded.Unbounded_Wide_String;
       Search   : in     Wide_String;
       New_Item : in     Wide_String;
       Mapping  : in     Maps.Wide_Character_Mapping := Maps.Identity)
   is
      Found : Boolean := Boolean'First;
   begin
      Insert_Last
         (Source   => Source,
          Search   => Search,
          New_Item => New_Item,
          Mapping  => Mapping,
          Found    => Found);
   end Insert_First;

   ---
   --  Searches for last occurence of text "Search" and Inserts text "Insert" bevore when "Insert" is there.
   --
   --: @param Source    Wide_String to be changed
   --: @param Search    Wide_String we look for
   --: @param New_Item  Wide_String we want to insert
   --: @param Mapping   Search mapping
   --: @param Found     Count of replaces done
   procedure Insert_First
      (Source   : in out Unbounded.Unbounded_Wide_String;
       Search   : in     Wide_String;
       New_Item : in     Wide_String;
       Mapping  : in     Maps.Wide_Character_Mapping := Maps.Identity;
       Found    :    out Boolean)
   is
      use Ada.Strings.Wide_Unbounded;

      High : constant Natural := Index
            (Source  => Source,
             Pattern => Search,
             Going   => Strings.Forward,
             Mapping => Mapping);
   begin
      if High = 0 then
         Found := False;
      elsif New_Item'Length >= High or else New_Item /= Slice
               (Source => Source,
                Low    => High - New_Item'Length,
                High   => High - 1)
      then
         Insert
            (Source   => Source,
             Before   => High,
             New_Item => New_Item);
         Found := True;
      else
         Found := False;
      end if;
   end Insert_First;

   ---
   --  Searches for last occurence of text "Search" and Inserts text "Insert" bevore when "Insert" is there.
   --
   --: @param Source    Wide_String to be changed
   --: @param Search    Wide_String we look for
   --: @param New_Item  Wide_String we want to insert
   --: @param Mapping   Search mapping
   procedure Insert_Last
      (Source   : in out Unbounded.Unbounded_Wide_String;
       Search   : in     Wide_String;
       New_Item : in     Wide_String;
       Mapping  : in     Maps.Wide_Character_Mapping := Maps.Identity)
   is
      Found : Boolean := Boolean'First;
   begin
      Insert_Last
         (Source   => Source,
          Search   => Search,
          New_Item => New_Item,
          Mapping  => Mapping,
          Found    => Found);
   end Insert_Last;

   ---
   --  Searches for last occurence of text "Search" and Inserts text "Insert" bevore when "Insert" is there.
   --
   --: @param Source    Wide_String to be changed
   --: @param Search    Wide_String we look for
   --: @param New_Item  Wide_String we want to insert
   --: @param Mapping   Search mapping
   --: @param Found     Count of replaces done
   procedure Insert_Last
      (Source   : in out Unbounded.Unbounded_Wide_String;
       Search   : in     Wide_String;
       New_Item : in     Wide_String;
       Mapping  : in     Maps.Wide_Character_Mapping := Maps.Identity;
       Found    :    out Boolean)
   is
      use Ada.Strings.Wide_Unbounded;

      High : constant Natural := Index
            (Source  => Source,
             Pattern => Search,
             Going   => Strings.Backward,
             Mapping => Mapping);
   begin
      if High = 0 then
         Found := False;
      elsif New_Item'Length >= High or else New_Item /= Slice
               (Source => Source,
                Low    => High - New_Item'Length,
                High   => High - 1)
      then
         Insert
            (Source   => Source,
             Before   => High,
             New_Item => New_Item);
         Found := True;
      else
         Found := False;
      end if;
   end Insert_Last;

   function Value (Image : Ada.Strings.Wide_Unbounded.Unbounded_Wide_String) return Integer is
      Last   : Positive;
      Retval : Integer;
   begin
      Ada.Integer_Wide_Text_IO.Get
         (From => Unbounded.To_Wide_String (Image),
          Item => Retval,
          Last => Last);

      return Retval;
   end Value;
end AdaCL.Wide_Strings;

---------------------------------------------------------------- {{{ ----------
--: vim: set textwidth=0 nowrap tabstop=8 shiftwidth=3 softtabstop=3 expandtab :
--: vim: set filetype=ada fileencoding=utf-8 fileformat=unix foldmethod=expr :
--: vim: set spell spelllang=en_gb