1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2018, Free Software Foundation, Inc. --
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. --
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. --
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/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
33 with Output
; use Output
;
36 package body Stringt
is
38 -- The following table stores the sequence of character codes for the
39 -- stored string constants. The entries are referenced from the
40 -- separate Strings table.
42 package String_Chars
is new Table
.Table
(
43 Table_Component_Type
=> Char_Code
,
44 Table_Index_Type
=> Int
,
46 Table_Initial
=> Alloc
.String_Chars_Initial
,
47 Table_Increment
=> Alloc
.String_Chars_Increment
,
48 Table_Name
=> "String_Chars");
50 -- The String_Id values reference entries in the Strings table, which
51 -- contains String_Entry records that record the length of each stored
52 -- string and its starting location in the String_Chars table.
54 type String_Entry
is record
59 package Strings
is new Table
.Table
(
60 Table_Component_Type
=> String_Entry
,
61 Table_Index_Type
=> String_Id
'Base,
62 Table_Low_Bound
=> First_String_Id
,
63 Table_Initial
=> Alloc
.Strings_Initial
,
64 Table_Increment
=> Alloc
.Strings_Increment
,
65 Table_Name
=> "Strings");
67 -- Note: it is possible that two entries in the Strings table can share
68 -- string data in the String_Chars table, and in particular this happens
69 -- when Start_String is called with a parameter that is the last string
70 -- currently allocated in the table.
72 Strings_Last
: String_Id
:= First_String_Id
;
73 String_Chars_Last
: Int
:= 0;
74 -- Strings_Last and String_Chars_Last are used by procedure Mark and
75 -- Release to get a snapshot of the tables and to restore them to their
76 -- previous situation.
82 procedure Append
(Buf
: in out Bounded_String
; S
: String_Id
) is
84 for X
in 1 .. String_Length
(S
) loop
85 Append
(Buf
, Get_Character
(Get_String_Char
(S
, X
)));
93 function End_String
return String_Id
is
100 ---------------------
102 function Get_String_Char
(Id
: String_Id
; Index
: Int
) return Char_Code
is
104 pragma Assert
(Id
in First_String_Id
.. Strings
.Last
105 and then Index
in 1 .. Strings
.Table
(Id
).Length
);
107 return String_Chars
.Table
(Strings
.Table
(Id
).String_Index
+ Index
- 1);
114 procedure Initialize
is
119 -- Set up the null string
122 Null_String_Id
:= End_String
;
131 String_Chars
.Release
;
132 String_Chars
.Locked
:= True;
134 Strings
.Locked
:= True;
143 Strings_Last
:= Strings
.Last
;
144 String_Chars_Last
:= String_Chars
.Last
;
153 Strings
.Set_Last
(Strings_Last
);
154 String_Chars
.Set_Last
(String_Chars_Last
);
161 -- Version to start completely new string
163 procedure Start_String
is
165 Strings
.Append
((String_Index
=> String_Chars
.Last
+ 1, Length
=> 0));
168 -- Version to start from initially stored string
170 procedure Start_String
(S
: String_Id
) is
172 Strings
.Increment_Last
;
174 -- Case of initial string value is at the end of the string characters
175 -- table, so it does not need copying, instead it can be shared.
177 if Strings
.Table
(S
).String_Index
+ Strings
.Table
(S
).Length
=
178 String_Chars
.Last
+ 1
180 Strings
.Table
(Strings
.Last
).String_Index
:=
181 Strings
.Table
(S
).String_Index
;
183 -- Case of initial string value must be copied to new string
186 Strings
.Table
(Strings
.Last
).String_Index
:=
187 String_Chars
.Last
+ 1;
189 for J
in 1 .. Strings
.Table
(S
).Length
loop
191 (String_Chars
.Table
(Strings
.Table
(S
).String_Index
+ (J
- 1)));
195 -- In either case the result string length is copied from the argument
197 Strings
.Table
(Strings
.Last
).Length
:= Strings
.Table
(S
).Length
;
200 -----------------------
201 -- Store_String_Char --
202 -----------------------
204 procedure Store_String_Char
(C
: Char_Code
) is
206 String_Chars
.Append
(C
);
207 Strings
.Table
(Strings
.Last
).Length
:=
208 Strings
.Table
(Strings
.Last
).Length
+ 1;
209 end Store_String_Char
;
211 procedure Store_String_Char
(C
: Character) is
213 Store_String_Char
(Get_Char_Code
(C
));
214 end Store_String_Char
;
216 ------------------------
217 -- Store_String_Chars --
218 ------------------------
220 procedure Store_String_Chars
(S
: String) is
222 for J
in S
'First .. S
'Last loop
223 Store_String_Char
(Get_Char_Code
(S
(J
)));
225 end Store_String_Chars
;
227 procedure Store_String_Chars
(S
: String_Id
) is
229 -- We are essentially doing this:
231 -- for J in 1 .. String_Length (S) loop
232 -- Store_String_Char (Get_String_Char (S, J));
235 -- but when the string is long it's more efficient to grow the
236 -- String_Chars table all at once.
238 S_First
: constant Int
:= Strings
.Table
(S
).String_Index
;
239 S_Len
: constant Nat
:= String_Length
(S
);
240 Old_Last
: constant Int
:= String_Chars
.Last
;
241 New_Last
: constant Int
:= Old_Last
+ S_Len
;
244 String_Chars
.Set_Last
(New_Last
);
245 String_Chars
.Table
(Old_Last
+ 1 .. New_Last
) :=
246 String_Chars
.Table
(S_First
.. S_First
+ S_Len
- 1);
247 Strings
.Table
(Strings
.Last
).Length
:=
248 Strings
.Table
(Strings
.Last
).Length
+ S_Len
;
249 end Store_String_Chars
;
251 ----------------------
252 -- Store_String_Int --
253 ----------------------
255 procedure Store_String_Int
(N
: Int
) is
258 Store_String_Char
('-');
259 Store_String_Int
(-N
);
263 Store_String_Int
(N
/ 10);
266 Store_String_Char
(Character'Val (Character'Pos ('0') + N
mod 10));
268 end Store_String_Int
;
270 --------------------------
271 -- String_Chars_Address --
272 --------------------------
274 function String_Chars_Address
return System
.Address
is
276 return String_Chars
.Table
(0)'Address;
277 end String_Chars_Address
;
283 function String_Equal
(L
, R
: String_Id
) return Boolean is
284 Len
: constant Nat
:= Strings
.Table
(L
).Length
;
287 if Len
/= Strings
.Table
(R
).Length
then
290 for J
in 1 .. Len
loop
291 if Get_String_Char
(L
, J
) /= Get_String_Char
(R
, J
) then
300 -----------------------------
301 -- String_From_Name_Buffer --
302 -----------------------------
304 function String_From_Name_Buffer
305 (Buf
: Bounded_String
:= Global_Name_Buffer
) return String_Id
309 Store_String_Chars
(+Buf
);
311 end String_From_Name_Buffer
;
317 function String_Length
(Id
: String_Id
) return Nat
is
319 return Strings
.Table
(Id
).Length
;
326 function String_To_Name
(S
: String_Id
) return Name_Id
is
327 Buf
: Bounded_String
;
330 return Name_Find
(Buf
);
333 ---------------------------
334 -- String_To_Name_Buffer --
335 ---------------------------
337 procedure String_To_Name_Buffer
(S
: String_Id
) is
340 Append
(Global_Name_Buffer
, S
);
341 end String_To_Name_Buffer
;
343 ---------------------
344 -- Strings_Address --
345 ---------------------
347 function Strings_Address
return System
.Address
is
349 return Strings
.Table
(First_String_Id
)'Address;
356 procedure Tree_Read
is
358 String_Chars
.Tree_Read
;
366 procedure Tree_Write
is
368 String_Chars
.Tree_Write
;
378 String_Chars
.Locked
:= False;
379 Strings
.Locked
:= False;
382 -------------------------
383 -- Unstore_String_Char --
384 -------------------------
386 procedure Unstore_String_Char
is
388 String_Chars
.Decrement_Last
;
389 Strings
.Table
(Strings
.Last
).Length
:=
390 Strings
.Table
(Strings
.Last
).Length
- 1;
391 end Unstore_String_Char
;
393 ---------------------
394 -- Write_Char_Code --
395 ---------------------
397 procedure Write_Char_Code
(Code
: Char_Code
) is
399 procedure Write_Hex_Byte
(J
: Char_Code
);
400 -- Write single hex byte (value in range 0 .. 255) as two digits
406 procedure Write_Hex_Byte
(J
: Char_Code
) is
407 Hexd
: constant array (Char_Code
range 0 .. 15) of Character :=
410 Write_Char
(Hexd
(J
/ 16));
411 Write_Char
(Hexd
(J
mod 16));
414 -- Start of processing for Write_Char_Code
417 if Code
in 16#
20#
.. 16#
7E#
then
418 Write_Char
(Character'Val (Code
));
424 if Code
> 16#FF_FFFF#
then
425 Write_Hex_Byte
(Code
/ 2 ** 24);
428 if Code
> 16#FFFF#
then
429 Write_Hex_Byte
((Code
/ 2 ** 16) mod 256);
432 if Code
> 16#FF#
then
433 Write_Hex_Byte
((Code
/ 256) mod 256);
436 Write_Hex_Byte
(Code
mod 256);
442 ------------------------------
443 -- Write_String_Table_Entry --
444 ------------------------------
446 procedure Write_String_Table_Entry
(Id
: String_Id
) is
450 if Id
= No_String
then
451 Write_Str
("no string");
456 for J
in 1 .. String_Length
(Id
) loop
457 C
:= Get_String_Char
(Id
, J
);
459 if C
= Character'Pos ('"') then
465 -- If string is very long, quit
467 if J
>= 1000 then -- arbitrary limit
468 Write_Str
("""...etc (length = ");
469 Write_Int
(String_Length
(Id
));
477 end Write_String_Table_Entry
;