aboutsummaryrefslogtreecommitdiff
path: root/test/symbolic/klee/nvim/charset.c
blob: 59fe6a143084d17052c6a02cd79ff5a0be4cade1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <stdbool.h>

#include "nvim/ascii.h"
#include "nvim/macros.h"
#include "nvim/charset.h"
#include "nvim/eval/typval.h"
#include "nvim/vim.h"

bool vim_isIDc(int c)
{
  return ASCII_ISALNUM(c);
}

int hex2nr(int c)
{
  if ((c >= 'a') && (c <= 'f')) {
    return c - 'a' + 10;
  }

  if ((c >= 'A') && (c <= 'F')) {
    return c - 'A' + 10;
  }
  return c - '0';
}

void vim_str2nr(const char_u *const start, int *const prep, int *const len,
                const int what, varnumber_T *const nptr,
                uvarnumber_T *const unptr, const int maxlen)
{
  const char_u *ptr = start;
  int pre = 0;  // default is decimal
  bool negative = false;
  uvarnumber_T un = 0;

  if (ptr[0] == '-') {
    negative = true;
    ptr++;
  }

  // Recognize hex, octal and bin.
  if ((what & (STR2NR_HEX|STR2NR_OCT|STR2NR_BIN))
      && (maxlen == 0 || maxlen > 1)
      && (ptr[0] == '0') && (ptr[1] != '8') && (ptr[1] != '9')) {
    pre = ptr[1];

    if ((what & STR2NR_HEX)
        && (maxlen == 0 || maxlen > 2)
        && ((pre == 'X') || (pre == 'x'))
        && ascii_isxdigit(ptr[2])) {
      // hexadecimal
      ptr += 2;
    } else if ((what & STR2NR_BIN)
               && (maxlen == 0 || maxlen > 2)
               && ((pre == 'B') || (pre == 'b'))
               && ascii_isbdigit(ptr[2])) {
      // binary
      ptr += 2;
    } else {
      // decimal or octal, default is decimal
      pre = 0;

      if (what & STR2NR_OCT) {
        // Don't interpret "0", "08" or "0129" as octal.
        for (int n = 1; ascii_isdigit(ptr[n]); ++n) {
          if (ptr[n] > '7') {
            // can't be octal
            pre = 0;
            break;
          }
          if (ptr[n] >= '0') {
            // assume octal
            pre = '0';
          }
          if (n == maxlen) {
            break;
          }
        }
      }
    }
  }

  // Do the string-to-numeric conversion "manually" to avoid sscanf quirks.
  int n = 1;
  if (pre == 'B' || pre == 'b' || what == (STR2NR_BIN|STR2NR_FORCE)) {
    // bin
    if (pre != 0) {
      n += 2;  // skip over "0b"
    }
    while ('0' <= *ptr && *ptr <= '1') {
      // avoid ubsan error for overflow
      if (un < UVARNUMBER_MAX / 2) {
        un = 2 * un + (uvarnumber_T)(*ptr - '0');
      } else {
        un = UVARNUMBER_MAX;
      }
      ptr++;
      if (n++ == maxlen) {
        break;
      }
    }
  } else if (pre == '0' || what == (STR2NR_OCT|STR2NR_FORCE)) {
    // octal
    while ('0' <= *ptr && *ptr <= '7') {
      // avoid ubsan error for overflow
      if (un < UVARNUMBER_MAX / 8) {
        un = 8 * un + (uvarnumber_T)(*ptr - '0');
      } else {
        un = UVARNUMBER_MAX;
      }
      ptr++;
      if (n++ == maxlen) {
        break;
      }
    }
  } else if (pre == 'X' || pre == 'x' || what == (STR2NR_HEX|STR2NR_FORCE)) {
    // hex
    if (pre != 0) {
      n += 2;  // skip over "0x"
    }
    while (ascii_isxdigit(*ptr)) {
      // avoid ubsan error for overflow
      if (un < UVARNUMBER_MAX / 16) {
        un = 16 * un + (uvarnumber_T)hex2nr(*ptr);
      } else {
        un = UVARNUMBER_MAX;
      }
      ptr++;
      if (n++ == maxlen) {
        break;
      }
    }
  } else {
    // decimal
    while (ascii_isdigit(*ptr)) {
      // avoid ubsan error for overflow
      if (un < UVARNUMBER_MAX / 10) {
        un = 10 * un + (uvarnumber_T)(*ptr - '0');
      } else {
        un = UVARNUMBER_MAX;
      }
      ptr++;
      if (n++ == maxlen) {
        break;
      }
    }
  }

  if (prep != NULL) {
    *prep = pre;
  }

  if (len != NULL) {
    *len = (int)(ptr - start);
  }

  if (nptr != NULL) {
    if (negative) {  // account for leading '-' for decimal numbers
      // avoid ubsan error for overflow
      if (un > VARNUMBER_MAX) {
        *nptr = VARNUMBER_MIN;
      } else {
        *nptr = -(varnumber_T)un;
      }
    } else {
      if (un > VARNUMBER_MAX) {
        un = VARNUMBER_MAX;
      }
      *nptr = (varnumber_T)un;
    }
  }

  if (unptr != NULL) {
    *unptr = un;
  }
}