2008-05-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / libgfortran / runtime / select_inc.c
blob81a8dabf7399281cbe0c0cdc428a0ae20b40ada2
1 /* Implement the SELECT statement for character variables.
2 Copyright 2008 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 2, or (at your option)
9 any later version.
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
18 executable.)
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. */
31 #define select_string SUFFIX(select_string)
32 #define select_struct SUFFIX(select_struct)
33 #define compare_string SUFFIX(compare_string)
35 typedef struct
37 CHARTYPE *low;
38 gfc_charlen_type low_len;
39 CHARTYPE *high;
40 gfc_charlen_type high_len;
41 int address;
43 select_struct;
45 extern int select_string (select_struct *table, int table_len,
46 const CHARTYPE *selector,
47 gfc_charlen_type selector_len);
48 export_proto(select_string);
51 /* select_string()-- Given a selector string and a table of
52 * select_struct structures, return the address to jump to. */
54 int
55 select_string (select_struct *table, int table_len, const CHARTYPE *selector,
56 gfc_charlen_type selector_len)
58 select_struct *t;
59 int i, low, high, mid;
60 int default_jump = -1;
62 if (table_len == 0)
63 return -1;
65 /* Record the default address if present */
67 if (table->low == NULL && table->high == NULL)
69 default_jump = table->address;
71 table++;
72 table_len--;
73 if (table_len == 0)
74 return default_jump;
77 /* Try the high and low bounds if present. */
79 if (table->low == NULL)
81 if (compare_string (table->high_len, table->high,
82 selector_len, selector) >= 0)
83 return table->address;
85 table++;
86 table_len--;
87 if (table_len == 0)
88 return default_jump;
91 t = table + table_len - 1;
93 if (t->high == NULL)
95 if (compare_string (t->low_len, t->low, selector_len, selector) <= 0)
96 return t->address;
98 table_len--;
99 if (table_len == 0)
100 return default_jump;
103 /* At this point, the only table entries are bounded entries. Find
104 the right entry with a binary chop. */
106 low = -1;
107 high = table_len;
109 while (low + 1 < high)
111 mid = (low + high) / 2;
113 t = table + mid;
114 i = compare_string (t->low_len, t->low, selector_len, selector);
116 if (i == 0)
117 return t->address;
119 if (i < 0)
120 low = mid;
121 else
122 high = mid;
125 /* The string now lies between the low indeces of the now-adjacent
126 high and low entries. Because it is less than the low entry of
127 'high', it can't be that one. If low is still -1, then no
128 entries match. Otherwise, we have to check the high entry of
129 'low'. */
131 if (low == -1)
132 return default_jump;
134 t = table + low;
135 if (compare_string (selector_len, selector, t->high_len, t->high) <= 0)
136 return t->address;
138 return default_jump;