vss_24.0.0_b4d0be7c/source/text/implementation/vss-strings-converters-decoders-gb18030.adb

  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
--
--  Copyright (C) 2022-2023, AdaCore
--
--  SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
--

with Interfaces;

with VSS.Implementation.String_Handlers;

with VSS.Strings.Converters.Decoders.GB18030.Indices;
with VSS.Strings.Converters.Decoders.GB18030.Ranges;

package body VSS.Strings.Converters.Decoders.GB18030 is

   function Index_GB18030_Ranges_Code_Point
     (First  : Ada.Streams.Stream_Element;
      Second : Ada.Streams.Stream_Element;
      Third  : Ada.Streams.Stream_Element;
      Byte   : Ada.Streams.Stream_Element)
      return VSS.Unicode.Code_Point;

   function Index_Code_Point
     (First  : Ada.Streams.Stream_Element;
      Byte   : Ada.Streams.Stream_Element)
      return VSS.Unicode.Code_Point
     with Pre => First in 16#81# .. 16#FE#;

   ------------
   -- Decode --
   ------------

   overriding procedure Decode
     (Self        : in out GB18030_Decoder;
      Source      : Ada.Streams.Stream_Element_Array;
      End_Of_Data : Boolean;
      Target      : out VSS.Implementation.Strings.String_Data)
   is
      use type Ada.Streams.Stream_Element;
      use type Ada.Streams.Stream_Element_Offset;
      use type VSS.Unicode.Code_Point;

      Index  : Ada.Streams.Stream_Element_Offset := Source'First;
      First  : Ada.Streams.Stream_Element        := Self.First;
      Second : Ada.Streams.Stream_Element        := Self.Second;
      Third  : Ada.Streams.Stream_Element        := Self.Third;
      Byte   : Ada.Streams.Stream_Element;
      Offset : VSS.Implementation.Strings.Cursor_Offset := (0, 0, 0);

   begin
      if Self.Error and Self.Flags (Stop_On_Error) then
         --  Error was encountered in "stop on error" mode, return immidiately.

         return;
      end if;

      loop
         if Index > Source'Last then
            if (First /= 0 or Second /= 0 or Third /= 0)
              and (Self.Flags (Stateless) or End_Of_Data)
            then
               First  := 0;
               Second := 0;
               Third  := 0;

               Self.Error := True;

               if not Self.Flags (Stop_On_Error) then
                  VSS.Implementation.Strings.Handler (Target).Append
                    (Target, Replacement_Character, Offset);
               end if;
            end if;

            exit;
         end if;

         Byte := Source (Index);

         if Third /= 0 then
            case Byte is
               when 16#30# .. 16#39# =>
                  declare
                     Code : constant VSS.Unicode.Code_Point :=
                       Index_GB18030_Ranges_Code_Point
                         (First, Second, Third, Byte);

                  begin
                     First  := 0;
                     Second := 0;
                     Third  := 0;

                     if Code = 0 then
                        Self.Error := True;

                        if Self.Flags (Stop_On_Error) then
                           exit;

                        else
                           VSS.Implementation.Strings.Handler (Target).Append
                             (Target, Replacement_Character, Offset);
                        end if;

                     else
                        VSS.Implementation.Strings.Handler (Target).Append
                          (Target, Code, Offset);
                     end if;
                  end;

               when others =>
                  declare
                     Buffer : constant Ada.Streams.Stream_Element_Array :=
                       [Second, Third, Byte];

                  begin
                     First  := 0;
                     Second := 0;
                     Third  := 0;

                     Self.Error := True;

                     if Self.Flags (Stop_On_Error) then
                        exit;

                     else
                        VSS.Implementation.Strings.Handler (Target).Append
                          (Target, Replacement_Character, Offset);
                     end if;

                     Self.First  := First;
                     Self.Second := Second;
                     Self.Third  := Third;

                     Self.Decode (Buffer, False, Target);

                     First  := Self.First;
                     Second := Self.Second;
                     Third  := Self.Third;
                  end;
            end case;

         elsif Second /= 0 then
            case Byte is
               when 16#81# .. 16#FE# =>
                  Third := Byte;

               when others =>
                  declare
                     Buffer : constant Ada.Streams.Stream_Element_Array :=
                       [Second, Byte];

                  begin
                     First  := 0;
                     Second := 0;

                     Self.Error := True;

                     if Self.Flags (Stop_On_Error) then
                        exit;

                     else
                        VSS.Implementation.Strings.Handler (Target).Append
                          (Target, Replacement_Character, Offset);
                     end if;

                     Self.First  := First;
                     Self.Second := Second;
                     Self.Third  := Third;

                     Self.Decode (Buffer, False, Target);

                     First  := Self.First;
                     Second := Self.Second;
                     Third  := Self.Third;
                  end;
            end case;

         elsif First /= 0 then
            case Byte is
               when 16#30# .. 16#39# =>
                  Second := Byte;

               when others =>
                  declare
                     Code : constant VSS.Unicode.Code_Point :=
                       Index_Code_Point (First, Byte);

                  begin
                     First := 0;

                     if Code /= 0 then
                        VSS.Implementation.Strings.Handler (Target).Append
                          (Target, Code, Offset);

                     else
                        if Byte in ASCII_Byte_Range then
                           Index := Index - 1;
                        end if;

                        Self.Error := True;

                        if Self.Flags (Stop_On_Error) then
                           exit;

                        else
                           VSS.Implementation.Strings.Handler (Target).Append
                             (Target, Replacement_Character, Offset);
                        end if;
                     end if;
                  end;
            end case;

         else
            case Byte is
               when ASCII_Byte_Range =>
                  VSS.Implementation.Strings.Handler (Target).Append
                    (Target, VSS.Unicode.Code_Point (Byte), Offset);

               when 16#80# =>
                  VSS.Implementation.Strings.Handler (Target).Append
                    (Target, 16#20AC#, Offset);

               when 16#81# .. 16#FE# =>
                  First := Byte;

               when others =>
                  Self.Error := True;

                  if Self.Flags (Stop_On_Error) then
                     exit;

                  else
                     VSS.Implementation.Strings.Handler (Target).Append
                       (Target, Replacement_Character, Offset);
                  end if;
            end case;
         end if;

         Index := Index + 1;
      end loop;

      Self.First  := First;
      Self.Second := Second;
      Self.Third  := Third;
   end Decode;

   -------------------
   -- Error_Message --
   -------------------

   overriding function Error_Message
     (Self : GB18030_Decoder) return VSS.Strings.Virtual_String is
   begin
      if Self.Error then
         return "Iff-formed sequence";

      else
         return VSS.Strings.Empty_Virtual_String;
      end if;
   end Error_Message;

   -------------
   -- Factory --
   -------------

   function Factory
     (Flags : Converter_Flags)
      return VSS.Strings.Converters.Decoders.Decoder_Access is
   begin
      return Result : constant
        VSS.Strings.Converters.Decoders.Decoder_Access :=
          new GB18030_Decoder
      do
         declare
            Self : GB18030_Decoder renames GB18030_Decoder (Result.all);

         begin
            Self.Flags := Flags;
            Self.Reset_State;
         end;
      end return;
   end Factory;

   ---------------
   -- Has_Error --
   ---------------

   overriding function Has_Error (Self : GB18030_Decoder) return Boolean is
   begin
      return Self.Error;
   end Has_Error;

   ----------------------
   -- Index_Code_Point --
   ----------------------

   function Index_Code_Point
     (First  : Ada.Streams.Stream_Element;
      Byte   : Ada.Streams.Stream_Element) return VSS.Unicode.Code_Point
   is
      use type Ada.Streams.Stream_Element;
      use type Interfaces.Unsigned_32;

      Offset  : constant Interfaces.Unsigned_32 :=
        (if Byte < 16#7F# then 16#40# else 16#41#);
      Pointer : Interfaces.Unsigned_32 := 0;

   begin
      case Byte is
         when 16#40# .. 16#7E# | 16#80# .. 16#FE# =>
            Pointer :=
              (Interfaces.Unsigned_32 (First) - 16#81#) * 190
                 + (Interfaces.Unsigned_32 (Byte) - Offset);

            return VSS.Unicode.Code_Point (Indices.Table (Pointer));

         when others =>
            return 0;
      end case;
   end Index_Code_Point;

   -------------------------------------
   -- Index_GB18030_Ranges_Code_Point --
   -------------------------------------

   function Index_GB18030_Ranges_Code_Point
     (First  : Ada.Streams.Stream_Element;
      Second : Ada.Streams.Stream_Element;
      Third  : Ada.Streams.Stream_Element;
      Byte   : Ada.Streams.Stream_Element)
      return VSS.Unicode.Code_Point
   is
      use type Interfaces.Unsigned_32;

      Pointer           : constant Interfaces.Unsigned_32 :=
        (Interfaces.Unsigned_32 (First) - 16#81#) * 10 * 126 * 10
          + (Interfaces.Unsigned_32 (Second) - 16#30#) * 10 * 126
          + (Interfaces.Unsigned_32 (Third) - 16#81#) * 10
          + Interfaces.Unsigned_32 (Byte) - 16#30#;
      Pointer_Offset    : Interfaces.Unsigned_32 :=
        Ranges.Table (Ranges.Table'First).Pointer_Offset;
      Code_Point_Offset : Interfaces.Unsigned_32 :=
        Ranges.Table (Ranges.Table'First).Code_Point_Offset;

   begin
      if Pointer in 39_420 .. 188_999 or Pointer > 1_237_575 then
         return 0;
      end if;

      if Pointer = 7457 then
         return 16#E7C7#;
      end if;

      for J in Ranges.Table'Range loop
         exit when Ranges.Table (J).Pointer_Offset > Pointer;

         Pointer_Offset    := Ranges.Table (J).Pointer_Offset;
         Code_Point_Offset := Ranges.Table (J).Code_Point_Offset;
      end loop;

      return
        VSS.Unicode.Code_Point (Code_Point_Offset + Pointer - Pointer_Offset);
   end Index_GB18030_Ranges_Code_Point;

   -----------------
   -- Reset_State --
   -----------------

   overriding procedure Reset_State (Self : in out GB18030_Decoder) is
   begin
      Self.First  := 0;
      Self.Second := 0;
      Self.Third  := 0;
      Self.Error  := False;
   end Reset_State;

end VSS.Strings.Converters.Decoders.GB18030;