adacl_5.15.1_e7c1515b/src/adacl-strings.ads

  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
--------------------------------------------------------------- {{{1 ----------
--: Copyright © 2003 … 2023 Martin Krischik «krischik@users.sourceforge.net»
------------------------------------------------------------------------------
--: This library is free software; you can redistribute it and/or modify it
--: under the terms of the GNU Library General Public License as published by
--: the Free Software Foundation; either version 2 of the License, 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 MERCHANTABILITY
--: or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
--: License for more details.
--:
--: You should have received a copy of the GNU Library General Public License
--: along with this library; if not, write to the Free Software Foundation,
--: Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
--------------------------------------------------------------- }}}1 ----------

pragma License (Modified_Gpl);
pragma Ada_2022;

with Ada.Strings.Unbounded;
with Ada.Strings.Maps;
with Ada.Containers;

---
--  @summary
--
--  @description
--  No, I have not created some new String class - yet. Just a few string tools.
--
package AdaCL.Strings is

   subtype Hex_Digit is Natural range 0 .. 16#F#;

   ---
   --  Replace all Search with Replace
   --
   --: @param Source   String to be changed
   --: @param Search   String we look for
   --: @param Replace  String we want to have
   --: @param Mapping  Search mapping
   procedure Change_All
      (Source  : in out Ada.Strings.Unbounded.Unbounded_String;
       Search  : in     String;
       Replace : in     String;
       Mapping : in     Ada.Strings.Maps.Character_Mapping := Ada.Strings.Maps.Identity);

   ---
   --  Replace all Search with Replace and Count how often it was done.
   --
   --: @param Source   String to be changed
   --: @param Search   String we look for
   --: @param Replace  String we want to have
   --: @param Mapping  Search mapping
   --: @param Count    Count of replaces done
   procedure Change_All
      (Source  : in out Ada.Strings.Unbounded.Unbounded_String;
       Search  : in     String;
       Replace : in     String;
       Mapping : in     Ada.Strings.Maps.Character_Mapping := Ada.Strings.Maps.Identity;
       Count   :    out Natural);

   ---
   --  Replace all Search with Replace
   --
   --: @param Source   String to be changed
   --: @param Search   String we look for
   --: @param Replace  String we want to have
   --: @param Mapping  Search mapping
   procedure Change_First
      (Source  : in out Ada.Strings.Unbounded.Unbounded_String;
       Search  : in     String;
       Replace : in     String;
       Mapping : in     Ada.Strings.Maps.Character_Mapping := Ada.Strings.Maps.Identity);

   ---
   --  Replace First Search with Replace and return success flag.
   --
   --: @param Source   String to be changed
   --: @param Search   String we look for
   --: @param Replace  String we want to have
   --: @param Mapping  Search mapping
   --: @param Found    Count of replaces done
   procedure Change_First
      (Source  : in out Ada.Strings.Unbounded.Unbounded_String;
       Search  : in     String;
       Replace : in     String;
       Mapping : in     Ada.Strings.Maps.Character_Mapping := Ada.Strings.Maps.Identity;
       Found   :    out Boolean);

   ---
   --  Replace Last Search with Replace
   --
   --: @param Source  : String we look for
   --: @param Search  : String to be changed
   --: @param Replace : String we want to have
   --: @param Mapping : Search mapping
   procedure Change_Last
      (Source  : in out Ada.Strings.Unbounded.Unbounded_String;
       Search  : in     String;
       Replace : in     String;
       Mapping : in     Ada.Strings.Maps.Character_Mapping := Ada.Strings.Maps.Identity);

   ---
   --  Replace Last Search with Replace and return success flag.
   --
   --: @param Source   String to be changed
   --: @param Search   String we look for
   --: @param Replace  String we want to have
   --: @param Mapping  Search mapping
   --: @param Found    Count of replaces done
   procedure Change_Last
      (Source  : in out Ada.Strings.Unbounded.Unbounded_String;
       Search  : in     String;
       Replace : in     String;
       Mapping : in     Ada.Strings.Maps.Character_Mapping := Ada.Strings.Maps.Identity;
       Found   :    out Boolean);

   ---
   --  Searches for all occurences of text "Search" and Inserts text "Insert" after the found text but only when
   --  "Insert" is not allready there.
   --
   --: @param Source    String to be changed
   --: @param Search    String we look for
   --: @param New_Item  String we want to insert
   --: @param Mapping   Search mapping
   procedure Append_All
      (Source   : in out Ada.Strings.Unbounded.Unbounded_String;
       Search   : in     String;
       New_Item : in     String;
       Mapping  : in     Ada.Strings.Maps.Character_Mapping := Ada.Strings.Maps.Identity);

   ---
   --  Searches for all occurences of text "Search" and Inserts text "Insert" after the found text but only when
   --  "Insert" is not allready there.
   --
   --: @param Source    String to be changed
   --: @param Search    String we look for
   --: @param New_Item  String we want to insert
   --: @param Mapping   Search mapping
   --: @param Count     Count of replaces done
   procedure Append_All
      (Source   : in out Ada.Strings.Unbounded.Unbounded_String;
       Search   : in     String;
       New_Item : in     String;
       Mapping  : in     Ada.Strings.Maps.Character_Mapping := Ada.Strings.Maps.Identity;
       Count    :    out Natural);

   ---
   --  Searches for first occurence of text "Search" and Inserts text "Insert" after the found text but only when
   --  "Insert" is not allready there.
   --
   --: @param Source    String to be changed
   --: @param Search    String we look for
   --: @param New_Item  String we want to insert
   --: @param Mapping   Search mapping
   procedure Append_First
      (Source   : in out Ada.Strings.Unbounded.Unbounded_String;
       Search   : in     String;
       New_Item : in     String;
       Mapping  : in     Ada.Strings.Maps.Character_Mapping := Ada.Strings.Maps.Identity);

   ---
   --  Searches for first occurence of text "Search" and Inserts text "Insert" after the found text but only when
   --  "Insert" is not allready there.
   --
   --: @param Source    String to be changed
   --: @param Search    String we look for
   --: @param New_Item  String we want to insert
   --: @param Mapping   Search mapping
   --: @param Found     Count of replaces done
   procedure Append_First
      (Source   : in out Ada.Strings.Unbounded.Unbounded_String;
       Search   : in     String;
       New_Item : in     String;
       Mapping  : in     Ada.Strings.Maps.Character_Mapping := Ada.Strings.Maps.Identity;
       Found    :    out Boolean);

   ---
   --  Searches for last occurence of text "Search" and Inserts text "Insert" after the found text but only when
   --  "Insert" is not allready there.
   --
   --: @param Source    String to be changed
   --: @param Search    String we look for
   --: @param New_Item  String we want to insert
   --: @param Mapping   Search mapping
   procedure Append_Last
      (Source   : in out Ada.Strings.Unbounded.Unbounded_String;
       Search   : in     String;
       New_Item : in     String;
       Mapping  : in     Ada.Strings.Maps.Character_Mapping := Ada.Strings.Maps.Identity);

   ---
   --  Searches for last occurence of text "Search" and Inserts text "Insert" after the found text but only when
   --  "Insert" is not allready there.
   --
   --: @param Source    String to be changed
   --: @param Search    String we look for
   --: @param New_Item  String we want to insert
   --: @param Mapping   Search mapping
   --: @param Found     Count of replaces done
   procedure Append_Last
      (Source   : in out Ada.Strings.Unbounded.Unbounded_String;
       Search   : in     String;
       New_Item : in     String;
       Mapping  : in     Ada.Strings.Maps.Character_Mapping := Ada.Strings.Maps.Identity;
       Found    :    out Boolean);

   ---
   --  Return the end-of-field position in Data after "Starting_Index", assuming that fields are separated by the
   --  Field_Separator. If there's no Field_Separator, return the end of the Data.
   --: @param Source           String to search in
   --: @param Field_Separator  Field seperator.
   --: @param Starting_At      Start search at.
   function Field_End
      (Source          : in String;
       Field_Separator : in Character;
       Starting_At     :    Positive)
       return Natural;
   pragma Pure_Function (Field_End);

   ---
   --  Searches for first occurence of text "Search" and Inserts text "Insert" bevore when "Insert" is there.
   --
   --: @param Source    String to be changed
   --: @param Search    String we look for
   --: @param New_Item  String we want to insert
   --: @param Mapping   Search mapping
   procedure Insert_First
      (Source   : in out Ada.Strings.Unbounded.Unbounded_String;
       Search   : in     String;
       New_Item : in     String;
       Mapping  : in     Ada.Strings.Maps.Character_Mapping := Ada.Strings.Maps.Identity);

   ---
   --  Searches for first occurence of text "Search" and Inserts text "Insert" bevore when "Insert" is there.
   --
   --: @param Source    String to be changed
   --: @param Search    String we look for
   --: @param New_Item  String we want to insert
   --: @param Mapping   Search mapping
   --: @param Found     Count of replaces done
   procedure Insert_First
      (Source   : in out Ada.Strings.Unbounded.Unbounded_String;
       Search   : in     String;
       New_Item : in     String;
       Mapping  : in     Ada.Strings.Maps.Character_Mapping := Ada.Strings.Maps.Identity;
       Found    :    out Boolean);

   ---
   --  Searches for all occurences of text "Search" and Inserts text "Insert" bevore when "Insert" is there.
   --
   --: @param Source    Search mapping
   --: @param Search    String we want to insert
   --: @param New_Item  String we look for
   --: @param Mapping   String to be changed
   procedure Insert_All
      (Source   : in out Ada.Strings.Unbounded.Unbounded_String;
       Search   : in     String;
       New_Item : in     String;
       Mapping  : in     Ada.Strings.Maps.Character_Mapping := Ada.Strings.Maps.Identity);

   ---
   --  Searches for all occurences of text "Search" and Inserts text "Insert" bevore when "Insert" is there.
   --
   --: @param Source    String to be changed
   --: @param Search    String we look for
   --: @param New_Item  String we want to insert
   --: @param Mapping   Search mapping
   --: @param Count     Count of replaces done
   procedure Insert_All
      (Source   : in out Ada.Strings.Unbounded.Unbounded_String;
       Search   : in     String;
       New_Item : in     String;
       Mapping  : in     Ada.Strings.Maps.Character_Mapping := Ada.Strings.Maps.Identity;
       Count    :    out Natural);

   ---
   --  Searches for last occurence of text "Search" and Inserts text "Insert" bevore when "Insert" is there.
   --
   --: @param Source    String to be changed
   --: @param Search    String we look for
   --: @param New_Item  String we want to insert
   --: @param Mapping   Search mapping
   procedure Insert_Last
      (Source   : in out Ada.Strings.Unbounded.Unbounded_String;
       Search   : in     String;
       New_Item : in     String;
       Mapping  : in     Ada.Strings.Maps.Character_Mapping := Ada.Strings.Maps.Identity);

   ---
   --  Searches for last occurence of text "Search" and Inserts text "Insert" bevore when "Insert" is there.
   --
   --: @param Source    String to be changed
   --: @param Search    String we look for
   --: @param New_Item  String we want to insert
   --: @param Mapping   Search mapping
   --: @param Found     Count of replaces done
   procedure Insert_Last
      (Source   : in out Ada.Strings.Unbounded.Unbounded_String;
       Search   : in     String;
       New_Item : in     String;
       Mapping  : in     Ada.Strings.Maps.Character_Mapping := Ada.Strings.Maps.Identity;
       Found    :    out Boolean);

   ---
   --  Hash function for booch components.
   --
   --: @param Key     String to calculate a hash value form
   --: @return        hash value
   function Hash (Key : String) return Natural;
   pragma Pure_Function (Hash);

   ---
   --  Hash function for booch components.
   --
   --: @param Key     String to calculate a hash value form
   --: @return        hash value
   function Hash (Key : Ada.Strings.Unbounded.Unbounded_String) return Natural;
   pragma Pure_Function (Hash);

   ---
   --  Hash function for Ada components.
   --
   --: @param Key     String to calculate a hash value form
   --: @return        hash value
   function Hash (Key : Ada.Strings.Unbounded.Unbounded_String) return Ada.Containers.Hash_Type;
   pragma Pure_Function (Hash);

   ---
   --  Hash function for Ada components.
   --
   --: @param Key     String to calculate a hash value form
   --: @return        hash value
   function Hash (Key : String) return Ada.Containers.Hash_Type;
   pragma Pure_Function (Hash);

   ---
   --  convert String into Integer
   --
   --: @param Image   String to to be shown as Integer
   --: @return        pased integer value
   function Value (Image : Ada.Strings.Unbounded.Unbounded_String) return Integer is
      (Integer'Value (Ada.Strings.Unbounded.To_String (Image)));
   pragma Inline (Value);
   pragma Pure_Function (Value);

end AdaCL.Strings;

---------------------------------------------------------------- {{{ ----------
--: vim: set textwidth=0 nowrap tabstop=8 shiftwidth=3 softtabstop=3 expandtab :
--: vim: set filetype=ada fileencoding=utf-8 fileformat=unix foldmethod=expr :
--: vim: set spell spelllang=en_gb