gnoga_2.1.2_5f127c56/deps/zanyblue/src/text/zanyblue-text-utils.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
--  -*- coding: utf-8 -*-
--
--  ZanyBlue, an Ada library and framework for finite element analysis.
--
--  Copyright (c) 2012, 2016, Michael Rohan <mrohan@zanyblue.com>
--  All rights reserved.
--
--  Redistribution and use in source and binary forms, with or without
--  modification, are permitted provided that the following conditions
--  are met:
--
--    * Redistributions of source code must retain the above copyright
--      notice, this list of conditions and the following disclaimer.
--
--    * Redistributions in binary form must reproduce the above copyright
--      notice, this list of conditions and the following disclaimer in the
--      documentation and/or other materials provided with the distribution.
--
--    * Neither the name of ZanyBlue nor the names of its contributors may
--      be used to endorse or promote products derived from this software
--      without specific prior written permission.
--
--  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
--  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
--  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
--  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
--  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
--  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
--  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
--  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
--  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
--  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
--  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--

--
--  Language 2-letter codes and names defined by Library of Congress:
--
--  http://www.loc.gov/standards/iso639-2
--
--  Territory codes defined by ISO 3166
--
--  http://www.iso.org/iso/list-en1-semic-3.txt
--

with Ada.Characters.Handling;
with Ada.Characters.Conversions;

