adacl-strings.ads

1--------------------------------------------------------------- {{{1 ----------
2--: Copyright © 2003 … 2023 Martin Krischik «krischik@users.sourceforge.net»
3------------------------------------------------------------------------------
4--: This library is free software; you can redistribute it and/or modify it
5--: under the terms of the GNU Library General Public License as published by
6--: the Free Software Foundation; either version 2 of the License, or (at your
7--: option) any later version.
8--:
9--: This library is distributed in the hope that it will be useful, but
10--: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11--: or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12--: License for more details.
13--:
14--: You should have received a copy of the GNU Library General Public License
15--: along with this library; if not, write to the Free Software Foundation,
16--: Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17--------------------------------------------------------------- }}}1 ----------
18
19pragma License (Modified_Gpl);
20pragma Ada_2022;
21
22with Ada.Strings.Unbounded;
23with Ada.Strings.Maps;
24with Ada.Containers;
25
26---
27-- @summary
28-- Tools for changing strings.
29--
30-- @description
31-- No, I have not created some new String class - yet. Just a few string tools.
32--
33package AdaCL.Strings is
34
35 subtype Hex_Digit is Natural range 0 .. 16#F#;
36
37 ---
38 -- Replace all Search with Replace
39 --
40 --: @param Source String to be changed
41 --: @param Search String we look for
42 --: @param Replace String we want to have
43 --: @param Mapping Search mapping
44 procedure Change_All
45 (Source : in out Ada.Strings.Unbounded.Unbounded_String;
46 Search : in String;
47 Replace : in String;
48 Mapping : in Ada.Strings.Maps.Character_Mapping := Ada.Strings.Maps.Identity);
49
50 ---
51 -- Replace all Search with Replace and Count how often it was done.
52 --
53 --: @param Source String to be changed
54 --: @param Search String we look for
55 --: @param Replace String we want to have
56 --: @param Mapping Search mapping
57 --: @param Count Count of replaces done
58 procedure Change_All
59 (Source : in out Ada.Strings.Unbounded.Unbounded_String;
60 Search : in String;
61 Replace : in String;
62 Mapping : in Ada.Strings.Maps.Character_Mapping := Ada.Strings.Maps.Identity;
63 Count : out Natural);
64
65 ---
66 -- Replace all Search with Replace
67 --
68 --: @param Source String to be changed
69 --: @param Search String we look for
70 --: @param Replace String we want to have
71 --: @param Mapping Search mapping
72 procedure Change_First
73 (Source : in out Ada.Strings.Unbounded.Unbounded_String;
74 Search : in String;
75 Replace : in String;
76 Mapping : in Ada.Strings.Maps.Character_Mapping := Ada.Strings.Maps.Identity);
77
78 ---
79 -- Replace First Search with Replace and return success flag.
80 --
81 --: @param Source String to be changed
82 --: @param Search String we look for
83 --: @param Replace String we want to have
84 --: @param Mapping Search mapping
85 --: @param Found Count of replaces done
86 procedure Change_First
87 (Source : in out Ada.Strings.Unbounded.Unbounded_String;
88 Search : in String;
89 Replace : in String;
90 Mapping : in Ada.Strings.Maps.Character_Mapping := Ada.Strings.Maps.Identity;
91 Found : out Boolean);
92
93 ---
94 -- Replace Last Search with Replace
95 --
96 --: @param Source : String we look for
97 --: @param Search : String to be changed
98 --: @param Replace : String we want to have
99 --: @param Mapping : Search mapping
100 procedure Change_Last
101 (Source : in out Ada.Strings.Unbounded.Unbounded_String;
102 Search : in String;
103 Replace : in String;
104 Mapping : in Ada.Strings.Maps.Character_Mapping := Ada.Strings.Maps.Identity);
105
106 ---
107 -- Replace Last Search with Replace and return success flag.
108 --
109 --: @param Source String to be changed
110 --: @param Search String we look for
111 --: @param Replace String we want to have
112 --: @param Mapping Search mapping
113 --: @param Found Count of replaces done
114 procedure Change_Last
115 (Source : in out Ada.Strings.Unbounded.Unbounded_String;
116 Search : in String;
117 Replace : in String;
118 Mapping : in Ada.Strings.Maps.Character_Mapping := Ada.Strings.Maps.Identity;
119 Found : out Boolean);
120
121 ---
122 -- Searches for all occurences of text "Search" and Inserts text "Insert" after the found text but only when
123 -- "Insert" is not allready there.
124 --
125 --: @param Source String to be changed
126 --: @param Search String we look for
127 --: @param New_Item String we want to insert
128 --: @param Mapping Search mapping
129 procedure Append_All
130 (Source : in out Ada.Strings.Unbounded.Unbounded_String;
131 Search : in String;
132 New_Item : in String;
133 Mapping : in Ada.Strings.Maps.Character_Mapping := Ada.Strings.Maps.Identity);
134
135 ---
136 -- Searches for all occurences of text "Search" and Inserts text "Insert" after the found text but only when
137 -- "Insert" is not allready there.
138 --
139 --: @param Source String to be changed
140 --: @param Search String we look for
141 --: @param New_Item String we want to insert
142 --: @param Mapping Search mapping
143 --: @param Count Count of replaces done
144 procedure Append_All
145 (Source : in out Ada.Strings.Unbounded.Unbounded_String;
146 Search : in String;
147 New_Item : in String;
148 Mapping : in Ada.Strings.Maps.Character_Mapping := Ada.Strings.Maps.Identity;
149 Count : out Natural);
150
151 ---
152 -- Searches for first occurence of text "Search" and Inserts text "Insert" after the found text but only when
153 -- "Insert" is not allready there.
154 --
155 --: @param Source String to be changed
156 --: @param Search String we look for
157 --: @param New_Item String we want to insert
158 --: @param Mapping Search mapping
159 procedure Append_First
160 (Source : in out Ada.Strings.Unbounded.Unbounded_String;
161 Search : in String;
162 New_Item : in String;
163 Mapping : in Ada.Strings.Maps.Character_Mapping := Ada.Strings.Maps.Identity);
164
165 ---
166 -- Searches for first occurence of text "Search" and Inserts text "Insert" after the found text but only when
167 -- "Insert" is not allready there.
168 --
169 --: @param Source String to be changed
170 --: @param Search String we look for
171 --: @param New_Item String we want to insert
172 --: @param Mapping Search mapping
173 --: @param Found Count of replaces done
174 procedure Append_First
175 (Source : in out Ada.Strings.Unbounded.Unbounded_String;
176 Search : in String;
177 New_Item : in String;
178 Mapping : in Ada.Strings.Maps.Character_Mapping := Ada.Strings.Maps.Identity;
179 Found : out Boolean);
180
181 ---
182 -- Searches for last occurence of text "Search" and Inserts text "Insert" after the found text but only when
183 -- "Insert" is not allready there.
184 --
185 --: @param Source String to be changed
186 --: @param Search String we look for
187 --: @param New_Item String we want to insert
188 --: @param Mapping Search mapping
189 procedure Append_Last
190 (Source : in out Ada.Strings.Unbounded.Unbounded_String;
191 Search : in String;
192 New_Item : in String;
193 Mapping : in Ada.Strings.Maps.Character_Mapping := Ada.Strings.Maps.Identity);
194
195 ---
196 -- Searches for last occurence of text "Search" and Inserts text "Insert" after the found text but only when
197 -- "Insert" is not allready there.
198 --
199 --: @param Source String to be changed
200 --: @param Search String we look for
201 --: @param New_Item String we want to insert
202 --: @param Mapping Search mapping
203 --: @param Found Count of replaces done
204 procedure Append_Last
205 (Source : in out Ada.Strings.Unbounded.Unbounded_String;
206 Search : in String;
207 New_Item : in String;
208 Mapping : in Ada.Strings.Maps.Character_Mapping := Ada.Strings.Maps.Identity;
209 Found : out Boolean);
210
211 ---
212 -- Return the end-of-field position in Data after "Starting_Index", assuming that fields are separated by the
213 -- Field_Separator. If there's no Field_Separator, return the end of the Data.
214 --: @param Source String to search in
215 --: @param Field_Separator Field seperator.
216 --: @param Starting_At Start search at.
217 function Field_End
218 (Source : in String;
219 Field_Separator : in Character;
220 Starting_At : Positive)
221 return Natural with
222 Pure_Function;
223
224 ---
225 -- Searches for first occurence of text "Search" and Inserts text "Insert" bevore when "Insert" is there.
226 --
227 --: @param Source String to be changed
228 --: @param Search String we look for
229 --: @param New_Item String we want to insert
230 --: @param Mapping Search mapping
231 procedure Insert_First
232 (Source : in out Ada.Strings.Unbounded.Unbounded_String;
233 Search : in String;
234 New_Item : in String;
235 Mapping : in Ada.Strings.Maps.Character_Mapping := Ada.Strings.Maps.Identity);
236
237 ---
238 -- Searches for first occurence of text "Search" and Inserts text "Insert" bevore when "Insert" is there.
239 --
240 --: @param Source String to be changed
241 --: @param Search String we look for
242 --: @param New_Item String we want to insert
243 --: @param Mapping Search mapping
244 --: @param Found Count of replaces done
245 procedure Insert_First
246 (Source : in out Ada.Strings.Unbounded.Unbounded_String;
247 Search : in String;
248 New_Item : in String;
249 Mapping : in Ada.Strings.Maps.Character_Mapping := Ada.Strings.Maps.Identity;
250 Found : out Boolean);
251
252 ---
253 -- Searches for all occurences of text "Search" and Inserts text "Insert" bevore when "Insert" is there.
254 --
255 --: @param Source Search mapping
256 --: @param Search String we want to insert
257 --: @param New_Item String we look for
258 --: @param Mapping String to be changed
259 procedure Insert_All
260 (Source : in out Ada.Strings.Unbounded.Unbounded_String;
261 Search : in String;
262 New_Item : in String;
263 Mapping : in Ada.Strings.Maps.Character_Mapping := Ada.Strings.Maps.Identity);
264
265 ---
266 -- Searches for all occurences of text "Search" and Inserts text "Insert" bevore when "Insert" is there.
267 --
268 --: @param Source String to be changed
269 --: @param Search String we look for
270 --: @param New_Item String we want to insert
271 --: @param Mapping Search mapping
272 --: @param Count Count of replaces done
273 procedure Insert_All
274 (Source : in out Ada.Strings.Unbounded.Unbounded_String;
275 Search : in String;
276 New_Item : in String;
277 Mapping : in Ada.Strings.Maps.Character_Mapping := Ada.Strings.Maps.Identity;
278 Count : out Natural);
279
280 ---
281 -- Searches for last occurence of text "Search" and Inserts text "Insert" bevore when "Insert" is there.
282 --
283 --: @param Source String to be changed
284 --: @param Search String we look for
285 --: @param New_Item String we want to insert
286 --: @param Mapping Search mapping
287 procedure Insert_Last
288 (Source : in out Ada.Strings.Unbounded.Unbounded_String;
289 Search : in String;
290 New_Item : in String;
291 Mapping : in Ada.Strings.Maps.Character_Mapping := Ada.Strings.Maps.Identity);
292
293 ---
294 -- Searches for last occurence of text "Search" and Inserts text "Insert" bevore when "Insert" is there.
295 --
296 --: @param Source String to be changed
297 --: @param Search String we look for
298 --: @param New_Item String we want to insert
299 --: @param Mapping Search mapping
300 --: @param Found Count of replaces done
301 procedure Insert_Last
302 (Source : in out Ada.Strings.Unbounded.Unbounded_String;
303 Search : in String;
304 New_Item : in String;
305 Mapping : in Ada.Strings.Maps.Character_Mapping := Ada.Strings.Maps.Identity;
306 Found : out Boolean);
307
308 ------------------------------------------------------------------------------
309
310 ---
311 -- Hash function for booch components.
312 --
313 --: @param Key String to calculate a hash value form
314 --: @return hash value
315 function Hash (Key : String) return Natural with
316 Pure_Function;
317
318 ---
319 -- Hash function for booch components.
320 --
321 --: @param Key String to calculate a hash value form
322 --: @return hash value
323 function Hash (Key : Ada.Strings.Unbounded.Unbounded_String) return Natural with
324 Pure_Function;
325
326 ---
327 -- Hash function for Ada components.
328 --
329 --: @param Key String to calculate a hash value form
330 --: @return hash value
331 function Hash (Key : Ada.Strings.Unbounded.Unbounded_String) return Ada.Containers.Hash_Type with
332 Pure_Function;
333
334 ---
335 -- Hash function for Ada components.
336 --
337 --: @param Key String to calculate a hash value form
338 --: @return hash value
339 function Hash (Key : String) return Ada.Containers.Hash_Type with
340 Pure_Function;
341
342 ---
343 -- convert String into Integer
344 --
345 --: @param Image String to to be shown as Integer
346 --: @return pased integer value
347 function Value (Image : Ada.Strings.Unbounded.Unbounded_String) return Integer is
348 (Integer'Value (Ada.Strings.Unbounded.To_String (Image))) with
349 Pure_Function, Inline;
350
351 ------------------------------------------------------------------------------
352
353 ---
354 -- Extract the first word.
355 --
356 --: @param Source String to search
357 --: @param Delimiter word delimiter. Default is space.
358 function First_Word
359 (Source : in String;
360 Delimiter : in Character := ' ')
361 return String with
362 Pure_Function;
363
364 ---
365 -- Extract the last word.
366 --
367 --: @param Source String to search
368 --: @param Delimiter word delimiter. Default is space.
369 function Last_Word
370 (Source : in String;
371 Delimiter : in Character := ' ')
372 return String with
373 Pure_Function;
374
375end AdaCL.Strings;
376
377---------------------------------------------------------------- {{{ ----------
378--: vim: set textwidth=0 nowrap tabstop=8 shiftwidth=3 softtabstop=3 expandtab :
379--: vim: set filetype=ada fileencoding=utf-8 fileformat=unix foldmethod=expr :
380--: vim: set spell spelllang=en_gb