1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2015, 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.
78 -------------------------------
79 -- Add_String_To_Name_Buffer --
80 -------------------------------
82 procedure Add_String_To_Name_Buffer
(S
: String_Id
) is
84 Append
(Global_Name_Buffer
, S
);
85 end Add_String_To_Name_Buffer
;
87 procedure Append
(Buf
: in out Bounded_String
; S
: String_Id
) is
89 for X
in 1 .. String_Length
(S
) loop
90 Append
(Buf
, Get_Character
(Get_String_Char
(S
, X
)));
98 function End_String
return String_Id
is
103 ---------------------
104 -- Get_String_Char --
105 ---------------------
107 function Get_String_Char
(Id
: String_Id
; Index
: Int
) return Char_Code
is
109 pragma Assert
(Id
in First_String_Id
.. Strings
.Last
110 and then Index
in 1 .. Strings
.Table
(Id
).Length
);
112 return String_Chars
.Table
(Strings
.Table
(Id
).String_Index
+ Index
- 1);
119 procedure Initialize
is
124 -- Set up the null string
127 Null_String_Id
:= End_String
;
136 String_Chars
.Locked
:= True;
137 Strings
.Locked
:= True;
138 String_Chars
.Release
;
148 Strings_Last
:= Strings
.Last
;
149 String_Chars_Last
:= String_Chars
.Last
;
158 Strings
.Set_Last
(Strings_Last
);
159 String_Chars
.Set_Last
(String_Chars_Last
);
166 -- Version to start completely new string
168 procedure Start_String
is
170 Strings
.Append
((String_Index
=> String_Chars
.Last
+ 1, Length
=> 0));
173 -- Version to start from initially stored string
175 procedure Start_String
(S
: String_Id
) is
177 Strings
.Increment_Last
;
179 -- Case of initial string value is at the end of the string characters
180 -- table, so it does not need copying, instead it can be shared.
182 if Strings
.Table
(S
).String_Index
+ Strings
.Table
(S
).Length
=
183 String_Chars
.Last
+ 1
185 Strings
.Table
(Strings
.Last
).String_Index
:=
186 Strings
.Table
(S
).String_Index
;
188 -- Case of initial string value must be copied to new string
191 Strings
.Table
(Strings
.Last
).String_Index
:=
192 String_Chars
.Last
+ 1;
194 for J
in 1 .. Strings
.Table
(S
).Length
loop
196 (String_Chars
.Table
(Strings
.Table
(S
).String_Index
+ (J
- 1)));
200 -- In either case the result string length is copied from the argument
202 Strings
.Table
(Strings
.Last
).Length
:= Strings
.Table
(S
).Length
;
205 -----------------------
206 -- Store_String_Char --
207 -----------------------
209 procedure Store_String_Char
(C
: Char_Code
) is
211 String_Chars
.Append
(C
);
212 Strings
.Table
(Strings
.Last
).Length
:=
213 Strings
.Table
(Strings
.Last
).Length
+ 1;
214 end Store_String_Char
;
216 procedure Store_String_Char
(C
: Character) is
218 Store_String_Char
(Get_Char_Code
(C
));
219 end Store_String_Char
;
221 ------------------------
222 -- Store_String_Chars --
223 ------------------------
225 procedure Store_String_Chars
(S
: String) is
227 for J
in S
'First .. S
'Last loop
228 Store_String_Char
(Get_Char_Code
(S
(J
)));
230 end Store_String_Chars
;
232 procedure Store_String_Chars
(S
: String_Id
) is
234 -- We are essentially doing this:
236 -- for J in 1 .. String_Length (S) loop
237 -- Store_String_Char (Get_String_Char (S, J));
240 -- but when the string is long it's more efficient to grow the
241 -- String_Chars table all at once.
243 S_First
: constant Int
:= Strings
.Table
(S
).String_Index
;
244 S_Len
: constant Nat
:= String_Length
(S
);
245 Old_Last
: constant Int
:= String_Chars
.Last
;
246 New_Last
: constant Int
:= Old_Last
+ S_Len
;
249 String_Chars
.Set_Last
(New_Last
);
250 String_Chars
.Table
(Old_Last
+ 1 .. New_Last
) :=
251 String_Chars
.Table
(S_First
.. S_First
+ S_Len
- 1);
252 Strings
.Table
(Strings
.Last
).Length
:=
253 Strings
.Table
(Strings
.Last
).Length
+ S_Len
;
254 end Store_String_Chars
;
256 ----------------------
257 -- Store_String_Int --
258 ----------------------
260 procedure Store_String_Int
(N
: Int
) is
263 Store_String_Char
('-');
264 Store_String_Int
(-N
);
268 Store_String_Int
(N
/ 10);
271 Store_String_Char
(Character'Val (Character'Pos ('0') + N
mod 10));
273 end Store_String_Int
;
275 --------------------------
276 -- String_Chars_Address --
277 --------------------------
279 function String_Chars_Address
return System
.Address
is
281 return String_Chars
.Table
(0)'Address;
282 end String_Chars_Address
;
288 function String_Equal
(L
, R
: String_Id
) return Boolean is
289 Len
: constant Nat
:= Strings
.Table
(L
).Length
;
292 if Len
/= Strings
.Table
(R
).Length
then
295 for J
in 1 .. Len
loop
296 if Get_String_Char
(L
, J
) /= Get_String_Char
(R
, J
) then
305 -----------------------------
306 -- String_From_Name_Buffer --
307 -----------------------------
309 function String_From_Name_Buffer
310 (Buf
: Bounded_String
:= Global_Name_Buffer
) return String_Id
314 Store_String_Chars
(+Buf
);
316 end String_From_Name_Buffer
;
322 function String_Length
(Id
: String_Id
) return Nat
is
324 return Strings
.Table
(Id
).Length
;
327 ---------------------------
328 -- String_To_Name_Buffer --
329 ---------------------------
331 procedure String_To_Name_Buffer
(S
: String_Id
) is
334 Append
(Global_Name_Buffer
, S
);
335 end String_To_Name_Buffer
;
337 ---------------------
338 -- Strings_Address --
339 ---------------------
341 function Strings_Address
return System
.Address
is
343 return Strings
.Table
(First_String_Id
)'Address;
350 procedure Tree_Read
is
352 String_Chars
.Tree_Read
;
360 procedure Tree_Write
is
362 String_Chars
.Tree_Write
;
372 String_Chars
.Locked
:= False;
373 Strings
.Locked
:= False;
376 -------------------------
377 -- Unstore_String_Char --
378 -------------------------
380 procedure Unstore_String_Char
is
382 String_Chars
.Decrement_Last
;
383 Strings
.Table
(Strings
.Last
).Length
:=
384 Strings
.Table
(Strings
.Last
).Length
- 1;
385 end Unstore_String_Char
;
387 ---------------------
388 -- Write_Char_Code --
389 ---------------------
391 procedure Write_Char_Code
(Code
: Char_Code
) is
393 procedure Write_Hex_Byte
(J
: Char_Code
);
394 -- Write single hex byte (value in range 0 .. 255) as two digits
400 procedure Write_Hex_Byte
(J
: Char_Code
) is
401 Hexd
: constant array (Char_Code
range 0 .. 15) of Character :=
404 Write_Char
(Hexd
(J
/ 16));
405 Write_Char
(Hexd
(J
mod 16));
408 -- Start of processing for Write_Char_Code
411 if Code
in 16#
20#
.. 16#
7E#
then
412 Write_Char
(Character'Val (Code
));
418 if Code
> 16#FF_FFFF#
then
419 Write_Hex_Byte
(Code
/ 2 ** 24);
422 if Code
> 16#FFFF#
then
423 Write_Hex_Byte
((Code
/ 2 ** 16) mod 256);
426 if Code
> 16#FF#
then
427 Write_Hex_Byte
((Code
/ 256) mod 256);
430 Write_Hex_Byte
(Code
mod 256);
436 ------------------------------
437 -- Write_String_Table_Entry --
438 ------------------------------
440 procedure Write_String_Table_Entry
(Id
: String_Id
) is
444 if Id
= No_String
then
445 Write_Str
("no string");
450 for J
in 1 .. String_Length
(Id
) loop
451 C
:= Get_String_Char
(Id
, J
);
453 if C
= Character'Pos ('"') then
459 -- If string is very long, quit
461 if J
>= 1000 then -- arbitrary limit
462 Write_Str
("""...etc (length = ");
463 Write_Int
(String_Length
(Id
));
471 end Write_String_Table_Entry
;