File: util-strings.adb

package info (click to toggle)
adabrowse 4.0.2-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,316 kB
  • ctags: 253
  • sloc: ada: 29,761; makefile: 150; ansic: 4
file content (835 lines) | stat: -rw-r--r-- 26,879 bytes parent folder | download | duplicates (9)
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
832
833
834
835
-------------------------------------------------------------------------------
--
--  <STRONG>Copyright &copy; 2001, 2002 by Thomas Wolf.</STRONG>
--  <BLOCKQUOTE>
--    This piece of software is free software; you can redistribute it and/or
--    modify it under the terms of the  GNU General Public License as published
--    by the Free Software  Foundation; either version 2, or (at your option)
--    any later version. This software is distributed in the hope that it will
--    be useful, but <EM>without any warranty</EM>; without even the implied
--    warranty of <EM>merchantability or fitness for a particular purpose.</EM>
--    See the GNU General Public License for  more details. You should have
--    received a copy of the GNU General Public License with this distribution,
--    see file "<A HREF="GPL.txt">GPL.txt</A>". If not, write to the Free
--    Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
--    USA.
--  </BLOCKQUOTE>
--  <BLOCKQUOTE>
--    As a special exception from the GPL, if other files instantiate generics
--    from this unit, or you link this unit with other files to produce an
--    executable, this unit does not by itself cause the resulting executable
--    to be covered by the GPL. This exception does not however invalidate any
--    other reasons why the executable file might be covered by the GPL.
--  </BLOCKQUOTE>
--
--  <AUTHOR>
--    Thomas Wolf  (TW) <E_MAIL>
--  </AUTHOR>
--
--  <PURPOSE>
--    Various string utilities not provided in the standard library. Some
--    of these also are repeated here, so that one can get all one needs
--    with a single "@with@".
--  </PURPOSE>
--
--  <NOT_TASK_SAFE>
--
--  <NO_STORAGE>
--
--  <HISTORY>
--    01-MAR-2002   TW  Initial version.
--    14-MAR-2002   TW  Added 'Count'.
--    18-MAR-2002   TW  Added 'Letters'.
--    02-MAY-2002   TW  Added 'Identifier'.
--    02-AUG-2002   TW  Added 'Replace'; corrected bug in 'To_Mixed'.
--    06-AUG-2002   TW  Replaced the body of 'Replace' with a non-recursive
--                      algorithm. The recursive one got slow on large strings
--                      with many replacements.
--    07-AUG-2002   TW  Added 'First_Index', 'Last_Index', and 'Count' with
--                      a string pattern.
--    12-OCT-2002   TW  Added 'Next_Blank'.
--    18-JUN-2003   TW  Added 'Equal'.
--    07-JUL-2003   TW  Added 'Cardinality'.
--  </HISTORY>
-------------------------------------------------------------------------------

pragma License (Modified_GPL);

with Ada.Exceptions;

