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

SRI-CSL / yices2 / 12367760548

17 Dec 2024 06:41AM UTC coverage: 65.285% (-0.6%) from 65.92%
12367760548

push

github

web-flow
Fixes GitHub coverage issue (#544)

* Update action.yml

* Update action.yml

81020 of 124102 relevant lines covered (65.29%)

1509382.01 hits per line

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

32.31
/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) {
35✔
43
  uint32_t l, h, k;
44
  int cmp;
45

46
  assert(n > 0);
47

48
  l = 0;
35✔
49
  h = (uint32_t) n;
35✔
50
  for (;;) {
51
    k = (l + h)/2;
147✔
52
    assert(l <= k && k < h);
53
    cmp = strcmp(s, a[k]);
147✔
54
    if (cmp == 0) return k;
147✔
55
    if (k == l) return -1;
112✔
56
    if (cmp < 0) {
112✔
57
      h = k;
71✔
58
    } else {
59
      assert(cmp > 0);
60
      l = k+1;
41✔
61
    }
62
  }
63
}
64

65

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

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

85
  return i;
×
86
}
87

88

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

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

108
  return r;
×
109
}
110

111

112

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

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

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

141
  while (isspace((int) *b)) b++;
1✔
142

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

148
  return invalid_integer;
×
149
}
150

151

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

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

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

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

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

180
  return invalid_integer;
×
181
}
182

183

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

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

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

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

211
  return invalid_double;
×
212
}
213

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