| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT RUN-TIME COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- A D A . S T R I N G S . F I X E D -- |
| 6 |
-- -- |
| 7 |
-- S p e c -- |
| 8 |
-- -- |
| 9 |
-- This specification is derived from the Ada Reference Manual for use with -- |
| 10 |
-- GNAT. In accordance with the copyright of that document, you can freely -- |
| 11 |
-- copy and modify this specification, provided that if you redistribute a -- |
| 12 |
-- modified version, any changes that you have made are clearly indicated. -- |
| 13 |
-- -- |
| 14 |
------------------------------------------------------------------------------ |
| 15 |
|
| 16 |
-- The language-defined package Strings.Fixed provides string-handling |
| 17 |
-- subprograms for fixed-length strings; that is, for values of type |
| 18 |
-- Standard.String. Several of these subprograms are procedures that modify |
| 19 |
-- the contents of a String that is passed as an out or an in out parameter; |
| 20 |
-- each has additional parameters to control the effect when the logical |
| 21 |
-- length of the result differs from the parameter's length. |
| 22 |
-- |
| 23 |
-- For each function that returns a String, the lower bound of the returned |
| 24 |
-- value is 1. |
| 25 |
-- |
| 26 |
-- The basic model embodied in the package is that a fixed-length string |
| 27 |
-- comprises significant characters and possibly padding (with space |
| 28 |
-- characters) on either or both ends. When a shorter string is copied to a |
| 29 |
-- longer string, padding is inserted, and when a longer string is copied to a |
| 30 |
-- shorter one, padding is stripped. The Move procedure in Strings.Fixed, |
| 31 |
-- which takes a String as an out parameter, allows the programmer to control |
| 32 |
-- these effects. Similar control is provided by the string transformation |
| 33 |
-- procedures. |
| 34 |
|
| 35 |
-- Preconditions in this unit are meant for analysis only, not for run-time |
| 36 |
-- checking, so that the expected exceptions are raised. This is enforced by |
| 37 |
-- setting the corresponding assertion policy to Ignore. Postconditions and |
| 38 |
-- contract cases should not be executed at runtime as well, in order not to |
| 39 |
-- slow down the execution of these functions. |
| 40 |
|
| 41 |
pragma Assertion_Policy (Pre => Ignore, |
| 42 |
Post => Ignore, |
| 43 |
Contract_Cases => Ignore, |
| 44 |
Ghost => Ignore); |
| 45 |
|
| 46 |
with Ada.Strings.Maps; use type Ada.Strings.Maps.Character_Mapping_Function; |
| 47 |
with Ada.Strings.Search; |
| 48 |
|
| 49 |
package Ada.Strings.Fixed with |
| 50 |
SPARK_Mode, |
| 51 |
Always_Terminates |
| 52 |
is |
| 53 |
pragma Preelaborate; |
| 54 |
|
| 55 |
-------------------------------------------------------------- |
| 56 |
-- Copy Procedure for Strings of Possibly Different Lengths -- |
| 57 |
-------------------------------------------------------------- |
| 58 |
|
| 59 |
procedure Move |
| 60 |
(Source : String; |
| 61 |
Target : out String; |
| 62 |
Drop : Truncation := Error; |
| 63 |
Justify : Alignment := Left; |
| 64 |
Pad : Character := Space) |
| 65 |
with |
| 66 |
Global => null, |
| 67 |
Exceptional_Cases => |
| 68 |
(Length_Error => Target'Length'Old < Source'Length and Drop = Error); |
| 69 |
-- The Move procedure copies characters from Source to Target. If Source |
| 70 |
-- has the same length as Target, then the effect is to assign Source to |
| 71 |
-- Target. If Source is shorter than Target then: |
| 72 |
-- |
| 73 |
-- * If Justify=Left, then Source is copied into the first Source'Length |
| 74 |
-- characters of Target. |
| 75 |
-- |
| 76 |
-- * If Justify=Right, then Source is copied into the last Source'Length |
| 77 |
-- characters of Target. |
| 78 |
-- |
| 79 |
-- * If Justify=Center, then Source is copied into the middle Source'Length |
| 80 |
-- characters of Target. In this case, if the difference in length |
| 81 |
-- between Target and Source is odd, then the extra Pad character is on |
| 82 |
-- the right. |
| 83 |
-- |
| 84 |
-- * Pad is copied to each Target character not otherwise assigned. |
| 85 |
-- |
| 86 |
-- If Source is longer than Target, then the effect is based on Drop. |
| 87 |
-- |
| 88 |
-- * If Drop=Left, then the rightmost Target'Length characters of Source |
| 89 |
-- are copied into Target. |
| 90 |
-- |
| 91 |
-- * If Drop=Right, then the leftmost Target'Length characters of Source |
| 92 |
-- are copied into Target. |
| 93 |
-- |
| 94 |
-- * If Drop=Error, then the effect depends on the value of the Justify |
| 95 |
-- parameter and also on whether any characters in Source other than Pad |
| 96 |
-- would fail to be copied: |
| 97 |
-- |
| 98 |
-- * If Justify=Left, and if each of the rightmost |
| 99 |
-- Source'Length-Target'Length characters in Source is Pad, then the |
| 100 |
-- leftmost Target'Length characters of Source are copied to Target. |
| 101 |
-- |
| 102 |
-- * If Justify=Right, and if each of the leftmost |
| 103 |
-- Source'Length-Target'Length characters in Source is Pad, then the |
| 104 |
-- rightmost Target'Length characters of Source are copied to Target. |
| 105 |
-- |
| 106 |
-- * Otherwise, Length_Error is propagated. |
| 107 |
|
| 108 |
------------------------ |
| 109 |
-- Search Subprograms -- |
| 110 |
------------------------ |
| 111 |
|
| 112 |
function Index |
| 113 |
(Source : String; |
| 114 |
Pattern : String; |
| 115 |
From : Positive; |
| 116 |
Going : Direction := Forward; |
| 117 |
Mapping : Maps.Character_Mapping_Function) return Natural |
| 118 |
with |
| 119 |
Pre => Pattern'Length > 0 |
| 120 |
and then Mapping /= null |
| 121 |
and then (if Source'Length > 0 then From in Source'Range), |
| 122 |
|
| 123 |
Post => Index'Result in 0 | Source'Range, |
| 124 |
Contract_Cases => |
| 125 |
|
| 126 |
-- If Source is the empty string, then 0 is returned |
| 127 |
|
| 128 |
(Source'Length = 0 |
| 129 |
=> |
| 130 |
Index'Result = 0, |
| 131 |
|
| 132 |
-- If some slice of Source matches Pattern, then a valid index is |
| 133 |
-- returned. |
| 134 |
|
| 135 |
Source'Length > 0 |
| 136 |
and then |
| 137 |
(for some J in |
| 138 |
(if Going = Forward then From else Source'First) |
| 139 |
.. (if Going = Forward then Source'Last else From) |
| 140 |
- (Pattern'Length - 1) => |
| 141 |
Ada.Strings.Search.Match (Source, Pattern, Mapping, J)) |
| 142 |
=> |
| 143 |
-- The result is in the considered range of Source |
| 144 |
|
| 145 |
Index'Result in |
| 146 |
(if Going = Forward then From else Source'First) |
| 147 |
.. (if Going = Forward then Source'Last else From) |
| 148 |
- (Pattern'Length - 1) |
| 149 |
|
| 150 |
-- The slice beginning at the returned index matches Pattern |
| 151 |
|
| 152 |
and then |
| 153 |
Ada.Strings.Search.Match (Source, Pattern, Mapping, Index'Result) |
| 154 |
|
| 155 |
-- The result is the smallest or largest index which satisfies |
| 156 |
-- the matching, respectively when Going = Forward and Going = |
| 157 |
-- Backward. |
| 158 |
|
| 159 |
and then |
| 160 |
(for all J in Source'Range => |
| 161 |
(if (if Going = Forward |
| 162 |
then J in From .. Index'Result - 1 |
| 163 |
else J - 1 in Index'Result |
| 164 |
.. From - Pattern'Length) |
| 165 |
then not Ada.Strings.Search.Match |
| 166 |
(Source, Pattern, Mapping, J))), |
| 167 |
|
| 168 |
-- Otherwise, 0 is returned |
| 169 |
|
| 170 |
others |
| 171 |
=> |
| 172 |
Index'Result = 0), |
| 173 |
Global => null; |
| 174 |
pragma Ada_05 (Index); |
| 175 |
|
| 176 |
function Index |
| 177 |
(Source : String; |
| 178 |
Pattern : String; |
| 179 |
From : Positive; |
| 180 |
Going : Direction := Forward; |
| 181 |
Mapping : Maps.Character_Mapping := Maps.Identity) return Natural |
| 182 |
with |
| 183 |
Pre => Pattern'Length > 0 |
| 184 |
and then (if Source'Length > 0 then From in Source'Range), |
| 185 |
|
| 186 |
Post => Index'Result in 0 | Source'Range, |
| 187 |
Contract_Cases => |
| 188 |
|
| 189 |
-- If Source is the empty string, then 0 is returned |
| 190 |
|
| 191 |
(Source'Length = 0 |
| 192 |
=> |
| 193 |
Index'Result = 0, |
| 194 |
|
| 195 |
-- If some slice of Source matches Pattern, then a valid index is |
| 196 |
-- returned. |
| 197 |
|
| 198 |
Source'Length > 0 |
| 199 |
and then |
| 200 |
(for some J in |
| 201 |
(if Going = Forward then From else Source'First) |
| 202 |
.. (if Going = Forward then Source'Last else From) |
| 203 |
- (Pattern'Length - 1) => |
| 204 |
Ada.Strings.Search.Match (Source, Pattern, Mapping, J)) |
| 205 |
=> |
| 206 |
-- The result is in the considered range of Source |
| 207 |
|
| 208 |
Index'Result in |
| 209 |
(if Going = Forward then From else Source'First) |
| 210 |
.. (if Going = Forward then Source'Last else From) |
| 211 |
- (Pattern'Length - 1) |
| 212 |
|
| 213 |
-- The slice beginning at the returned index matches Pattern |
| 214 |
|
| 215 |
and then |
| 216 |
Ada.Strings.Search.Match (Source, Pattern, Mapping, Index'Result) |
| 217 |
|
| 218 |
-- The result is the smallest or largest index which satisfies the |
| 219 |
-- matching, respectively when Going = Forward and |
| 220 |
-- Going = Backward. |
| 221 |
|
| 222 |
and then |
| 223 |
(for all J in Source'Range => |
| 224 |
(if (if Going = Forward |
| 225 |
then J in From .. Index'Result - 1 |
| 226 |
else J - 1 in Index'Result |
| 227 |
.. From - Pattern'Length) |
| 228 |
then not Ada.Strings.Search.Match |
| 229 |
(Source, Pattern, Mapping, J))), |
| 230 |
|
| 231 |
-- Otherwise, 0 is returned |
| 232 |
|
| 233 |
others |
| 234 |
=> |
| 235 |
Index'Result = 0), |
| 236 |
Global => null; |
| 237 |
pragma Ada_05 (Index); |
| 238 |
|
| 239 |
-- Each Index function searches, starting from From, for a slice of |
| 240 |
-- Source, with length Pattern'Length, that matches Pattern with respect to |
| 241 |
-- Mapping; the parameter Going indicates the direction of the lookup. If |
| 242 |
-- Source is the null string, Index returns 0; otherwise, if From is not in |
| 243 |
-- Source'Range, then Index_Error is propagated. If Going = Forward, then |
| 244 |
-- Index returns the smallest index I which is greater than or equal to |
| 245 |
-- From such that the slice of Source starting at I matches Pattern. If |
| 246 |
-- Going = Backward, then Index returns the largest index I such that the |
| 247 |
-- slice of Source starting at I matches Pattern and has an upper bound |
| 248 |
-- less than or equal to From. If there is no such slice, then 0 is |
| 249 |
-- returned. If Pattern is the null string, then Pattern_Error is |
| 250 |
-- propagated. |
| 251 |
|
| 252 |
function Index |
| 253 |
(Source : String; |
| 254 |
Pattern : String; |
| 255 |
Going : Direction := Forward; |
| 256 |
Mapping : Maps.Character_Mapping := Maps.Identity) return Natural |
| 257 |
with |
| 258 |
Pre => Pattern'Length > 0, |
| 259 |
|
| 260 |
Post => Index'Result in 0 | Source'Range, |
| 261 |
Contract_Cases => |
| 262 |
|
| 263 |
-- If Source is the empty string, then 0 is returned |
| 264 |
|
| 265 |
(Source'Length = 0 |
| 266 |
=> |
| 267 |
Index'Result = 0, |
| 268 |
|
| 269 |
-- If some slice of Source matches Pattern, then a valid index is |
| 270 |
-- returned. |
| 271 |
|
| 272 |
Source'Length > 0 |
| 273 |
and then |
| 274 |
(for some J in |
| 275 |
Source'First .. Source'Last - (Pattern'Length - 1) => |
| 276 |
Ada.Strings.Search.Match (Source, Pattern, Mapping, J)) |
| 277 |
=> |
| 278 |
-- The result is in the considered range of Source |
| 279 |
|
| 280 |
Index'Result in Source'First .. Source'Last - (Pattern'Length - 1) |
| 281 |
|
| 282 |
-- The slice beginning at the returned index matches Pattern |
| 283 |
|
| 284 |
and then |
| 285 |
Ada.Strings.Search.Match (Source, Pattern, Mapping, Index'Result) |
| 286 |
|
| 287 |
-- The result is the smallest or largest index which satisfies |
| 288 |
-- the matching, respectively when Going = Forward and Going = |
| 289 |
-- Backward. |
| 290 |
|
| 291 |
and then |
| 292 |
(for all J in Source'Range => |
| 293 |
(if (if Going = Forward |
| 294 |
then J <= Index'Result - 1 |
| 295 |
else J - 1 in Index'Result |
| 296 |
.. Source'Last - Pattern'Length) |
| 297 |
then not Ada.Strings.Search.Match |
| 298 |
(Source, Pattern, Mapping, J))), |
| 299 |
|
| 300 |
-- Otherwise, 0 is returned |
| 301 |
|
| 302 |
others |
| 303 |
=> |
| 304 |
Index'Result = 0), |
| 305 |
Global => null; |
| 306 |
|
| 307 |
function Index |
| 308 |
(Source : String; |
| 309 |
Pattern : String; |
| 310 |
Going : Direction := Forward; |
| 311 |
Mapping : Maps.Character_Mapping_Function) return Natural |
| 312 |
with |
| 313 |
Pre => Pattern'Length > 0 and then Mapping /= null, |
| 314 |
|
| 315 |
Post => Index'Result in 0 | Source'Range, |
| 316 |
Contract_Cases => |
| 317 |
|
| 318 |
-- If Source is the empty string, then 0 is returned |
| 319 |
|
| 320 |
(Source'Length = 0 |
| 321 |
=> |
| 322 |
Index'Result = 0, |
| 323 |
|
| 324 |
-- If some slice of Source matches Pattern, then a valid index is |
| 325 |
-- returned. |
| 326 |
|
| 327 |
Source'Length > 0 |
| 328 |
and then |
| 329 |
(for some J in |
| 330 |
Source'First .. Source'Last - (Pattern'Length - 1) => |
| 331 |
Ada.Strings.Search.Match (Source, Pattern, Mapping, J)) |
| 332 |
=> |
| 333 |
-- The result is in the considered range of Source |
| 334 |
|
| 335 |
Index'Result in Source'First .. Source'Last - (Pattern'Length - 1) |
| 336 |
|
| 337 |
-- The slice beginning at the returned index matches Pattern |
| 338 |
|
| 339 |
and then |
| 340 |
Ada.Strings.Search.Match (Source, Pattern, Mapping, Index'Result) |
| 341 |
|
| 342 |
-- The result is the smallest or largest index which satisfies |
| 343 |
-- the matching, respectively when Going = Forward and Going = |
| 344 |
-- Backward. |
| 345 |
|
| 346 |
and then |
| 347 |
(for all J in Source'Range => |
| 348 |
(if (if Going = Forward |
| 349 |
then J <= Index'Result - 1 |
| 350 |
else J - 1 in Index'Result |
| 351 |
.. Source'Last - Pattern'Length) |
| 352 |
then not Ada.Strings.Search.Match |
| 353 |
(Source, Pattern, Mapping, J))), |
| 354 |
|
| 355 |
-- Otherwise, 0 is returned |
| 356 |
|
| 357 |
others |
| 358 |
=> |
| 359 |
Index'Result = 0), |
| 360 |
Global => null; |
| 361 |
|
| 362 |
-- If Going = Forward, returns: |
| 363 |
-- |
| 364 |
-- Index (Source, Pattern, Source'First, Forward, Mapping) |
| 365 |
-- |
| 366 |
-- otherwise, returns: |
| 367 |
-- |
| 368 |
-- Index (Source, Pattern, Source'Last, Backward, Mapping). |
| 369 |
|
| 370 |
function Index |
| 371 |
(Source : String; |
| 372 |
Set : Maps.Character_Set; |
| 373 |
Test : Membership := Inside; |
| 374 |
Going : Direction := Forward) return Natural |
| 375 |
with |
| 376 |
Post => Index'Result in 0 | Source'Range, |
| 377 |
Contract_Cases => |
| 378 |
|
| 379 |
-- If no character of Source satisfies the property Test on Set, then |
| 380 |
-- 0 is returned. |
| 381 |
|
| 382 |
((for all C of Source => |
| 383 |
(Test = Inside) /= Ada.Strings.Maps.Is_In (C, Set)) |
| 384 |
=> |
| 385 |
Index'Result = 0, |
| 386 |
|
| 387 |
-- Otherwise, an index in the range of Source is returned |
| 388 |
|
| 389 |
others |
| 390 |
=> |
| 391 |
|
| 392 |
-- The result is in the range of Source |
| 393 |
|
| 394 |
Index'Result in Source'Range |
| 395 |
|
| 396 |
-- The character at the returned index satisfies the property |
| 397 |
-- Test on Set. |
| 398 |
|
| 399 |
and then |
| 400 |
(Test = Inside) |
| 401 |
= Ada.Strings.Maps.Is_In (Source (Index'Result), Set) |
| 402 |
|
| 403 |
-- The result is the smallest or largest index which satisfies |
| 404 |
-- the property, respectively when Going = Forward and Going = |
| 405 |
-- Backward. |
| 406 |
|
| 407 |
and then |
| 408 |
(for all J in Source'Range => |
| 409 |
(if J /= Index'Result |
| 410 |
and then (J < Index'Result) = (Going = Forward) |
| 411 |
then (Test = Inside) |
| 412 |
/= Ada.Strings.Maps.Is_In (Source (J), Set)))), |
| 413 |
Global => null; |
| 414 |
|
| 415 |
function Index |
| 416 |
(Source : String; |
| 417 |
Set : Maps.Character_Set; |
| 418 |
From : Positive; |
| 419 |
Test : Membership := Inside; |
| 420 |
Going : Direction := Forward) return Natural |
| 421 |
with |
| 422 |
Pre => (if Source'Length > 0 then From in Source'Range), |
| 423 |
Post => Index'Result in 0 | Source'Range, |
| 424 |
Contract_Cases => |
| 425 |
|
| 426 |
-- If Source is the empty string, or no character of the considered |
| 427 |
-- slice of Source satisfies the property Test on Set, then 0 is |
| 428 |
-- returned. |
| 429 |
|
| 430 |
(Source'Length = 0 |
| 431 |
or else |
| 432 |
(for all J in Source'Range => |
| 433 |
(if J = From or else (J > From) = (Going = Forward) then |
| 434 |
(Test = Inside) /= Ada.Strings.Maps.Is_In (Source (J), Set))) |
| 435 |
=> |
| 436 |
Index'Result = 0, |
| 437 |
|
| 438 |
-- Otherwise, an index in the considered range of Source is returned |
| 439 |
|
| 440 |
others |
| 441 |
=> |
| 442 |
|
| 443 |
-- The result is in the considered range of Source |
| 444 |
|
| 445 |
Index'Result in Source'Range |
| 446 |
and then (Index'Result = From |
| 447 |
or else |
| 448 |
(Index'Result > From) = (Going = Forward)) |
| 449 |
|
| 450 |
-- The character at the returned index satisfies the property |
| 451 |
-- Test on Set. |
| 452 |
|
| 453 |
and then |
| 454 |
(Test = Inside) |
| 455 |
= Ada.Strings.Maps.Is_In (Source (Index'Result), Set) |
| 456 |
|
| 457 |
-- The result is the smallest or largest index which satisfies |
| 458 |
-- the property, respectively when Going = Forward and Going = |
| 459 |
-- Backward. |
| 460 |
|
| 461 |
and then |
| 462 |
(for all J in Source'Range => |
| 463 |
(if J /= Index'Result |
| 464 |
and then (J < Index'Result) = (Going = Forward) |
| 465 |
and then (J = From |
| 466 |
or else (J > From) = (Going = Forward)) |
| 467 |
then (Test = Inside) |
| 468 |
/= Ada.Strings.Maps.Is_In (Source (J), Set)))), |
| 469 |
Global => null; |
| 470 |
pragma Ada_05 (Index); |
| 471 |
-- Index searches for the first or last occurrence of any of a set of |
| 472 |
-- characters (when Test=Inside), or any of the complement of a set of |
| 473 |
-- characters (when Test=Outside). If Source is the null string, Index |
| 474 |
-- returns 0; otherwise, if From is not in Source'Range, then Index_Error |
| 475 |
-- is propagated. Otherwise, it returns the smallest index I >= From (if |
| 476 |
-- Going=Forward) or the largest index I <= From (if Going=Backward) such |
| 477 |
-- that Source(I) satisfies the Test condition with respect to Set; it |
| 478 |
-- returns 0 if there is no such Character in Source. |
| 479 |
|
| 480 |
function Index_Non_Blank |
| 481 |
(Source : String; |
| 482 |
From : Positive; |
| 483 |
Going : Direction := Forward) return Natural |
| 484 |
with |
| 485 |
Pre => (if Source'Length /= 0 then From in Source'Range), |
| 486 |
|
| 487 |
Post => Index_Non_Blank'Result in 0 | Source'Range, |
| 488 |
Contract_Cases => |
| 489 |
|
| 490 |
-- If Source is the empty string, or all characters in the considered |
| 491 |
-- slice of Source are Space characters, then 0 is returned. |
| 492 |
|
| 493 |
(Source'Length = 0 |
| 494 |
or else |
| 495 |
(for all J in Source'Range => |
| 496 |
(if J = From or else (J > From) = (Going = Forward) then |
| 497 |
Source (J) = ' ')) |
| 498 |
=> |
| 499 |
Index_Non_Blank'Result = 0, |
| 500 |
|
| 501 |
-- Otherwise, a valid index is returned |
| 502 |
|
| 503 |
others |
| 504 |
=> |
| 505 |
|
| 506 |
-- The result is in the considered range of Source |
| 507 |
|
| 508 |
Index_Non_Blank'Result in Source'Range |
| 509 |
and then (Index_Non_Blank'Result = From |
| 510 |
or else (Index_Non_Blank'Result > From) |
| 511 |
= (Going = Forward)) |
| 512 |
|
| 513 |
-- The character at the returned index is not a Space character |
| 514 |
|
| 515 |
and then Source (Index_Non_Blank'Result) /= ' ' |
| 516 |
|
| 517 |
-- The result is the smallest or largest index which is not a |
| 518 |
-- Space character, respectively when Going = Forward and |
| 519 |
-- Going = Backward. |
| 520 |
|
| 521 |
and then |
| 522 |
(for all J in Source'Range => |
| 523 |
(if J /= Index_Non_Blank'Result |
| 524 |
and then (J < Index_Non_Blank'Result) |
| 525 |
= (Going = Forward) |
| 526 |
and then (J = From or else (J > From) |
| 527 |
= (Going = Forward)) |
| 528 |
then Source (J) = ' '))), |
| 529 |
Global => null; |
| 530 |
pragma Ada_05 (Index_Non_Blank); |
| 531 |
-- Returns Index (Source, Maps.To_Set(Space), From, Outside, Going) |
| 532 |
|
| 533 |
function Index_Non_Blank |
| 534 |
(Source : String; |
| 535 |
Going : Direction := Forward) return Natural |
| 536 |
with |
| 537 |
Post => Index_Non_Blank'Result in 0 | Source'Range, |
| 538 |
Contract_Cases => |
| 539 |
|
| 540 |
-- If all characters of Source are Space characters, then 0 is |
| 541 |
-- returned. |
| 542 |
|
| 543 |
((for all C of Source => C = ' ') => Index_Non_Blank'Result = 0, |
| 544 |
|
| 545 |
-- Otherwise, a valid index is returned |
| 546 |
|
| 547 |
others => |
| 548 |
|
| 549 |
-- The result is in the range of Source |
| 550 |
|
| 551 |
Index_Non_Blank'Result in Source'Range |
| 552 |
|
| 553 |
-- The character at the returned index is not a Space character |
| 554 |
|
| 555 |
and then Source (Index_Non_Blank'Result) /= ' ' |
| 556 |
|
| 557 |
-- The result is the smallest or largest index which is not a |
| 558 |
-- Space character, respectively when Going = Forward and Going |
| 559 |
-- = Backward. |
| 560 |
|
| 561 |
and then |
| 562 |
(for all J in Source'Range => |
| 563 |
(if J /= Index_Non_Blank'Result |
| 564 |
and then (J < Index_Non_Blank'Result) |
| 565 |
= (Going = Forward) |
| 566 |
then Source (J) = ' '))), |
| 567 |
Global => null; |
| 568 |
-- Returns Index (Source, Maps.To_Set(Space), Outside, Going) |
| 569 |
|
| 570 |
function Count |
| 571 |
(Source : String; |
| 572 |
Pattern : String; |
| 573 |
Mapping : Maps.Character_Mapping := Maps.Identity) return Natural |
| 574 |
with |
| 575 |
Pre => Pattern'Length /= 0, |
| 576 |
Global => null; |
| 577 |
|
| 578 |
function Count |
| 579 |
(Source : String; |
| 580 |
Pattern : String; |
| 581 |
Mapping : Maps.Character_Mapping_Function) return Natural |
| 582 |
with |
| 583 |
Pre => Pattern'Length /= 0 and then Mapping /= null, |
| 584 |
Global => null; |
| 585 |
|
| 586 |
-- Returns the maximum number of nonoverlapping slices of Source that match |
| 587 |
-- Pattern with respect to Mapping. If Pattern is the null string then |
| 588 |
-- Pattern_Error is propagated. |
| 589 |
|
| 590 |
function Count |
| 591 |
(Source : String; |
| 592 |
Set : Maps.Character_Set) return Natural |
| 593 |
with |
| 594 |
Global => null; |
| 595 |
-- Returns the number of occurrences in Source of characters that are in |
| 596 |
-- Set. |
| 597 |
|
| 598 |
procedure Find_Token |
| 599 |
(Source : String; |
| 600 |
Set : Maps.Character_Set; |
| 601 |
From : Positive; |
| 602 |
Test : Membership; |
| 603 |
First : out Positive; |
| 604 |
Last : out Natural) |
| 605 |
with |
| 606 |
Pre => (if Source'Length /= 0 then From in Source'Range), |
| 607 |
Contract_Cases => |
| 608 |
|
| 609 |
-- If Source is the empty string, or if no character of the considered |
| 610 |
-- slice of Source satisfies the property Test on Set, then First is |
| 611 |
-- set to From and Last is set to 0. |
| 612 |
|
| 613 |
(Source'Length = 0 |
| 614 |
or else |
| 615 |
(for all C of Source (From .. Source'Last) => |
| 616 |
(Test = Inside) /= Ada.Strings.Maps.Is_In (C, Set)) |
| 617 |
=> |
| 618 |
First = From and then Last = 0, |
| 619 |
|
| 620 |
-- Otherwise, First and Last are set to valid indexes |
| 621 |
|
| 622 |
others |
| 623 |
=> |
| 624 |
|
| 625 |
-- First and Last are in the considered range of Source |
| 626 |
|
| 627 |
First in From .. Source'Last |
| 628 |
and then Last in First .. Source'Last |
| 629 |
|
| 630 |
-- No character between From and First satisfies the property Test |
| 631 |
-- on Set. |
| 632 |
|
| 633 |
and then |
| 634 |
(for all C of Source (From .. First - 1) => |
| 635 |
(Test = Inside) /= Ada.Strings.Maps.Is_In (C, Set)) |
| 636 |
|
| 637 |
-- All characters between First and Last satisfy the property Test |
| 638 |
-- on Set. |
| 639 |
|
| 640 |
and then |
| 641 |
(for all C of Source (First .. Last) => |
| 642 |
(Test = Inside) = Ada.Strings.Maps.Is_In (C, Set)) |
| 643 |
|
| 644 |
-- If Last is not Source'Last, then the character at position |
| 645 |
-- Last + 1 does not satify the property Test on Set. |
| 646 |
|
| 647 |
and then |
| 648 |
(if Last < Source'Last |
| 649 |
then |
| 650 |
(Test = Inside) |
| 651 |
/= Ada.Strings.Maps.Is_In (Source (Last + 1), Set))), |
| 652 |
Global => null; |
| 653 |
pragma Ada_2012 (Find_Token); |
| 654 |
-- If Source is not the null string and From is not in Source'Range, then |
| 655 |
-- Index_Error is raised. Otherwise, First is set to the index of the first |
| 656 |
-- character in Source(From .. Source'Last) that satisfies the Test |
| 657 |
-- condition. Last is set to the largest index such that all characters in |
| 658 |
-- Source(First .. Last) satisfy the Test condition. If no characters in |
| 659 |
-- Source(From .. Source'Last) satisfy the Test condition, First is set to |
| 660 |
-- From, and Last is set to 0. |
| 661 |
|
| 662 |
procedure Find_Token |
| 663 |
(Source : String; |
| 664 |
Set : Maps.Character_Set; |
| 665 |
Test : Membership; |
| 666 |
First : out Positive; |
| 667 |
Last : out Natural) |
| 668 |
with |
| 669 |
Pre => Source'First > 0, |
| 670 |
Contract_Cases => |
| 671 |
|
| 672 |
-- If Source is the empty string, or if no character of Source |
| 673 |
-- satisfies the property Test on Set, then First is set to From and |
| 674 |
-- Last is set to 0. |
| 675 |
|
| 676 |
(Source'Length = 0 |
| 677 |
or else |
| 678 |
(for all C of Source => |
| 679 |
(Test = Inside) /= Ada.Strings.Maps.Is_In (C, Set)) |
| 680 |
=> |
| 681 |
First = Source'First and then Last = 0, |
| 682 |
|
| 683 |
-- Otherwise, First and Last are set to valid indexes |
| 684 |
|
| 685 |
others |
| 686 |
=> |
| 687 |
|
| 688 |
-- First and Last are in the considered range of Source |
| 689 |
|
| 690 |
First in Source'Range |
| 691 |
and then Last in First .. Source'Last |
| 692 |
|
| 693 |
-- No character before First satisfies the property Test on Set |
| 694 |
|
| 695 |
and then |
| 696 |
(for all C of Source (Source'First .. First - 1) => |
| 697 |
(Test = Inside) /= Ada.Strings.Maps.Is_In (C, Set)) |
| 698 |
|
| 699 |
-- All characters between First and Last satisfy the property Test |
| 700 |
-- on Set. |
| 701 |
|
| 702 |
and then |
| 703 |
(for all C of Source (First .. Last) => |
| 704 |
(Test = Inside) = Ada.Strings.Maps.Is_In (C, Set)) |
| 705 |
|
| 706 |
-- If Last is not Source'Last, then the character at position |
| 707 |
-- Last + 1 does not satify the property Test on Set. |
| 708 |
|
| 709 |
and then |
| 710 |
(if Last < Source'Last |
| 711 |
then |
| 712 |
(Test = Inside) |
| 713 |
/= Ada.Strings.Maps.Is_In (Source (Last + 1), Set))), |
| 714 |
Global => null; |
| 715 |
-- Equivalent to Find_Token (Source, Set, Source'First, Test, First, Last) |
| 716 |
|
| 717 |
------------------------------------ |
| 718 |
-- String Translation Subprograms -- |
| 719 |
------------------------------------ |
| 720 |
|
| 721 |
function Translate |
| 722 |
(Source : String; |
| 723 |
Mapping : Maps.Character_Mapping_Function) return String |
| 724 |
with |
| 725 |
Pre => Mapping /= null, |
| 726 |
Post => |
| 727 |
|
| 728 |
-- Lower bound of the returned string is 1 |
| 729 |
|
| 730 |
Translate'Result'First = 1 |
| 731 |
|
| 732 |
-- The returned string has the same length as Source |
| 733 |
|
| 734 |
and then Translate'Result'Last = Source'Length |
| 735 |
|
| 736 |
-- Each character in the returned string is the translation of the |
| 737 |
-- character at the same position in Source through Mapping. |
| 738 |
|
| 739 |
and then |
| 740 |
(for all J in Source'Range => |
| 741 |
Translate'Result (J - Source'First + 1) |
| 742 |
= Mapping (Source (J))), |
| 743 |
Global => null; |
| 744 |
pragma Annotate (GNATprove, False_Positive, |
| 745 |
"call via access-to-subprogram", |
| 746 |
"function Mapping must always terminate"); |
| 747 |
|
| 748 |
function Translate |
| 749 |
(Source : String; |
| 750 |
Mapping : Maps.Character_Mapping) return String |
| 751 |
with |
| 752 |
Post => |
| 753 |
|
| 754 |
-- Lower bound of the returned string is 1 |
| 755 |
|
| 756 |
Translate'Result'First = 1 |
| 757 |
|
| 758 |
-- The returned string has the same length as Source |
| 759 |
|
| 760 |
and then Translate'Result'Last = Source'Length |
| 761 |
|
| 762 |
-- Each character in the returned string is the translation of the |
| 763 |
-- character at the same position in Source through Mapping. |
| 764 |
|
| 765 |
and then |
| 766 |
(for all J in Source'Range => |
| 767 |
Translate'Result (J - Source'First + 1) |
| 768 |
= Ada.Strings.Maps.Value (Mapping, Source (J))), |
| 769 |
Global => null; |
| 770 |
|
| 771 |
-- Returns the string S whose length is Source'Length and such that S (I) |
| 772 |
-- is the character to which Mapping maps the corresponding element of |
| 773 |
-- Source, for I in 1 .. Source'Length. |
| 774 |
|
| 775 |
procedure Translate |
| 776 |
(Source : in out String; |
| 777 |
Mapping : Maps.Character_Mapping_Function) |
| 778 |
with |
| 779 |
Pre => Mapping /= null, |
| 780 |
Post => |
| 781 |
|
| 782 |
-- Each character in Source after the call is the translation of the |
| 783 |
-- character at the same position before the call, through Mapping. |
| 784 |
|
| 785 |
(for all J in Source'Range => Source (J) = Mapping (Source'Old (J))), |
| 786 |
Global => null; |
| 787 |
pragma Annotate (GNATprove, False_Positive, |
| 788 |
"call via access-to-subprogram", |
| 789 |
"function Mapping must always terminate"); |
| 790 |
|
| 791 |
procedure Translate |
| 792 |
(Source : in out String; |
| 793 |
Mapping : Maps.Character_Mapping) |
| 794 |
with |
| 795 |
Post => |
| 796 |
|
| 797 |
-- Each character in Source after the call is the translation of the |
| 798 |
-- character at the same position before the call, through Mapping. |
| 799 |
|
| 800 |
(for all J in Source'Range => |
| 801 |
Source (J) = Ada.Strings.Maps.Value (Mapping, Source'Old (J))), |
| 802 |
Global => null; |
| 803 |
|
| 804 |
-- Equivalent to Source := Translate(Source, Mapping) |
| 805 |
|
| 806 |
--------------------------------------- |
| 807 |
-- String Transformation Subprograms -- |
| 808 |
--------------------------------------- |
| 809 |
|
| 810 |
function Replace_Slice |
| 811 |
(Source : String; |
| 812 |
Low : Positive; |
| 813 |
High : Natural; |
| 814 |
By : String) return String |
| 815 |
with |
| 816 |
Pre => |
| 817 |
Low - 1 <= Source'Last |
| 818 |
and then High >= Source'First - 1 |
| 819 |
and then (if High >= Low |
| 820 |
then Natural'Max (0, Low - Source'First) |
| 821 |
<= Natural'Last |
| 822 |
- By'Length |
| 823 |
- Natural'Max (Source'Last - High, 0) |
| 824 |
else Source'Length <= Natural'Last - By'Length), |
| 825 |
|
| 826 |
-- Lower bound of the returned string is 1 |
| 827 |
|
| 828 |
Post => Replace_Slice'Result'First = 1, |
| 829 |
Contract_Cases => |
| 830 |
|
| 831 |
-- If High >= Low, then the returned string comprises |
| 832 |
-- Source (Source'First .. Low - 1) & By |
| 833 |
-- & Source(High + 1 .. Source'Last). |
| 834 |
|
| 835 |
(High >= Low => |
| 836 |
|
| 837 |
-- Length of the returned string |
| 838 |
|
| 839 |
Replace_Slice'Result'Length |
| 840 |
= Integer'Max (0, Low - Source'First) |
| 841 |
+ By'Length |
| 842 |
+ Integer'Max (Source'Last - High, 0) |
| 843 |
|
| 844 |
-- Elements starting at Low are replaced by elements of By |
| 845 |
|
| 846 |
and then |
| 847 |
Replace_Slice'Result (1 .. Integer'Max (0, Low - Source'First)) |
| 848 |
= Source (Source'First .. Low - 1) |
| 849 |
and then |
| 850 |
Replace_Slice'Result |
| 851 |
(Integer'Max (0, Low - Source'First) + 1 |
| 852 |
.. Integer'Max (0, Low - Source'First) + By'Length) |
| 853 |
= By |
| 854 |
|
| 855 |
-- When there are remaining characters after the replaced slice, |
| 856 |
-- they are appended to the result. |
| 857 |
|
| 858 |
and then |
| 859 |
(if High < Source'Last |
| 860 |
then |
| 861 |
Replace_Slice'Result |
| 862 |
(Integer'Max (0, Low - Source'First) + By'Length + 1 |
| 863 |
.. Replace_Slice'Result'Last) |
| 864 |
= Source (High + 1 .. Source'Last)), |
| 865 |
|
| 866 |
-- If High < Low, then the returned string is |
| 867 |
-- Insert (Source, Before => Low, New_Item => By). |
| 868 |
|
| 869 |
others => |
| 870 |
|
| 871 |
-- Length of the returned string |
| 872 |
|
| 873 |
Replace_Slice'Result'Length = Source'Length + By'Length |
| 874 |
|
| 875 |
-- Elements of By are inserted after the element at Low |
| 876 |
|
| 877 |
and then |
| 878 |
Replace_Slice'Result (1 .. Low - Source'First) |
| 879 |
= Source (Source'First .. Low - 1) |
| 880 |
and then |
| 881 |
Replace_Slice'Result |
| 882 |
(Low - Source'First + 1 .. Low - Source'First + By'Length) |
| 883 |
= By |
| 884 |
|
| 885 |
-- When there are remaining characters after Low in Source, they |
| 886 |
-- are appended to the result. |
| 887 |
|
| 888 |
and then |
| 889 |
(if Low < Source'Last |
| 890 |
then |
| 891 |
Replace_Slice'Result |
| 892 |
(Low - Source'First + By'Length + 1 |
| 893 |
.. Replace_Slice'Result'Last) |
| 894 |
= Source (Low .. Source'Last))), |
| 895 |
Global => null; |
| 896 |
-- If Low > Source'Last + 1, or High < Source'First - 1, then Index_Error |
| 897 |
-- is propagated. Otherwise: |
| 898 |
-- |
| 899 |
-- * If High >= Low, then the returned string comprises |
| 900 |
-- Source (Source'First .. Low - 1) |
| 901 |
-- & By & Source(High + 1 .. Source'Last), but with lower bound 1. |
| 902 |
-- |
| 903 |
-- * If High < Low, then the returned string is |
| 904 |
-- Insert (Source, Before => Low, New_Item => By). |
| 905 |
|
| 906 |
procedure Replace_Slice |
| 907 |
(Source : in out String; |
| 908 |
Low : Positive; |
| 909 |
High : Natural; |
| 910 |
By : String; |
| 911 |
Drop : Truncation := Error; |
| 912 |
Justify : Alignment := Left; |
| 913 |
Pad : Character := Space) |
| 914 |
with |
| 915 |
Pre => |
| 916 |
Low - 1 <= Source'Last |
| 917 |
and then High >= Source'First - 1 |
| 918 |
and then (if High >= Low |
| 919 |
then Natural'Max (0, Low - Source'First) |
| 920 |
<= Natural'Last |
| 921 |
- By'Length |
| 922 |
- Natural'Max (Source'Last - High, 0) |
| 923 |
else Source'Length <= Natural'Last - By'Length), |
| 924 |
Global => null, |
| 925 |
Exceptional_Cases => (Length_Error => Drop = Error); |
| 926 |
-- Equivalent to: |
| 927 |
-- |
| 928 |
-- Move (Replace_Slice (Source, Low, High, By), |
| 929 |
-- Source, Drop, Justify, Pad). |
| 930 |
|
| 931 |
function Insert |
| 932 |
(Source : String; |
| 933 |
Before : Positive; |
| 934 |
New_Item : String) return String |
| 935 |
with |
| 936 |
Pre => |
| 937 |
Before - 1 in Source'First - 1 .. Source'Last |
| 938 |
and then Source'Length <= Natural'Last - New_Item'Length, |
| 939 |
|
| 940 |
Post => |
| 941 |
|
| 942 |
-- Lower bound of the returned string is 1 |
| 943 |
|
| 944 |
Insert'Result'First = 1 |
| 945 |
|
| 946 |
-- Length of the returned string |
| 947 |
|
| 948 |
and then Insert'Result'Length = Source'Length + New_Item'Length |
| 949 |
|
| 950 |
-- Elements of New_Item are inserted after element at Before |
| 951 |
|
| 952 |
and then |
| 953 |
Insert'Result (1 .. Before - Source'First) |
| 954 |
= Source (Source'First .. Before - 1) |
| 955 |
and then |
| 956 |
Insert'Result |
| 957 |
(Before - Source'First + 1 |
| 958 |
.. Before - Source'First + New_Item'Length) |
| 959 |
= New_Item |
| 960 |
|
| 961 |
-- When there are remaining characters after Before in Source, they |
| 962 |
-- are appended to the returned string. |
| 963 |
|
| 964 |
and then |
| 965 |
(if Before <= Source'Last |
| 966 |
then |
| 967 |
Insert'Result |
| 968 |
(Before - Source'First + New_Item'Length + 1 |
| 969 |
.. Insert'Result'Last) |
| 970 |
= Source (Before .. Source'Last)), |
| 971 |
Global => null; |
| 972 |
-- Propagates Index_Error if Before is not in |
| 973 |
-- Source'First .. Source'Last + 1; otherwise, returns |
| 974 |
-- Source (Source'First .. Before - 1) |
| 975 |
-- & New_Item & Source(Before..Source'Last), but with lower bound 1. |
| 976 |
|
| 977 |
procedure Insert |
| 978 |
(Source : in out String; |
| 979 |
Before : Positive; |
| 980 |
New_Item : String; |
| 981 |
Drop : Truncation := Error) |
| 982 |
with |
| 983 |
Pre => |
| 984 |
Before - 1 in Source'First - 1 .. Source'Last |
| 985 |
and then Source'Length <= Natural'Last - New_Item'Length, |
| 986 |
Global => null, |
| 987 |
Exceptional_Cases => (Length_Error => Drop = Error); |
| 988 |
-- Equivalent to Move (Insert (Source, Before, New_Item), Source, Drop) |
| 989 |
|
| 990 |
function Overwrite |
| 991 |
(Source : String; |
| 992 |
Position : Positive; |
| 993 |
New_Item : String) return String |
| 994 |
with |
| 995 |
Pre => |
| 996 |
Position - 1 in Source'First - 1 .. Source'Last |
| 997 |
and then |
| 998 |
(if Position - Source'First >= Source'Length - New_Item'Length |
| 999 |
then Position - Source'First <= Natural'Last - New_Item'Length), |
| 1000 |
|
| 1001 |
Post => |
| 1002 |
|
| 1003 |
-- Lower bound of the returned string is 1 |
| 1004 |
|
| 1005 |
Overwrite'Result'First = 1 |
| 1006 |
|
| 1007 |
-- Length of the returned string |
| 1008 |
|
| 1009 |
and then |
| 1010 |
Overwrite'Result'Length |
| 1011 |
= Integer'Max (Source'Length, |
| 1012 |
Position - Source'First + New_Item'Length) |
| 1013 |
|
| 1014 |
-- Elements after Position are replaced by elements of New_Item |
| 1015 |
|
| 1016 |
and then |
| 1017 |
Overwrite'Result (1 .. Position - Source'First) |
| 1018 |
= Source (Source'First .. Position - 1) |
| 1019 |
and then |
| 1020 |
Overwrite'Result |
| 1021 |
(Position - Source'First + 1 |
| 1022 |
.. Position - Source'First + New_Item'Length) |
| 1023 |
= New_Item |
| 1024 |
|
| 1025 |
-- If the end of Source is reached before the characters in New_Item |
| 1026 |
-- are exhausted, the remaining characters from New_Item are appended |
| 1027 |
-- to the string. |
| 1028 |
|
| 1029 |
and then |
| 1030 |
(if Position <= Source'Last - New_Item'Length |
| 1031 |
then |
| 1032 |
Overwrite'Result |
| 1033 |
(Position - Source'First + New_Item'Length + 1 |
| 1034 |
.. Overwrite'Result'Last) |
| 1035 |
= Source (Position + New_Item'Length .. Source'Last)), |
| 1036 |
Global => null; |
| 1037 |
-- Propagates Index_Error if Position is not in |
| 1038 |
-- Source'First .. Source'Last + 1; otherwise, returns the string obtained |
| 1039 |
-- from Source by consecutively replacing characters starting at Position |
| 1040 |
-- with corresponding characters from New_Item. If the end of Source is |
| 1041 |
-- reached before the characters in New_Item are exhausted, the remaining |
| 1042 |
-- characters from New_Item are appended to the string. |
| 1043 |
|
| 1044 |
procedure Overwrite |
| 1045 |
(Source : in out String; |
| 1046 |
Position : Positive; |
| 1047 |
New_Item : String; |
| 1048 |
Drop : Truncation := Right) |
| 1049 |
with |
| 1050 |
Pre => |
| 1051 |
Position - 1 in Source'First - 1 .. Source'Last |
| 1052 |
and then |
| 1053 |
(if Position - Source'First >= Source'Length - New_Item'Length |
| 1054 |
then Position - Source'First <= Natural'Last - New_Item'Length), |
| 1055 |
Global => null, |
| 1056 |
Exceptional_Cases => (Length_Error => Drop = Error); |
| 1057 |
-- Equivalent to Move(Overwrite(Source, Position, New_Item), Source, Drop) |
| 1058 |
|
| 1059 |
function Delete |
| 1060 |
(Source : String; |
| 1061 |
From : Positive; |
| 1062 |
Through : Natural) return String |
| 1063 |
with |
| 1064 |
Pre => From > Through |
| 1065 |
or else (From - 1 <= Source'Last |
| 1066 |
and then Through >= Source'First - 1), |
| 1067 |
|
| 1068 |
-- Lower bound of the returned string is 1 |
| 1069 |
|
| 1070 |
Post => |
| 1071 |
Delete'Result'First = 1, |
| 1072 |
|
| 1073 |
Contract_Cases => |
| 1074 |
|
| 1075 |
-- If From <= Through, the characters between From and Through are |
| 1076 |
-- removed. |
| 1077 |
|
| 1078 |
(From <= Through => |
| 1079 |
|
| 1080 |
-- Length of the returned string |
| 1081 |
|
| 1082 |
Delete'Result'Length = |
| 1083 |
Integer'Max (0, From - Source'First) |
| 1084 |
+ Integer'Max (Source'Last - Through, 0) |
| 1085 |
|
| 1086 |
-- Elements before From are preserved |
| 1087 |
|
| 1088 |
and then |
| 1089 |
Delete'Result (1 .. Integer'Max (0, From - Source'First)) |
| 1090 |
= Source (Source'First .. From - 1) |
| 1091 |
|
| 1092 |
-- If there are remaining characters after Through, they are |
| 1093 |
-- appended to the returned string. |
| 1094 |
|
| 1095 |
and then |
| 1096 |
(if Through < Source'Last |
| 1097 |
then |
| 1098 |
Delete'Result |
| 1099 |
(Integer'Max (0, From - Source'First) + 1 |
| 1100 |
.. Delete'Result'Last) |
| 1101 |
= Source (Through + 1 .. Source'Last)), |
| 1102 |
|
| 1103 |
-- Otherwise, the returned string is Source with lower bound 1 |
| 1104 |
|
| 1105 |
others => |
| 1106 |
Delete'Result'Length = Source'Length |
| 1107 |
and then Delete'Result = Source), |
| 1108 |
Global => null; |
| 1109 |
-- If From <= Through, the returned string is |
| 1110 |
-- Replace_Slice(Source, From, Through, ""); otherwise, it is Source with |
| 1111 |
-- lower bound 1. |
| 1112 |
|
| 1113 |
procedure Delete |
| 1114 |
(Source : in out String; |
| 1115 |
From : Positive; |
| 1116 |
Through : Natural; |
| 1117 |
Justify : Alignment := Left; |
| 1118 |
Pad : Character := Space) |
| 1119 |
with |
| 1120 |
Pre => (if From <= Through |
| 1121 |
then (From in Source'Range |
| 1122 |
and then Through <= Source'Last)), |
| 1123 |
Global => null; |
| 1124 |
-- Equivalent to: |
| 1125 |
-- |
| 1126 |
-- Move (Delete (Source, From, Through), |
| 1127 |
-- Source, Justify => Justify, Pad => Pad). |
| 1128 |
|
| 1129 |
--------------------------------- |
| 1130 |
-- String Selector Subprograms -- |
| 1131 |
--------------------------------- |
| 1132 |
|
| 1133 |
function Trim |
| 1134 |
(Source : String; |
| 1135 |
Side : Trim_End) return String |
| 1136 |
with |
| 1137 |
Post => |
| 1138 |
|
| 1139 |
-- Lower bound of the returned string is 1 |
| 1140 |
|
| 1141 |
Trim'Result'First = 1 |
| 1142 |
|
| 1143 |
-- If all characters in Source are Space, the returned string is |
| 1144 |
-- empty. |
| 1145 |
|
| 1146 |
and then |
| 1147 |
(if (for all J in Source'Range => Source (J) = ' ') |
| 1148 |
then Trim'Result = "" |
| 1149 |
|
| 1150 |
-- Otherwise, the returned string is a slice of Source |
| 1151 |
|
| 1152 |
else |
| 1153 |
(declare |
| 1154 |
Low : constant Positive := |
| 1155 |
(if Side = Right then Source'First |
| 1156 |
else Index_Non_Blank (Source, Forward)); |
| 1157 |
High : constant Positive := |
| 1158 |
(if Side = Left then Source'Last |
| 1159 |
else Index_Non_Blank (Source, Backward)); |
| 1160 |
begin |
| 1161 |
Trim'Result = Source (Low .. High))), |
| 1162 |
Global => null; |
| 1163 |
-- Returns the string obtained by removing from Source all leading Space |
| 1164 |
-- characters (if Side = Left), all trailing Space characters (if |
| 1165 |
-- Side = Right), or all leading and trailing Space characters (if |
| 1166 |
-- Side = Both). |
| 1167 |
|
| 1168 |
procedure Trim |
| 1169 |
(Source : in out String; |
| 1170 |
Side : Trim_End; |
| 1171 |
Justify : Alignment := Left; |
| 1172 |
Pad : Character := Space) |
| 1173 |
with |
| 1174 |
Global => null; |
| 1175 |
-- Equivalent to: |
| 1176 |
-- |
| 1177 |
-- Move (Trim (Source, Side), Source, Justify=>Justify, Pad=>Pad). |
| 1178 |
|
| 1179 |
function Trim |
| 1180 |
(Source : String; |
| 1181 |
Left : Maps.Character_Set; |
| 1182 |
Right : Maps.Character_Set) return String |
| 1183 |
with |
| 1184 |
Post => |
| 1185 |
|
| 1186 |
-- Lower bound of the returned string is 1 |
| 1187 |
|
| 1188 |
Trim'Result'First = 1 |
| 1189 |
|
| 1190 |
-- If all characters are contained in one of the sets Left and Right, |
| 1191 |
-- then the returned string is empty. |
| 1192 |
|
| 1193 |
and then |
| 1194 |
(if (for all K in Source'Range => |
| 1195 |
Ada.Strings.Maps.Is_In (Source (K), Left)) |
| 1196 |
or |
| 1197 |
(for all K in Source'Range => |
| 1198 |
Ada.Strings.Maps.Is_In (Source (K), Right)) |
| 1199 |
then Trim'Result = "" |
| 1200 |
|
| 1201 |
-- Otherwise, the returned string is a slice of Source |
| 1202 |
|
| 1203 |
else |
| 1204 |
(declare |
| 1205 |
Low : constant Positive := |
| 1206 |
Index (Source, Left, Outside, Forward); |
| 1207 |
High : constant Positive := |
| 1208 |
Index (Source, Right, Outside, Backward); |
| 1209 |
begin |
| 1210 |
Trim'Result = Source (Low .. High))), |
| 1211 |
Global => null; |
| 1212 |
-- Returns the string obtained by removing from Source all leading |
| 1213 |
-- characters in Left and all trailing characters in Right. |
| 1214 |
|
| 1215 |
procedure Trim |
| 1216 |
(Source : in out String; |
| 1217 |
Left : Maps.Character_Set; |
| 1218 |
Right : Maps.Character_Set; |
| 1219 |
Justify : Alignment := Strings.Left; |
| 1220 |
Pad : Character := Space) |
| 1221 |
with |
| 1222 |
Global => null; |
| 1223 |
-- Equivalent to: |
| 1224 |
-- |
| 1225 |
-- Move (Trim (Source, Left, Right), |
| 1226 |
-- Source, Justify => Justify, Pad=>Pad). |
| 1227 |
|
| 1228 |
function Head |
| 1229 |
(Source : String; |
| 1230 |
Count : Natural; |
| 1231 |
Pad : Character := Space) return String |
| 1232 |
with |
| 1233 |
Post => |
| 1234 |
|
| 1235 |
-- Lower bound of the returned string is 1 |
| 1236 |
|
| 1237 |
Head'Result'First = 1 |
| 1238 |
|
| 1239 |
-- Length of the returned string is Count. |
| 1240 |
|
| 1241 |
and then Head'Result'Length = Count, |
| 1242 |
|
| 1243 |
Contract_Cases => |
| 1244 |
|
| 1245 |
-- If Count <= Source'Length, then the first Count characters of |
| 1246 |
-- Source are returned. |
| 1247 |
|
| 1248 |
(Count <= Source'Length => |
| 1249 |
Head'Result = Source (Source'First .. Source'First - 1 + Count), |
| 1250 |
|
| 1251 |
-- Otherwise, the returned string is Source concatenated with |
| 1252 |
-- Count - Source'Length Pad characters. |
| 1253 |
|
| 1254 |
others => |
| 1255 |
Head'Result (1 .. Source'Length) = Source |
| 1256 |
and then |
| 1257 |
Head'Result (Source'Length + 1 .. Count) |
| 1258 |
= [1 .. Count - Source'Length => Pad]), |
| 1259 |
Global => null; |
| 1260 |
-- Returns a string of length Count. If Count <= Source'Length, the string |
| 1261 |
-- comprises the first Count characters of Source. Otherwise, its contents |
| 1262 |
-- are Source concatenated with Count - Source'Length Pad characters. |
| 1263 |
|
| 1264 |
procedure Head |
| 1265 |
(Source : in out String; |
| 1266 |
Count : Natural; |
| 1267 |
Justify : Alignment := Left; |
| 1268 |
Pad : Character := Space) |
| 1269 |
with |
| 1270 |
Global => null, |
| 1271 |
Exceptional_Cases => (Length_Error => Count > Source'Length'Old); |
| 1272 |
-- Equivalent to: |
| 1273 |
-- |
| 1274 |
-- Move (Head (Source, Count, Pad), |
| 1275 |
-- Source, Drop => Error, Justify => Justify, Pad => Pad). |
| 1276 |
|
| 1277 |
function Tail |
| 1278 |
(Source : String; |
| 1279 |
Count : Natural; |
| 1280 |
Pad : Character := Space) return String |
| 1281 |
with |
| 1282 |
Post => |
| 1283 |
|
| 1284 |
-- Lower bound of the returned string is 1 |
| 1285 |
|
| 1286 |
Tail'Result'First = 1 |
| 1287 |
|
| 1288 |
-- Length of the returned string is Count |
| 1289 |
|
| 1290 |
and then Tail'Result'Length = Count, |
| 1291 |
Contract_Cases => |
| 1292 |
|
| 1293 |
-- If Count is zero, then the returned string is empty |
| 1294 |
|
| 1295 |
(Count = 0 => |
| 1296 |
Tail'Result = "", |
| 1297 |
|
| 1298 |
-- Otherwise, if Count <= Source'Length, then the last Count |
| 1299 |
-- characters of Source are returned. |
| 1300 |
|
| 1301 |
(Count in 1 .. Source'Length) => |
| 1302 |
Tail'Result = Source (Source'Last - Count + 1 .. Source'Last), |
| 1303 |
|
| 1304 |
-- Otherwise, the returned string is Count - Source'Length Pad |
| 1305 |
-- characters concatenated with Source. |
| 1306 |
|
| 1307 |
others => |
| 1308 |
|
| 1309 |
-- If Source is empty, then the returned string is Count Pad |
| 1310 |
-- characters. |
| 1311 |
|
| 1312 |
(if Source'Length = 0 |
| 1313 |
then Tail'Result = [1 .. Count => Pad] |
| 1314 |
else |
| 1315 |
Tail'Result (1 .. Count - Source'Length) |
| 1316 |
= [1 .. Count - Source'Length => Pad] |
| 1317 |
and then |
| 1318 |
Tail'Result (Count - Source'Length + 1 .. Tail'Result'Last) |
| 1319 |
= Source)), |
| 1320 |
Global => null; |
| 1321 |
-- Returns a string of length Count. If Count <= Source'Length, the string |
| 1322 |
-- comprises the last Count characters of Source. Otherwise, its contents |
| 1323 |
-- are Count-Source'Length Pad characters concatenated with Source. |
| 1324 |
|
| 1325 |
procedure Tail |
| 1326 |
(Source : in out String; |
| 1327 |
Count : Natural; |
| 1328 |
Justify : Alignment := Left; |
| 1329 |
Pad : Character := Space) |
| 1330 |
with |
| 1331 |
Global => null, |
| 1332 |
Exceptional_Cases => (Length_Error => Count > Source'Length'Old); |
| 1333 |
-- Equivalent to: |
| 1334 |
-- |
| 1335 |
-- Move (Tail (Source, Count, Pad), |
| 1336 |
-- Source, Drop => Error, Justify => Justify, Pad => Pad). |
| 1337 |
|
| 1338 |
---------------------------------- |
| 1339 |
-- String Constructor Functions -- |
| 1340 |
---------------------------------- |
| 1341 |
|
| 1342 |
function "*" |
| 1343 |
(Left : Natural; |
| 1344 |
Right : Character) return String |
| 1345 |
with |
| 1346 |
Post => |
| 1347 |
|
| 1348 |
-- Lower bound of the returned string is 1 |
| 1349 |
|
| 1350 |
"*"'Result'First = 1 |
| 1351 |
|
| 1352 |
-- Length of the returned string |
| 1353 |
|
| 1354 |
and then "*"'Result'Length = Left |
| 1355 |
|
| 1356 |
-- All characters of the returned string are Right |
| 1357 |
|
| 1358 |
and then (for all C of "*"'Result => C = Right), |
| 1359 |
Global => null; |
| 1360 |
|
| 1361 |
function "*" |
| 1362 |
(Left : Natural; |
| 1363 |
Right : String) return String |
| 1364 |
with |
| 1365 |
Pre => (if Right'Length /= 0 then Left <= Natural'Last / Right'Length), |
| 1366 |
|
| 1367 |
Post => |
| 1368 |
|
| 1369 |
-- Lower bound of the returned string is 1 |
| 1370 |
|
| 1371 |
"*"'Result'First = 1 |
| 1372 |
|
| 1373 |
-- Length of the returned string |
| 1374 |
|
| 1375 |
and then "*"'Result'Length = Left * Right'Length |
| 1376 |
|
| 1377 |
-- Content of the string is Right concatenated with itself Left times |
| 1378 |
|
| 1379 |
and then |
| 1380 |
(for all K in "*"'Result'Range => |
| 1381 |
"*"'Result (K) = Right (Right'First + (K - 1) mod Right'Length)), |
| 1382 |
Global => null; |
| 1383 |
|
| 1384 |
-- These functions replicate a character or string a specified number of |
| 1385 |
-- times. The first function returns a string whose length is Left and each |
| 1386 |
-- of whose elements is Right. The second function returns a string whose |
| 1387 |
-- length is Left * Right'Length and whose value is the null string if |
| 1388 |
-- Left = 0 and otherwise is (Left - 1)*Right & Right with lower bound 1. |
| 1389 |
|
| 1390 |
end Ada.Strings.Fixed; |