Debian Codegraph @

gcc/ada/libgnat/s-valuen.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S Y S T E M . V A L U E _ N --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2021-2025, Free Software Foundation, Inc. --
10 -- --
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. --
17 -- --
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. --
21 -- --
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/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
31
32 with Ada.Unchecked_Conversion;
33
34 with System.Val_Util; use System.Val_Util;
35
36 package body System.Value_N is
37
38 function Value_Enumeration_Pos
39 (Names : String;
40 Indexes : System.Address;
41 Hash : Hash_Function_Ptr;
42 Num : Natural;
43 Is_Wide : Boolean;
44 Str : String)
45 return Integer with Pure_Function;
46 -- Same as Value_Enumeration, except returns negative if Value_Enumeration
47 -- would raise Constraint_Error.
48
49 ---------------------------
50 -- Value_Enumeration_Pos --
51 ---------------------------
52
53 function Value_Enumeration_Pos
54 (Names : String;
55 Indexes : System.Address;
56 Hash : Hash_Function_Ptr;
57 Num : Natural;
58 Is_Wide : Boolean;
59 Str : String)
60 return Integer
61 is
62 F, L : Integer;
63 H : Natural;
64 S : String (Str'Range) := Str;
65
66 subtype Names_Index is
67 Index_Type range Index_Type (Names'First)
68 .. Index_Type (Names'Last) + 1;
69 subtype Index is Natural range Natural'First .. Names'Length;
70 type Index_Table is array (Index) of Names_Index;
71 type Index_Table_Ptr is access Index_Table;
72
73 function To_Index_Table_Ptr is
74 new Ada.Unchecked_Conversion (System.Address, Index_Table_Ptr);
75
76 IndexesT : constant Index_Table_Ptr := To_Index_Table_Ptr (Indexes);
77
78 pragma Assert (Num + 1 in IndexesT'Range);
79
80 begin
81 Normalize_String (S, F, L, To_Upper_Case => not Is_Wide);
82
83 declare
84 Normal : String renames S (F .. L);
85
86 begin
87 -- If we have a valid hash value, do a single lookup
88
89 H := (if Hash /= null then Hash.all (Normal) else Natural'Last);
90
91 if H /= Natural'Last then
92 if Names
93 (Natural (IndexesT (H)) ..
94 Natural (IndexesT (H + 1)) - 1) = Normal
95 then
96 return H;
97 end if;
98
99 -- Otherwise do a linear search
100
101 else
102 for J in 0 .. Num loop
103 if Names
104 (Natural (IndexesT (J)) ..
105 Natural (IndexesT (J + 1)) - 1) = Normal
106 then
107 return J;
108 end if;
109 end loop;
110 end if;
111 end;
112
113 return -1;
114 end Value_Enumeration_Pos;
115
116 -----------------------------
117 -- Valid_Value_Enumeration --
118 -----------------------------
119
120 function Valid_Value_Enumeration
121 (Names : String;
122 Indexes : System.Address;
123 Hash : Hash_Function_Ptr;
124 Num : Natural;
125 Is_Wide : Boolean;
126 Str : String)
127 return Boolean
128 is
129 begin
130 return
131 Value_Enumeration_Pos (Names, Indexes, Hash, Num, Is_Wide, Str) >= 0;
132 end Valid_Value_Enumeration;
133
134 -----------------------
135 -- Value_Enumeration --
136 -----------------------
137
138 function Value_Enumeration
139 (Names : String;
140 Indexes : System.Address;
141 Hash : Hash_Function_Ptr;
142 Num : Natural;
143 Is_Wide : Boolean;
144 Str : String)
145 return Natural
146 is
147 Result : constant Integer :=
148 Value_Enumeration_Pos (Names, Indexes, Hash, Num, Is_Wide, Str);
149
150 begin
151 -- The comparison eliminates the need for a range check on return
152
153 if Result < 0 then
154 Bad_Value (Str);
155 else
156 return Result;
157 end if;
158 end Value_Enumeration;
159
160 end System.Value_N;