Debian Codegraph @

gcc/ada/libgnat/a-strsea.ads

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- A D A . S T R I N G S . S E A R C H --
6 -- --
7 -- S p e c --
8 -- --
9 -- Copyright (C) 1992-2025, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- 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- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
31
32 -- This package contains the search functions from Ada.Strings.Fixed. They
33 -- are separated out because they are shared by Ada.Strings.Bounded and
34 -- Ada.Strings.Unbounded, and we don't want to drag in other irrelevant stuff
35 -- from Ada.Strings.Fixed when using the other two packages. Although user
36 -- programs should access these subprograms via one of the standard string
37 -- packages, we do not make this a private package, since ghost function
38 -- Match is used in the contracts of the standard string packages.
39
40 -- Preconditions in this unit are meant for analysis only, not for run-time
41 -- checking, so that the expected exceptions are raised. This is enforced
42 -- by setting the corresponding assertion policy to Ignore. Postconditions,
43 -- contract cases and ghost code should not be executed at runtime as well,
44 -- in order not to slow down the execution of these functions.
45
46 pragma Assertion_Policy (Pre => Ignore,
47 Post => Ignore,
48 Contract_Cases => Ignore,
49 Ghost => Ignore);
50
51 with Ada.Strings.Maps; use type Ada.Strings.Maps.Character_Mapping_Function;
52
53 package Ada.Strings.Search with
54 SPARK_Mode,
55 Always_Terminates
56 is
57 pragma Preelaborate;
58
59 -- The ghost function Match tells whether the slice of Source starting at
60 -- From and of length Pattern'Length matches with Pattern with respect to
61 -- Mapping. Pattern should be non-empty and the considered slice should be
62 -- fully included in Source'Range.
63
64 function Match
65 (Source : String;
66 Pattern : String;
67 Mapping : Maps.Character_Mapping_Function;
68 From : Integer) return Boolean
69 is
70 (for all K in Pattern'Range =>
71 Pattern (K) = Mapping (Source (From + (K - Pattern'First))))
72 with
73 Ghost,
74 Pre => Mapping /= null
75 and then Pattern'Length > 0
76 and then Source'Length > 0
77 and then From in Source'First .. Source'Last - (Pattern'Length - 1),
78 Global => null;
79 pragma Annotate (GNATprove, False_Positive,
80 "call via access-to-subprogram",
81 "function Mapping must always terminate");
82
83 function Match
84 (Source : String;
85 Pattern : String;
86 Mapping : Maps.Character_Mapping;
87 From : Integer) return Boolean
88 is
89 (for all K in Pattern'Range =>
90 Pattern (K) =
91 Ada.Strings.Maps.Value
92 (Mapping, Source (From + (K - Pattern'First))))
93 with
94 Ghost,
95 Pre => Pattern'Length > 0
96 and then Source'Length > 0
97 and then From in Source'First .. Source'Last - (Pattern'Length - 1),
98 Global => null;
99
100 function Is_Identity
101 (Mapping : Maps.Character_Mapping) return Boolean
102 with
103 Post => (if Is_Identity'Result then
104 (for all K in Character =>
105 Ada.Strings.Maps.Value (Mapping, K) = K)),
106 Global => null;
107
108 function Index
109 (Source : String;
110 Pattern : String;
111 Going : Direction := Forward;
112 Mapping : Maps.Character_Mapping := Maps.Identity) return Natural
113 with
114 Pre => Pattern'Length > 0,
115
116 Post => Index'Result in 0 | Source'Range,
117 Contract_Cases =>
118
119 -- If Source is the empty string, then 0 is returned
120
121 (Source'Length = 0 => Index'Result = 0,
122
123 -- If some slice of Source matches Pattern, then a valid index is
124 -- returned.
125
126 Source'Length > 0
127 and then
128 (for some J in
129 Source'First .. Source'Last - (Pattern'Length - 1) =>
130 Match (Source, Pattern, Mapping, J))
131 =>
132
133 -- The result is in the considered range of Source
134
135 Index'Result in Source'First .. Source'Last - (Pattern'Length - 1)
136
137 -- The slice beginning at the returned index matches Pattern
138
139 and then Match (Source, Pattern, Mapping, Index'Result)
140
141 -- The result is the smallest or largest index which satisfies
142 -- the matching, respectively when Going = Forward and Going =
143 -- Backward.
144
145 and then
146 (for all J in Source'Range =>
147 (if (if Going = Forward
148 then J <= Index'Result - 1
149 else J - 1 in Index'Result
150 .. Source'Last - Pattern'Length)
151 then not Match (Source, Pattern, Mapping, J))),
152
153 -- Otherwise, 0 is returned
154
155 others => Index'Result = 0),
156 Global => null;
157
158 function Index
159 (Source : String;
160 Pattern : String;
161 Going : Direction := Forward;
162 Mapping : Maps.Character_Mapping_Function) return Natural
163 with
164 Pre => Pattern'Length > 0 and then Mapping /= null,
165 Post => Index'Result in 0 | Source'Range,
166 Contract_Cases =>
167
168 -- If Source is the null string, then 0 is returned
169
170 (Source'Length = 0 => Index'Result = 0,
171
172 -- If some slice of Source matches Pattern, then a valid index is
173 -- returned.
174
175 Source'Length > 0 and then
176 (for some J in Source'First .. Source'Last - (Pattern'Length - 1) =>
177 Match (Source, Pattern, Mapping, J))
178 =>
179
180 -- The result is in the considered range of Source
181
182 Index'Result in Source'First .. Source'Last - (Pattern'Length - 1)
183
184 -- The slice beginning at the returned index matches Pattern
185
186 and then Match (Source, Pattern, Mapping, Index'Result)
187
188 -- The result is the smallest or largest index which satisfies
189 -- the matching, respectively when Going = Forward and Going =
190 -- Backward.
191
192 and then
193 (for all J in Source'Range =>
194 (if (if Going = Forward
195 then J <= Index'Result - 1
196 else J - 1 in Index'Result
197 .. Source'Last - Pattern'Length)
198 then not Match (Source, Pattern, Mapping, J))),
199
200 -- Otherwise, 0 is returned
201
202 others => Index'Result = 0),
203 Global => null;
204
205 function Index
206 (Source : String;
207 Set : Maps.Character_Set;
208 Test : Membership := Inside;
209 Going : Direction := Forward) return Natural
210 with
211 Post => Index'Result in 0 | Source'Range,
212 Contract_Cases =>
213
214 -- If no character of Source satisfies the property Test on Set, then
215 -- 0 is returned.
216
217 ((for all C of Source =>
218 (Test = Inside) /= Ada.Strings.Maps.Is_In (C, Set))
219 =>
220 Index'Result = 0,
221
222 -- Otherwise, an index in the range of Source is returned
223
224 others =>
225
226 -- The result is in the range of Source
227
228 Index'Result in Source'Range
229
230 -- The character at the returned index satisfies the property
231 -- Test on Set.
232
233 and then (Test = Inside)
234 = Ada.Strings.Maps.Is_In (Source (Index'Result), Set)
235
236 -- The result is the smallest or largest index which satisfies
237 -- the property, respectively when Going = Forward and Going =
238 -- Backward.
239
240 and then
241 (for all J in Source'Range =>
242 (if J /= Index'Result
243 and then (J < Index'Result) = (Going = Forward)
244 then (Test = Inside)
245 /= Ada.Strings.Maps.Is_In (Source (J), Set)))),
246 Global => null;
247
248 function Index
249 (Source : String;
250 Pattern : String;
251 From : Positive;
252 Going : Direction := Forward;
253 Mapping : Maps.Character_Mapping := Maps.Identity) return Natural
254 with
255 Pre => Pattern'Length > 0
256 and then (if Source'Length > 0 then From in Source'Range),
257
258 Post => Index'Result in 0 | Source'Range,
259 Contract_Cases =>
260
261 -- If Source is the empty string, then 0 is returned
262
263 (Source'Length = 0 => Index'Result = 0,
264
265 -- If some slice of Source matches Pattern, then a valid index is
266 -- returned.
267
268 Source'Length > 0
269 and then
270 (for some J in
271 (if Going = Forward then From else Source'First)
272 .. (if Going = Forward then Source'Last else From)
273 - (Pattern'Length - 1) =>
274 Match (Source, Pattern, Mapping, J))
275 =>
276
277 -- The result is in the considered range of Source
278
279 Index'Result in
280 (if Going = Forward then From else Source'First)
281 .. (if Going = Forward then Source'Last else From)
282 - (Pattern'Length - 1)
283
284 -- The slice beginning at the returned index matches Pattern
285
286 and then Match (Source, Pattern, Mapping, Index'Result)
287
288 -- The result is the smallest or largest index which satisfies
289 -- the matching, respectively when Going = Forward and Going =
290 -- Backward.
291
292 and then
293 (for all J in Source'Range =>
294 (if (if Going = Forward
295 then J in From .. Index'Result - 1
296 else J - 1 in Index'Result
297 .. From - Pattern'Length)
298 then not Match (Source, Pattern, Mapping, J))),
299
300 -- Otherwise, 0 is returned
301
302 others => Index'Result = 0),
303 Global => null;
304
305 function Index
306 (Source : String;
307 Pattern : String;
308 From : Positive;
309 Going : Direction := Forward;
310 Mapping : Maps.Character_Mapping_Function) return Natural
311 with
312 Pre => Pattern'Length > 0
313 and then Mapping /= null
314 and then (if Source'Length > 0 then From in Source'Range),
315
316 Post => Index'Result in 0 | Source'Range,
317 Contract_Cases =>
318
319 -- If Source is the empty string, then 0 is returned
320
321 (Source'Length = 0 => Index'Result = 0,
322
323 -- If some slice of Source matches Pattern, then a valid index is
324 -- returned.
325
326 Source'Length > 0
327 and then
328 (for some J in
329 (if Going = Forward then From else Source'First)
330 .. (if Going = Forward then Source'Last else From)
331 - (Pattern'Length - 1) =>
332 Match (Source, Pattern, Mapping, J))
333 =>
334
335 -- The result is in the considered range of Source
336
337 Index'Result in
338 (if Going = Forward then From else Source'First)
339 .. (if Going = Forward then Source'Last else From)
340 - (Pattern'Length - 1)
341
342 -- The slice beginning at the returned index matches Pattern
343
344 and then Match (Source, Pattern, Mapping, Index'Result)
345
346 -- The result is the smallest or largest index which satisfies
347 -- the matching, respectively when Going = Forward and Going =
348 -- Backwards.
349
350 and then
351 (for all J in Source'Range =>
352 (if (if Going = Forward
353 then J in From .. Index'Result - 1
354 else J - 1 in Index'Result
355 .. From - Pattern'Length)
356 then not Match (Source, Pattern, Mapping, J))),
357
358 -- Otherwise, 0 is returned
359
360 others => Index'Result = 0),
361 Global => null;
362
363 function Index
364 (Source : String;
365 Set : Maps.Character_Set;
366 From : Positive;
367 Test : Membership := Inside;
368 Going : Direction := Forward) return Natural
369 with
370 Pre => (if Source'Length > 0 then From in Source'Range),
371 Post => Index'Result in 0 | Source'Range,
372 Contract_Cases =>
373
374 -- If Source is the empty string, or no character of the considered
375 -- slice of Source satisfies the property Test on Set, then 0 is
376 -- returned.
377
378 (Source'Length = 0
379 or else
380 (for all J in Source'Range =>
381 (if J = From or else (J > From) = (Going = Forward) then
382 (Test = Inside) /= Ada.Strings.Maps.Is_In (Source (J), Set)))
383 =>
384 Index'Result = 0,
385
386 -- Otherwise, an index in the considered range of Source is returned
387
388 others =>
389
390 -- The result is in the considered range of Source
391
392 Index'Result in Source'Range
393 and then (Index'Result = From
394 or else
395 (Index'Result > From) = (Going = Forward))
396
397 -- The character at the returned index satisfies the property
398 -- Test on Set
399
400 and then
401 (Test = Inside)
402 = Ada.Strings.Maps.Is_In (Source (Index'Result), Set)
403
404 -- The result is the smallest or largest index which satisfies
405 -- the property, respectively when Going = Forward and Going =
406 -- Backward.
407
408 and then
409 (for all J in Source'Range =>
410 (if J /= Index'Result
411 and then (J < Index'Result) = (Going = Forward)
412 and then (J = From
413 or else (J > From) = (Going = Forward))
414 then (Test = Inside)
415 /= Ada.Strings.Maps.Is_In (Source (J), Set)))),
416 Global => null;
417
418 function Index_Non_Blank
419 (Source : String;
420 Going : Direction := Forward) return Natural
421 with
422 Post => Index_Non_Blank'Result in 0 | Source'Range,
423 Contract_Cases =>
424
425 -- If all characters of Source are Space characters, then 0 is
426 -- returned.
427
428 ((for all C of Source => C = ' ') => Index_Non_Blank'Result = 0,
429
430 -- Otherwise, a valid index is returned
431
432 others =>
433
434 -- The result is in the range of Source
435
436 Index_Non_Blank'Result in Source'Range
437
438 -- The character at the returned index is not a Space character
439
440 and then Source (Index_Non_Blank'Result) /= ' '
441
442 -- The result is the smallest or largest index which is not a
443 -- Space character, respectively when Going = Forward and
444 -- Going = Backward.
445
446 and then
447 (for all J in Source'Range =>
448 (if J /= Index_Non_Blank'Result
449 and then (J < Index_Non_Blank'Result)
450 = (Going = Forward)
451 then Source (J) = ' '))),
452 Global => null;
453
454 function Index_Non_Blank
455 (Source : String;
456 From : Positive;
457 Going : Direction := Forward) return Natural
458 with
459 Pre => (if Source'Length /= 0 then From in Source'Range),
460 Post => Index_Non_Blank'Result in 0 | Source'Range,
461 Contract_Cases =>
462
463 -- If Source is the null string, or all characters in the considered
464 -- slice of Source are Space characters, then 0 is returned.
465
466 (Source'Length = 0
467 or else
468 (for all J in Source'Range =>
469 (if J = From or else (J > From) = (Going = Forward) then
470 Source (J) = ' '))
471 =>
472 Index_Non_Blank'Result = 0,
473
474 -- Otherwise, a valid index is returned
475
476 others =>
477
478 -- The result is in the considered range of Source
479
480 Index_Non_Blank'Result in Source'Range
481 and then (Index_Non_Blank'Result = From
482 or else (Index_Non_Blank'Result > From)
483 = (Going = Forward))
484
485 -- The character at the returned index is not a Space character
486
487 and then Source (Index_Non_Blank'Result) /= ' '
488
489 -- The result is the smallest or largest index which is not a
490 -- Space character, respectively when Going = Forward and
491 -- Going = Backward.
492
493 and then
494 (for all J in Source'Range =>
495 (if J /= Index_Non_Blank'Result
496 and then (J < Index_Non_Blank'Result)
497 = (Going = Forward)
498 and then (J = From or else (J > From)
499 = (Going = Forward))
500 then Source (J) = ' '))),
501 Global => null;
502
503 function Count
504 (Source : String;
505 Pattern : String;
506 Mapping : Maps.Character_Mapping := Maps.Identity) return Natural
507 with
508 Pre => Pattern'Length > 0,
509 Global => null;
510
511 function Count
512 (Source : String;
513 Pattern : String;
514 Mapping : Maps.Character_Mapping_Function) return Natural
515 with
516 Pre => Pattern'Length > 0 and then Mapping /= null,
517 Global => null;
518
519 function Count
520 (Source : String;
521 Set : Maps.Character_Set) return Natural
522 with
523 Global => null;
524
525 procedure Find_Token
526 (Source : String;
527 Set : Maps.Character_Set;
528 From : Positive;
529 Test : Membership;
530 First : out Positive;
531 Last : out Natural)
532 with
533 Pre => (if Source'Length /= 0 then From in Source'Range),
534 Contract_Cases =>
535
536 -- If Source is the empty string, or if no character of the considered
537 -- slice of Source satisfies the property Test on Set, then First is
538 -- set to From and Last is set to 0.
539
540 (Source'Length = 0
541 or else
542 (for all C of Source (From .. Source'Last) =>
543 (Test = Inside) /= Ada.Strings.Maps.Is_In (C, Set))
544 =>
545 First = From and then Last = 0,
546
547 -- Otherwise, First and Last are set to valid indexes
548
549 others =>
550
551 -- First and Last are in the considered range of Source
552
553 First in From .. Source'Last
554 and then Last in First .. Source'Last
555
556 -- No character between From and First satisfies the property Test
557 -- on Set.
558
559 and then
560 (for all C of Source (From .. First - 1) =>
561 (Test = Inside) /= Ada.Strings.Maps.Is_In (C, Set))
562
563 -- All characters between First and Last satisfy the property Test
564 -- on Set.
565
566 and then
567 (for all C of Source (First .. Last) =>
568 (Test = Inside) = Ada.Strings.Maps.Is_In (C, Set))
569
570 -- If Last is not Source'Last, then the character at position
571 -- Last + 1 does not satify the property Test on Set.
572
573 and then
574 (if Last < Source'Last
575 then (Test = Inside)
576 /= Ada.Strings.Maps.Is_In (Source (Last + 1), Set))),
577 Global => null;
578
579 procedure Find_Token
580 (Source : String;
581 Set : Maps.Character_Set;
582 Test : Membership;
583 First : out Positive;
584 Last : out Natural)
585 with
586 Pre => Source'First > 0,
587 Contract_Cases =>
588
589 -- If Source is the empty string, or if no character of Source
590 -- satisfies the property Test on Set, then First is set to From
591 -- and Last is set to 0.
592
593 (Source'Length = 0
594 or else
595 (for all C of Source =>
596 (Test = Inside) /= Ada.Strings.Maps.Is_In (C, Set))
597 =>
598 First = Source'First and then Last = 0,
599
600 -- Otherwise, First and Last are set to valid indexes
601
602 others =>
603
604 -- First and Last are in the considered range of Source
605
606 First in Source'Range
607 and then Last in First .. Source'Last
608
609 -- No character before First satisfies the property Test on Set
610
611 and then
612 (for all C of Source (Source'First .. First - 1) =>
613 (Test = Inside) /= Ada.Strings.Maps.Is_In (C, Set))
614
615 -- All characters between First and Last satisfy the property Test
616 -- on Set.
617
618 and then
619 (for all C of Source (First .. Last) =>
620 (Test = Inside) = Ada.Strings.Maps.Is_In (C, Set))
621
622 -- If Last is not Source'Last, then the character at position
623 -- Last + 1 does not satify the property Test on Set.
624
625 and then
626 (if Last < Source'Last
627 then (Test = Inside)
628 /= Ada.Strings.Maps.Is_In (Source (Last + 1), Set))),
629 Global => null;
630
631 end Ada.Strings.Search;