"vscode:/vscode.git/clone" did not exist on "588e554082249ac49a888ee6889e5d14103556fe"
string.cpp 9.01 KB
Newer Older
1
2
3
4
5
6
7
8
9
// [AsmJit]
// Complete x86/x64 JIT and Remote Assembler for C++.
//
// [License]
// Zlib - See LICENSE.md file in the package.

// [Export]
#define ASMJIT_EXPORTS

10
// [Dependencies]
11
#include "../base/string.h"
12
#include "../base/utils.h"
13
14

// [Api-Begin]
15
#include "../asmjit_apibegin.h"
16
17
18
19
20
21
22

namespace asmjit {

// ============================================================================
// [asmjit::StringBuilder - Construction / Destruction]
// ============================================================================

23
24
25
26
27
28
29
30
// Should be placed in read-only memory.
static const char StringBuilder_empty[4] = { 0 };

StringBuilder::StringBuilder() noexcept
  : _data(const_cast<char*>(StringBuilder_empty)),
    _length(0),
    _capacity(0),
    _canFree(false) {}
31

32
StringBuilder::~StringBuilder() noexcept {
33
  if (_canFree)
34
    Internal::releaseMemory(_data);
35
36
37
38
39
40
}

// ============================================================================
// [asmjit::StringBuilder - Prepare / Reserve]
// ============================================================================

41
ASMJIT_FAVOR_SIZE char* StringBuilder::prepare(uint32_t op, size_t len) noexcept {
42
  if (op == kStringOpSet) {
43
    // We don't care here, but we can't return a null pointer since it indicates
44
45
46
47
48
49
50
51
52
53
    // failure in memory allocation.
    if (len == 0) {
      if (_data != StringBuilder_empty)
        _data[0] = 0;

      _length = 0;
      return _data;
    }

    if (_capacity < len) {
54
55
      if (len >= IntTraits<size_t>::maxValue() - sizeof(intptr_t) * 2)
        return nullptr;
56

57
      size_t to = Utils::alignTo<size_t>(len, sizeof(intptr_t));
58
59
60
      if (to < 256 - sizeof(intptr_t))
        to = 256 - sizeof(intptr_t);

61
62
      char* newData = static_cast<char*>(Internal::allocMemory(to + sizeof(intptr_t)));
      if (!newData) {
63
        clear();
64
        return nullptr;
65
66
67
      }

      if (_canFree)
68
        Internal::releaseMemory(_data);
69
70
71
72
73
74
75
76
77
78
79
80
81

      _data = newData;
      _capacity = to + sizeof(intptr_t) - 1;
      _canFree = true;
    }

    _data[len] = 0;
    _length = len;

    ASMJIT_ASSERT(_length <= _capacity);
    return _data;
  }
  else {
82
83
    // We don't care here, but we can't return a null pointer since it indicates
    // failure of memory allocation.
84
85
86
87
    if (len == 0)
      return _data + _length;

    // Overflow.
88
89
    if (IntTraits<size_t>::maxValue() - sizeof(intptr_t) * 2 - _length < len)
      return nullptr;
90
91
92
93
94
95
96
97
98
99
100
101
102

    size_t after = _length + len;
    if (_capacity < after) {
      size_t to = _capacity;

      if (to < 256)
        to = 256;

      while (to < 1024 * 1024 && to < after)
        to *= 2;

      if (to < after) {
        to = after;
103
104
        if (to < (IntTraits<size_t>::maxValue() - 1024 * 32))
          to = Utils::alignTo<size_t>(to, 1024 * 32);
105
106
      }

107
108
109
      to = Utils::alignTo<size_t>(to, sizeof(intptr_t));
      char* newData = static_cast<char*>(Internal::allocMemory(to + sizeof(intptr_t)));
      if (!newData) return nullptr;
110
111
112

      ::memcpy(newData, _data, _length);
      if (_canFree)
113
        Internal::releaseMemory(_data);
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128

      _data = newData;
      _capacity = to + sizeof(intptr_t) - 1;
      _canFree = true;
    }

    char* ret = _data + _length;
    _data[after] = 0;
    _length = after;

    ASMJIT_ASSERT(_length <= _capacity);
    return ret;
  }
}

129
ASMJIT_FAVOR_SIZE Error StringBuilder::reserve(size_t to) noexcept {
130
  if (_capacity >= to)
131
    return kErrorOk;
132

133
134
  if (to >= IntTraits<size_t>::maxValue() - sizeof(intptr_t) * 2)
    return DebugUtils::errored(kErrorNoHeapMemory);
135

136
137
  to = Utils::alignTo<size_t>(to, sizeof(intptr_t));
  char* newData = static_cast<char*>(Internal::allocMemory(to + sizeof(intptr_t)));
138

139
140
  if (!newData)
    return DebugUtils::errored(kErrorNoHeapMemory);
141
142
143

  ::memcpy(newData, _data, _length + 1);
  if (_canFree)
144
    Internal::releaseMemory(_data);
145
146
147
148

  _data = newData;
  _capacity = to + sizeof(intptr_t) - 1;
  _canFree = true;
149
  return kErrorOk;
150
151
152
153
154
155
}

// ============================================================================
// [asmjit::StringBuilder - Clear]
// ============================================================================

156
void StringBuilder::clear() noexcept {
157
158
159
160
161
162
163
164
165
  if (_data != StringBuilder_empty)
    _data[0] = 0;
  _length = 0;
}

// ============================================================================
// [asmjit::StringBuilder - Methods]
// ============================================================================

166
167
168
Error StringBuilder::_opString(uint32_t op, const char* str, size_t len) noexcept {
  if (len == Globals::kInvalidIndex)
    len = str ? ::strlen(str) : static_cast<size_t>(0);
169
170

  char* p = prepare(op, len);
171
  if (!p) return DebugUtils::errored(kErrorNoHeapMemory);
172
173

  ::memcpy(p, str, len);
174
  return kErrorOk;
175
176
}

177
Error StringBuilder::_opChar(uint32_t op, char c) noexcept {
178
  char* p = prepare(op, 1);
179
  if (!p) return DebugUtils::errored(kErrorNoHeapMemory);
180
181

  *p = c;
182
  return kErrorOk;
183
184
}

185
186
187
Error StringBuilder::_opChars(uint32_t op, char c, size_t n) noexcept {
  char* p = prepare(op, n);
  if (!p) return DebugUtils::errored(kErrorNoHeapMemory);
188

189
190
  ::memset(p, c, n);
  return kErrorOk;
191
192
193
194
}

static const char StringBuilder_numbers[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

195
Error StringBuilder::_opNumber(uint32_t op, uint64_t i, uint32_t base, size_t width, uint32_t flags) noexcept {
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
  if (base < 2 || base > 36)
    base = 10;

  char buf[128];
  char* p = buf + ASMJIT_ARRAY_SIZE(buf);

  uint64_t orig = i;
  char sign = '\0';

  // --------------------------------------------------------------------------
  // [Sign]
  // --------------------------------------------------------------------------

  if ((flags & kStringFormatSigned) != 0 && static_cast<int64_t>(i) < 0) {
    i = static_cast<uint64_t>(-static_cast<int64_t>(i));
    sign = '-';
  }
  else if ((flags & kStringFormatShowSign) != 0) {
    sign = '+';
  }
  else if ((flags & kStringFormatShowSpace) != 0) {
    sign = ' ';
  }

  // --------------------------------------------------------------------------
  // [Number]
  // --------------------------------------------------------------------------

  do {
    uint64_t d = i / base;
    uint64_t r = i % base;

    *--p = StringBuilder_numbers[r];
    i = d;
  } while (i);

  size_t numberLength = (size_t)(buf + ASMJIT_ARRAY_SIZE(buf) - p);

  // --------------------------------------------------------------------------
  // [Alternate Form]
  // --------------------------------------------------------------------------

  if ((flags & kStringFormatAlternate) != 0) {
    if (base == 8) {
      if (orig != 0)
        *--p = '0';
    }
    if (base == 16) {
      *--p = 'x';
      *--p = '0';
    }
  }

  // --------------------------------------------------------------------------
  // [Width]
  // --------------------------------------------------------------------------

  if (sign != 0)
    *--p = sign;

  if (width > 256)
    width = 256;

  if (width <= numberLength)
    width = 0;
  else
    width -= numberLength;

  // --------------------------------------------------------------------------
  // Write]
  // --------------------------------------------------------------------------

  size_t prefixLength = (size_t)(buf + ASMJIT_ARRAY_SIZE(buf) - p) - numberLength;
  char* data = prepare(op, prefixLength + width + numberLength);

271
272
  if (!data)
    return DebugUtils::errored(kErrorNoHeapMemory);
273
274
275
276
277
278
279
280

  ::memcpy(data, p, prefixLength);
  data += prefixLength;

  ::memset(data, '0', width);
  data += width;

  ::memcpy(data, p + prefixLength, numberLength);
281
  return kErrorOk;
282
283
}

284
285
Error StringBuilder::_opHex(uint32_t op, const void* data, size_t len) noexcept {
  char* dst;
286

287
288
  if (len >= IntTraits<size_t>::maxValue() / 2 || !(dst = prepare(op, len * 2)))
    return DebugUtils::errored(kErrorNoHeapMemory);;
289
290

  const char* src = static_cast<const char*>(data);
291
  for (size_t i = 0; i < len; i++, dst += 2, src++) {
292
293
294
    dst[0] = StringBuilder_numbers[(src[0] >> 4) & 0xF];
    dst[1] = StringBuilder_numbers[(src[0]     ) & 0xF];
  }
295
  return kErrorOk;
296
297
}

298
Error StringBuilder::_opVFormat(uint32_t op, const char* fmt, va_list ap) noexcept {
299
300
301
302
303
304
305
306
  char buf[1024];

  vsnprintf(buf, ASMJIT_ARRAY_SIZE(buf), fmt, ap);
  buf[ASMJIT_ARRAY_SIZE(buf) - 1] = '\0';

  return _opString(op, buf);
}

307
Error StringBuilder::setFormat(const char* fmt, ...) noexcept {
308
309
310
311
312
313
314
315
316
317
  bool result;

  va_list ap;
  va_start(ap, fmt);
  result = _opVFormat(kStringOpSet, fmt, ap);
  va_end(ap);

  return result;
}

318
Error StringBuilder::appendFormat(const char* fmt, ...) noexcept {
319
320
321
322
323
324
325
326
327
328
  bool result;

  va_list ap;
  va_start(ap, fmt);
  result = _opVFormat(kStringOpAppend, fmt, ap);
  va_end(ap);

  return result;
}

329
bool StringBuilder::eq(const char* str, size_t len) const noexcept {
330
331
332
333
334
335
  const char* aData = _data;
  const char* bData = str;

  size_t aLength = _length;
  size_t bLength = len;

336
  if (bLength == Globals::kInvalidIndex) {
337
    size_t i;
338
    for (i = 0; i < aLength; i++)
339
340
341
342
343
344
345
346
347
348
349
350
351
352
      if (aData[i] != bData[i] || bData[i] == 0)
        return false;
    return bData[i] == 0;
  }
  else {
    if (aLength != bLength)
      return false;
    return ::memcmp(aData, bData, aLength) == 0;
  }
}

} // asmjit namespace

// [Api-End]
353
#include "../asmjit_apiend.h"