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 | -- -----------------------------------------------------------------------------
-- bbt, the black box tester (https://github.com/LionelDraghi/bbt)
-- Author : Lionel Draghi
-- SPDX-License-Identifier: APSL-2.0
-- SPDX-FileCopyrightText: 2024, Lionel Draghi
-- -----------------------------------------------------------------------------
with Ada.Characters.Handling;
with Ada.Strings.Equal_Case_Insensitive;
package body Text_Utilities is
-- --------------------------------------------------------------------------
function Is_Eq (S1, S2 : String;
Case_Insensitive : Boolean) return Boolean is
((Case_Insensitive and Ada.Strings.Equal_Case_Insensitive (S1, S2))
or else S1 = S2);
-- --------------------------------------------------------------------------
function Is_Equal (S1, S2 : String;
Case_Insensitive : Boolean := True;
Ignore_Blanks : Boolean := True) return Boolean is
begin
if Ignore_Blanks then
return Is_Eq (Join_Spaces (S1), Join_Spaces (S2), Case_Insensitive);
else
return Is_Eq (S1, S2, Case_Insensitive);
end if;
end Is_Equal;
-- --------------------------------------------------------------------------
function Is_Equal (Text1, Text2 : Text;
Case_Insensitive : Boolean := True;
Ignore_Blanks : Boolean := True) return Boolean
is
use type Ada.Containers.Count_Type;
begin
if Text1.Length /= Text2.Length then
return False;
end if;
for I in Text2.First_Index .. Text2.Last_Index loop
if not Is_Equal (Text1 (I), Text2 (I),
Case_Insensitive => Case_Insensitive,
Ignore_Blanks => Ignore_Blanks)
then
return False;
end if;
end loop;
return True;
end Is_Equal;
-- --------------------------------------------------------------------------
procedure Create_File (File_Name : String;
With_Content : Text) is
Output : File_Type;
pragma Warnings (Off, Output);
begin
Create (Output, Out_File, File_Name);
for L of With_Content loop
Put_Line (Output, Item => L);
end loop;
Close (Output);
end Create_File;
-- --------------------------------------------------------------------------
function Create_File (File_Name : Unbounded_String;
With_Content : Text) return Boolean is
begin
Create_File (To_String (File_Name), With_Content);
return True;
exception
when others => return False;
end Create_File;
-- --------------------------------------------------------------------------
procedure Put_Text (File : File_Type := Standard_Output;
Item : Text) is
begin
for L of Item loop
Put_Line (File, L);
end loop;
end Put_Text;
-- -- --------------------------------------------------------------------------
-- procedure Put_Text (Item : Text;
-- File_Name : String) is
-- File : File_Type;
-- pragma Warnings (Off, File);
-- begin
-- Put_Text (File, Item);
-- -- Close (File);
-- end Put_Text;
-- --------------------------------------------------------------------------
procedure Put_Text_Head (Item : Text;
File : File_Type := Standard_Output;
Line_Count : Positive) is
I : Positive := 1;
begin
for L of Item loop
exit when I = Line_Count;
Put_Line (File, L);
I := @ + I;
end loop;
end Put_Text_Head;
-- --------------------------------------------------------------------------
-- procedure Put_Text_Head (Item : Text;
-- File_Name : String;
-- Line_Count : Positive) is
-- File : File_Type;
-- pragma Warnings (Off, File);
-- begin
-- Open (File, Name => File_Name, Mode => Out_File);
-- Put_Text_Head (Item, File, Line_Count);
-- Close (File);
-- end Put_Text_Head;
-- --------------------------------------------------------------------------
procedure Put_Text_Tail (Item : Text;
File : File_Type := Standard_Output;
Line_Count : Positive) is
I : Positive := 1;
begin
for L of reverse Item loop
exit when I = Line_Count;
Put_Line (File, L);
I := @ + I;
end loop;
end Put_Text_Tail;
-- --------------------------------------------------------------------------
-- procedure Put_Text_Tail (Item : Text;
-- File_Name : String;
-- Line_Count : Positive) is
-- File : File_Type;
-- pragma Warnings (Off, File);
-- begin
-- Open (File, Name => File_Name, Mode => Out_File);
-- Put_Text_Tail (Item, File, Line_Count);
-- Close (File);
-- end Put_Text_Tail;
-- --------------------------------------------------------------------------
function Get_Text (File : File_Type) return Text is
T : Text := Empty_Text;
begin
while not End_Of_File (File) loop
T.Append (Get_Line (File));
end loop;
return T;
end Get_Text;
-- --------------------------------------------------------------------------
function Get_Text (File_Name : String) return Text is
File : File_Type;
T : Text;
begin
Open (File, Name => File_Name, Mode => In_File);
T := Get_Text (File);
while not End_Of_File (File) loop
T.Append (Get_Line (File));
end loop;
Close (File);
return T;
end Get_Text;
-- --------------------------------------------------------------------------
-- function Get_Text (File_Name : Unbounded_String) return Text is
-- (Get_Text (To_String (File_Name)));
--
-- -- --------------------------------------------------------------------------
-- function Get_Text_Head (From : Text;
-- Line_Count : Positive) return Text is
-- use Ada.Containers;
-- begin
-- if From = Empty_Text then
-- return Empty_Text;
-- elsif From.Length < Count_Type (Line_Count) then
-- return From;
-- else
-- declare
-- Tmp : Text;
-- begin
-- for I in From.First_Index .. From.First_Index + Line_Count - 1 loop
-- Tmp.Append (From (From.First_Index + I - 1));
-- end loop;
-- return Tmp;
-- end;
-- end if;
-- end Get_Text_Head;
-- -- --------------------------------------------------------------------------
-- function Get_Text_Tail (From : Text;
-- Line_Count : Positive) return Text is
-- use Ada.Containers;
-- use Texts;
-- begin
-- if From = Empty_Text then
-- return Empty_Text;
-- elsif From.Length < Count_Type (Line_Count) then
-- return From;
-- else
-- declare
-- Tmp : Text := To_Vector (Count_Type (Line_Count));
-- begin
-- for I in reverse Tmp.Iterate loop
-- Tmp (I) := From (From.Last_Index - To_Index (I) + 1);
-- end loop;
-- return Tmp;
-- end;
-- end if;
-- end Get_Text_Tail;
--
-- -- --------------------------------------------------------------------------
-- function Shrink (The_Text : Text;
-- Line_Count : Min_Shrinked_Length;
-- Cut_Mark : String := "...") return Text is
-- use Ada.Containers;
-- use Texts;
-- begin
-- if The_Text = Empty_Text or else
-- The_Text.Length <= Count_Type (Line_Count) then
-- return The_Text;
--
-- elsif Line_Count = 2 then
-- return [The_Text (The_Text.First), Cut_Mark];
--
-- else
-- declare
-- Tmp : Text := To_Vector (Count_Type (Line_Count));
-- subtype Head_Index is Positive range The_Text.First ..
-- The_Text.First + Texts.Count (Line_Count / 2 - 1);
-- subtype Tail_Index is Positive range
-- Head_Index.Last + 2 .. The_Text.Last;
-- Shift : constant Positive := The_Text.Last_Index - Tmp.Last_Index;
-- begin
-- for I in Head_Index loop
-- Tmp (I) := The_Text (I);
-- end loop;
-- Tmp (Head_Index'Last + 1) := Cut_Mark;
-- for I in Tail_Index loop
-- Tmp (I) := The_Text (I);
-- end loop;
-- return Tmp;
-- end;
-- end if;
-- end Shrink;
procedure Sort (The_Text : in out Text) renames Texts_Sorting.Sort;
-- --------------------------------------------------------------------------
procedure Compare (Text1, Text2 : Text;
Case_Insensitive : Boolean := True;
Ignore_Blanks : Boolean := True;
Ignore_Blank_Lines : Boolean := True;
Sort_Texts : Boolean := False;
Identical : out Boolean;
Diff_Index : out Natural)
is
T1 : Text := (if Ignore_Blank_Lines then Remove_Blank_Lines (Text1)
else Text1);
T2 : Text := (if Ignore_Blank_Lines then Remove_Blank_Lines (Text2)
else Text2);
begin
Identical := False;
if Sort_Texts then
Sort (T1);
Sort (T2);
end if;
-- Brut compare
for Diff_Index in T1.First_Index .. T1.Last_Index loop
Identical := Is_Equal (T1 (Diff_Index),
T2 (Diff_Index),
Case_Insensitive => Case_Insensitive,
Ignore_Blanks => Ignore_Blanks);
exit when not Identical;
end loop;
if Identical then
Diff_Index := 0;
end if;
end Compare;
-- --------------------------------------------------------------------------
function Is_Equal (Text1, Text2 : Text;
Case_Insensitive : Boolean := True;
Ignore_Blanks : Boolean := True;
Ignore_Blank_Lines : Boolean := True;
Sort_Texts : Boolean := False) return Boolean
is
Identical : Boolean;
Diff_Index : Natural;
begin
Compare (Text1, Text2,
Ignore_Blank_Lines => Ignore_Blank_Lines,
Case_Insensitive => Case_Insensitive,
Sort_Texts => Sort_Texts,
Identical => Identical,
Diff_Index => Diff_Index);
return Identical;
end Is_Equal;
-- --------------------------------------------------------------------------
function Search (Source,
Pattern : String;
Case_Insensitive : Boolean := True;
Ignore_Blanks : Boolean := True) return Boolean is
use Ada.Strings;
use Ada.Strings.Fixed;
Src : constant String := (if Ignore_Blanks then Join_Spaces (Source)
else Source);
Pat : constant String := (if Ignore_Blanks then Join_Spaces (Pattern)
else Pattern);
begin
return (Index (Source => Src,
Pattern => Pat,
From => Src'First) /= 0)
or else (Case_Insensitive and
Index (Source => Ada.Characters.Handling.To_Lower (Src),
Pattern => Ada.Characters.Handling.To_Lower (Pat),
From => Src'First) /= 0);
end Search;
-- --------------------------------------------------------------------------
function Contains (Text1, Text2 : Text;
Sort_Texts : Boolean := False;
Case_Insensitive : Boolean := True;
Ignore_Blanks : Boolean := True;
Ignore_Blank_Lines : Boolean := True) return Boolean is
-- After eliminating easy cases T1 = T2 and T2 is longer than T1, the
-- comparison algorithm is :
-- I1 and I2 (in the loop below) are the two cursor respectively
-- in T1 and T2.
-- For each line in T1 (until there is enough lines left in T1 to
-- match all T2), we search for a matching line in T2.
-- Then, we move I1 and I2 to see if following line in both
-- text matches also.
-- If it matches until T2 last lines, return True, false otherwise.
use type Ada.Containers.Count_Type;
T1 : Text := (if Ignore_Blank_Lines then Remove_Blank_Lines (Text1)
else Text1);
T2 : Text := (if Ignore_Blank_Lines then Remove_Blank_Lines (Text2)
else Text2);
begin
if T1.Length < T2.Length then
return False;
elsif T1 = T2 then
return True;
else
declare
Last_I1 : constant Positive
:= T1.Last_Index - Positive (T2.Length) + 1;
I1 : Positive;
begin
if Sort_Texts then
Sort (T1);
Sort (T2);
end if;
for Start in T1.First_Index .. Last_I1 loop
I1 := Start;
Inner : for I2 in T2.First_Index .. T2.Last_Index loop
-- We look for a first match between texts.
if Search (T1 (I1), T2 (I2),
Case_Insensitive,
Ignore_Blanks)
then
-- Lines match
if I2 = T2.Last_Index then
-- It was the last line of T2
-- => Text match
return True;
else
-- Not the last line of T2, so lets' go to T1 next line
-- (T2 next line will be set by the loop)
I1 := @ + 1;
end if;
else
exit Inner;
end if;
end loop Inner;
end loop;
return False;
end;
end if;
end Contains;
-- --------------------------------------------------------------------------
function Contains_Line (The_Text : Text;
The_Line : String;
Case_Insensitive : Boolean := True;
Ignore_Blanks : Boolean := True) return Boolean is
begin
for L of The_Text loop
if Is_Equal (L, The_Line,
Case_Insensitive => Case_Insensitive,
Ignore_Blanks => Ignore_Blanks)
then
return True;
end if;
end loop;
return False;
end Contains_Line;
-- --------------------------------------------------------------------------
function Contains_String
(The_Text : Text;
The_String : String;
Case_Insensitive : Boolean := True;
Ignore_Blanks : Boolean := True) return Boolean is
begin
for L of The_Text loop
if Search (L, The_String,
Case_Insensitive => Case_Insensitive,
Ignore_Blanks => Ignore_Blanks)
then
return True;
end if;
end loop;
return False;
end Contains_String;
-- --------------------------------------------------------------------------
function Contains_Line
(File_Name : String;
The_Line : String;
Case_Insensitive : Boolean := True;
Ignore_Blanks : Boolean := True) return Boolean is
begin
return Contains_Line (Get_Text (File_Name),
The_Line,
Case_Insensitive => Case_Insensitive,
Ignore_Blanks => Ignore_Blanks);
end Contains_Line;
-- --------------------------------------------------------------------------
function Contains_String
(File_Name : String;
The_String : String;
Case_Insensitive : Boolean := True;
Ignore_Blanks : Boolean := True) return Boolean is
begin
return Contains_String (Get_Text (File_Name),
The_String,
Case_Insensitive => Case_Insensitive,
Ignore_Blanks => Ignore_Blanks);
end Contains_String;
-- --------------------------------------------------------------------------
function First_Non_Blank_Line (In_Text : Text;
From : Positive := 1) return Natural is
begin
if In_Text.Last_Index = 0 then
-- Null Text
return 0;
elsif From > In_Text.Last_Index then
Put_Line ("Non_Blank_Line : starting search outside of Text range");
Put_Line ("First Non_Blank_Line : From " & From'Image
& " not in In_Text range " & In_Text.First_Index'Image
& " .. "
& In_Text.Last_Index'Image);
else
for I in From .. In_Text.Last_Index loop
-- Put_Line ("First_Non_Blank_Line : I = " & I'Image);
if Ada.Strings.Fixed.Index_Non_Blank (In_Text (I)) /= 0 then
-- Character found on that line
-- Put_Line (" First_Non_Blank_Line returning I = " & I'Image);
return I;
end if;
end loop;
end if;
return 0;
end First_Non_Blank_Line;
-- --------------------------------------------------------------------------
function Remove_Blank_Lines (From_Text : Text) return Text is
T : Text := Empty_Text;
begin
for L of From_Text loop
if Ada.Strings.Fixed.Index_Non_Blank (L) /= 0 then
T.Append (L);
end if;
end loop;
return T;
end Remove_Blank_Lines;
function Join_Spaces (From : String) return String is
Tmp : String (From'Range);
I : Natural := Tmp'First;
begin
for J in From'Range loop
if From (J) /= ' ' then
Tmp (I) := From (J);
I := @ + 1;
end if;
end loop;
return Tmp (From'First .. I - 1);
end Join_Spaces;
end Text_Utilities;
|