| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT RUN-TIME COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- A D A . S T R I N G S . U N B O U N D E D -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 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 |
with Ada.Strings.Fixed; |
| 33 |
with Ada.Unchecked_Deallocation; |
| 34 |
|
| 35 |
package body Ada.Strings.Unbounded is |
| 36 |
|
| 37 |
function Sum (Left, Right : Natural) return Natural with Inline; |
| 38 |
-- Returns summary of Left and Right, raise Constraint_Error on overflow |
| 39 |
|
| 40 |
function Mul (Left, Right : Natural) return Natural with Inline; |
| 41 |
-- Returns multiplication of Left and Right, raise Constraint_Error on |
| 42 |
-- overflow. |
| 43 |
|
| 44 |
function Saturated_Sum (Left, Right : Natural) return Natural; |
| 45 |
-- Returns summary of Left and Right or Natural'Last on overflow |
| 46 |
|
| 47 |
function Saturated_Mul (Left, Right : Positive) return Positive; |
| 48 |
-- Returns multiplication of Left and Right or Natural'Last on overflow |
| 49 |
|
| 50 |
--------- |
| 51 |
-- "&" -- |
| 52 |
--------- |
| 53 |
|
| 54 |
function "&" |
| 55 |
(Left : Unbounded_String; |
| 56 |
Right : Unbounded_String) return Unbounded_String |
| 57 |
is |
| 58 |
L_Length : constant Natural := Left.Last; |
| 59 |
R_Length : constant Natural := Right.Last; |
| 60 |
Result : Unbounded_String; |
| 61 |
|
| 62 |
begin |
| 63 |
Result.Last := Sum (L_Length, R_Length); |
| 64 |
|
| 65 |
Result.Reference := new String (1 .. Result.Last); |
| 66 |
|
| 67 |
Result.Reference (1 .. L_Length) := |
| 68 |
Left.Reference (1 .. Left.Last); |
| 69 |
|
| 70 |
if L_Length < Natural'Last then |
| 71 |
Result.Reference (L_Length + 1 .. Result.Last) := |
| 72 |
Right.Reference (1 .. Right.Last); |
| 73 |
end if; |
| 74 |
|
| 75 |
return Result; |
| 76 |
end "&"; |
| 77 |
|
| 78 |
function "&" |
| 79 |
(Left : Unbounded_String; |
| 80 |
Right : String) return Unbounded_String |
| 81 |
is |
| 82 |
L_Length : constant Natural := Left.Last; |
| 83 |
Result : Unbounded_String; |
| 84 |
|
| 85 |
begin |
| 86 |
Result.Last := Sum (L_Length, Right'Length); |
| 87 |
|
| 88 |
Result.Reference := new String (1 .. Result.Last); |
| 89 |
|
| 90 |
Result.Reference (1 .. L_Length) := Left.Reference (1 .. Left.Last); |
| 91 |
|
| 92 |
if L_Length < Natural'Last then |
| 93 |
Result.Reference (L_Length + 1 .. Result.Last) := Right; |
| 94 |
end if; |
| 95 |
|
| 96 |
return Result; |
| 97 |
end "&"; |
| 98 |
|
| 99 |
function "&" |
| 100 |
(Left : String; |
| 101 |
Right : Unbounded_String) return Unbounded_String |
| 102 |
is |
| 103 |
R_Length : constant Natural := Right.Last; |
| 104 |
Result : Unbounded_String; |
| 105 |
|
| 106 |
begin |
| 107 |
Result.Last := Sum (Left'Length, R_Length); |
| 108 |
|
| 109 |
Result.Reference := new String (1 .. Result.Last); |
| 110 |
|
| 111 |
Result.Reference (1 .. Left'Length) := Left; |
| 112 |
|
| 113 |
if Left'Length < Natural'Last then |
| 114 |
Result.Reference (Left'Length + 1 .. Result.Last) := |
| 115 |
Right.Reference (1 .. Right.Last); |
| 116 |
end if; |
| 117 |
|
| 118 |
return Result; |
| 119 |
end "&"; |
| 120 |
|
| 121 |
function "&" |
| 122 |
(Left : Unbounded_String; |
| 123 |
Right : Character) return Unbounded_String |
| 124 |
is |
| 125 |
Result : Unbounded_String; |
| 126 |
|
| 127 |
begin |
| 128 |
Result.Last := Sum (Left.Last, 1); |
| 129 |
|
| 130 |
Result.Reference := new String (1 .. Result.Last); |
| 131 |
|
| 132 |
Result.Reference (1 .. Result.Last - 1) := |
| 133 |
Left.Reference (1 .. Left.Last); |
| 134 |
Result.Reference (Result.Last) := Right; |
| 135 |
|
| 136 |
return Result; |
| 137 |
end "&"; |
| 138 |
|
| 139 |
function "&" |
| 140 |
(Left : Character; |
| 141 |
Right : Unbounded_String) return Unbounded_String |
| 142 |
is |
| 143 |
Result : Unbounded_String; |
| 144 |
|
| 145 |
begin |
| 146 |
Result.Last := Sum (Right.Last, 1); |
| 147 |
|
| 148 |
Result.Reference := new String (1 .. Result.Last); |
| 149 |
Result.Reference (1) := Left; |
| 150 |
Result.Reference (2 .. Result.Last) := |
| 151 |
Right.Reference (1 .. Right.Last); |
| 152 |
return Result; |
| 153 |
end "&"; |
| 154 |
|
| 155 |
--------- |
| 156 |
-- "*" -- |
| 157 |
--------- |
| 158 |
|
| 159 |
function "*" |
| 160 |
(Left : Natural; |
| 161 |
Right : Character) return Unbounded_String |
| 162 |
is |
| 163 |
Result : Unbounded_String; |
| 164 |
|
| 165 |
begin |
| 166 |
Result.Last := Left; |
| 167 |
|
| 168 |
Result.Reference := new String (1 .. Left); |
| 169 |
for J in Result.Reference'Range loop |
| 170 |
Result.Reference (J) := Right; |
| 171 |
end loop; |
| 172 |
|
| 173 |
return Result; |
| 174 |
end "*"; |
| 175 |
|
| 176 |
function "*" |
| 177 |
(Left : Natural; |
| 178 |
Right : String) return Unbounded_String |
| 179 |
is |
| 180 |
Len : constant Natural := Right'Length; |
| 181 |
K : Natural; |
| 182 |
Result : Unbounded_String; |
| 183 |
|
| 184 |
begin |
| 185 |
if Len = 0 then |
| 186 |
return Null_Unbounded_String; |
| 187 |
end if; |
| 188 |
|
| 189 |
Result.Last := Mul (Left, Len); |
| 190 |
|
| 191 |
Result.Reference := new String (1 .. Result.Last); |
| 192 |
|
| 193 |
K := 0; |
| 194 |
for J in 1 .. Left loop |
| 195 |
Result.Reference (K + 1 .. K + Len) := Right; |
| 196 |
K := K + Len; |
| 197 |
end loop; |
| 198 |
|
| 199 |
return Result; |
| 200 |
end "*"; |
| 201 |
|
| 202 |
function "*" |
| 203 |
(Left : Natural; |
| 204 |
Right : Unbounded_String) return Unbounded_String |
| 205 |
is |
| 206 |
Len : constant Natural := Right.Last; |
| 207 |
K : Natural; |
| 208 |
Result : Unbounded_String; |
| 209 |
|
| 210 |
begin |
| 211 |
if Len = 0 then |
| 212 |
return Null_Unbounded_String; |
| 213 |
end if; |
| 214 |
|
| 215 |
Result.Last := Mul (Left, Len); |
| 216 |
|
| 217 |
Result.Reference := new String (1 .. Result.Last); |
| 218 |
|
| 219 |
K := 0; |
| 220 |
for J in 1 .. Left loop |
| 221 |
Result.Reference (K + 1 .. K + Len) := |
| 222 |
Right.Reference (1 .. Right.Last); |
| 223 |
K := K + Len; |
| 224 |
end loop; |
| 225 |
|
| 226 |
return Result; |
| 227 |
end "*"; |
| 228 |
|
| 229 |
--------- |
| 230 |
-- "<" -- |
| 231 |
--------- |
| 232 |
|
| 233 |
function "<" |
| 234 |
(Left : Unbounded_String; |
| 235 |
Right : Unbounded_String) return Boolean |
| 236 |
is |
| 237 |
begin |
| 238 |
return |
| 239 |
Left.Reference (1 .. Left.Last) < Right.Reference (1 .. Right.Last); |
| 240 |
end "<"; |
| 241 |
|
| 242 |
function "<" |
| 243 |
(Left : Unbounded_String; |
| 244 |
Right : String) return Boolean |
| 245 |
is |
| 246 |
begin |
| 247 |
return Left.Reference (1 .. Left.Last) < Right; |
| 248 |
end "<"; |
| 249 |
|
| 250 |
function "<" |
| 251 |
(Left : String; |
| 252 |
Right : Unbounded_String) return Boolean |
| 253 |
is |
| 254 |
begin |
| 255 |
return Left < Right.Reference (1 .. Right.Last); |
| 256 |
end "<"; |
| 257 |
|
| 258 |
---------- |
| 259 |
-- "<=" -- |
| 260 |
---------- |
| 261 |
|
| 262 |
function "<=" |
| 263 |
(Left : Unbounded_String; |
| 264 |
Right : Unbounded_String) return Boolean |
| 265 |
is |
| 266 |
begin |
| 267 |
return |
| 268 |
Left.Reference (1 .. Left.Last) <= Right.Reference (1 .. Right.Last); |
| 269 |
end "<="; |
| 270 |
|
| 271 |
function "<=" |
| 272 |
(Left : Unbounded_String; |
| 273 |
Right : String) return Boolean |
| 274 |
is |
| 275 |
begin |
| 276 |
return Left.Reference (1 .. Left.Last) <= Right; |
| 277 |
end "<="; |
| 278 |
|
| 279 |
function "<=" |
| 280 |
(Left : String; |
| 281 |
Right : Unbounded_String) return Boolean |
| 282 |
is |
| 283 |
begin |
| 284 |
return Left <= Right.Reference (1 .. Right.Last); |
| 285 |
end "<="; |
| 286 |
|
| 287 |
--------- |
| 288 |
-- "=" -- |
| 289 |
--------- |
| 290 |
|
| 291 |
function "=" |
| 292 |
(Left : Unbounded_String; |
| 293 |
Right : Unbounded_String) return Boolean |
| 294 |
is |
| 295 |
begin |
| 296 |
return |
| 297 |
Left.Reference (1 .. Left.Last) = Right.Reference (1 .. Right.Last); |
| 298 |
end "="; |
| 299 |
|
| 300 |
function "=" |
| 301 |
(Left : Unbounded_String; |
| 302 |
Right : String) return Boolean |
| 303 |
is |
| 304 |
begin |
| 305 |
return Left.Reference (1 .. Left.Last) = Right; |
| 306 |
end "="; |
| 307 |
|
| 308 |
function "=" |
| 309 |
(Left : String; |
| 310 |
Right : Unbounded_String) return Boolean |
| 311 |
is |
| 312 |
begin |
| 313 |
return Left = Right.Reference (1 .. Right.Last); |
| 314 |
end "="; |
| 315 |
|
| 316 |
--------- |
| 317 |
-- ">" -- |
| 318 |
--------- |
| 319 |
|
| 320 |
function ">" |
| 321 |
(Left : Unbounded_String; |
| 322 |
Right : Unbounded_String) return Boolean |
| 323 |
is |
| 324 |
begin |
| 325 |
return |
| 326 |
Left.Reference (1 .. Left.Last) > Right.Reference (1 .. Right.Last); |
| 327 |
end ">"; |
| 328 |
|
| 329 |
function ">" |
| 330 |
(Left : Unbounded_String; |
| 331 |
Right : String) return Boolean |
| 332 |
is |
| 333 |
begin |
| 334 |
return Left.Reference (1 .. Left.Last) > Right; |
| 335 |
end ">"; |
| 336 |
|
| 337 |
function ">" |
| 338 |
(Left : String; |
| 339 |
Right : Unbounded_String) return Boolean |
| 340 |
is |
| 341 |
begin |
| 342 |
return Left > Right.Reference (1 .. Right.Last); |
| 343 |
end ">"; |
| 344 |
|
| 345 |
---------- |
| 346 |
-- ">=" -- |
| 347 |
---------- |
| 348 |
|
| 349 |
function ">=" |
| 350 |
(Left : Unbounded_String; |
| 351 |
Right : Unbounded_String) return Boolean |
| 352 |
is |
| 353 |
begin |
| 354 |
return |
| 355 |
Left.Reference (1 .. Left.Last) >= Right.Reference (1 .. Right.Last); |
| 356 |
end ">="; |
| 357 |
|
| 358 |
function ">=" |
| 359 |
(Left : Unbounded_String; |
| 360 |
Right : String) return Boolean |
| 361 |
is |
| 362 |
begin |
| 363 |
return Left.Reference (1 .. Left.Last) >= Right; |
| 364 |
end ">="; |
| 365 |
|
| 366 |
function ">=" |
| 367 |
(Left : String; |
| 368 |
Right : Unbounded_String) return Boolean |
| 369 |
is |
| 370 |
begin |
| 371 |
return Left >= Right.Reference (1 .. Right.Last); |
| 372 |
end ">="; |
| 373 |
|
| 374 |
------------ |
| 375 |
-- Adjust -- |
| 376 |
------------ |
| 377 |
|
| 378 |
procedure Adjust (Object : in out Unbounded_String) is |
| 379 |
begin |
| 380 |
-- Copy string, except we do not copy the statically allocated null |
| 381 |
-- string since it can never be deallocated. Note that we do not copy |
| 382 |
-- extra string room here to avoid dragging unused allocated memory. |
| 383 |
|
| 384 |
if Object.Reference /= Null_String'Access then |
| 385 |
Object.Reference := new String'(Object.Reference (1 .. Object.Last)); |
| 386 |
end if; |
| 387 |
end Adjust; |
| 388 |
|
| 389 |
------------ |
| 390 |
-- Append -- |
| 391 |
------------ |
| 392 |
|
| 393 |
procedure Append |
| 394 |
(Source : in out Unbounded_String; |
| 395 |
New_Item : Unbounded_String) |
| 396 |
is |
| 397 |
begin |
| 398 |
Realloc_For_Chunk (Source, New_Item.Last); |
| 399 |
|
| 400 |
if Source.Last < Natural'Last then |
| 401 |
Source.Reference (Source.Last + 1 .. Source.Last + New_Item.Last) := |
| 402 |
New_Item.Reference (1 .. New_Item.Last); |
| 403 |
end if; |
| 404 |
|
| 405 |
Source.Last := Source.Last + New_Item.Last; |
| 406 |
end Append; |
| 407 |
|
| 408 |
procedure Append |
| 409 |
(Source : in out Unbounded_String; |
| 410 |
New_Item : String) |
| 411 |
is |
| 412 |
begin |
| 413 |
Realloc_For_Chunk (Source, New_Item'Length); |
| 414 |
|
| 415 |
if Source.Last < Natural'Last then |
| 416 |
Source.Reference (Source.Last + 1 .. Source.Last + New_Item'Length) := |
| 417 |
New_Item; |
| 418 |
end if; |
| 419 |
|
| 420 |
Source.Last := Source.Last + New_Item'Length; |
| 421 |
end Append; |
| 422 |
|
| 423 |
procedure Append |
| 424 |
(Source : in out Unbounded_String; |
| 425 |
New_Item : Character) |
| 426 |
is |
| 427 |
begin |
| 428 |
Realloc_For_Chunk (Source, 1); |
| 429 |
Source.Reference (Source.Last + 1) := New_Item; |
| 430 |
Source.Last := Source.Last + 1; |
| 431 |
end Append; |
| 432 |
|
| 433 |
----------- |
| 434 |
-- Count -- |
| 435 |
----------- |
| 436 |
|
| 437 |
function Count |
| 438 |
(Source : Unbounded_String; |
| 439 |
Pattern : String; |
| 440 |
Mapping : Maps.Character_Mapping := Maps.Identity) return Natural |
| 441 |
is |
| 442 |
begin |
| 443 |
return |
| 444 |
Search.Count (Source.Reference (1 .. Source.Last), Pattern, Mapping); |
| 445 |
end Count; |
| 446 |
|
| 447 |
function Count |
| 448 |
(Source : Unbounded_String; |
| 449 |
Pattern : String; |
| 450 |
Mapping : Maps.Character_Mapping_Function) return Natural |
| 451 |
is |
| 452 |
begin |
| 453 |
return |
| 454 |
Search.Count (Source.Reference (1 .. Source.Last), Pattern, Mapping); |
| 455 |
end Count; |
| 456 |
|
| 457 |
function Count |
| 458 |
(Source : Unbounded_String; |
| 459 |
Set : Maps.Character_Set) return Natural |
| 460 |
is |
| 461 |
begin |
| 462 |
return Search.Count (Source.Reference (1 .. Source.Last), Set); |
| 463 |
end Count; |
| 464 |
|
| 465 |
------------ |
| 466 |
-- Delete -- |
| 467 |
------------ |
| 468 |
|
| 469 |
function Delete |
| 470 |
(Source : Unbounded_String; |
| 471 |
From : Positive; |
| 472 |
Through : Natural) return Unbounded_String |
| 473 |
is |
| 474 |
begin |
| 475 |
return |
| 476 |
To_Unbounded_String |
| 477 |
(Fixed.Delete (Source.Reference (1 .. Source.Last), From, Through)); |
| 478 |
end Delete; |
| 479 |
|
| 480 |
procedure Delete |
| 481 |
(Source : in out Unbounded_String; |
| 482 |
From : Positive; |
| 483 |
Through : Natural) |
| 484 |
is |
| 485 |
begin |
| 486 |
if From > Through then |
| 487 |
null; |
| 488 |
|
| 489 |
elsif From - 1 > Source.Last then |
| 490 |
raise Index_Error; |
| 491 |
|
| 492 |
else |
| 493 |
declare |
| 494 |
Len : constant Natural := |
| 495 |
Natural'Min (Source.Last, Through) - From + 1; |
| 496 |
|
| 497 |
begin |
| 498 |
if Through < Natural'Last then |
| 499 |
Source.Reference (From .. Source.Last - Len) := |
| 500 |
Source.Reference (Through + 1 .. Source.Last); |
| 501 |
end if; |
| 502 |
|
| 503 |
Source.Last := Source.Last - Len; |
| 504 |
end; |
| 505 |
end if; |
| 506 |
end Delete; |
| 507 |
|
| 508 |
------------- |
| 509 |
-- Element -- |
| 510 |
------------- |
| 511 |
|
| 512 |
function Element |
| 513 |
(Source : Unbounded_String; |
| 514 |
Index : Positive) return Character |
| 515 |
is |
| 516 |
begin |
| 517 |
if Index <= Source.Last then |
| 518 |
return Source.Reference (Index); |
| 519 |
else |
| 520 |
raise Strings.Index_Error; |
| 521 |
end if; |
| 522 |
end Element; |
| 523 |
|
| 524 |
-------------- |
| 525 |
-- Finalize -- |
| 526 |
-------------- |
| 527 |
|
| 528 |
procedure Finalize (Object : in out Unbounded_String) is |
| 529 |
procedure Deallocate is |
| 530 |
new Ada.Unchecked_Deallocation (String, String_Access); |
| 531 |
|
| 532 |
begin |
| 533 |
-- Note: Don't try to free statically allocated null string |
| 534 |
|
| 535 |
if Object.Reference /= Null_String'Access then |
| 536 |
declare |
| 537 |
Old : String_Access := Object.Reference; |
| 538 |
-- The original reference cannot be null, so we must create a |
| 539 |
-- copy which will become null when deallocated. |
| 540 |
begin |
| 541 |
Deallocate (Old); |
| 542 |
Object.Reference := Null_Unbounded_String.Reference; |
| 543 |
end; |
| 544 |
Object.Last := 0; |
| 545 |
end if; |
| 546 |
end Finalize; |
| 547 |
|
| 548 |
---------------- |
| 549 |
-- Find_Token -- |
| 550 |
---------------- |
| 551 |
|
| 552 |
procedure Find_Token |
| 553 |
(Source : Unbounded_String; |
| 554 |
Set : Maps.Character_Set; |
| 555 |
From : Positive; |
| 556 |
Test : Strings.Membership; |
| 557 |
First : out Positive; |
| 558 |
Last : out Natural) |
| 559 |
is |
| 560 |
begin |
| 561 |
Search.Find_Token |
| 562 |
(Source.Reference (From .. Source.Last), Set, Test, First, Last); |
| 563 |
end Find_Token; |
| 564 |
|
| 565 |
procedure Find_Token |
| 566 |
(Source : Unbounded_String; |
| 567 |
Set : Maps.Character_Set; |
| 568 |
Test : Strings.Membership; |
| 569 |
First : out Positive; |
| 570 |
Last : out Natural) |
| 571 |
is |
| 572 |
begin |
| 573 |
Search.Find_Token |
| 574 |
(Source.Reference (1 .. Source.Last), Set, Test, First, Last); |
| 575 |
end Find_Token; |
| 576 |
|
| 577 |
---------- |
| 578 |
-- Free -- |
| 579 |
---------- |
| 580 |
|
| 581 |
procedure Free (X : in out String_Access) is |
| 582 |
procedure Deallocate is |
| 583 |
new Ada.Unchecked_Deallocation (String, String_Access); |
| 584 |
|
| 585 |
begin |
| 586 |
-- Note: Do not try to free statically allocated null string |
| 587 |
|
| 588 |
if X /= Null_Unbounded_String.Reference then |
| 589 |
Deallocate (X); |
| 590 |
end if; |
| 591 |
end Free; |
| 592 |
|
| 593 |
---------- |
| 594 |
-- Head -- |
| 595 |
---------- |
| 596 |
|
| 597 |
function Head |
| 598 |
(Source : Unbounded_String; |
| 599 |
Count : Natural; |
| 600 |
Pad : Character := Space) return Unbounded_String |
| 601 |
is |
| 602 |
begin |
| 603 |
return To_Unbounded_String |
| 604 |
(Fixed.Head (Source.Reference (1 .. Source.Last), Count, Pad)); |
| 605 |
end Head; |
| 606 |
|
| 607 |
procedure Head |
| 608 |
(Source : in out Unbounded_String; |
| 609 |
Count : Natural; |
| 610 |
Pad : Character := Space) |
| 611 |
is |
| 612 |
Old : String_Access := Source.Reference; |
| 613 |
begin |
| 614 |
Source.Reference := |
| 615 |
new String'(Fixed.Head (Source.Reference (1 .. Source.Last), |
| 616 |
Count, Pad)); |
| 617 |
Source.Last := Source.Reference'Length; |
| 618 |
Free (Old); |
| 619 |
end Head; |
| 620 |
|
| 621 |
----------- |
| 622 |
-- Index -- |
| 623 |
----------- |
| 624 |
|
| 625 |
function Index |
| 626 |
(Source : Unbounded_String; |
| 627 |
Pattern : String; |
| 628 |
Going : Strings.Direction := Strings.Forward; |
| 629 |
Mapping : Maps.Character_Mapping := Maps.Identity) return Natural |
| 630 |
is |
| 631 |
begin |
| 632 |
return Search.Index |
| 633 |
(Source.Reference (1 .. Source.Last), Pattern, Going, Mapping); |
| 634 |
end Index; |
| 635 |
|
| 636 |
function Index |
| 637 |
(Source : Unbounded_String; |
| 638 |
Pattern : String; |
| 639 |
Going : Direction := Forward; |
| 640 |
Mapping : Maps.Character_Mapping_Function) return Natural |
| 641 |
is |
| 642 |
begin |
| 643 |
return Search.Index |
| 644 |
(Source.Reference (1 .. Source.Last), Pattern, Going, Mapping); |
| 645 |
end Index; |
| 646 |
|
| 647 |
function Index |
| 648 |
(Source : Unbounded_String; |
| 649 |
Set : Maps.Character_Set; |
| 650 |
Test : Strings.Membership := Strings.Inside; |
| 651 |
Going : Strings.Direction := Strings.Forward) return Natural |
| 652 |
is |
| 653 |
begin |
| 654 |
return Search.Index |
| 655 |
(Source.Reference (1 .. Source.Last), Set, Test, Going); |
| 656 |
end Index; |
| 657 |
|
| 658 |
function Index |
| 659 |
(Source : Unbounded_String; |
| 660 |
Pattern : String; |
| 661 |
From : Positive; |
| 662 |
Going : Direction := Forward; |
| 663 |
Mapping : Maps.Character_Mapping := Maps.Identity) return Natural |
| 664 |
is |
| 665 |
begin |
| 666 |
return Search.Index |
| 667 |
(Source.Reference (1 .. Source.Last), Pattern, From, Going, Mapping); |
| 668 |
end Index; |
| 669 |
|
| 670 |
function Index |
| 671 |
(Source : Unbounded_String; |
| 672 |
Pattern : String; |
| 673 |
From : Positive; |
| 674 |
Going : Direction := Forward; |
| 675 |
Mapping : Maps.Character_Mapping_Function) return Natural |
| 676 |
is |
| 677 |
begin |
| 678 |
return Search.Index |
| 679 |
(Source.Reference (1 .. Source.Last), Pattern, From, Going, Mapping); |
| 680 |
end Index; |
| 681 |
|
| 682 |
function Index |
| 683 |
(Source : Unbounded_String; |
| 684 |
Set : Maps.Character_Set; |
| 685 |
From : Positive; |
| 686 |
Test : Membership := Inside; |
| 687 |
Going : Direction := Forward) return Natural |
| 688 |
is |
| 689 |
begin |
| 690 |
return Search.Index |
| 691 |
(Source.Reference (1 .. Source.Last), Set, From, Test, Going); |
| 692 |
end Index; |
| 693 |
|
| 694 |
function Index_Non_Blank |
| 695 |
(Source : Unbounded_String; |
| 696 |
Going : Strings.Direction := Strings.Forward) return Natural |
| 697 |
is |
| 698 |
begin |
| 699 |
return |
| 700 |
Search.Index_Non_Blank |
| 701 |
(Source.Reference (1 .. Source.Last), Going); |
| 702 |
end Index_Non_Blank; |
| 703 |
|
| 704 |
function Index_Non_Blank |
| 705 |
(Source : Unbounded_String; |
| 706 |
From : Positive; |
| 707 |
Going : Direction := Forward) return Natural |
| 708 |
is |
| 709 |
begin |
| 710 |
return |
| 711 |
Search.Index_Non_Blank |
| 712 |
(Source.Reference (1 .. Source.Last), From, Going); |
| 713 |
end Index_Non_Blank; |
| 714 |
|
| 715 |
---------------- |
| 716 |
-- Initialize -- |
| 717 |
---------------- |
| 718 |
|
| 719 |
procedure Initialize (Object : in out Unbounded_String) is |
| 720 |
begin |
| 721 |
Object.Reference := Null_Unbounded_String.Reference; |
| 722 |
Object.Last := 0; |
| 723 |
end Initialize; |
| 724 |
|
| 725 |
------------ |
| 726 |
-- Insert -- |
| 727 |
------------ |
| 728 |
|
| 729 |
function Insert |
| 730 |
(Source : Unbounded_String; |
| 731 |
Before : Positive; |
| 732 |
New_Item : String) return Unbounded_String |
| 733 |
is |
| 734 |
begin |
| 735 |
return To_Unbounded_String |
| 736 |
(Fixed.Insert (Source.Reference (1 .. Source.Last), Before, New_Item)); |
| 737 |
end Insert; |
| 738 |
|
| 739 |
procedure Insert |
| 740 |
(Source : in out Unbounded_String; |
| 741 |
Before : Positive; |
| 742 |
New_Item : String) |
| 743 |
is |
| 744 |
begin |
| 745 |
if Before - 1 > Source.Last then |
| 746 |
raise Index_Error; |
| 747 |
end if; |
| 748 |
|
| 749 |
Realloc_For_Chunk (Source, New_Item'Length); |
| 750 |
|
| 751 |
if Before <= Source.Last then |
| 752 |
Source.Reference |
| 753 |
(Before + New_Item'Length .. Source.Last + New_Item'Length) := |
| 754 |
Source.Reference (Before .. Source.Last); |
| 755 |
end if; |
| 756 |
|
| 757 |
Source.Reference (Before .. Before - 1 + New_Item'Length) := New_Item; |
| 758 |
Source.Last := Source.Last + New_Item'Length; |
| 759 |
end Insert; |
| 760 |
|
| 761 |
------------ |
| 762 |
-- Length -- |
| 763 |
------------ |
| 764 |
|
| 765 |
function Length (Source : Unbounded_String) return Natural is |
| 766 |
(Source.Last); |
| 767 |
|
| 768 |
--------- |
| 769 |
-- Mul -- |
| 770 |
--------- |
| 771 |
|
| 772 |
function Mul (Left, Right : Natural) return Natural is |
| 773 |
pragma Unsuppress (Overflow_Check); |
| 774 |
begin |
| 775 |
return Left * Right; |
| 776 |
end Mul; |
| 777 |
|
| 778 |
--------------- |
| 779 |
-- Overwrite -- |
| 780 |
--------------- |
| 781 |
|
| 782 |
function Overwrite |
| 783 |
(Source : Unbounded_String; |
| 784 |
Position : Positive; |
| 785 |
New_Item : String) return Unbounded_String |
| 786 |
is |
| 787 |
begin |
| 788 |
return To_Unbounded_String |
| 789 |
(Fixed.Overwrite |
| 790 |
(Source.Reference (1 .. Source.Last), Position, New_Item)); |
| 791 |
end Overwrite; |
| 792 |
|
| 793 |
procedure Overwrite |
| 794 |
(Source : in out Unbounded_String; |
| 795 |
Position : Positive; |
| 796 |
New_Item : String) |
| 797 |
is |
| 798 |
NL : constant Natural := New_Item'Length; |
| 799 |
begin |
| 800 |
if Position - 1 <= Source.Last - NL then |
| 801 |
Source.Reference (Position .. Position - 1 + NL) := New_Item; |
| 802 |
else |
| 803 |
declare |
| 804 |
Old : String_Access := Source.Reference; |
| 805 |
begin |
| 806 |
Source.Reference := new String' |
| 807 |
(Fixed.Overwrite |
| 808 |
(Source.Reference (1 .. Source.Last), Position, New_Item)); |
| 809 |
Source.Last := Source.Reference'Length; |
| 810 |
Free (Old); |
| 811 |
end; |
| 812 |
end if; |
| 813 |
end Overwrite; |
| 814 |
|
| 815 |
--------------- |
| 816 |
-- Put_Image -- |
| 817 |
--------------- |
| 818 |
|
| 819 |
procedure Put_Image |
| 820 |
(S : in out Ada.Strings.Text_Buffers.Root_Buffer_Type'Class; |
| 821 |
V : Unbounded_String) is |
| 822 |
begin |
| 823 |
String'Put_Image (S, To_String (V)); |
| 824 |
end Put_Image; |
| 825 |
|
| 826 |
----------------------- |
| 827 |
-- Realloc_For_Chunk -- |
| 828 |
----------------------- |
| 829 |
|
| 830 |
procedure Realloc_For_Chunk |
| 831 |
(Source : in out Unbounded_String; |
| 832 |
Chunk_Size : Natural) |
| 833 |
is |
| 834 |
Growth_Factor : constant := 2; |
| 835 |
-- The growth factor controls how much extra space is allocated when |
| 836 |
-- we have to increase the size of an allocated unbounded string. By |
| 837 |
-- allocating extra space, we avoid the need to reallocate on every |
| 838 |
-- append, particularly important when a string is built up by repeated |
| 839 |
-- append operations of small pieces. This is expressed as a factor so |
| 840 |
-- 2 means add 1/2 of the length of the string as growth space. |
| 841 |
|
| 842 |
Min_Mul_Alloc : constant := Standard'Maximum_Alignment; |
| 843 |
-- Allocation will be done by a multiple of Min_Mul_Alloc This causes |
| 844 |
-- no memory loss as most (all?) malloc implementations are obliged to |
| 845 |
-- align the returned memory on the maximum alignment as malloc does not |
| 846 |
-- know the target alignment. |
| 847 |
|
| 848 |
S_Length : constant Natural := Source.Reference'Length; |
| 849 |
|
| 850 |
begin |
| 851 |
if Chunk_Size > S_Length - Source.Last then |
| 852 |
declare |
| 853 |
New_Size : constant Positive := |
| 854 |
Saturated_Sum |
| 855 |
(Sum (Source.Last, Chunk_Size), S_Length / Growth_Factor); |
| 856 |
|
| 857 |
New_Rounded_Up_Size : constant Positive := |
| 858 |
Saturated_Mul |
| 859 |
((New_Size - 1) / Min_Mul_Alloc + 1, Min_Mul_Alloc); |
| 860 |
|
| 861 |
Tmp : constant String_Access := |
| 862 |
new String (1 .. New_Rounded_Up_Size); |
| 863 |
|
| 864 |
Old : String_Access := Source.Reference; |
| 865 |
-- The original reference cannot be null, so we must create a copy |
| 866 |
-- which will become null when deallocated. |
| 867 |
|
| 868 |
begin |
| 869 |
Tmp (1 .. Source.Last) := Source.Reference (1 .. Source.Last); |
| 870 |
Free (Old); |
| 871 |
Source.Reference := Tmp; |
| 872 |
end; |
| 873 |
end if; |
| 874 |
end Realloc_For_Chunk; |
| 875 |
|
| 876 |
--------------------- |
| 877 |
-- Replace_Element -- |
| 878 |
--------------------- |
| 879 |
|
| 880 |
procedure Replace_Element |
| 881 |
(Source : in out Unbounded_String; |
| 882 |
Index : Positive; |
| 883 |
By : Character) |
| 884 |
is |
| 885 |
begin |
| 886 |
if Index <= Source.Last then |
| 887 |
Source.Reference (Index) := By; |
| 888 |
else |
| 889 |
raise Strings.Index_Error; |
| 890 |
end if; |
| 891 |
end Replace_Element; |
| 892 |
|
| 893 |
------------------- |
| 894 |
-- Replace_Slice -- |
| 895 |
------------------- |
| 896 |
|
| 897 |
function Replace_Slice |
| 898 |
(Source : Unbounded_String; |
| 899 |
Low : Positive; |
| 900 |
High : Natural; |
| 901 |
By : String) return Unbounded_String |
| 902 |
is |
| 903 |
begin |
| 904 |
return To_Unbounded_String |
| 905 |
(Fixed.Replace_Slice |
| 906 |
(Source.Reference (1 .. Source.Last), Low, High, By)); |
| 907 |
end Replace_Slice; |
| 908 |
|
| 909 |
procedure Replace_Slice |
| 910 |
(Source : in out Unbounded_String; |
| 911 |
Low : Positive; |
| 912 |
High : Natural; |
| 913 |
By : String) |
| 914 |
is |
| 915 |
Old : String_Access := Source.Reference; |
| 916 |
begin |
| 917 |
Source.Reference := new String' |
| 918 |
(Fixed.Replace_Slice |
| 919 |
(Source.Reference (1 .. Source.Last), Low, High, By)); |
| 920 |
Source.Last := Source.Reference'Length; |
| 921 |
Free (Old); |
| 922 |
end Replace_Slice; |
| 923 |
|
| 924 |
------------------- |
| 925 |
-- Saturated_Mul -- |
| 926 |
------------------- |
| 927 |
|
| 928 |
function Saturated_Mul (Left, Right : Positive) return Positive is |
| 929 |
begin |
| 930 |
return Mul (Left, Right); |
| 931 |
exception |
| 932 |
when Constraint_Error => |
| 933 |
return Natural'Last; |
| 934 |
end Saturated_Mul; |
| 935 |
|
| 936 |
------------------- |
| 937 |
-- Saturated_Sum -- |
| 938 |
------------------- |
| 939 |
|
| 940 |
function Saturated_Sum (Left, Right : Natural) return Natural is |
| 941 |
begin |
| 942 |
return Sum (Left, Right); |
| 943 |
exception |
| 944 |
when Constraint_Error => |
| 945 |
return Natural'Last; |
| 946 |
end Saturated_Sum; |
| 947 |
|
| 948 |
-------------------------- |
| 949 |
-- Set_Unbounded_String -- |
| 950 |
-------------------------- |
| 951 |
|
| 952 |
procedure Set_Unbounded_String |
| 953 |
(Target : out Unbounded_String; |
| 954 |
Source : String) |
| 955 |
is |
| 956 |
Old : String_Access := Target.Reference; |
| 957 |
begin |
| 958 |
Target.Last := Source'Length; |
| 959 |
Target.Reference := new String (1 .. Source'Length); |
| 960 |
Target.Reference.all := Source; |
| 961 |
Free (Old); |
| 962 |
end Set_Unbounded_String; |
| 963 |
|
| 964 |
----------- |
| 965 |
-- Slice -- |
| 966 |
----------- |
| 967 |
|
| 968 |
function Slice |
| 969 |
(Source : Unbounded_String; |
| 970 |
Low : Positive; |
| 971 |
High : Natural) return String |
| 972 |
is |
| 973 |
begin |
| 974 |
-- Note: test of High > Length is in accordance with AI95-00128 |
| 975 |
|
| 976 |
if Low - 1 > Source.Last or else High > Source.Last then |
| 977 |
raise Index_Error; |
| 978 |
else |
| 979 |
return Source.Reference (Low .. High); |
| 980 |
end if; |
| 981 |
end Slice; |
| 982 |
|
| 983 |
--------- |
| 984 |
-- Sum -- |
| 985 |
--------- |
| 986 |
|
| 987 |
function Sum (Left, Right : Natural) return Natural is |
| 988 |
pragma Unsuppress (Overflow_Check); |
| 989 |
begin |
| 990 |
return Left + Right; |
| 991 |
end Sum; |
| 992 |
|
| 993 |
---------- |
| 994 |
-- Tail -- |
| 995 |
---------- |
| 996 |
|
| 997 |
function Tail |
| 998 |
(Source : Unbounded_String; |
| 999 |
Count : Natural; |
| 1000 |
Pad : Character := Space) return Unbounded_String is |
| 1001 |
begin |
| 1002 |
return To_Unbounded_String |
| 1003 |
(Fixed.Tail (Source.Reference (1 .. Source.Last), Count, Pad)); |
| 1004 |
end Tail; |
| 1005 |
|
| 1006 |
procedure Tail |
| 1007 |
(Source : in out Unbounded_String; |
| 1008 |
Count : Natural; |
| 1009 |
Pad : Character := Space) |
| 1010 |
is |
| 1011 |
Old : String_Access := Source.Reference; |
| 1012 |
begin |
| 1013 |
Source.Reference := new String' |
| 1014 |
(Fixed.Tail (Source.Reference (1 .. Source.Last), Count, Pad)); |
| 1015 |
Source.Last := Source.Reference'Length; |
| 1016 |
Free (Old); |
| 1017 |
end Tail; |
| 1018 |
|
| 1019 |
--------------- |
| 1020 |
-- To_String -- |
| 1021 |
--------------- |
| 1022 |
|
| 1023 |
function To_String (Source : Unbounded_String) return String is |
| 1024 |
(Source.Reference (1 .. Source.Last)); |
| 1025 |
|
| 1026 |
------------------------- |
| 1027 |
-- To_Unbounded_String -- |
| 1028 |
------------------------- |
| 1029 |
|
| 1030 |
function To_Unbounded_String (Source : String) return Unbounded_String is |
| 1031 |
Result : Unbounded_String; |
| 1032 |
begin |
| 1033 |
-- Do not allocate an empty string: keep the default |
| 1034 |
|
| 1035 |
if Source'Length > 0 then |
| 1036 |
Result.Last := Source'Length; |
| 1037 |
Result.Reference := new String (1 .. Source'Length); |
| 1038 |
Result.Reference.all := Source; |
| 1039 |
end if; |
| 1040 |
|
| 1041 |
return Result; |
| 1042 |
end To_Unbounded_String; |
| 1043 |
|
| 1044 |
function To_Unbounded_String |
| 1045 |
(Length : Natural) return Unbounded_String |
| 1046 |
is |
| 1047 |
Result : Unbounded_String; |
| 1048 |
|
| 1049 |
begin |
| 1050 |
-- Do not allocate an empty string: keep the default |
| 1051 |
|
| 1052 |
if Length > 0 then |
| 1053 |
Result.Last := Length; |
| 1054 |
Result.Reference := new String (1 .. Length); |
| 1055 |
end if; |
| 1056 |
|
| 1057 |
return Result; |
| 1058 |
end To_Unbounded_String; |
| 1059 |
|
| 1060 |
--------------- |
| 1061 |
-- Translate -- |
| 1062 |
--------------- |
| 1063 |
|
| 1064 |
function Translate |
| 1065 |
(Source : Unbounded_String; |
| 1066 |
Mapping : Maps.Character_Mapping) return Unbounded_String |
| 1067 |
is |
| 1068 |
begin |
| 1069 |
return To_Unbounded_String |
| 1070 |
(Fixed.Translate (Source.Reference (1 .. Source.Last), Mapping)); |
| 1071 |
end Translate; |
| 1072 |
|
| 1073 |
procedure Translate |
| 1074 |
(Source : in out Unbounded_String; |
| 1075 |
Mapping : Maps.Character_Mapping) |
| 1076 |
is |
| 1077 |
begin |
| 1078 |
Fixed.Translate (Source.Reference (1 .. Source.Last), Mapping); |
| 1079 |
end Translate; |
| 1080 |
|
| 1081 |
function Translate |
| 1082 |
(Source : Unbounded_String; |
| 1083 |
Mapping : Maps.Character_Mapping_Function) return Unbounded_String |
| 1084 |
is |
| 1085 |
begin |
| 1086 |
return To_Unbounded_String |
| 1087 |
(Fixed.Translate (Source.Reference (1 .. Source.Last), Mapping)); |
| 1088 |
end Translate; |
| 1089 |
|
| 1090 |
procedure Translate |
| 1091 |
(Source : in out Unbounded_String; |
| 1092 |
Mapping : Maps.Character_Mapping_Function) |
| 1093 |
is |
| 1094 |
begin |
| 1095 |
Fixed.Translate (Source.Reference (1 .. Source.Last), Mapping); |
| 1096 |
end Translate; |
| 1097 |
|
| 1098 |
---------- |
| 1099 |
-- Trim -- |
| 1100 |
---------- |
| 1101 |
|
| 1102 |
function Trim |
| 1103 |
(Source : Unbounded_String; |
| 1104 |
Side : Trim_End) return Unbounded_String |
| 1105 |
is |
| 1106 |
begin |
| 1107 |
return To_Unbounded_String |
| 1108 |
(Fixed.Trim (Source.Reference (1 .. Source.Last), Side)); |
| 1109 |
end Trim; |
| 1110 |
|
| 1111 |
procedure Trim |
| 1112 |
(Source : in out Unbounded_String; |
| 1113 |
Side : Trim_End) |
| 1114 |
is |
| 1115 |
Old : String_Access := Source.Reference; |
| 1116 |
begin |
| 1117 |
Source.Reference := new String' |
| 1118 |
(Fixed.Trim (Source.Reference (1 .. Source.Last), Side)); |
| 1119 |
Source.Last := Source.Reference'Length; |
| 1120 |
Free (Old); |
| 1121 |
end Trim; |
| 1122 |
|
| 1123 |
function Trim |
| 1124 |
(Source : Unbounded_String; |
| 1125 |
Left : Maps.Character_Set; |
| 1126 |
Right : Maps.Character_Set) return Unbounded_String |
| 1127 |
is |
| 1128 |
begin |
| 1129 |
return To_Unbounded_String |
| 1130 |
(Fixed.Trim (Source.Reference (1 .. Source.Last), Left, Right)); |
| 1131 |
end Trim; |
| 1132 |
|
| 1133 |
procedure Trim |
| 1134 |
(Source : in out Unbounded_String; |
| 1135 |
Left : Maps.Character_Set; |
| 1136 |
Right : Maps.Character_Set) |
| 1137 |
is |
| 1138 |
Old : String_Access := Source.Reference; |
| 1139 |
begin |
| 1140 |
Source.Reference := new String' |
| 1141 |
(Fixed.Trim (Source.Reference (1 .. Source.Last), Left, Right)); |
| 1142 |
Source.Last := Source.Reference'Length; |
| 1143 |
Free (Old); |
| 1144 |
end Trim; |
| 1145 |
|
| 1146 |
--------------------- |
| 1147 |
-- Unbounded_Slice -- |
| 1148 |
--------------------- |
| 1149 |
|
| 1150 |
function Unbounded_Slice |
| 1151 |
(Source : Unbounded_String; |
| 1152 |
Low : Positive; |
| 1153 |
High : Natural) return Unbounded_String |
| 1154 |
is |
| 1155 |
begin |
| 1156 |
if Low - 1 > Source.Last or else High > Source.Last then |
| 1157 |
raise Index_Error; |
| 1158 |
else |
| 1159 |
return To_Unbounded_String (Source.Reference.all (Low .. High)); |
| 1160 |
end if; |
| 1161 |
end Unbounded_Slice; |
| 1162 |
|
| 1163 |
procedure Unbounded_Slice |
| 1164 |
(Source : Unbounded_String; |
| 1165 |
Target : out Unbounded_String; |
| 1166 |
Low : Positive; |
| 1167 |
High : Natural) |
| 1168 |
is |
| 1169 |
begin |
| 1170 |
if Low - 1 > Source.Last or else High > Source.Last then |
| 1171 |
raise Index_Error; |
| 1172 |
else |
| 1173 |
Target := To_Unbounded_String (Source.Reference.all (Low .. High)); |
| 1174 |
end if; |
| 1175 |
end Unbounded_Slice; |
| 1176 |
|
| 1177 |
end Ada.Strings.Unbounded; |