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 (libgfor).
6 Libgfor 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 Libgfor is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with libgfor; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include "libgfortran.h"
33 extern void * select_string (select_struct
*table
, int table_len
,
34 void *default_jump
, const char *selector
,
36 export_proto(select_string
);
39 /* select_string()-- Given a selector string and a table of
40 * select_struct structures, return the address to jump to. */
43 select_string (select_struct
*table
, int table_len
, void *default_jump
,
44 const char *selector
, int selector_len
)
47 int i
, low
, high
, mid
;
52 /* Record the default address if present */
54 if (table
->low
== NULL
&& table
->high
== NULL
)
56 default_jump
= table
->address
;
64 /* Try the high and low bounds if present. */
66 if (table
->low
== NULL
)
68 if (compare_string (table
->high_len
, table
->high
,
69 selector_len
, selector
) >= 0)
70 return table
->address
;
78 t
= table
+ table_len
- 1;
82 if (compare_string (t
->low_len
, t
->low
,
83 selector_len
, selector
) <= 0)
91 /* At this point, the only table entries are bounded entries. Find
92 the right entry with a binary chop. */
97 while (low
+ 1 < high
)
99 mid
= (low
+ high
) / 2;
102 i
= compare_string (t
->low_len
, t
->low
, selector_len
, selector
);
113 /* The string now lies between the low indeces of the now-adjacent
114 high and low entries. Because it is less than the low entry of
115 'high', it can't be that one. If low is still -1, then no
116 entries match. Otherwise, we have to check the high entry of
123 if (compare_string (selector_len
, selector
,
124 t
->high_len
, t
->high
) <= 0)