2018-03-08 Richard Biener <rguenther@suse.de>
[official-gcc.git] / libgfortran / runtime / select_inc.c
blobc2a3584dd38ad59da668017e145dc28057c3d7d9
1 /* Implement the SELECT statement for character variables.
2 Copyright (C) 2008-2018 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)
9 any later version.
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)
29 typedef struct
31 CHARTYPE *low;
32 gfc_charlen_type low_len;
33 CHARTYPE *high;
34 gfc_charlen_type high_len;
35 int address;
37 select_struct;
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. */
48 int
49 select_string (select_struct *table, int table_len, const CHARTYPE *selector,
50 gfc_charlen_type selector_len)
52 select_struct *t;
53 int i, low, high, mid;
54 int default_jump = -1;
56 if (table_len == 0)
57 return -1;
59 /* Record the default address if present */
61 if (table->low == NULL && table->high == NULL)
63 default_jump = table->address;
65 table++;
66 table_len--;
67 if (table_len == 0)
68 return default_jump;
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;
79 table++;
80 table_len--;
81 if (table_len == 0)
82 return default_jump;
85 t = table + table_len - 1;
87 if (t->high == NULL)
89 if (compare_string (t->low_len, t->low, selector_len, selector) <= 0)
90 return t->address;
92 table_len--;
93 if (table_len == 0)
94 return default_jump;
97 /* At this point, the only table entries are bounded entries. Find
98 the right entry with a binary chop. */
100 low = -1;
101 high = table_len;
103 while (low + 1 < high)
105 mid = (low + high) / 2;
107 t = table + mid;
108 i = compare_string (t->low_len, t->low, selector_len, selector);
110 if (i == 0)
111 return t->address;
113 if (i < 0)
114 low = mid;
115 else
116 high = mid;
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
123 'low'. */
125 if (low == -1)
126 return default_jump;
128 t = table + low;
129 if (compare_string (selector_len, selector, t->high_len, t->high) <= 0)
130 return t->address;
132 return default_jump;