mold_lib_2.2.1_9048c6c2/src/text_filters/impl/predefined_text_filters.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
-------------------------------------------------------------------------------
--
--  Mold_Lib - Meta-variable Operations for Lean Development
--  Copyright (c) 2023 Francesc Rocher <francesc.rocher@gmail.com>
--  SPDX-License-Identifier: MIT
--
-------------------------------------------------------------------------------

with Ada.Strings.Maps.Constants;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Characters.Handling;

package body Predefined_Text_Filters is

   package CHAR renames Ada.Characters.Handling;

   --------------
   -- Is_Blank --
   --------------

   function Is_Blank (C : Character) return Boolean is
     (CHAR.Is_Space (C) or else C = ASCII.HT);

   ---------------
   -- Trim_Left --
   ---------------

   function Trim_Left (S : UString) return UString is
      First_Char : Natural := 1;
   begin
      loop
         exit when not Is_Blank (S.Element (First_Char));
         First_Char := First_Char + 1;
      end loop;
      return S.Unbounded_Slice (First_Char, S.Length);
   end Trim_Left;

   ----------------
   -- Trim_Right --
   ----------------

   function Trim_Right (S : UString) return UString is
      Last_Char : Natural := S.Length;
   begin
      loop
         exit when not Is_Blank (S.Element (Last_Char));
         Last_Char := Last_Char - 1;
      end loop;
      return S.Unbounded_Slice (1, Last_Char);
   end Trim_Right;

   ---------------
   -- Trim_Both --
   ---------------

   function Trim_Both (S : UString) return UString is
     (Trim_Left (Trim_Right (S)));

   -----------------
   -- Trim_Squash --
   -----------------

   function Trim_Squash (S : UString) return UString is
      Trimmed   : constant UString := S;
      Add_Space : Boolean          := True;
   begin
      return Aux : UString := To_Unbounded_String ("") do
         for I in 1 .. Trimmed.Length loop
            if Is_Blank (Trimmed.Element (I)) then
               if Add_Space then
                  Aux := Aux & ' ';
               end if;
               Add_Space := False;
            else
               Aux       := Aux & Trimmed.Element (I);
               Add_Space := True;
            end if;
         end loop;
      end return;
   end Trim_Squash;

   --------------
   -- Trim_All --
   --------------

   function Trim_All (S : UString) return UString is
     (Trim_Both (Trim_Squash (S)));

   -------------------
   -- Remove_Blanks --
   -------------------

   function Remove_Blanks (S : UString) return UString is
   begin
      return Aux : UString := To_Unbounded_String ("") do
         for I in 1 .. S.Length loop
            if not Is_Blank (S.Element (I)) then
               Aux := Aux & S.Element (I);
            end if;
         end loop;
      end return;
   end Remove_Blanks;

   -----------------
   -- Replace_All --
   -----------------

   function Replace_All (S : UString; Src, Dst : Character) return UString is
   begin
      return Aux : UString := S do
         for I in 1 .. Aux.Length loop
            if Aux.Element (I) = Src then
               Aux.Replace_Element (I, Dst);
            end if;
         end loop;
      end return;
   end Replace_All;

   -------------------
   -- Replace_First --
   -------------------

   function Replace_First (S : UString; Src, Dst : Character) return UString is
   begin
      return Aux : UString := S do
         for I in 1 .. Aux.Length loop
            if Aux.Element (I) = Src then
               Aux.Replace_Element (I, Dst);
               exit;
            end if;
         end loop;
      end return;
   end Replace_First;

   ------------------
   -- Replace_Last --
   ------------------

   function Replace_Last (S : UString; Src, Dst : Character) return UString is
   begin
      return Aux : UString := S do
         for I in reverse 1 .. Aux.Length loop
            if Aux.Element (I) = Src then
               Aux.Replace_Element (I, Dst);
               exit;
            end if;
         end loop;
      end return;
   end Replace_Last;

   --------------
   -- Sequence --
   --------------

   function Sequence (S : UString; Dst : Character) return UString is
     (S.Length * Dst);

   ----------------
   -- Delete_All --
   ----------------

   function Delete_All (S : UString; Src : Character) return UString is
   begin
      return Aux : UString := To_Unbounded_String ("") do
         for I in 1 .. S.Length loop
            if S.Element (I) /= Src then
               Aux := Aux & S.Element (I);
            end if;
         end loop;
      end return;
   end Delete_All;

   --------------------
   -- Case_Lowercase --
   --------------------

   function Case_Lowercase (S : UString) return UString is
     (S.Translate (Ada.Strings.Maps.Constants.Lower_Case_Map));

   -----------------
   -- To_Capitals --
   -----------------

   function To_Capitals (S : UString; First_Also : Boolean) return UString is
      Capitalize : Boolean := First_Also;
   begin
      return Aux : UString := S do
         for I in 1 .. Aux.Length loop
            if Is_Blank (Aux.Element (I)) then
               Capitalize := True;
            else
               if Capitalize then
                  Aux.Replace_Element (I, CHAR.To_Upper (Aux.Element (I)));
               else
                  Aux.Replace_Element (I, CHAR.To_Lower (Aux.Element (I)));
               end if;
               Capitalize := False;
            end if;
         end loop;
      end return;
   end To_Capitals;

   -------------------
   -- Case_Capitals --
   -------------------

   function Case_Capitals (S : UString) return UString is
     (To_Capitals (S, True));

   --------------------
   -- Case_Uppercase --
   --------------------

   function Case_Uppercase (S : UString) return UString is
     (S.Translate (Ada.Strings.Maps.Constants.Upper_Case_Map));

   -------------
   -- Padding --
   -------------

   function Padding
     (Value : UString; Dir : Direction; Char : Character; Length : Natural)
      return UString
   is
   begin
      if Length <= Value.Length then
         return Value;
      else
         declare
            Pad : constant UString := (Length - Value.Length) * Char;
         begin
            if Dir = left then
               return Pad & Value;
            else
               return Value & Pad;
            end if;
         end;
      end if;
   end Padding;

   --------------
   -- Truncate --
   --------------

   function Truncate (S : UString; Length : Natural) return UString is
   begin
      if Length <= S.Length then
         return S.Unbounded_Slice (1, Length);
      else
         return S;
      end if;
   end Truncate;

   ---------------------
   -- Style_Flat_Case --
   ---------------------

   function Style_Flat_Case (S : UString) return UString is
     (Delete_All (Case_Lowercase (Trim_All (S)), ' '));

   ----------------------------
   -- Style_Lower_Camel_Case --
   ----------------------------

   function Style_Lower_Camel_Case (S : UString) return UString is
     (Delete_All (To_Capitals (Trim_All (S), False), ' '));

   ----------------------------
   -- Style_Upper_Camel_Case --
   ----------------------------

   function Style_Upper_Camel_Case (S : UString) return UString is
     (Delete_All (To_Capitals (Trim_All (S), True), ' '));

   ---------------------
   -- Style_Uppercase --
   ---------------------

   function Style_Uppercase (S : UString) return UString is
     (Delete_All (Case_Uppercase (Trim_All (S)), ' '));

   ----------------------
   -- Style_Snake_Case --
   ----------------------

   function Style_Snake_Case (S : UString) return UString is
     (Replace_All (Case_Lowercase (Trim_All (S)), ' ', '_'));

   ----------------------------
   -- Style_Camel_Snake_Case --
   ----------------------------

   function Style_Camel_Snake_Case (S : UString) return UString is
     (Replace_All (To_Capitals (Trim_All (S), False), ' ', '_'));

   ----------------------
   -- Style_Title_Case --
   ----------------------

   function Style_Title_Case (S : UString) return UString is
     (Replace_All (To_Capitals (Trim_All (S), True), ' ', '_'));

   --------------------
   -- Style_All_Caps --
   --------------------

   function Style_All_Caps (S : UString) return UString is
     (Replace_All (Case_Uppercase (Trim_All (S)), ' ', '_'));

   ---------------------
   -- Style_Dash_Case --
   ---------------------

   function Style_Dash_Case (S : UString) return UString is
     (Replace_All (Case_Lowercase (Trim_All (S)), ' ', '-'));

   ----------------------
   -- Style_Train_Case --
   ----------------------

   function Style_Train_Case (S : UString) return UString is
     (Replace_All (To_Capitals (Trim_All (S), True), ' ', '-'));

   ---------------------------
   -- Style_Train_Uppercase --
   ---------------------------

   function Style_Train_Uppercase (S : UString) return UString is
     (Replace_All (Case_Uppercase (Trim_All (S)), ' ', '-'));

end Predefined_Text_Filters;