• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SRI-CSL / yices2 / 15463254785

05 Jun 2025 09:15AM UTC coverage: 65.37% (+0.09%) from 65.284%
15463254785

Pull #550

github

web-flow
Merge ef74ca5ee into 03e4f0ddd
Pull Request #550: Fix binary_search_string and assert in test_api8

4 of 5 new or added lines in 1 file covered. (80.0%)

714 existing lines in 8 files now uncovered.

81199 of 124214 relevant lines covered (65.37%)

1462115.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

31.82
/src/utils/string_utils.c
1
/*
2
 * This file is part of the Yices SMT Solver.
3
 * Copyright (C) 2017 SRI International.
4
 *
5
 * Yices is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * Yices is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with Yices.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18

19
/*
20
 * UTILITIES TO PARSE STRINGS
21
 */
22

23
#include <stdlib.h>
24
#include <string.h>
25
#include <ctype.h>
26
#include <errno.h>
27
#include <assert.h>
28

29
#include "utils/string_utils.h"
30

31

32
/*
33
 * Binary search in a sorted array of strings.
34
 * - a must be sorted in lexicographic order (i.e.,
35
 *   strcmp(a[i], a[i+1]) must be non-negative).
36
 * - n = size of the array. n must be > 0
37
 * - s = string to search for
38
 *
39
 * Return the index i such that a[i] = s if s is in the array.
40
 * Return -1 otherwise.
41
 */
42
int32_t binary_search_string(const char *s, const char * const *a, int32_t n) {
37✔
43
  int32_t l, h, k;
44
  int cmp;
45
  assert(n > 0);
46
  
47
  l = 0;
37✔
48
  h = n - 1;
37✔
49
  while (l <= h) {
176✔
50
    k = (l + h)/2;
176✔
51
    assert(l <= k && k <= h);
52
    cmp = strcmp(s, a[k]);
176✔
53
    if (cmp == 0) return k;
176✔
54
    if (cmp < 0) {
139✔
55
      h = k - 1;
71✔
56
    } else {
57
      assert(cmp > 0);
58
      l = k + 1;
68✔
59
    }
60
  }
NEW
61
  return -1;
×
62
}
63

64

65
/*
66
 * Parse s as a keyword:
67
 * - a must be an array of strings sorted in lexicographic order
68
 * - b must be an array of integer codes of same size as a
69
 * - n = size of a and b (n must be positive)
70
 * - s = string to search for
71
 * If s is equal to a[i] for some i in 0 .. n-1, then
72
 * the function returns b[i].
73
 *
74
 * Otherwise, return -1.
75
 */
76
int32_t parse_as_keyword(const char *s, const char * const *a, const int32_t *b, int32_t n) {
×
77
  int32_t i;
78

79
  i = binary_search_string(s, a, n);
×
80
  if (i >= 0) {
×
81
    i = b[i];
×
82
  }
83

84
  return i;
×
85
}
86

87

88
/*
89
 * Parse s as a boolean: "true" or "TRUE" or "false" or "FALSE"
90
 * - store the result in *val
91
 * Return code:
92
 * - valid_boolean means correct
93
 * - invalid_boolean means wrong format
94
 */
95
boolean_parse_code_t parse_as_boolean(const char *s, bool *val) {
×
96
  boolean_parse_code_t r;
97

98
  r = invalid_boolean;
×
99
  if ((strcmp(s, "true") == 0) || (strcmp(s, "TRUE") == 0)) {
×
100
    *val = true;
×
101
    r = valid_boolean;
×
102
  } else if ((strcmp(s, "false") == 0) || (strcmp(s, "FALSE") == 0)) {
×
103
    *val = false;
×
104
    r = valid_boolean;
×
105
  }
106

107
  return r;
×
108
}
109

110

111

112
/*
113
 * Parse s as a decimal number in the format
114
 *  <optional_signs><digits>
115
 * and store the corresponding number into val
116
 *
117
 * Return code:
118
 * - valid_integer means correct format, no overflow
119
 * - integer_overflow means correct format but overflow
120
 * - invalid_integer means wrong format
121
 */
122
integer_parse_code_t parse_as_integer(const char *s, int32_t *val) {
3✔
123
  long aux;
124
  char *b;
125

126
  while (isspace((int) *s)) s ++;
3✔
127
  errno = 0;
3✔
128
  aux = strtol(s, &b, 10);
3✔
129
  if (errno == ERANGE) {
3✔
130
    return integer_overflow;  //overflow or underflow
×
131
  }
132
  if (errno == EINVAL) {
3✔
133
    return invalid_integer;
×
134
  }
135

136
  if (aux > (long) INT32_MAX || aux < (long) INT32_MIN) {
3✔
137
    return integer_overflow;
×
138
  }
139

140
  while (isspace((int) *b)) b++;
3✔
141

142
  if (*b == '\0' && b != s) {
3✔
143
    *val = (int32_t) aux;
3✔
144
    return valid_integer;
3✔
145
  }
146

147
  return invalid_integer;
×
148
}
149

150

151
/*
152
 * Variant: parse s as an unsigned integer
153
 */
154
integer_parse_code_t parse_as_uint(const char *s, uint32_t *val) {
×
155
  unsigned long aux;
156
  char *b;
157

158
  while (isspace((int) *s)) s ++;
×
159
  errno = 0;
×
160
  aux = strtoul(s, &b, 0);
×
161
  if (errno == ERANGE) {
×
162
    return integer_overflow;
×
163
  }
164
  if (errno == EINVAL) {
×
165
    return invalid_integer;
×
166
  }
167

168
  if (aux > (unsigned long) UINT32_MAX) {
×
169
    return integer_overflow;
×
170
  }
171

172
  while (isspace((int) *b)) b++;
×
173

174
  if (*b == '\0' && b != s) {
×
175
    *val = (uint32_t) aux;
×
176
    return valid_integer;
×
177
  }
178

179
  return invalid_integer;
×
180
}
181

182

183
/*
184
 * Parse s as a floating point number in the format recognized by
185
 * strtod, and store the corresponding number into val
186
 *
187
 * Return code:
188
 * - valid_double means correct format, no overflow
189
 * - double_overflow means correct format but overflow
190
 * - invalid_double means wrong format
191
 */
192
double_parse_code_t parse_as_double(const char *s, double *val) {
×
193
  double aux;
194
  char *b;
195

196
  while (isspace((int) *s)) s ++;
×
197
  errno = 0;
×
198
  aux = strtod(s, &b);
×
199
  if (errno == ERANGE) {
×
200
    return double_overflow;  //overflow or underflow
×
201
  }
202

203
  while (isspace((int) *b)) b++;
×
204

205
  if (*b == '\0' && b != s) {
×
206
    *val = aux;
×
207
    return valid_double;
×
208
  }
209

210
  return invalid_double;
×
211
}
212

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc