select_inc.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* Implement the SELECT statement for character variables.
  2. Copyright (C) 2008-2015 Free Software Foundation, Inc.
  3. This file is part of the GNU Fortran runtime library (libgfortran).
  4. Libgfortran is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. Libgfortran is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. #define select_string SUFFIX(select_string)
  20. #define select_struct SUFFIX(select_struct)
  21. #define compare_string SUFFIX(compare_string)
  22. typedef struct
  23. {
  24. CHARTYPE *low;
  25. gfc_charlen_type low_len;
  26. CHARTYPE *high;
  27. gfc_charlen_type high_len;
  28. int address;
  29. }
  30. select_struct;
  31. extern int select_string (select_struct *table, int table_len,
  32. const CHARTYPE *selector,
  33. gfc_charlen_type selector_len);
  34. export_proto(select_string);
  35. /* select_string()-- Given a selector string and a table of
  36. * select_struct structures, return the address to jump to. */
  37. int
  38. select_string (select_struct *table, int table_len, const CHARTYPE *selector,
  39. gfc_charlen_type selector_len)
  40. {
  41. select_struct *t;
  42. int i, low, high, mid;
  43. int default_jump = -1;
  44. if (table_len == 0)
  45. return -1;
  46. /* Record the default address if present */
  47. if (table->low == NULL && table->high == NULL)
  48. {
  49. default_jump = table->address;
  50. table++;
  51. table_len--;
  52. if (table_len == 0)
  53. return default_jump;
  54. }
  55. /* Try the high and low bounds if present. */
  56. if (table->low == NULL)
  57. {
  58. if (compare_string (table->high_len, table->high,
  59. selector_len, selector) >= 0)
  60. return table->address;
  61. table++;
  62. table_len--;
  63. if (table_len == 0)
  64. return default_jump;
  65. }
  66. t = table + table_len - 1;
  67. if (t->high == NULL)
  68. {
  69. if (compare_string (t->low_len, t->low, selector_len, selector) <= 0)
  70. return t->address;
  71. table_len--;
  72. if (table_len == 0)
  73. return default_jump;
  74. }
  75. /* At this point, the only table entries are bounded entries. Find
  76. the right entry with a binary chop. */
  77. low = -1;
  78. high = table_len;
  79. while (low + 1 < high)
  80. {
  81. mid = (low + high) / 2;
  82. t = table + mid;
  83. i = compare_string (t->low_len, t->low, selector_len, selector);
  84. if (i == 0)
  85. return t->address;
  86. if (i < 0)
  87. low = mid;
  88. else
  89. high = mid;
  90. }
  91. /* The string now lies between the low indeces of the now-adjacent
  92. high and low entries. Because it is less than the low entry of
  93. 'high', it can't be that one. If low is still -1, then no
  94. entries match. Otherwise, we have to check the high entry of
  95. 'low'. */
  96. if (low == -1)
  97. return default_jump;
  98. t = table + low;
  99. if (compare_string (selector_len, selector, t->high_len, t->high) <= 0)
  100. return t->address;
  101. return default_jump;
  102. }