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

SRI-CSL / yices2 / 44167754935

17 Nov 2026 08:17PM UTC coverage: 71.322% (+0.003%) from 71.319%
44167754935

Pull #697

github

web-flow
Merge 10b1dd050 into ccdf89988
Pull Request #697: Integrate Yices 2.8.0 licensing and release fixes into master

96243 of 134941 relevant lines covered (71.32%)

1720253.78 hits per line

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

68.18
/src/utils/string_utils.c
1
/*
2
 * Copyright (c) 2026, SRI International
3
 * SPDX-License-Identifier: BSD-3-Clause
4
 */
5

6
/*
7
 * UTILITIES TO PARSE STRINGS
8
 */
9

10
#include <stdlib.h>
11
#include <string.h>
12
#include <ctype.h>
13
#include <errno.h>
14
#include <assert.h>
15

16
#include "utils/string_utils.h"
17

18

19
/*
20
 * Binary search in a sorted array of strings.
21
 * - a must be sorted in lexicographic order (i.e.,
22
 *   strcmp(a[i], a[i+1]) must be non-negative).
23
 * - n = size of the array. n must be > 0
24
 * - s = string to search for
25
 *
26
 * Return the index i such that a[i] = s if s is in the array.
27
 * Return -1 otherwise.
28
 */
29
int32_t binary_search_string(const char *s, const char * const *a, int32_t n) {
806 ✔
30
  int32_t l, h, k;
31
  int cmp;
32
  assert(n > 0);
33
  
34
  l = 0;
806 ✔
35
  h = n - 1;
806 ✔
36
  while (l <= h) {
2,203 ✔
37
    k = (l + h)/2;
2,201 ✔
38
    assert(l <= k && k <= h);
39
    cmp = strcmp(s, a[k]);
2,201 ✔
40
    if (cmp == 0) return k;
2,201 ✔
41
    if (cmp < 0) {
1,397 ✔
42
      h = k - 1;
495 ✔
43
    } else {
44
      assert(cmp > 0);
45
      l = k + 1;
902 ✔
46
    }
47
  }
48
  return -1;
2 ✔
49
}
50

51

52
/*
53
 * Parse s as a keyword:
54
 * - a must be an array of strings sorted in lexicographic order
55
 * - b must be an array of integer codes of same size as a
56
 * - n = size of a and b (n must be positive)
57
 * - s = string to search for
58
 * If s is equal to a[i] for some i in 0 .. n-1, then
59
 * the function returns b[i].
60
 *
61
 * Otherwise, return -1.
62
 */
63
int32_t parse_as_keyword(const char *s, const char * const *a, const int32_t *b, int32_t n) {
750 ✔
64
  int32_t i;
65

66
  i = binary_search_string(s, a, n);
750 ✔
67
  if (i >= 0) {
750 ✔
68
    i = b[i];
748 ✔
69
  }
70

71
  return i;
750 ✔
72
}
73

74

75
/*
76
 * Parse s as a boolean: "true" or "TRUE" or "false" or "FALSE"
77
 * - store the result in *val
78
 * Return code:
79
 * - valid_boolean means correct
80
 * - invalid_boolean means wrong format
81
 */
82
boolean_parse_code_t parse_as_boolean(const char *s, bool *val) {
37 ✔
83
  boolean_parse_code_t r;
84

85
  r = invalid_boolean;
37 ✔
86
  if ((strcmp(s, "true") == 0) || (strcmp(s, "TRUE") == 0)) {
37 ✔
87
    *val = true;
36 ✔
88
    r = valid_boolean;
36 ✔
89
  } else if ((strcmp(s, "false") == 0) || (strcmp(s, "FALSE") == 0)) {
1 ✔
90
    *val = false;
1 ✔
91
    r = valid_boolean;
1 ✔
92
  }
93

94
  return r;
37 ✔
95
}
96

97

98

99
/*
100
 * Parse s as a decimal number in the format
101
 *  <optional_signs><digits>
102
 * and store the corresponding number into val
103
 *
104
 * Return code:
105
 * - valid_integer means correct format, no overflow
106
 * - integer_overflow means correct format but overflow
107
 * - invalid_integer means wrong format
108
 */
109
integer_parse_code_t parse_as_integer(const char *s, int32_t *val) {
4 ✔
110
  long aux;
111
  char *b;
112

113
  while (isspace((int) *s)) s ++;
4 ✔
114
  errno = 0;
4 ✔
115
  aux = strtol(s, &b, 10);
4 ✔
116
  if (errno == ERANGE) {
4 ✔
117
    return integer_overflow;  //overflow or underflow
×
118
  }
119
  if (errno == EINVAL) {
4 ✔
120
    return invalid_integer;
×
121
  }
122

123
  if (aux > (long) INT32_MAX || aux < (long) INT32_MIN) {
4 ✔
124
    return integer_overflow;
×
125
  }
126

127
  while (isspace((int) *b)) b++;
4 ✔
128

129
  if (*b == '\0' && b != s) {
4 ✔
130
    *val = (int32_t) aux;
4 ✔
131
    return valid_integer;
4 ✔
132
  }
133

134
  return invalid_integer;
×
135
}
136

137

138
/*
139
 * Variant: parse s as an unsigned integer
140
 */
141
integer_parse_code_t parse_as_uint(const char *s, uint32_t *val) {
×
142
  unsigned long aux;
143
  char *b;
144

145
  while (isspace((int) *s)) s ++;
×
146
  errno = 0;
×
147
  aux = strtoul(s, &b, 0);
×
148
  if (errno == ERANGE) {
×
149
    return integer_overflow;
×
150
  }
151
  if (errno == EINVAL) {
×
152
    return invalid_integer;
×
153
  }
154

155
  if (aux > (unsigned long) UINT32_MAX) {
×
156
    return integer_overflow;
×
157
  }
158

159
  while (isspace((int) *b)) b++;
×
160

161
  if (*b == '\0' && b != s) {
×
162
    *val = (uint32_t) aux;
×
163
    return valid_integer;
×
164
  }
165

166
  return invalid_integer;
×
167
}
168

169

170
/*
171
 * Parse s as a floating point number in the format recognized by
172
 * strtod, and store the corresponding number into val
173
 *
174
 * Return code:
175
 * - valid_double means correct format, no overflow
176
 * - double_overflow means correct format but overflow
177
 * - invalid_double means wrong format
178
 */
179
double_parse_code_t parse_as_double(const char *s, double *val) {
1 ✔
180
  double aux;
181
  char *b;
182

183
  while (isspace((int) *s)) s ++;
1 ✔
184
  errno = 0;
1 ✔
185
  aux = strtod(s, &b);
1 ✔
186
  if (errno == ERANGE) {
1 ✔
187
    return double_overflow;  //overflow or underflow
×
188
  }
189

190
  while (isspace((int) *b)) b++;
1 ✔
191

192
  if (*b == '\0' && b != s) {
1 ✔
193
    *val = aux;
1 ✔
194
    return valid_double;
1 ✔
195
  }
196

197
  return invalid_double;
×
198
}
199

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

© 2026 Coveralls, Inc