1 /* Implement the SELECT statement for character variables.
2 Contributed by Andy Vaught
4 This file is part of the GNU Fortran 95 runtime library (libgfortran).
6 Libgfortran is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 In addition to the permissions in the GNU General Public License, the
12 Free Software Foundation gives you unlimited permission to link the
13 compiled version of this file into combinations with other programs,
14 and to distribute those combinations without any restriction coming
15 from the use of this file. (The General Public License restrictions
16 do apply in other respects; for example, they cover modification of
17 the file, and distribution when not linked into a combine
20 Libgfortran is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with libgfortran; see the file COPYING. If not, write to
27 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
28 Boston, MA 02110-1301, USA. */
30 #include "libgfortran.h"
42 extern int select_string (select_struct
*table
, int table_len
,
43 const char *selector
, int selector_len
);
44 export_proto(select_string
);
47 /* select_string()-- Given a selector string and a table of
48 * select_struct structures, return the address to jump to. */
51 select_string (select_struct
*table
, int table_len
, const char *selector
,
55 int i
, low
, high
, mid
;
56 int default_jump
= -1;
61 /* Record the default address if present */
63 if (table
->low
== NULL
&& table
->high
== NULL
)
65 default_jump
= table
->address
;
73 /* Try the high and low bounds if present. */
75 if (table
->low
== NULL
)
77 if (compare_string (table
->high_len
, table
->high
,
78 selector_len
, selector
) >= 0)
79 return table
->address
;
87 t
= table
+ table_len
- 1;
91 if (compare_string (t
->low_len
, t
->low
,
92 selector_len
, selector
) <= 0)
100 /* At this point, the only table entries are bounded entries. Find
101 the right entry with a binary chop. */
106 while (low
+ 1 < high
)
108 mid
= (low
+ high
) / 2;
111 i
= compare_string (t
->low_len
, t
->low
, selector_len
, selector
);
122 /* The string now lies between the low indeces of the now-adjacent
123 high and low entries. Because it is less than the low entry of
124 'high', it can't be that one. If low is still -1, then no
125 entries match. Otherwise, we have to check the high entry of
132 if (compare_string (selector_len
, selector
,
133 t
->high_len
, t
->high
) <= 0)