-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmapcode_utils-str_tools.adb
More file actions
123 lines (113 loc) · 4.33 KB
/
mapcode_utils-str_tools.adb
File metadata and controls
123 lines (113 loc) · 4.33 KB
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
-- -----------------------------------------------------------------------------
-- Copyright (C) 2003-2019 Stichting Mapcode Foundation (http://www.mapcode.com)
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
-- -----------------------------------------------------------------------------
-- Various utilities on strings
package body Mapcode_Utils.Str_Tools is
-- Locate the Nth occurence of a fragment within a string,
-- between a given index (first/last if 0) and the end/beginning of the
-- string, searching forward or backward
-- Return the index in Within of the char matching the start of Fragment
-- Return 0 if Index not in Within, if Within or Fragment is empty,
-- or if not found
-- Locate Nth occurence of a fragment within a string,
-- between a given index (first/last if 0) and the end/beginning of string,
-- searching forward or backward
-- Returns index in Within of char matching start of Fragment
-- or 0 if not found or if Within or Fragment is empty
function Locate (Within : String;
Fragment : String;
From_Index : Natural := 0;
Forward : Boolean := True;
Occurence : Positive := 1)
return Natural is
Index : Natural;
Found_Occurence : Natural := 0;
begin
-- Fix Index
Index := (if From_Index = 0 then
(if Forward then Within'First else Within'Last)
else From_Index);
-- Handle limit or incorrect values
if Within'Length = 0
or else Fragment'Length = 0
or else Index not in Within'First .. Within'Last then
return 0;
end if;
if Forward then
for I in Index .. Within'Last - Fragment'Length + 1 loop
if Within(I .. I + Fragment'Length - 1) = Fragment then
Found_Occurence := Found_Occurence + 1;
if Found_Occurence = Occurence then
return I;
end if;
end if;
end loop;
else
for I in reverse Within'First .. Index - Fragment'Length + 1 loop
if Within(I .. I + Fragment'Length - 1) = Fragment then
Found_Occurence := Found_Occurence + 1;
if Found_Occurence = Occurence then
return I;
end if;
end if;
end loop;
end if;
return 0;
exception
when Constraint_Error =>
return 0;
end Locate;
-- Convert a character into upper char
function Upper_Char (Char : Character) return Character is
Offset : constant Integer := Character'Pos('A') - Character'Pos('a');
begin
return (if Char not in 'a' .. 'z' then Char
else Character'Val (Character'Pos(Char) + Offset));
end Upper_Char;
-- Convert a character into lower char
function Lower_Char (Char : Character) return Character is
Offset : constant Integer := Character'Pos('A') - Character'Pos('a');
begin
return (if Char not in 'A' .. 'Z' then Char
else Character'Val (Character'Pos(Char) - Offset));
end Lower_Char;
-- Convert the characters of Str into upper char
function Upper_Str (Str : String) return String is
Result : String := Str;
begin
for C of Result loop
C := Upper_Char (C);
end loop;
return Result;
end Upper_Str;
-- Convert the characters of Str:
-- Any letter that follows a letter is lower char
-- Any other letter (including the first letter) is UPPER char
function Mixed_Str (Str : String) return String is
Result : String := Str;
Prev_Separator : Boolean := True;
begin
for C of Result loop
if Prev_Separator and then C in 'a' .. 'z' then
C := Upper_Char (C);
elsif not Prev_Separator and then C in 'A' .. 'Z' then
C := Lower_Char (C);
end if;
Prev_Separator := C not in 'a' .. 'z'
and then C not in 'A' .. 'Z';
end loop;
return Result;
end Mixed_Str;
end Mapcode_Utils.Str_Tools;