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
|
------------------------------------------------------------------------------
-- --
-- GNAT COMPILER COMPONENTS --
-- --
-- S T R I N G T --
-- --
-- B o d y --
-- --
-- Copyright (C) 1992-2005, Free Software Foundation, Inc. --
-- --
-- GNAT 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. GNAT 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 GNAT; see file COPYING. If not, write --
-- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
-- Boston, MA 02110-1301, 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. --
-- --
-- GNAT was originally developed by the GNAT team at New York University. --
-- Extensive contributions were provided by Ada Core Technologies Inc. --
-- --
------------------------------------------------------------------------------
with Alloc;
with Namet; use Namet;
with Output; use Output;
with Table;
package body Stringt is
-- The following table stores the sequence of character codes for the
-- stored string constants. The entries are referenced from the
-- separate Strings table.
package String_Chars is new Table.Table (
Table_Component_Type => Char_Code,
Table_Index_Type => Int,
Table_Low_Bound => 0,
Table_Initial => Alloc.String_Chars_Initial,
Table_Increment => Alloc.String_Chars_Increment,
Table_Name => "String_Chars");
-- The String_Id values reference entries in the Strings table, which
-- contains String_Entry records that record the length of each stored
-- string and its starting location in the String_Chars table.
type String_Entry is record
String_Index : Int;
Length : Nat;
end record;
package Strings is new Table.Table (
Table_Component_Type => String_Entry,
Table_Index_Type => String_Id,
Table_Low_Bound => First_String_Id,
Table_Initial => Alloc.Strings_Initial,
Table_Increment => Alloc.Strings_Increment,
Table_Name => "Strings");
-- Note: it is possible that two entries in the Strings table can share
-- string data in the String_Chars table, and in particular this happens
-- when Start_String is called with a parameter that is the last string
-- currently allocated in the table.
-------------------------------
-- Add_String_To_Name_Buffer --
-------------------------------
procedure Add_String_To_Name_Buffer (S : String_Id) is
Len : constant Natural := Natural (String_Length (S));
begin
for J in 1 .. Len loop
Name_Buffer (Name_Len + J) :=
Get_Character (Get_String_Char (S, Int (J)));
end loop;
Name_Len := Name_Len + Len;
end Add_String_To_Name_Buffer;
----------------
-- End_String --
----------------
function End_String return String_Id is
begin
return Strings.Last;
end End_String;
---------------------
-- Get_String_Char --
---------------------
function Get_String_Char (Id : String_Id; Index : Int) return Char_Code is
begin
pragma Assert (Id in First_String_Id .. Strings.Last
and then Index in 1 .. Strings.Table (Id).Length);
return String_Chars.Table (Strings.Table (Id).String_Index + Index - 1);
end Get_String_Char;
----------------
-- Initialize --
----------------
procedure Initialize is
begin
String_Chars.Init;
Strings.Init;
end Initialize;
----------
-- Lock --
----------
procedure Lock is
begin
String_Chars.Locked := True;
Strings.Locked := True;
String_Chars.Release;
Strings.Release;
end Lock;
------------------
-- Start_String --
------------------
-- Version to start completely new string
procedure Start_String is
begin
Strings.Increment_Last;
Strings.Table (Strings.Last).String_Index := String_Chars.Last + 1;
Strings.Table (Strings.Last).Length := 0;
end Start_String;
-- Version to start from initially stored string
procedure Start_String (S : String_Id) is
begin
Strings.Increment_Last;
-- Case of initial string value is at the end of the string characters
-- table, so it does not need copying, instead it can be shared.
if Strings.Table (S).String_Index + Strings.Table (S).Length =
String_Chars.Last + 1
then
Strings.Table (Strings.Last).String_Index :=
Strings.Table (S).String_Index;
-- Case of initial string value must be copied to new string
else
Strings.Table (Strings.Last).String_Index :=
String_Chars.Last + 1;
for J in 1 .. Strings.Table (S).Length loop
String_Chars.Increment_Last;
String_Chars.Table (String_Chars.Last) :=
String_Chars.Table (Strings.Table (S).String_Index + (J - 1));
end loop;
end if;
-- In either case the result string length is copied from the argument
Strings.Table (Strings.Last).Length := Strings.Table (S).Length;
end Start_String;
-----------------------
-- Store_String_Char --
-----------------------
procedure Store_String_Char (C : Char_Code) is
begin
String_Chars.Increment_Last;
String_Chars.Table (String_Chars.Last) := C;
Strings.Table (Strings.Last).Length :=
Strings.Table (Strings.Last).Length + 1;
end Store_String_Char;
procedure Store_String_Char (C : Character) is
begin
Store_String_Char (Get_Char_Code (C));
end Store_String_Char;
------------------------
-- Store_String_Chars --
------------------------
procedure Store_String_Chars (S : String) is
begin
for J in S'First .. S'Last loop
Store_String_Char (Get_Char_Code (S (J)));
end loop;
end Store_String_Chars;
procedure Store_String_Chars (S : String_Id) is
begin
for J in 1 .. String_Length (S) loop
Store_String_Char (Get_String_Char (S, J));
end loop;
end Store_String_Chars;
----------------------
-- Store_String_Int --
----------------------
procedure Store_String_Int (N : Int) is
begin
if N < 0 then
Store_String_Char ('-');
Store_String_Int (-N);
else
if N > 9 then
Store_String_Int (N / 10);
end if;
Store_String_Char (Character'Val (Character'Pos ('0') + N mod 10));
end if;
end Store_String_Int;
--------------------------
-- String_Chars_Address --
--------------------------
function String_Chars_Address return System.Address is
begin
return String_Chars.Table (0)'Address;
end String_Chars_Address;
------------------
-- String_Equal --
------------------
function String_Equal (L, R : String_Id) return Boolean is
Len : constant Nat := Strings.Table (L).Length;
begin
if Len /= Strings.Table (R).Length then
return False;
else
for J in 1 .. Len loop
if Get_String_Char (L, J) /= Get_String_Char (R, J) then
return False;
end if;
end loop;
return True;
end if;
end String_Equal;
-----------------------------
-- String_From_Name_Buffer --
-----------------------------
function String_From_Name_Buffer return String_Id is
begin
Start_String;
for J in 1 .. Name_Len loop
Store_String_Char (Get_Char_Code (Name_Buffer (J)));
end loop;
return End_String;
end String_From_Name_Buffer;
-------------------
-- String_Length --
-------------------
function String_Length (Id : String_Id) return Nat is
begin
return Strings.Table (Id).Length;
end String_Length;
---------------------------
-- String_To_Name_Buffer --
---------------------------
procedure String_To_Name_Buffer (S : String_Id) is
begin
Name_Len := Natural (String_Length (S));
for J in 1 .. Name_Len loop
Name_Buffer (J) :=
Get_Character (Get_String_Char (S, Int (J)));
end loop;
end String_To_Name_Buffer;
---------------------
-- Strings_Address --
---------------------
function Strings_Address return System.Address is
begin
return Strings.Table (First_String_Id)'Address;
end Strings_Address;
---------------
-- Tree_Read --
---------------
procedure Tree_Read is
begin
String_Chars.Tree_Read;
Strings.Tree_Read;
end Tree_Read;
----------------
-- Tree_Write --
----------------
procedure Tree_Write is
begin
String_Chars.Tree_Write;
Strings.Tree_Write;
end Tree_Write;
------------
-- Unlock --
------------
procedure Unlock is
begin
String_Chars.Locked := False;
Strings.Locked := False;
end Unlock;
-------------------------
-- Unstore_String_Char --
-------------------------
procedure Unstore_String_Char is
begin
String_Chars.Decrement_Last;
Strings.Table (Strings.Last).Length :=
Strings.Table (Strings.Last).Length - 1;
end Unstore_String_Char;
---------------------
-- Write_Char_Code --
---------------------
procedure Write_Char_Code (Code : Char_Code) is
procedure Write_Hex_Byte (J : Char_Code);
-- Write single hex byte (value in range 0 .. 255) as two digits
--------------------
-- Write_Hex_Byte --
--------------------
procedure Write_Hex_Byte (J : Char_Code) is
Hexd : constant array (Char_Code range 0 .. 15) of Character :=
"0123456789abcdef";
begin
Write_Char (Hexd (J / 16));
Write_Char (Hexd (J mod 16));
end Write_Hex_Byte;
-- Start of processing for Write_Char_Code
begin
if Code in 16#20# .. 16#7E# then
Write_Char (Character'Val (Code));
else
Write_Char ('[');
Write_Char ('"');
if Code > 16#FF_FFFF# then
Write_Hex_Byte (Code / 2 ** 24);
end if;
if Code > 16#FFFF# then
Write_Hex_Byte ((Code / 2 ** 16) mod 256);
end if;
if Code > 16#FF# then
Write_Hex_Byte ((Code / 256) mod 256);
end if;
Write_Hex_Byte (Code mod 256);
Write_Char ('"');
Write_Char (']');
end if;
end Write_Char_Code;
------------------------------
-- Write_String_Table_Entry --
------------------------------
procedure Write_String_Table_Entry (Id : String_Id) is
C : Char_Code;
begin
if Id = No_String then
Write_Str ("no string");
else
Write_Char ('"');
for J in 1 .. String_Length (Id) loop
C := Get_String_Char (Id, J);
if Character'Val (C) = '"' then
Write_Str ("""""");
else
Write_Char_Code (C);
end if;
end loop;
Write_Char ('"');
end if;
end Write_String_Table_Entry;
end Stringt;
|