1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- S Y S T E M . C A S E _ U T I L --
9 -- Copyright (C) 1995-2023, Free Software Foundation, Inc. --
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 -- Simple casing functions
34 -- This package provides simple casing functions that do not require the
35 -- overhead of the full casing tables found in Ada.Characters.Handling.
37 -- Preconditions in this unit are meant for analysis only, not for run-time
38 -- checking, so that the expected exceptions are raised. This is enforced by
39 -- setting the corresponding assertion policy to Ignore. Postconditions and
40 -- contract cases should not be executed at runtime as well, in order not to
41 -- slow down the execution of these functions.
43 pragma Assertion_Policy
(Pre
=> Ignore
,
45 Contract_Cases
=> Ignore
,
48 package System
.Case_Util
51 -- Note: all the following functions handle the full Latin-1 set
53 function To_Upper
(A
: Character) return Character
56 A_Val
: constant Natural := Character'Pos (A
);
59 or else A_Val
in 16#E0#
.. 16#F6#
60 or else A_Val
in 16#F8#
.. 16#FE#
62 To_Upper
'Result = Character'Val (A_Val
- 16#
20#
)
64 To_Upper
'Result = A
));
65 -- Converts A to upper case if it is a lower case letter, otherwise
66 -- returns the input argument unchanged.
68 procedure To_Upper
(A
: in out String)
70 Post
=> (for all J
in A
'Range => A
(J
) = To_Upper
(A
'Old (J
)));
72 function To_Upper
(A
: String) return String
74 Post
=> To_Upper
'Result'First = A'First
75 and then To_Upper'Result'Last
= A
'Last
76 and then (for all J
in A
'Range =>
77 To_Upper
'Result (J
) = To_Upper
(A
(J
)));
78 -- Folds all characters of string A to upper case
80 function To_Lower
(A
: Character) return Character
83 A_Val
: constant Natural := Character'Pos (A
);
86 or else A_Val
in 16#C0#
.. 16#D6#
87 or else A_Val
in 16#D8#
.. 16#DE#
89 To_Lower
'Result = Character'Val (A_Val
+ 16#
20#
)
91 To_Lower
'Result = A
));
92 -- Converts A to lower case if it is an upper case letter, otherwise
93 -- returns the input argument unchanged.
95 procedure To_Lower
(A
: in out String)
97 Post
=> (for all J
in A
'Range => A
(J
) = To_Lower
(A
'Old (J
)));
99 function To_Lower
(A
: String) return String
101 Post
=> To_Lower
'Result'First = A'First
102 and then To_Lower'Result'Last
= A
'Last
103 and then (for all J
in A
'Range =>
104 To_Lower
'Result (J
) = To_Lower
(A
(J
)));
105 -- Folds all characters of string A to lower case
107 procedure To_Mixed
(A
: in out String)
110 (for all J
in A
'Range =>
112 or else A
'Old (J
- 1) = '_'
114 A
(J
) = To_Upper
(A
'Old (J
))
116 A
(J
) = To_Lower
(A
'Old (J
))));
118 function To_Mixed
(A
: String) return String
120 Post
=> To_Mixed
'Result'First = A'First
121 and then To_Mixed'Result'Last
= A
'Last
122 and then (for all J
in A
'Range =>
124 or else A
(J
- 1) = '_'
126 To_Mixed
'Result (J
) = To_Upper
(A
(J
))
128 To_Mixed
'Result (J
) = To_Lower
(A
(J
))));
129 -- Converts A to mixed case (i.e. lower case, except for initial
130 -- character and any character after an underscore, which are
131 -- converted to upper case.
133 end System
.Case_Util
;