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 | --
-- Copyright (C) 2021-2023, AdaCore
--
-- SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
--
with Ada.Strings.UTF_Encoding.Wide_Wide_Strings;
package body UCD.Data_File_Loaders is
use Ada.Strings.UTF_Encoding.Wide_Wide_Strings;
procedure Scan_Next_Line (Self : in out File_Loader'Class);
function To_Code_Point (Item : Wide_Wide_String) return UCD.Code_Point;
function Parse_Sequence_Of_Code_Unit
(Buffer : Wide_Wide_String) return UCD.Code_Point_Vectors.Vector;
-----------
-- Close --
-----------
procedure Close (Self : in out File_Loader) is
begin
if Is_Open (Self.File) then
Close (Self.File);
end if;
end Close;
-----------------
-- End_Of_File --
-----------------
function End_Of_File (Self : File_Loader) return Boolean is
begin
return not Is_Open (Self.File) or Self.Buffer'First > Self.Line_Last;
end End_Of_File;
--------------
-- Finalize --
--------------
overriding procedure Finalize (Self : in out File_Loader) is
begin
Self.Close;
end Finalize;
--------------------------
-- Get_Code_Point_Range --
--------------------------
procedure Get_Code_Point_Range
(Self : in out File_Loader;
First_Code : out UCD.Code_Point;
Last_Code : out UCD.Code_Point)
is
function Is_Start_Of_Unicode_Data_Range return Boolean;
-- Returns True when current line opens range in UnicodeData.txt format,
-- thus first field ends with ", First>" string.
------------------------------------
-- Is_Start_Of_Unicode_Data_Range --
------------------------------------
function Is_Start_Of_Unicode_Data_Range return Boolean is
Buffer : constant Wide_Wide_String := Self.Get_Field (1);
Suffix : constant Wide_Wide_String := ", First>";
begin
return
Buffer'Length > Suffix'Length
and then Buffer (Buffer'First) = '<'
and then Buffer (Buffer'Last - Suffix'Length + 1 .. Buffer'Last)
= Suffix;
end Is_Start_Of_Unicode_Data_Range;
begin
if Is_Start_Of_Unicode_Data_Range then
First_Code := To_Code_Point (Self.Get_Field (0));
Self.Skip_Line;
Last_Code := To_Code_Point (Self.Get_Field (0));
else
declare
Buffer : constant Wide_Wide_String := Self.Get_Field (0);
Current : Positive;
First : Positive;
Last : Natural;
begin
First := Buffer'First;
Last := Buffer'Last;
Current := First;
while Current <= Buffer'Last loop
if Buffer (Current) not in '0' .. '9' | 'A' .. 'F' then
Last := Current - 1;
exit;
end if;
Current := Current + 1;
end loop;
First_Code := To_Code_Point (Buffer (First .. Last));
Last_Code := First_Code;
if Last = Buffer'Last then
return;
end if;
if Last + 3 < Buffer'Last
and then Buffer (Last + 1) /= '.'
and then Buffer (Last + 2) /= '.'
then
raise Program_Error;
end if;
First := Last + 3;
Last := Buffer'Last;
Current := First;
while Current <= Buffer'Last loop
if Buffer (Current) not in '0' .. '9' | 'A' .. 'F' then
Last := Current - 1;
exit;
end if;
Current := Current + 1;
end loop;
Last_Code := To_Code_Point (Buffer (First .. Last));
if Last /= Buffer'Last then
raise Program_Error;
end if;
end;
end if;
end Get_Code_Point_Range;
---------------
-- Get_Field --
---------------
function Get_Field
(Self : File_Loader;
Index : Field_Index) return Wide_Wide_String is
begin
return
Self.Buffer (Self.Fields (Index).First .. Self.Fields (Index).Last);
end Get_Field;
---------------
-- Get_Field --
---------------
function Get_Field
(Self : File_Loader;
Index : Field_Index) return UCD.Code_Point_Vectors.Vector is
begin
return Parse_Sequence_Of_Code_Unit (Self.Get_Field (Index));
end Get_Field;
---------------
-- Get_Field --
---------------
procedure Get_Field
(Self : File_Loader;
Index : Field_Index;
Tag : out Unbounded_Wide_Wide_String;
Data : out UCD.Code_Point_Vectors.Vector)
is
Buffer : constant Wide_Wide_String := Self.Get_Field (Index);
First : Positive;
Current : Positive;
begin
if Buffer'First > Buffer'Last then
Tag := Null_Unbounded_Wide_Wide_String;
Data := UCD.Code_Point_Vectors.Empty_Vector;
return;
end if;
First := Buffer'First;
if Buffer (First) = '<' then
Current := First + 1;
while Buffer (Current) /= '>' loop
Current := Current + 1;
end loop;
Tag := To_Unbounded_Wide_Wide_String (Buffer (First .. Current));
First := Current + 1;
-- Skip spaces
for J in First .. Buffer'Last loop
First := J;
exit when Buffer (J) /= ' ';
end loop;
else
Tag := Null_Unbounded_Wide_Wide_String;
end if;
Data := Parse_Sequence_Of_Code_Unit (Buffer (First .. Buffer'Last));
end Get_Field;
---------------
-- Has_Field --
---------------
function Has_Field
(Self : File_Loader; Index : Field_Index) return Boolean is
begin
return
Self.Fields (Index).First /= Self.Buffer'First
or else Self.Fields (Index).First <= Self.Fields (Index).Last;
end Has_Field;
----------
-- Open --
----------
procedure Open
(Self : in out File_Loader;
UCD_Root : Wide_Wide_String;
File_Name : Wide_Wide_String)
is
begin
Put_Line ("Loading " & File_Name & "...");
Open (Self.File, In_File, Encode (UCD_Root & '/' & File_Name), "wcem=8");
Self.Scan_Next_Line;
end Open;
---------------------------------
-- Parse_Sequence_Of_Code_Unit --
---------------------------------
function Parse_Sequence_Of_Code_Unit
(Buffer : Wide_Wide_String) return UCD.Code_Point_Vectors.Vector
is
First : Positive;
Last : Positive;
Current : Positive;
begin
return Result : UCD.Code_Point_Vectors.Vector do
First := Buffer'First;
while First <= Buffer'Last loop
Last := Buffer'Last;
Current := First;
while Current <= Buffer'Last loop
if Buffer (Current) not in '0' .. '9' | 'A' .. 'F' then
Last := Current - 1;
exit;
end if;
Current := Current + 1;
end loop;
Result.Append (To_Code_Point (Buffer (First .. Last)));
First := Last + 1;
-- Skip spaces
for J in First .. Buffer'Last loop
First := J;
exit when Buffer (J) /= ' ';
end loop;
end loop;
end return;
end Parse_Sequence_Of_Code_Unit;
--------------------
-- Scan_Next_Line --
--------------------
procedure Scan_Next_Line (Self : in out File_Loader'Class) is
Current_Field : Field_Index := Field_Index'First;
Field_First : Positive;
Field_Last : Natural;
Current : Positive;
Has_Separator : Boolean;
begin
Self.Fields := [others => (1, 0)];
loop
Self.Line_Last := 0;
exit when End_Of_File (Self.File);
Get_Line (Self.File, Self.Buffer, Self.Line_Last);
-- Remove comments
for J in Self.Buffer'First .. Self.Line_Last loop
if Self.Buffer (J) = '#' then
Self.Line_Last := J - 1;
exit;
end if;
end loop;
if Self.Buffer'First <= Self.Line_Last then
Current := Self.Buffer'First;
loop
Field_First := Current;
Field_Last := Self.Line_Last;
Has_Separator := False;
for J in Field_First .. Self.Line_Last loop
if Self.Buffer (J) = ';' then
Field_Last := J - 1;
Current := J + 1;
Has_Separator := True;
exit;
end if;
end loop;
-- Remove leading spaces
for J in Field_First .. Field_Last loop
Field_First := J;
exit when Self.Buffer (J) /= ' ';
end loop;
-- Remove trailing spaces
if Field_First = Field_Last
and then Self.Buffer (Field_Last) = ' '
then
Field_First := Field_First + 1;
else
for J in reverse Field_First .. Field_Last loop
Field_Last := J;
exit when Self.Buffer (J) /= ' ';
end loop;
end if;
Self.Fields (Current_Field) := (Field_First, Field_Last);
Current_Field := Current_Field + 1;
exit when not Has_Separator;
end loop;
exit;
end if;
end loop;
end Scan_Next_Line;
---------------
-- Skip_Line --
---------------
procedure Skip_Line (Self : in out File_Loader) is
begin
Self.Scan_Next_Line;
end Skip_Line;
-------------------
-- To_Code_Point --
-------------------
function To_Code_Point (Item : Wide_Wide_String) return UCD.Code_Point is
begin
return Result : UCD.Code_Point := 0 do
for J in Item'Range loop
Result := Result * 16;
case Item (J) is
when '0' .. '9' =>
Result :=
Result
+ Wide_Wide_Character'Pos (Item (J))
- Wide_Wide_Character'Pos ('0');
when 'A' .. 'F' =>
Result :=
Result
+ Wide_Wide_Character'Pos (Item (J))
- Wide_Wide_Character'Pos ('A')
+ 10;
when others =>
raise Constraint_Error;
end case;
end loop;
end return;
end To_Code_Point;
end UCD.Data_File_Loaders;
|