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 | ------------------------------------------------------------------------------
-- G N A T C O L L --
-- --
-- Copyright (C) 2020-2021, AdaCore --
-- --
-- This library is free software; you can redistribute it and/or modify it --
-- under terms of the GNU General Public License as published by the Free --
-- Software Foundation; either version 3, or (at your option) any later --
-- version. This library is distributed in the hope that it will be useful, --
-- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- --
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. --
-- --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception, --
-- version 3.1, as published by the Free Software Foundation. --
-- --
-- You should have received a copy of the GNU General Public License and --
-- a copy of the GCC Runtime Library Exception along with this program; --
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
-- <http://www.gnu.org/licenses/>. --
-- --
------------------------------------------------------------------------------
with Ada.Unchecked_Deallocation;
with Ada.Strings.UTF_Encoding.Wide_Strings;
package body GNATCOLL.WString_Builders is
package UTF renames Ada.Strings.UTF_Encoding.Wide_Strings;
WNUL : constant Wide_Character := Wide_Character'Val (0);
Minimal_Heap_Size : constant Natural := 64;
procedure Free is new Ada.Unchecked_Deallocation
(Wide_String, WString_Access);
procedure Allocate (Self : in out WString_Builder; Chars : Natural);
--------------
-- Allocate --
--------------
procedure Allocate (Self : in out WString_Builder; Chars : Natural)
is
Str_Max : Natural := (if Self.Heap_Str = null then Minimal_Heap_Size
else Self.Heap_Str'Length);
begin
-- Ensure we have room for total length + 1 (for WNUL)
while Self.Str_Last + Chars + 1 > Str_Max loop
Str_Max := Str_Max * 2;
end loop;
-- Perform reallocations
if Self.Heap_Str = null or else Str_Max > Self.Heap_Str'Length then
declare
New_Str : constant WString_Access :=
new Wide_String (1 .. Str_Max);
begin
if Self.Heap_Str /= null then
-- Copy previous content if necessary
New_Str (1 .. Self.Str_Last + 1) :=
Self.Heap_Str (1 .. Self.Str_Last + 1);
Free (Self.Heap_Str);
elsif Self.Str_Last > 0 then
New_Str (1 .. Self.Str_Last + 1) :=
Self.Stack_Str (1 .. Self.Str_Last + 1);
end if;
Self.Heap_Str := New_Str;
end;
end if;
end Allocate;
------------
-- Append --
------------
procedure Append (Self : in out WString_Builder; Str : UTF8.UTF_8_String) is
WStr : constant Wide_String := UTF.Decode (Str);
New_Last : constant Natural := Self.Str_Last + WStr'Length;
begin
if WStr'Length = 0 then
return;
end if;
if New_Last > WString_Builder_Short_Size then
Allocate (Self, WStr'Length);
Self.Heap_Str (Self.Str_Last + 1 .. New_Last) := WStr;
Self.Heap_Str (New_Last + 1) := WNUL;
else
Self.Stack_Str (Self.Str_Last + 1 .. New_Last) := WStr;
Self.Stack_Str (New_Last + 1) := WNUL;
end if;
Self.Str_Last := New_Last;
end Append;
procedure Append
(Self : in out Static_WString_Builder;
Str : UTF8.UTF_8_String)
is
WStr : constant Wide_String := UTF.Decode (Str);
New_Last : constant Natural := Self.Str_Last + WStr'Length;
begin
if WStr'Length = 0 then
return;
end if;
if New_Last > Self.Size_With_NUL - 1 then
raise Constraint_Error;
end if;
Self.Str (Self.Str_Last + 1 .. New_Last) := WStr;
Self.Str_Last := New_Last;
Self.Str (Self.Str_Last + 1) := WNUL;
end Append;
procedure Append (Self : in out WString_Builder; Char : Wide_Character) is
begin
if Self.Str_Last + 1 > WString_Builder_Short_Size then
Allocate (Self, 1);
Self.Str_Last := Self.Str_Last + 1;
Self.Heap_Str (Self.Str_Last) := Char;
Self.Heap_Str (Self.Str_Last + 1) := WNUL;
else
Self.Str_Last := Self.Str_Last + 1;
Self.Stack_Str (Self.Str_Last) := Char;
Self.Stack_Str (Self.Str_Last + 1) := WNUL;
end if;
end Append;
procedure Append
(Self : in out Static_WString_Builder;
Char : Wide_Character)
is
New_Last : constant Natural := Self.Str_Last + 1;
begin
if New_Last > Self.Size_With_NUL - 1 then
raise Constraint_Error;
end if;
Self.Str_Last := New_Last;
Self.Str (Self.Str_Last) := Char;
Self.Str (Self.Str_Last + 1) := WNUL;
end Append;
------------------
-- As_C_WString --
------------------
function As_C_WString
(Self : WString_Builder;
Null_If_Empty : Boolean := False)
return OS.C_WString
is
begin
if Self.Str_Last = 0 then
if Null_If_Empty then
return OS.Null_C_WString;
else
return OS.Empty_C_WString;
end if;
elsif Self.Str_Last > WString_Builder_Short_Size then
return OS.C_WString (Self.Heap_Str (1)'Address);
else
return OS.C_WString (Self.Stack_Str (1)'Address);
end if;
end As_C_WString;
function As_C_WString
(Self : Static_WString_Builder;
Null_If_Empty : Boolean := False)
return OS.C_WString
is
begin
if Self.Str_Last = 0 then
if Null_If_Empty then
return OS.Null_C_WString;
else
return OS.Empty_C_WString;
end if;
else
return OS.C_WString (Self.Str (1)'Address);
end if;
end As_C_WString;
---------------
-- As_String --
---------------
function As_String (Self : WString_Builder) return Wide_String is
begin
if Self.Str_Last > WString_Builder_Short_Size then
return Self.Heap_Str.all (1 .. Self.Str_Last);
else
return Self.Stack_Str (1 .. Self.Str_Last);
end if;
end As_String;
function As_String (Self : Static_WString_Builder) return Wide_String is
begin
return Self.Str (1 .. Self.Str_Last);
end As_String;
--------------------
-- As_UTF8_String --
--------------------
function As_UTF8_String (Self : WString_Builder) return UTF8.UTF_8_String is
begin
return UTF.Encode (As_String (Self));
end As_UTF8_String;
function As_UTF8_String
(Self : Static_WString_Builder) return UTF8.UTF_8_String
is
begin
return UTF.Encode (As_String (Self));
end As_UTF8_String;
----------------
-- Deallocate --
----------------
procedure Deallocate (Self : in out WString_Builder) is
begin
if Self.Heap_Str /= null then
Free (Self.Heap_Str);
Self.Str_Last := 0;
end if;
end Deallocate;
-------------
-- Element --
-------------
function Element
(Self : WString_Builder;
N : Positive)
return Wide_Character
is
begin
if N > Self.Str_Last then
raise Constraint_Error;
elsif Self.Str_Last > WString_Builder_Short_Size then
return Self.Heap_Str.all (N);
else
return Self.Stack_Str (N);
end if;
end Element;
function Element
(Self : Static_WString_Builder; N : Positive)
return Wide_Character
is
begin
if N > Self.Str_Last then
raise Constraint_Error;
else
return Self.Str (N);
end if;
end Element;
------------
-- Length --
------------
function Length (Self : WString_Builder) return Natural is
begin
return Self.Str_Last;
end Length;
function Length (Self : Static_WString_Builder) return Natural is
begin
return Self.Str_Last;
end Length;
---------
-- Set --
---------
procedure Set (Self : in out WString_Builder; Str : UTF8.UTF_8_String) is
begin
Self.Str_Last := 0;
Append (Self, Str);
end Set;
procedure Set
(Self : in out Static_WString_Builder;
Str : UTF8.UTF_8_String)
is
begin
Self.Str_Last := 0;
Append (Self, Str);
end Set;
end GNATCOLL.WString_Builders;
|