package body Util.Strings is

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

   package ASF renames Ada.Strings.Fixed;

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

   function To_Mixed (S : in String) return String
   is
      Result : String (S'Range);
      Prev   : Character := '_';
   begin
      for I in Result'Range loop
         if Prev = '_' or else Prev = '.' or else
            Ada.Strings.Maps.Is_In (Prev, Blanks)
         then
            Result (I) := To_Upper (S (I));
         else
            Result (I) := To_Lower (S (I));
         end if;
         Prev := S (I);
      end loop;
      return Result;
   end To_Mixed;

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

   function Cardinality
     (Set : in Ada.Strings.Maps.Character_Set)
     return Natural
   is
      N : Natural := 0;
   begin
      for Ch in Character loop
         if Ada.Strings.Maps.Is_In (Ch, Set) then N := N + 1; end if;
      end loop;
      return N;
   end Cardinality;

   --  Returns the number of characters in @Set@.

   function Equal
     (Left, Right : in String)
     return Boolean
   is
   begin
      return To_Lower (Left) = To_Lower (Right);
   end Equal;

   function First_Index
     (Src : in String;
      Ch  : in Character)
     return Natural
   is
   begin
      for I in Src'Range loop
         if Src (I) = Ch then return I; end if;
      end loop;
      return 0;
   end First_Index;

   function Last_Index
     (Src : in String;
      Ch  : in Character)
     return Natural
   is
   begin
      for I in reverse Src'Range loop
         if Src (I) = Ch then return I; end if;
      end loop;
      return 0;
   end Last_Index;

   function First_Index
     (Source  : in String;
      Pattern : in String)
     return Natural
   is
      Length : constant Natural := Pattern'Length;
   begin
      if Length = 0 then raise Ada.Strings.Pattern_Error; end if;
      for I in Source'First .. Source'Last - Length + 1 loop
         if Source (I .. I + Length - 1) = Pattern then
            return I;
         end if;
      end loop;
      return 0;
   end First_Index;

   function Last_Index
     (Source   : in String;
      Pattern  : in String)
     return Natural
   is
      Length : constant Natural := Pattern'Length;
   begin
      if Length = 0 then raise Ada.Strings.Pattern_Error; end if;
      for I in reverse Source'First .. Source'Last - Length + 1 loop
         if Source (I .. I + Length - 1) = Pattern then
            return I;
         end if;
      end loop;
      return 0;
   end Last_Index;

   function Index
     (Src : in String;
      Ch  : in Character;
      Dir : in Ada.Strings.Direction := Forward)
     return Natural
   is
      use type Ada.Strings.Direction;
   begin
      if Dir = Forward then
         return First_Index (Src, Ch);
      else
         return Last_Index (Src, Ch);
      end if;
   end Index;

   function Index
     (Source  : in String;
      Pattern : in String;
      Dir     : in Ada.Strings.Direction := Forward)
     return Natural
   is
      use type Ada.Strings.Direction;
   begin
      if Dir = Forward then
         return First_Index (Source, Pattern);
      else
         return Last_Index (Source, Pattern);
      end if;
   end Index;

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

   function Count
     (Src : in String;
      Ch  : in Character)
     return Natural
   is
      N : Natural := 0;
   begin
      for I in Src'Range loop
         if Src (I) = Ch then N := N + 1; end if;
      end loop;
      return N;
   end Count;

   function Count
     (Source  : in String;
      Pattern : in String)
     return Natural
   is
      Length : constant Natural := Pattern'Length;
   begin
      if Length = 0 then raise Ada.Strings.Pattern_Error; end if;
      if Length > Source'Length then return 0; end if;
      declare
         Stop : constant Natural := Source'Last - Length + 1;
         I    :          Natural := Source'First;
         N    :          Natural := 0;
      begin
         while I <= Stop loop
            if Source (I .. I + Length - 1) = Pattern then
               N := N + 1; I := I + Length;
            else
               I := I + 1;
            end if;
         end loop;
         return N;
      end;
   end Count;

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

   function Is_Blank
     (Ch : in Character)
     return Boolean
   is
   begin
      return Ada.Strings.Maps.Is_In (Ch, Blanks);
   end Is_Blank;

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

   function Is_In
     (Set : in Ada.Strings.Maps.Character_Set;
      Ch  : in Character)
     return Boolean
   is
   begin
      return Ada.Strings.Maps.Is_In (Ch, Set);
   end Is_In;

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

   function Trim
     (S    : in String;
      Side : in Ada.Strings.Trim_End := Both)
     return String
   is
   begin
      case Side is
         when Both =>
            return ASF.Trim (S, Blanks, Blanks);
         when Left =>
            return ASF.Trim (S, Blanks, Null_Set);
         when Right =>
            return ASF.Trim (S, Null_Set, Blanks);
      end case;
   end Trim;

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

   procedure Get_String
     (S        : in     String;
      From, To :    out Natural;
      Delim    : in     Character := '"';
      Escape   : in     Character := No_Escape)
   is
   begin
      From := S'First; To := 0;
      while From <= S'Last and then S (From) /= Delim loop
         From := From + 1;
      end loop;
      if From = S'Last then
         return;
      elsif From > S'Last then
         From := 0; return;
      end if;
      To := Skip_String (S (From .. S'Last), Delim, Escape);
   end Get_String;

   function In_String
     (S      : in String;
      Delim  : in Character := '"';
      Escape : in Character := No_Escape)
     return Boolean
   is
      I, From, To : Natural;
   begin
      I := S'First;
      loop
         Get_String (S (I .. S'Last), From, To, Delim, Escape);
         exit when From = 0 or else To = 0;
         I := To + 1;
      end loop;
      return From /= 0 and then To = 0;
   end In_String;

   function Skip_String
     (S      : in String;
      Delim  : in Character := '"';
      Escape : in Character := No_Escape)
     return Natural
   is
      From : Natural;
      To   : Natural := 0;
   begin
      if S'Last < S'First then return 0; end if;
      From := S'First + 1;
      if Escape = No_Escape then
         To := First_Index (S (From .. S'Last), Delim);
      else
         To := From;
         if Escape /= Delim then
            declare
               Escaped : Boolean := False;
            begin
               --  Be careful: a double occurrence of the escape is a literal
               --  escape!
               while To <= S'Last loop
                  if S (To) = Escape then
                     Escaped := not Escaped;
                  elsif S (To) = Delim and then not Escaped then
                     return To;
                  else
                     Escaped := False;
                  end if;
                  To := To + 1;
               end loop;
            end;
         else
            --  Delimiters are escaped by doubling!
            while To < S'Last loop
               if S (To) = Delim then
                  if S (To + 1) /= Delim then
                     return To;
                  end if;
                  To := To + 2;
               else
                  To := To + 1;
               end if;
            end loop;
            if To <= S'Last and then S (To) = Delim then return To; end if;
         end if;
         To := 0;
      end if;
      return To;
   end Skip_String;

   function Quote
     (S      : in String;
      Delim  : in Character;
      Escape : in Character)
     return String
   is
   begin
      if Escape = No_Escape then
         return S;
      else
         declare
            Result : String (1 .. S'Length * 2);
            J      : Natural := 1;
         begin
            if Escape = Delim then
               for I in S'Range loop
                  if S (I) = Delim then
                     Result (J) := Escape; J := J + 1;
                  end if;
                  Result (J) := S (I); J := J + 1;
               end loop;
            else
               for I in S'Range loop
                  if S (I) = Delim or else S (I) = Escape then
                     Result (J) := Escape; J := J + 1;
                  end if;
                  Result (J) := S (I); J := J + 1;
               end loop;
            end if;
            return Result (1 .. J - 1);
         end;
      end if;
   end Quote;

   function Unquote
     (S      : in String;
      Delim  : in Character;
      Escape : in Character)
     return String
   is
      Result : String (1 .. S'Length);
      I      : Natural := S'First;
      J      : Natural := Result'First;
   begin
      if Escape = Delim then
         while I <= S'Last loop
            if S (I) = Delim and then
               I < S'Last and then S (I + 1) = Delim
            then
               I := I + 1;
            end if;
            Result (J) := S (I);
            J := J + 1; I := I + 1;
         end loop;
      else
         while I <= S'Last loop
            if S (I) = Escape and then
               I < S'Last and then
               (S (I + 1) = Escape or else S (I + 1) = Delim)
            then
               I := I + 1;
            end if;
            Result (J) := S (I);
            J := J + 1; I := I + 1;
         end loop;
      end if;
      return Result (Result'First .. J - 1);
   end Unquote;

   function Unquote_All
     (S      : in String;
      Quotes : in Ada.Strings.Maps.Character_Set;
      Escape : in Character := No_Escape)
     return String
   is
      I, J : Natural;
      Ch   : Character;
   begin
      I := ASF.Index (S, Quotes);
      if I = 0 then return S; end if; --  No string found
      if Escape = No_Escape then
         Ch := S (I);
      else
         Ch := Escape;
      end if;
      J := Skip_String (S (I .. S'Last), S (I), Ch);
      if J = 0 then
         --  Unterminated string!
         return S;
      end if;
      return S (S'First .. I) &
             Unquote (S (I + 1 .. J - 1), S (I), Ch) & S (I) &
             Unquote_All (S (J + 1 .. S'Last), Quotes, Escape);
   end Unquote_All;

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

   function Is_Prefix
     (Source : in String;
      Prefix : in String)
     return Boolean
   is
   begin
      return
        Source (Source'First ..
                Natural'Min (Source'Last,
                             Source'First + Prefix'Length - 1)) =
        Prefix;
   end Is_Prefix;

   function Is_Suffix
     (Source : in String;
      Suffix : in String)
     return Boolean
   is
   begin
      if Suffix'Length > Source'Length then return False; end if;
      return
        Source (Source'Last - Suffix'Length + 1 .. Source'Last) = Suffix;
   end Is_Suffix;

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

   ID_Chars : constant Ada.Strings.Maps.Character_Set :=
     Ada.Strings.Maps."or" (Letters, Ada.Strings.Maps.To_Set ("0123456789_"));

   function Identifier
     (S : in String)
     return Natural
   is
   begin
      if S'Last < S'First or else not Is_In (Letters, S (S'First)) then
         return 0;
      end if;
      declare
         I : Natural := S'First + 1;
      begin
         while I <= S'Last and then Is_In (ID_Chars, S (I)) loop
            I := I + 1;
         end loop;
         return I - 1;
      end;
   end Identifier;

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

   function Next_Non_Blank
     (S : in String)
     return Natural
   is
   begin
      for I in S'Range loop
         if not Is_Blank (S (I)) then return I; end if;
      end loop;
      return 0;
   end Next_Non_Blank;

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

   function Next_Blank
     (S : in String)
     return Natural
   is
   begin
      for I in S'Range loop
         if Is_Blank (S (I)) then return I; end if;
      end loop;
      return 0;
   end Next_Blank;

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

   function Replace
     (Source : in String;
      What   : in String;
      By     : in String)
     return String
   is
      --  Speed optimized for both small and large strings, with few and many
      --  replacements.
      N : constant Natural := Count (Source, What);
   begin
      if N = 0 then return Source; end if;
      declare
         By_Length   : constant Natural := By'Length;
         What_Length : constant Natural := What'Length;
         Result      : String (1 ..
                               Source'Length - N * (What_Length - By_Length));
         --  Now if only the compiler was smart enough to allocate 'Result'
         --  directly on the secondary stack instead of using 'alloca'...
         --  Note that 'Result' has exactly the length it needs!
         I, J, K     : Natural;
      begin
         J := 1;
         I := Source'First;
         while I <= Source'Last loop
            K := First_Index (Source (I .. Source'Last), What);
            if K = 0 then
               Result (J .. Result'Last) := Source (I .. Source'Last);
               --  J := Result'Last + 1;
               I := Source'Last + 1;
            else
               Result (J .. J + (K - I) - 1) := Source (I .. K - 1);
               J := J + (K - I);
               Result (J .. J + By_Length - 1) := By;
               J := J + By_Length;
               I := K + What_Length;
            end if;
         end loop;
         return Result;
      end;
   end Replace;

   ----------------------------------------------------------------------------
   --  Wildcard string matching

   package ASM renames Ada.Strings.Maps;

   procedure Raise_Unterminated_Set
     (Pattern : in String)
   is
   begin
      Ada.Exceptions.Raise_Exception
        (Illegal_Pattern'Identity,
         "Unterminated character set: '" & Pattern & "'");
   end Raise_Unterminated_Set;

   procedure Raise_Backslash_At_End
   is
   begin
      Ada.Exceptions.Raise_Exception
        (Illegal_Pattern'Identity,
         "'\' at end of pattern (must be followed by a character)");
   end Raise_Backslash_At_End;

   --  generic...
   function Wildcard_Match
     (Pattern      : in String;
      Text         : in String)
     return Boolean
   is

      procedure Parse_Set
        (Pattern      : in     String;
         Set          :    out ASM.Character_Set;
         Stop         :    out Natural;
         Inverted     :    out Boolean)
      is
         use type ASM.Character_Set;

         Lower : Character;
         Start : Natural;
      begin
         Set := Null_Set;
         if Pattern'Last <= Pattern'First + 1 then
            --  Even if it is "[]", it is unterminated!
            Raise_Unterminated_Set (Pattern);
         end if;
         Start := Pattern'First + 1;
         Inverted :=
           Set_Inverter /= No_Set_Inverter and then
           Pattern (Start) = Set_Inverter;
         if Inverted then Start := Start + 1; end if;
         --  Go looking for the end of the set.
         if Start <= Pattern'Last and then Pattern (Start) = ']' then
            --  This ']' is to be part of the set!
            Stop := Start + 1;
         else
            Stop := Start;
         end if;
         Stop := First_Index (Pattern (Stop .. Pattern'Last), ']');
         if Stop <= Start then
            Raise_Unterminated_Set (Pattern);
         end if;
         --  The set is defined by 'Pattern (Start .. Stop-1)'
         while Start < Stop loop
            Lower := Pattern (Start);
            Start := Start + 1;
            if Pattern (Start) = '-' and then (Start + 1 < Stop) then
               --  We have a range.
               Start := Start + 1;
               if Pattern (Start) < Lower then
                  Ada.Exceptions.Raise_Exception
                    (Illegal_Pattern'Identity,
                     "Upper bound of range in character set is smaller " &
                     "than lower bound: '" &
                     Lower & '-' & Pattern (Start) & "'");
               end if;
               Set :=
                 Set or
                 ASM.To_Set (ASM.Character_Range'(Lower, Pattern (Start)));
               Start := Start + 1;
            else
               Set :=
                 Set or ASM.To_Set (ASM.Character_Range'(Lower, Lower));
            end if;
         end loop;
      end Parse_Set;

      Match_Impossible : exception;

      function Internal_Match
        (Pattern      : in String;
         Text         : in String)
        return Boolean
      is
         Pattern_I : Natural := Pattern'First;
         Text_I    : Natural := Text'First;
         Switch    : Character;
      begin
         while Text_I <= Text'Last loop
            if (Pattern_I > Pattern'Last) then
               --  The text is not yet exhausted, hence it can't match!
               return False;
            end if;
            Switch := Pattern (Pattern_I);
            if Has_Escape and then Switch = '\' then
               --  Literal match with next pattern character
               if Pattern_I = Pattern'Last then
                  Raise_Backslash_At_End;
               end if;
               Pattern_I := Pattern_I + 1;
               if Pattern (Pattern_I) /= Text (Text_I) then
                  return False;
               end if;
               Pattern_I := Pattern_I + 1;
               Text_I    := Text_I + 1;
            elsif Has_Char_Set and then Switch = '[' then
               --  Character set
               declare
                  Set      : ASM.Character_Set;
                  Inverted : Boolean;
               begin
                  Parse_Set (Pattern (Pattern_I .. Pattern'Last),
                             Set, Pattern_I, Inverted);
                  if Inverted = ASM.Is_In (Text (Text_I), Set) then
                     return False;
                  end if;
               end;
               Pattern_I := Pattern_I + 1;
               Text_I    := Text_I + 1;
            elsif Switch = Any_One then
               if Zero_Or_One then
                  --  Null matches are OK!
                  declare
                     Result : Boolean;
                  begin
                     --  First try the null match, i.e. advance the
                     --  pattern, but not the text.
                     Result :=
                       Internal_Match
                         (Pattern (Pattern_I + 1 .. Pattern'Last),
                          Text (Text_I .. Text'Last));
                     if Result then return Result; end if;
                     --  No match: try the any-one match: advance both.
                  end; --  block
               end if;
               --  Match any character.
               Pattern_I := Pattern_I + 1;
               Text_I    := Text_I + 1;
            elsif Switch = Zero_Or_More then
               --  First, collate sequences of '*'s (and maybe '?'s).
               while Pattern_I < Pattern'Last and then
                     (Pattern (Pattern_I + 1) = Zero_Or_More or
                      (Zero_Or_One and then
                       Pattern (Pattern_I + 1) = Any_One))
               loop
                  Pattern_I := Pattern_I + 1;
               end loop;
               --  '*' at the end of the pattern matches anything:
               if Pattern_I >= Pattern'Last then return True; end if;
               --  Find next possible match, if any.
               declare
                  Next : Natural;
                  P_I  : constant Natural := Pattern_I + 1;
               begin
                  if Pattern (P_I) = Any_One then
                     --  Can match any one character: don't skip.
                     Next := Text_I;
                  elsif Has_Char_Set and then Pattern (P_I) = '[' then
                     --  Skip to the next character matching the set
                     declare
                        Set      : ASM.Character_Set;
                        Dummy    : Natural;
                        Inverted : Boolean;
                     begin
                        Parse_Set (Pattern (P_I .. Pattern'Last),
                                   Set, Dummy, Inverted);
                        if Inverted then
                           Next := ASF.Index (Text (Text_I .. Text'Last),
                                              Set, Ada.Strings.Outside);
                        else
                           Next := ASF.Index (Text (Text_I .. Text'Last),
                                              Set, Ada.Strings.Inside);
                        end if;
                     end; --  block
                  else
                     --  Skip ahead to the next matching character
                     declare
                        Ch : Character := Pattern (P_I);
                     begin
                        if Has_Escape and then Ch = '\' then
                           if P_I = Pattern'Last then
                              Raise_Backslash_At_End;
                           end if;
                           Ch := Pattern (P_I + 1);
                        end if;
                        Next :=
                          First_Index (Text (Text_I .. Text'Last), Ch);
                     end;
                  end if;
                  if Next = 0 then raise Match_Impossible; end if;
                  --  No match was possible, so abort the whole thing.
                  --  (This can be done safely because any other match
                  --  for any previous '*' could only require an even
                  --  later match of the character following the current
                  --  '*'-sequence - but we know that this is impossible.
                  --  Therefore, any other matching for previous '*'s is
                  --  bound to fail and hence we may give up. Without
                  --  this, the algorithm would have quadratic behaviour
                  --  in some failure cases, e.g. the "-adobe" negative
                  --  case in the test program.)
                  declare
                     Result : Boolean;
                  begin
                     Result :=
                       Internal_Match (Pattern (P_I .. Pattern'Last),
                                       Text (Next .. Text'Last));
                     --  This recursion is limited by the number of '*'
                     --  sequences in the pattern.
                     if Result then return Result; end if;
                  end;
                  --  This star couldn't match this minimal text
                  --  sequence; try to extend it. Note: 'Pattern_I' has
                  --  not been incremented here.
                  Text_I := Next + 1;
               end; --  block
            else
               --  Literal match between the pattern and the text
               if Pattern (Pattern_I) /= Text (Text_I) then
                  return False;
               end if;
               Pattern_I := Pattern_I + 1;
               Text_I    := Text_I + 1;
            end if;
         end loop;
         --  Skip remaining '*'s (and maybe '?'s) at the end of the pattern.
         while Pattern_I <= Pattern'Last and then
               (Pattern (Pattern_I) = Zero_Or_More or
                (Zero_Or_One and then Pattern (Pattern_I) = Any_One))
         loop
            Pattern_I := Pattern_I + 1;
         end loop;
         return Pattern_I > Pattern'Last;
      end Internal_Match;

   begin --  Match_G
      return Internal_Match (Pattern, Text);
   exception
      when Match_Impossible =>
         return False;
   end Wildcard_Match;

   function Instance is new Wildcard_Match;

   function Match
     (Pattern : in String;
      Text    : in String)
     return Boolean
     renames Instance;

end Util.Strings;