Convert diagnostics to use quoting flag q 4/n
[official-gcc.git] / libgfortran / runtime / select.c
blob5ee873aefcb738f403c236422083a7b9f1f49f8a
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)
9 any later version.
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"
23 typedef struct
25 char *low;
26 int low_len;
27 char *high;
28 int high_len;
29 void *address;
31 select_struct;
34 #define select_string prefix(select_string)
37 /* select_string()-- Given a selector string and a table of
38 * select_struct structures, return the address to jump to. */
40 void *select_string (select_struct *table, int table_len, void *default_jump,
41 const char *selector, int selector_len)
43 select_struct *t;
44 int i, low, high, mid;
46 if (table_len == 0)
47 return default_jump;
49 /* Record the default address if present */
51 if (table->low == NULL && table->high == NULL)
53 default_jump = table->address;
55 table++;
56 table_len--;
57 if (table_len == 0)
58 return default_jump;
61 /* Try the high and low bounds if present. */
63 if (table->low == NULL)
65 if (compare_string (table->high_len, table->high,
66 selector_len, selector) >= 0)
67 return table->address;
69 table++;
70 table_len--;
71 if (table_len == 0)
72 return default_jump;
75 t = table + table_len - 1;
77 if (t->high == NULL)
79 if (compare_string (t->low_len, t->low,
80 selector_len, selector) <= 0)
81 return t->address;
83 table_len--;
84 if (table_len == 0)
85 return default_jump;
88 /* At this point, the only table entries are bounded entries. Find
89 the right entry with a binary chop. */
91 low = -1;
92 high = table_len;
94 while (low + 1 < high)
96 mid = (low + high) / 2;
98 t = table + mid;
99 i = compare_string (t->low_len, t->low, selector_len, selector);
101 if (i == 0)
102 return t->address;
104 if (i < 0)
105 low = mid;
106 else
107 high = mid;
110 /* The string now lies between the low indeces of the now-adjacent
111 high and low entries. Because it is less than the low entry of
112 'high', it can't be that one. If low is still -1, then no
113 entries match. Otherwise, we have to check the high entry of
114 'low'. */
116 if (low == -1)
117 return default_jump;
119 t = table + low;
120 if (compare_string (selector_len, selector,
121 t->high_len, t->high) <= 0)
122 return t->address;
124 return default_jump;