package body ZanyBlue.Text.Utils is

   function Escape_Character
     (C : Unicode_Character)
      return String;
   --  Return the String value for a Wide Character, either the character
   --  itself if within Character range or the Unicode_Escape string for
   --  the character.

   function Requires_Unicode_Escape
     (C : Unicode_Character)
      return Boolean;
   --  Determine if a Wide character requires Unicode escaping for strings.

   function Unicode_Escape
     (C : Unicode_Character)
      return String;
   --  Return Unicode escape sequence for a wide character, e.g., "\u009e".

   -----------------------
   --  ASCII_Capitalize --
   -----------------------

   procedure ASCII_Capitalize (S : in out String) is
   begin
      for I in S loop
         S.Replace_Unicode (I, ASCII_Lowercase (S (I)));
      end loop;
      S.Replace_Unicode ((S.First), ASCII_Uppercase (S (S.First)));
   end ASCII_Capitalize;

   ---------------------
   -- ASCII_Lowercase --
   ---------------------

   procedure ASCII_Lowercase (S : in out String) is
   begin
      for I in S loop
         S.Replace_Unicode (I, ASCII_Lowercase (S (I)));
      end loop;
   end ASCII_Lowercase;

   ---------------------
   -- ASCII_Lowercase --
   ---------------------

   function ASCII_Lowercase
     (C : Unicode_Character)
      return Unicode_Character
   is

      use Ada.Characters.Handling;
      use Ada.Characters.Conversions;

      N_C : constant Character := To_Character (C);

   begin
      if Is_Letter (N_C) and then Is_Upper (N_C) then
         return To_Wide_Wide_Character (To_Lower (N_C));
      else
         return C;
      end if;
   end ASCII_Lowercase;

   ---------------------
   -- ASCII_Uppercase --
   ---------------------

   procedure ASCII_Uppercase (S : in out String) is
   begin
      for I in S loop
         S.Replace_Unicode (I, ASCII_Uppercase (S (I)));
      end loop;
   end ASCII_Uppercase;

   ---------------------
   -- ASCII_Uppercase --
   ---------------------

   function ASCII_Uppercase
     (C : Unicode_Character)
      return Unicode_Character
   is

      use Ada.Characters.Handling;
      use Ada.Characters.Conversions;

      N_C : constant Character := To_Character (C);

   begin
      if Is_Letter (N_C) and then Is_Lower (N_C) then
         return To_Wide_Wide_Character (To_Upper (N_C));
      else
         return C;
      end if;
   end ASCII_Uppercase;

   ----------------------
   -- Escape_Character --
   ----------------------

   function Escape_Character
     (C : Unicode_Character)
      return String
   is
   begin
      if Requires_Unicode_Escape (C) then
         return Unicode_Escape (C);
      else
         return From_Unicode (C);
      end if;
   end Escape_Character;

   -------------------
   -- Escape_String --
   -------------------

   function Escape_String
     (Source : String)
      return String
   is
      Result : String;
   begin
      for I in Source loop
         Result := Result & Escape_Character (Source (I));
      end loop;
      return Result;
   end Escape_String;

   ----------------------
   -- Non_Blank_Prefix --
   ----------------------

   function Non_Blank_Prefix
     (S : String)
      return String
   is
   begin
      for I in S loop
         if S (I) = ' ' then
            return S.Slice (S.First, I - 1);
         end if;
      end loop;
      return S;
   end Non_Blank_Prefix;

   -----------------------------
   -- Requires_Unicode_Escape --
   -----------------------------

   function Requires_Unicode_Escape
     (C : Unicode_Character)
      return Boolean
   is
      Pos : constant Natural := Unicode_Character'Pos (C);
      Ch  : Character;
   begin
      if Pos >= 127 then
         return True;
      end if;
      Ch := Character'Val (Pos);
      return Ch = '"' or else not Ada.Characters.Handling.Is_Graphic (Ch);
   end Requires_Unicode_Escape;

   -----------------
   -- Starts_With --
   -----------------

   function Starts_With
     (S      : String;
      Start  : Positive;
      Prefix : String)
      return Boolean
   is
   begin
      return Head (S.Slice (Start, S.Last), Prefix.Length) = Prefix;
   end Starts_With;

   ---------------------
   -- Unescape_String --
   ---------------------

   function Unescape_String
     (Source : String)
      return String
   is

      use Ada.Characters.Conversions;

      Wide_BS : constant Unicode_Character :=
        To_Wide_Wide_Character (ASCII.BS);
      Wide_HT : constant Unicode_Character :=
        To_Wide_Wide_Character (ASCII.HT);
      Wide_LF : constant Unicode_Character :=
        To_Wide_Wide_Character (ASCII.LF);
      Wide_FF : constant Unicode_Character :=
        To_Wide_Wide_Character (ASCII.FF);
      Wide_CR : constant Unicode_Character :=
        To_Wide_Wide_Character (ASCII.CR);

      Buffer : String;
      WCh    : Unicode_Character;
      Ch     : Unicode_Character;
      I      : Positive := Source.First;
      Done   : Boolean;

      procedure Get
        (Ch   : out Unicode_Character;
         Done : out Boolean);
      procedure Get
        (X    : out Natural;
         Done : out Boolean);

      procedure Get
        (Ch   : out Unicode_Character;
         Done : out Boolean)
      is
      begin
         Ch   := 'x';
         Done := I > Source.Last;
         if not Done then
            Ch := Source (I);
            I  := I + 1;
         end if;
      end Get;

      procedure Get
        (X    : out Natural;
         Done : out Boolean)
      is
         Offset : Natural;
         Base   : Natural;
         Ch     : Unicode_Character;
      begin
         Get (Ch, Done);
         case Ch is
            when '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' =>
               Offset := Character'Pos ('0');
               Base   := 0;
            when 'a' | 'b' | 'c' | 'd' | 'e' | 'f' =>
               Offset := Character'Pos ('a');
               Base   := 10;
            when 'A' | 'B' | 'C' | 'D' | 'E' | 'F' =>
               Offset := Character'Pos ('A');
               Base   := 10;
            when others =>
               raise Unicode_Format_Error
                 with To_Latin_1 (Source) & ':' & To_Character (Ch);
         end case;
         X := Base + Unicode_Character'Pos (Ch) - Offset;
      end Get;

      Digit : Natural;
      Value : Natural;

   begin
      Character_Loop :
      loop
         Get (Ch, Done);
         exit Character_Loop when Done;
         if Ch = '\' then
            Get (Ch, Done);
            exit Character_Loop when Done;
            case Ch is

               when 'b' =>               -- \b, backspace
                  WCh := Wide_BS;

               when 'f' =>               -- \f, form feed
                  WCh := Wide_FF;

               when 'n' =>               -- \n, newline
                  WCh := Wide_LF;

               when 'r' =>               -- \r, carriage return
                  WCh := Wide_CR;

               when 't' =>              -- \t, horizontal tab
                  WCh := Wide_HT;

               when 'u' =>               -- Unicode escape
                  Value := 0;
                  Unicode_Character_Loop :
                  for I in 1 .. 4 loop
                     Get (Digit, Done);
                     Value := Value * 16 + Digit;
                  end loop Unicode_Character_Loop;
                  WCh := Unicode_Character'Val (Value);

               when others =>
                  WCh := Ch;

            end case;
         else
            WCh := Ch;
         end if;
         Append (Buffer, WCh);
      end loop Character_Loop;
      return Buffer;
   end Unescape_String;

   --------------------
   -- Unicode_Escape --
   --------------------

   function Unicode_Escape
     (C : Unicode_Character)
      return String
   is
      Hex_Map : constant String := "0123456789abcdef";
      Result  : String;
      Pos     : Natural         := Unicode_Character'Pos (C);
   begin
      for I in 1 .. 4 loop
         Result.Replace_Unicode ((5 - I), Hex_Map ((Pos rem 16) + 1));
         Pos := Pos / 16;
      end loop;
      return "\u" & Result;
   end Unicode_Escape;

end ZanyBlue.Text.Utils;