/usr/share/ada/adainclude/texttools/strings.adb is in libtexttools3-dev 2.1.0-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 | ------------------------------------------------------------------------------
-- STRINGS (package body) --
-- --
-- Part of TextTools --
-- Designed and Programmed by Ken O. Burtch --
-- --
------------------------------------------------------------------------------
-- --
-- Copyright (C) 1999-2007 Ken O. Burtch --
-- --
-- This is free software; you can redistribute it and/or modify it under --
-- terms of the GNU General Public License as published by the Free Soft- --
-- ware Foundation; either version 2, or (at your option) any later ver- --
-- sion. This is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
-- for more details. You should have received a copy of the GNU General --
-- Public License distributed with this; see file COPYING. If not, write --
-- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
-- MA 02111-1307, USA. --
-- --
-- As a special exception, if other files instantiate generics from this --
-- unit, or you link this unit with other files to produce an executable, --
-- this unit does not by itself cause the resulting executable to be --
-- covered by the GNU General Public License. This exception does not --
-- however invalidate any other reasons why the executable file might be --
-- covered by the GNU Public License. --
-- --
-- This is maintained at http://www.pegasoft.ca/tt.html --
-- --
------------------------------------------------------------------------------
with Ada.Strings.Fixed;
with Ada.Strings.Maps.Constants;
package body strings is
dips : constant string := "upanlyscolableutalisifensusteasauayeeieoeseyiaotoouuichetirontrshaithoaghurngeregundewhbackamedorvarine a d f o n r s e_r_s_e.";
Case_Mappings : constant array (Boolean) of Ada.Strings.Maps.Character_Mapping
:= (True => Ada.Strings.Maps.Constants.Upper_Case_Map,
False => Ada.Strings.Maps.Identity);
procedure FixSpacing( s : in out unbounded_string ) is
-- remove leading and trailing spaces, as well as any double-spaces inside
i : Integer := 1;
begin
Trim (S, Side => Ada.Strings.Both);
while i < length(s) loop
if Element( s, i ) = ' ' and then Element( s, i+1 ) = ' ' then
Delete( s, i, i );
i := i - 1;
end if;
i := i + 1;
end loop;
end FixSpacing;
function PhoneticsOf( s : string ) return String is
-- reduce string to ENGLISH phonetics
-- equivalences from Talking Tools pg.12 (and from guessing)
pos : natural := S'First; -- position in s
ppos : natural := 1; -- position in PhoneticString
PhoneticString : Unbounded_String := Null_Unbounded_String; -- the resulting phonetics
ch : character; -- current character in s
AllowDuplicate : boolean := false; -- TRUE to discard same adjacents
function NextChar return character is
-- get the next character (if none, return a space)
ch : character;
begin
if Pos < S'Last then
Ch := S (pos+1);
if ch >= 'A' and ch <= 'Z' then
Ch := character'val( character'pos(ch) + 32 );
end if;
return ch;
else
return ' ';
end if;
end NextChar;
procedure Add( c : character ) is
-- add a phoeme to the Phonetic String, discarding adjacent duplicates
-- if it's OK. Some very similar sounds are grouped together (th & d)
begin
if ppos = 1 or AllowDuplicate then
Append( PhoneticString, c );
ppos := ppos + 1;
AllowDuplicate := false;
else
if Element( PhoneticString, ppos-1 ) /= c then
Append( PhoneticString, c );
ppos := ppos + 1;
end if;
end if;
end Add;
procedure SkipChar is
-- macro to advance to next position in s
begin
pos := pos + 1;
end SkipChar;
pragma Inline( SkipChar );
begin
while Pos <= S'Last loop
ch := S (Pos);
if ch >= 'A' and ch <= 'Z' then
ch := character'val( character'pos(ch) + 32 );
end if;
case ch is
when 'a' =>
case NextChar is
when 'a'|'e'|'i'|'y' => -- aa, ae, ai, ay
Add( 'A' );
SkipChar;
when 'r' => -- ar
Add( 'R' );
SkipChar;
when 'u' => -- au
Add( 'U' );
SkipChar;
when others =>
Add( 'A' ); -- a
end case;
when 'b' => -- b
Add( 'B' );
when 'd' => -- d
Add( 'D' );
when 't' =>
if NextChar = 'h' then -- th (H)
Add( 'H' );
SkipChar;
else
Add( 'D' ); -- t (=d)
end if;
when 'p' =>
if NextChar = 'h' then -- ph (F)
Add( 'F' );
SkipChar;
else
Add( 'P' ); -- p
end if;
when 'c' => -- c*
if NextChar = 'h' then -- ch (Y)
Add( 'Y' );
SkipChar;
else
Add( 'C' );
end if;
when 'e' =>
case NextChar is
when 'a' => Add( 'E' ); SkipChar; -- ea
when 'i' => Add( 'I' ); SkipChar; -- ei
when 'e' => Add( 'E' ); SkipChar; -- ee
when 'r' => Add( 'R' ); SkipChar; -- er
when 'u' => Add( 'U' ); SkipChar; -- eu
when 'y' => Add( 'A' ); SkipChar; -- ey
when ' '|'?'|'''|':'|';'|'.'|',' => SkipChar; -- e (silent)
when others => -- e
Add( 'E' );
end case;
when 'f' => -- f
Add( 'F' );
when 'g' => -- gh
if NextChar = 'h' then
SkipChar;
else
Add( 'G' ); -- g*
end if;
when 'h' => -- h
null;
when 'i' => -- i
if NextChar = 'e' then -- ie
Add( 'E' );
SkipChar;
elsif NextChar = 'r' then -- ir
Add( 'R' );
SkipChar;
elsif NextChar = 'o' then -- ion
pos := pos + 1;
if NextChar = 'n' then
Add( 'U' );
Add( 'N' );
SkipChar;
else
pos := pos - 1; -- treat normally
Add( 'I' );
end if;
else
Add( 'I' );
end if;
when 'j' => -- j
Add( 'J' );
when 'k'|'q' => -- k
Add('K');
if NextChar = 'u' then -- qu (KW)
Add( 'W' );
SkipChar;
end if;
when 'l'|'r' => -- l, r
Add( 'R' );
when 'm' => -- m
Add( 'N' );
when 'n' =>
if NextChar = 'g' then
SkipChar; -- ng (=n)
end if;
Add( 'N' ); -- n
when 'o' =>
case NextChar is
when 'a' => -- oa
Add( 'O' );
SkipChar;
when 'o' => -- oo
Add( 'U' );
SkipChar;
when 'r' => -- or
Add( 'R' );
SkipChar;
when 'u' => -- ou
Add( 'U' );
SkipChar;
when others => -- o
Add( 'O' );
end case;
when 's' => -- sh (H)
if NextChar = 'h' then
Add( 'H' );
SkipChar;
else
Add( 'S' ); -- s
end if;
when 'u' =>
if NextChar = 'y' then -- uy
Add( 'I' );
SkipChar;
elsif NextChar = 'r' then -- ur
Add( 'R' );
SkipChar;
else
Add ( 'U' ); -- u
end if;
when 'v' => -- v
Add( 'V' );
when 'w' => -- w
Add( 'W' );
when 'x'|'z' => -- x, z
Add( 'Z' );
when 'y' => -- y
Add( 'I' );
when others =>
AllowDuplicate := true; -- allow two together if sep by sp, ', etc
if ch >= '0' and ch <= '9' then -- 0...9
Add( ch );
AllowDuplicate := true;
end if;
end case;
pos := pos + 1;
end loop;
return To_String (PhoneticString);
end PhoneticsOf;
function TypoOf( BadString, GoodString : String) return boolean is
-- 80% of all typos are single insertions, deletions, exchanges, or subs.
begin
if BadString = GoodString
or BadString'Length < 4
or GoodString'Length < 4 then
-- identical or too short to test reliably?
return false;
end if;
-- Single Insertion
if BadString'Length = GoodString'Length + 1 then
for I in BadString'Range loop
if BadString (Badstring'First .. I - 1)
& Badstring (I + 1 .. Badstring'Last) = GoodString then
return True;
end if;
end loop;
end if;
-- Single Deletion
if BadString'Length = GoodString'Length - 1 then
for i in GoodString'Range loop
if GoodString (Goodstring'First .. I - 1)
& Goodstring (I + 1 .. Goodstring'Last) = BadString then
return True;
end if;
end loop;
end if;
-- Single Exchange or Substitution
if BadString'Length = GoodString'Length then
declare
TempStr : String := BadString;
Tempchar : Character;
begin
for i in Badstring'First .. BadString'Last - 1 loop
TempChar := tempstr (I);
tempstr (I) := Tempstr (I + 1);
tempstr (I + 1) := Tempchar;
if TempStr = GoodString then
return True;
end if;
Tempstr (I .. I + 1) := Badstring (I .. I + 1);
Tempstr (I) := Goodstring (I - Tempstr'First + Goodstring'First);
if Tempstr = Goodstring then
return True;
end if;
Tempstr (I) := Badstring (I);
end loop;
end;
end if;
return False;
end TypoOf;
procedure Tokenize (S : in string;
Words : in out strlist.Vector;
ch : in out character ) is
-- encode a word as a character > 127
Index : Natural;
begin
Index := Words.Find_Index (S);
if Index = 0 or Index > 128 then
ch := character'val( Index ); --' ';
else
ch := character'val( Index + 127 );
end if;
end Tokenize;
procedure Untokenize (Ch : in Character;
Words : in out Strlist.Vector;
S : in out unbounded_string) is
begin
s := Null_Unbounded_String;
if character'pos( ch ) > 127 then
S := To_Unbounded_String (Words.Element (Character'Pos (Ch) - 127));
end if;
end Untokenize;
function FGREP (s : string;
text : string;
filter_out : boolean := false;
case_insensitive : boolean := false )
return Boolean
is
begin
return Ada.Strings.Fixed.Index (Text,
S,
Mapping => Case_Mappings (Case_Insensitive)) > 0
xor Filter_Out;
end FGREP;
function FGREP (s : string;
text : string;
filter_out : boolean := false;
case_insensitive : boolean := false )
return String
is
begin
if FGREP (S, Text, Filter_Out, Case_Insensitive) then
return text;
else
return "";
end if;
end FGREP;
procedure FGREP (s : in String;
text : in Strlist.Vector;
result : out boolean;
filter_out : boolean := false;
case_insensitive : boolean := false )
is
begin
Result := False;
for I in 1 .. Integer (Text.Length) loop
Result := FGREP (S, Text.Element (I), Filter_Out, Case_Insensitive);
exit when Result;
end loop;
end FGREP;
procedure FGREP (s : string;
text : in out Strlist.Vector;
filter_out : boolean := false;
case_insensitive : boolean := false )
is
I : Positive := 1;
begin
while I <= Integer (Text.Length) loop
if FGREP (S, Text.Element (I), Filter_Out, case_insensitive) then
I := I + 1;
else
Text.Delete (I);
end if;
end loop;
end FGREP;
---> ASCII Encode/Decode
separator : constant character := character'val(1);
procedure Encode( estr : in out EncodedString; i : integer ) is
begin
Append( estr, integer'image( i ) );
Append( estr, separator );
end Encode;
procedure Encode( estr : in out EncodedString; r : ARect ) is
begin
Encode( estr, r.left );
Encode( estr, r.top );
Encode( estr, r.right );
Encode( estr, r.bottom );
end Encode;
procedure Encode( estr : in out EncodedString; l : long_integer ) is
begin
Append( estr, long_integer'image( l ) );
Append( estr, separator );
end Encode;
procedure Encode( estr : in out EncodedString; s : string) is
begin
Append( estr, s);
Append( estr, separator );
end Encode;
procedure Encode( estr : in out EncodedString; c : character ) is
begin
Append( estr, c );
end Encode;
procedure Encode( estr : in out EncodedString; b : boolean ) is
begin
if b then
Append( estr, 'T' );
else
Append( estr, 'F' );
end if;
end Encode;
procedure Decode( estr : in out EncodedString; i : out integer ) is
idx : integer := 1;
begin
while Element( estr, idx ) /= separator loop
idx := idx + 1;
end loop;
i := integer'Value (Slice (estr, 1, Idx - 1));
Tail (estr, Length (Estr) - Idx);
end Decode;
procedure Decode( estr : in out EncodedString; r : out ARect ) is
begin
Decode( estr, r.left );
Decode( estr, r.top );
Decode( estr, r.right );
Decode( estr, r.bottom );
end Decode;
procedure Decode( estr : in out EncodedString; l : out long_integer ) is
idx : integer := 2;
begin
while Element( estr, idx ) /= separator loop
idx := idx + 1;
end loop;
l := long_integer'Value (Slice (estr, 1, Idx - 1));
Tail (estr, Length (Estr) - Idx);
end Decode;
procedure Decode( estr : in out EncodedString; s : out Unbounded_String) is
pos : constant Natural := Index (Estr, (1 => Separator));
begin
s := Head (estr, pos - 1 );
Tail (estr, Length (Estr) - Pos);
end Decode;
procedure Decode( estr : in out EncodedString; c : out character ) is
begin
c := Element( estr, 1 );
Tail (estr, Length (Estr) - 1);
end Decode;
procedure Decode( estr : in out EncodedString; b : out boolean ) is
c : character := ASCII.NUL;
begin
Decode( estr, c );
pragma Assert (C = 'T' or C = 'F');
b := (c = 'T');
end Decode;
-- BASIC PACK
--
-- Compress string s using dipthong compression resulting in a new string of
-- 50% to 100% the size of the original. s must contain only lower ASCII
-- characters since the upper ASCII characters are used for the compression.
------------------------------------------------------------------------------
function basic_pack( s : string ) return packed_string is
dip : string(1..2);
i : positive;
dip_pos : natural;
result : unbounded_string;
begin
i := s'first;
result := null_unbounded_string;
loop
exit when i > s'last;
dip_pos := 0;
if i /= s'last then
dip := s(i..i+1);
for j in dips'first..dips'last-1 loop
if dip = dips(j..j+1) then
dip_pos := j;
exit;
end if;
end loop;
end if;
if dip_pos > 0 then
result := result & character'val( dip_pos + 127 );
i := i + 2;
else
result := result & s(i);
i := i + 1;
end if;
end loop;
return packed_string( to_string( result ) );
end basic_pack;
-- UNPACK
--
-- Decompress string s that was compressed using basic_pack.
------------------------------------------------------------------------------
function unpack( s : packed_string ) return string is
dip_pos : positive;
newstr : unbounded_string;
begin
for i in s'range loop
if character'pos( s(i) ) >= 128 then
dip_pos := character'pos( s(i) ) - 127;
newstr := newstr & dips( dip_pos..dip_pos+1 );
else
newstr := newstr & s(i);
end if;
end loop;
return to_string( newstr );
end unpack;
end strings;
|