1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- G N A T . S P E L L I N G _ C H E C K E R _ G E N E R I C --
9 -- Copyright (C) 1998-2023, AdaCore --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 package body GNAT
.Spelling_Checker_Generic
is
34 ------------------------
35 -- Is_Bad_Spelling_Of --
36 ------------------------
38 function Is_Bad_Spelling_Of
40 Expect
: String_Type
) return Boolean
42 FN
: constant Natural := Found
'Length;
43 FF
: constant Natural := Found
'First;
44 FL
: constant Natural := Found
'Last;
46 EN
: constant Natural := Expect
'Length;
47 EF
: constant Natural := Expect
'First;
48 EL
: constant Natural := Expect
'Last;
50 Letter_o
: constant Char_Type
:= Char_Type
'Val (Character'Pos ('o'));
51 Digit_0
: constant Char_Type
:= Char_Type
'Val (Character'Pos ('0'));
52 Digit_9
: constant Char_Type
:= Char_Type
'Val (Character'Pos ('9'));
55 -- If both strings null, then we consider this a match, but if one
56 -- is null and the other is not, then we definitely do not match
64 -- If first character does not match, then we consider that this is
65 -- definitely not a misspelling. An exception is when we expect a
66 -- letter O and found a zero.
68 elsif Found
(FF
) /= Expect
(EF
)
69 and then (Found
(FF
) /= Digit_0
or else Expect
(EF
) /= Letter_o
)
73 -- Not a bad spelling if both strings are 1-2 characters long
75 elsif FN
< 3 and then EN
< 3 then
78 -- Lengths match. Execute loop to check for a single error, single
79 -- transposition or exact match (we only fall through this loop if
80 -- one of these three conditions is found).
83 for J
in 1 .. FN
- 2 loop
84 if Expect
(EF
+ J
) /= Found
(FF
+ J
) then
86 -- If both mismatched characters are digits, then we do
87 -- not consider it a misspelling (e.g. B345 is not a
88 -- misspelling of B346, it is something quite different)
90 if Expect
(EF
+ J
) in Digit_0
.. Digit_9
91 and then Found
(FF
+ J
) in Digit_0
.. Digit_9
95 elsif Expect
(EF
+ J
+ 1) = Found
(FF
+ J
+ 1)
96 and then Expect
(EF
+ J
+ 2 .. EL
) = Found
(FF
+ J
+ 2 .. FL
)
100 elsif Expect
(EF
+ J
) = Found
(FF
+ J
+ 1)
101 and then Expect
(EF
+ J
+ 1) = Found
(FF
+ J
)
102 and then Expect
(EF
+ J
+ 2 .. EL
) = Found
(FF
+ J
+ 2 .. FL
)
112 -- At last character. Test digit case as above, otherwise we
113 -- have a match since at most this last character fails to match.
115 if Expect
(EL
) in Digit_0
.. Digit_9
116 and then Found
(FL
) in Digit_0
.. Digit_9
117 and then Expect
(EL
) /= Found
(FL
)
124 -- Length is 1 too short. Execute loop to check for single deletion
126 elsif FN
= EN
- 1 then
127 for J
in 1 .. FN
- 1 loop
128 if Found
(FF
+ J
) /= Expect
(EF
+ J
) then
129 return Found
(FF
+ J
.. FL
) = Expect
(EF
+ J
+ 1 .. EL
);
133 -- If we fall through then the last character was missing, which
134 -- we consider to be a match (e.g. found xyz, expected xyza).
138 -- Length is 1 too long. Execute loop to check for single insertion
140 elsif FN
= EN
+ 1 then
141 for J
in 1 .. EN
- 1 loop
142 if Found
(FF
+ J
) /= Expect
(EF
+ J
) then
143 return Found
(FF
+ J
+ 1 .. FL
) = Expect
(EF
+ J
.. EL
);
147 -- If we fall through then the last character was an additional
148 -- character, which is a match (e.g. found xyza, expected xyz).
152 -- Length is completely wrong
157 end Is_Bad_Spelling_Of
;
159 end GNAT
.Spelling_Checker_Generic
;