1 /* Implement the SELECT statement for character variables.
2 Copyright (C) 2008-2013 Free Software Foundation, Inc.
4 This file is part of the GNU Fortran 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 3, or (at your option)
11 Libgfortran 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 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
25 #define select_string SUFFIX(select_string)
26 #define select_struct SUFFIX(select_struct)
27 #define compare_string SUFFIX(compare_string)
32 gfc_charlen_type low_len;
34 gfc_charlen_type high_len;
39 extern int select_string (select_struct *table, int table_len,
40 const CHARTYPE *selector,
41 gfc_charlen_type selector_len);
42 export_proto(select_string);
45 /* select_string()-- Given a selector string and a table of
46 * select_struct structures, return the address to jump to. */
49 select_string (select_struct *table, int table_len, const CHARTYPE *selector,
50 gfc_charlen_type selector_len)
53 int i, low, high, mid;
54 int default_jump = -1;
59 /* Record the default address if present */
61 if (table->low == NULL && table->high == NULL)
63 default_jump = table->address;
71 /* Try the high and low bounds if present. */
73 if (table->low == NULL)
75 if (compare_string (table->high_len, table->high,
76 selector_len, selector) >= 0)
77 return table->address;
85 t = table + table_len - 1;
89 if (compare_string (t->low_len, t->low, selector_len, selector) <= 0)
97 /* At this point, the only table entries are bounded entries. Find
98 the right entry with a binary chop. */
103 while (low + 1 < high)
105 mid = (low + high) / 2;
108 i = compare_string (t->low_len, t->low, selector_len, selector);
119 /* The string now lies between the low indeces of the now-adjacent
120 high and low entries. Because it is less than the low entry of
121 'high', it can't be that one. If low is still -1, then no
122 entries match. Otherwise, we have to check the high entry of
129 if (compare_string (selector_len, selector, t->high_len, t->high) <= 0)