| 1 |
/** |
| 2 |
* An expandable buffer in which you can write text or binary data. |
| 3 |
* |
| 4 |
* Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved |
| 5 |
* Authors: Walter Bright, https://www.digitalmars.com |
| 6 |
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) |
| 7 |
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/common/outbuffer.d, root/_outbuffer.d) |
| 8 |
* Documentation: https://dlang.org/phobos/dmd_root_outbuffer.html |
| 9 |
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/common/outbuffer.d |
| 10 |
*/ |
| 11 |
|
| 12 |
module dmd.common.outbuffer; |
| 13 |
|
| 14 |
import core.stdc.stdarg; |
| 15 |
import core.stdc.stdio; |
| 16 |
import core.stdc.string; |
| 17 |
import core.stdc.stdlib; |
| 18 |
|
| 19 |
nothrow: |
| 20 |
|
| 21 |
// In theory these functions should also restore errno, but we don't care because |
| 22 |
// we abort application on error anyway. |
| 23 |
extern (C) private pure @system @nogc nothrow |
| 24 |
{ |
| 25 |
pragma(mangle, "malloc") void* pureMalloc(size_t); |
| 26 |
pragma(mangle, "realloc") void* pureRealloc(void* ptr, size_t size); |
| 27 |
pragma(mangle, "free") void pureFree(void* ptr); |
| 28 |
} |
| 29 |
|
| 30 |
debug |
| 31 |
{ |
| 32 |
debug = stomp; // flush out dangling pointer problems by stomping on unused memory |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
`OutBuffer` is a write-only output stream of untyped data. It is backed up by |
| 37 |
a contiguous array or a memory-mapped file. |
| 38 |
*/ |
| 39 |
struct OutBuffer |
| 40 |
{ |
| 41 |
import dmd.common.file : FileMapping, touchFile, writeFile; |
| 42 |
|
| 43 |
// IMPORTANT: PLEASE KEEP STATE AND DESTRUCTOR IN SYNC WITH DEFINITION IN ./outbuffer.h. |
| 44 |
// state { |
| 45 |
private ubyte[] data; |
| 46 |
private size_t offset; |
| 47 |
private bool notlinehead; |
| 48 |
/// File mapping, if any. Use a pointer for ABI compatibility with the C++ counterpart. |
| 49 |
/// If the pointer is non-null the store is a memory-mapped file, otherwise the store is RAM. |
| 50 |
private FileMapping!ubyte* fileMapping; |
| 51 |
/// Whether to indent |
| 52 |
bool doindent; |
| 53 |
/// Whether to indent by 4 spaces or by tabs; |
| 54 |
bool spaces; |
| 55 |
/// Current indent level |
| 56 |
int level; |
| 57 |
// state } |
| 58 |
|
| 59 |
nothrow: |
| 60 |
|
| 61 |
/** |
| 62 |
Construct given size. |
| 63 |
*/ |
| 64 |
this(size_t initialSize) nothrow @safe |
| 65 |
{ |
| 66 |
reserve(initialSize); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
Construct from filename. Will map the file into memory (or create it anew |
| 71 |
if necessary) and start writing at the beginning of it. |
| 72 |
|
| 73 |
Params: |
| 74 |
filename = zero-terminated name of file to map into memory |
| 75 |
*/ |
| 76 |
@trusted this(const(char)* filename) |
| 77 |
{ |
| 78 |
FileMapping!ubyte model; |
| 79 |
fileMapping = cast(FileMapping!ubyte*) malloc(model.sizeof); |
| 80 |
memcpy(fileMapping, &model, model.sizeof); |
| 81 |
fileMapping.__ctor(filename); |
| 82 |
//fileMapping = new FileMapping!ubyte(filename); |
| 83 |
data = (*fileMapping)[]; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
Frees resources associated. |
| 88 |
*/ |
| 89 |
extern (C++) void dtor() pure nothrow @trusted |
| 90 |
{ |
| 91 |
if (fileMapping) |
| 92 |
{ |
| 93 |
if (fileMapping.active) |
| 94 |
fileMapping.close(); |
| 95 |
} |
| 96 |
else |
| 97 |
{ |
| 98 |
debug (stomp) memset(data.ptr, 0xFF, data.length); |
| 99 |
pureFree(data.ptr); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
Frees resources associated automatically. |
| 105 |
*/ |
| 106 |
extern (C++) ~this() pure nothrow @trusted |
| 107 |
{ |
| 108 |
dtor(); |
| 109 |
} |
| 110 |
|
| 111 |
/// For porting with ease from dmd.backend.outbuf.Outbuffer |
| 112 |
ubyte* buf() nothrow @system { |
| 113 |
return data.ptr; |
| 114 |
} |
| 115 |
|
| 116 |
/// For porting with ease from dmd.backend.outbuf.Outbuffer |
| 117 |
ubyte** bufptr() nothrow @system { |
| 118 |
static struct Array { size_t length; ubyte* ptr; } |
| 119 |
auto a = cast(Array*) &data; |
| 120 |
assert(a.length == data.length && a.ptr == data.ptr); |
| 121 |
return &a.ptr; |
| 122 |
} |
| 123 |
|
| 124 |
extern (C++) size_t length() const pure @nogc @safe nothrow { return offset; } |
| 125 |
|
| 126 |
/********************** |
| 127 |
* Transfer ownership of the allocated data to the caller. |
| 128 |
* Returns: |
| 129 |
* pointer to the allocated data |
| 130 |
*/ |
| 131 |
extern (C++) char* extractData() pure nothrow @nogc @trusted |
| 132 |
{ |
| 133 |
char* p = cast(char*)data.ptr; |
| 134 |
data = null; |
| 135 |
offset = 0; |
| 136 |
return p; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
Releases all resources associated with `this` and resets it as an empty |
| 141 |
memory buffer. The config variables `notlinehead`, `doindent` etc. are |
| 142 |
not changed. |
| 143 |
*/ |
| 144 |
extern (C++) void destroy() pure nothrow |
| 145 |
{ |
| 146 |
dtor(); |
| 147 |
fileMapping = null; |
| 148 |
data = null; |
| 149 |
offset = 0; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
Reserves `nbytes` bytes of additional memory (or file space) in advance. |
| 154 |
The resulting capacity is at least the previous length plus `nbytes`. |
| 155 |
|
| 156 |
Params: |
| 157 |
nbytes = the number of additional bytes to reserve |
| 158 |
*/ |
| 159 |
extern (C++) void reserve(size_t nbytes) pure nothrow @trusted |
| 160 |
{ |
| 161 |
//debug (stomp) printf("OutBuffer::reserve: size = %lld, offset = %lld, nbytes = %lld\n", data.length, offset, nbytes); |
| 162 |
const minSize = offset + nbytes; |
| 163 |
if (data.length >= minSize) |
| 164 |
return; |
| 165 |
|
| 166 |
/* Increase by factor of 1.5; round up to 16 bytes. |
| 167 |
* The odd formulation is so it will map onto single x86 LEA instruction. |
| 168 |
*/ |
| 169 |
const size = ((minSize * 3 + 30) / 2) & ~15; |
| 170 |
|
| 171 |
if (fileMapping && fileMapping.active) |
| 172 |
{ |
| 173 |
fileMapping.resize(size); |
| 174 |
data = (*fileMapping)[]; |
| 175 |
} |
| 176 |
else |
| 177 |
{ |
| 178 |
debug (stomp) |
| 179 |
{ |
| 180 |
auto p = cast(ubyte*) pureMalloc(size); |
| 181 |
p || assert(0, "OutBuffer: out of memory."); |
| 182 |
memcpy(p, data.ptr, offset); |
| 183 |
memset(data.ptr, 0xFF, data.length); // stomp old location |
| 184 |
pureFree(data.ptr); |
| 185 |
memset(p + offset, 0xff, size - offset); // stomp unused data |
| 186 |
} |
| 187 |
else |
| 188 |
{ |
| 189 |
auto p = cast(ubyte*) pureRealloc(data.ptr, size); |
| 190 |
p || assert(0, "OutBuffer: out of memory."); |
| 191 |
memset(p + offset + nbytes, 0xff, size - offset - nbytes); |
| 192 |
} |
| 193 |
data = p[0 .. size]; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
/************************ |
| 198 |
* Shrink the size of the data to `size`. |
| 199 |
* Params: |
| 200 |
* size = new size of data, must be <= `.length` |
| 201 |
*/ |
| 202 |
extern (C++) void setsize(size_t size) pure nothrow @nogc @safe |
| 203 |
{ |
| 204 |
assert(size <= data.length); |
| 205 |
offset = size; |
| 206 |
} |
| 207 |
|
| 208 |
extern (C++) void reset() pure nothrow @nogc @safe |
| 209 |
{ |
| 210 |
offset = 0; |
| 211 |
} |
| 212 |
|
| 213 |
private void indent() pure nothrow @safe |
| 214 |
{ |
| 215 |
if (level) |
| 216 |
{ |
| 217 |
const indentLevel = spaces ? level * 4 : level; |
| 218 |
reserve(indentLevel); |
| 219 |
data[offset .. offset + indentLevel] = (spaces ? ' ' : '\t'); |
| 220 |
offset += indentLevel; |
| 221 |
} |
| 222 |
notlinehead = true; |
| 223 |
} |
| 224 |
|
| 225 |
// Write an array to the buffer, no reserve check |
| 226 |
@system nothrow |
| 227 |
void writen(const void *b, size_t len) |
| 228 |
{ |
| 229 |
memcpy(data.ptr + offset, b, len); |
| 230 |
offset += len; |
| 231 |
} |
| 232 |
|
| 233 |
extern (C++) void write(const(void)* data, size_t nbytes) pure nothrow @system |
| 234 |
{ |
| 235 |
write(data[0 .. nbytes]); |
| 236 |
} |
| 237 |
|
| 238 |
void write(scope const(void)[] buf) pure nothrow @trusted |
| 239 |
{ |
| 240 |
if (doindent && !notlinehead) |
| 241 |
indent(); |
| 242 |
reserve(buf.length); |
| 243 |
memcpy(this.data.ptr + offset, buf.ptr, buf.length); |
| 244 |
offset += buf.length; |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Writes a 16 bit value, no reserve check. |
| 249 |
*/ |
| 250 |
nothrow @safe |
| 251 |
void write16n(int v) |
| 252 |
{ |
| 253 |
auto x = cast(ushort) v; |
| 254 |
data[offset] = x & 0x00FF; |
| 255 |
data[offset + 1] = x >> 8u; |
| 256 |
offset += 2; |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Writes a 16 bit value. |
| 261 |
*/ |
| 262 |
void write16(int v) nothrow |
| 263 |
{ |
| 264 |
auto u = cast(ushort) v; |
| 265 |
write(&u, u.sizeof); |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Writes a 32 bit int. |
| 270 |
*/ |
| 271 |
void write32(int v) nothrow @trusted |
| 272 |
{ |
| 273 |
write(&v, v.sizeof); |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* Writes a 64 bit int. |
| 278 |
*/ |
| 279 |
@trusted void write64(long v) nothrow |
| 280 |
{ |
| 281 |
write(&v, v.sizeof); |
| 282 |
} |
| 283 |
|
| 284 |
/// Buffer will NOT be zero-terminated |
| 285 |
extern (C++) void writestring(const(char)* s) pure nothrow @system |
| 286 |
{ |
| 287 |
if (!s) |
| 288 |
return; |
| 289 |
import core.stdc.string : strlen; |
| 290 |
write(s[0 .. strlen(s)]); |
| 291 |
} |
| 292 |
|
| 293 |
/// ditto |
| 294 |
void writestring(scope const(char)[] s) pure nothrow @safe |
| 295 |
{ |
| 296 |
write(s); |
| 297 |
} |
| 298 |
|
| 299 |
/// ditto |
| 300 |
void writestring(scope string s) pure nothrow @safe |
| 301 |
{ |
| 302 |
write(s); |
| 303 |
} |
| 304 |
|
| 305 |
/// Buffer will NOT be zero-terminated, followed by newline |
| 306 |
void writestringln(const(char)[] s) pure nothrow @safe |
| 307 |
{ |
| 308 |
writestring(s); |
| 309 |
writenl(); |
| 310 |
} |
| 311 |
|
| 312 |
/** Write C string AND null byte |
| 313 |
*/ |
| 314 |
void writeStringz(const(char)* s) pure nothrow @system |
| 315 |
{ |
| 316 |
write(s[0 .. strlen(s)+1]); |
| 317 |
} |
| 318 |
|
| 319 |
/// ditto |
| 320 |
void writeStringz(const(char)[] s) pure nothrow @safe |
| 321 |
{ |
| 322 |
write(s); |
| 323 |
writeByte(0); |
| 324 |
} |
| 325 |
|
| 326 |
/// ditto |
| 327 |
void writeStringz(string s) pure nothrow @safe |
| 328 |
{ |
| 329 |
writeStringz(cast(const(char)[])(s)); |
| 330 |
} |
| 331 |
|
| 332 |
extern (C++) void prependstring(const(char)* string) pure nothrow @system |
| 333 |
{ |
| 334 |
size_t len = strlen(string); |
| 335 |
reserve(len); |
| 336 |
memmove(data.ptr + len, data.ptr, offset); |
| 337 |
memcpy(data.ptr, string, len); |
| 338 |
offset += len; |
| 339 |
} |
| 340 |
|
| 341 |
/// strip trailing tabs or spaces, write newline |
| 342 |
extern (C++) void writenl() pure nothrow @safe |
| 343 |
{ |
| 344 |
while (offset > 0 && (data[offset - 1] == ' ' || data[offset - 1] == '\t')) |
| 345 |
offset--; |
| 346 |
|
| 347 |
version (Windows) |
| 348 |
{ |
| 349 |
writeword(0x0A0D); // newline is CR,LF on Microsoft OS's |
| 350 |
} |
| 351 |
else |
| 352 |
{ |
| 353 |
writeByte('\n'); |
| 354 |
} |
| 355 |
if (doindent) |
| 356 |
notlinehead = false; |
| 357 |
} |
| 358 |
|
| 359 |
// Write n zeros; return pointer to start of zeros |
| 360 |
@trusted |
| 361 |
void *writezeros(size_t n) nothrow |
| 362 |
{ |
| 363 |
reserve(n); |
| 364 |
auto result = memset(data.ptr + offset, 0, n); |
| 365 |
offset += n; |
| 366 |
return result; |
| 367 |
} |
| 368 |
|
| 369 |
// Position buffer to accept the specified number of bytes at offset |
| 370 |
void position(size_t where, size_t nbytes) nothrow @safe |
| 371 |
{ |
| 372 |
if (where + nbytes > data.length) |
| 373 |
{ |
| 374 |
reserve(where + nbytes - offset); |
| 375 |
} |
| 376 |
offset = where; |
| 377 |
|
| 378 |
debug assert(offset + nbytes <= data.length); |
| 379 |
} |
| 380 |
|
| 381 |
/** |
| 382 |
* Writes an 8 bit byte, no reserve check. |
| 383 |
*/ |
| 384 |
extern (C++) nothrow @safe |
| 385 |
void writeByten(int b) |
| 386 |
{ |
| 387 |
this.data[offset++] = cast(ubyte) b; |
| 388 |
} |
| 389 |
|
| 390 |
extern (C++) void writeByte(uint b) pure nothrow @safe |
| 391 |
{ |
| 392 |
if (doindent && !notlinehead && b != '\n') |
| 393 |
indent(); |
| 394 |
reserve(1); |
| 395 |
this.data[offset] = cast(ubyte)b; |
| 396 |
offset++; |
| 397 |
} |
| 398 |
|
| 399 |
extern (C++) void writeUTF8(uint b) pure nothrow @safe |
| 400 |
{ |
| 401 |
reserve(6); |
| 402 |
if (b <= 0x7F) |
| 403 |
{ |
| 404 |
this.data[offset] = cast(ubyte)b; |
| 405 |
offset++; |
| 406 |
} |
| 407 |
else if (b <= 0x7FF) |
| 408 |
{ |
| 409 |
this.data[offset + 0] = cast(ubyte)((b >> 6) | 0xC0); |
| 410 |
this.data[offset + 1] = cast(ubyte)((b & 0x3F) | 0x80); |
| 411 |
offset += 2; |
| 412 |
} |
| 413 |
else if (b <= 0xFFFF) |
| 414 |
{ |
| 415 |
this.data[offset + 0] = cast(ubyte)((b >> 12) | 0xE0); |
| 416 |
this.data[offset + 1] = cast(ubyte)(((b >> 6) & 0x3F) | 0x80); |
| 417 |
this.data[offset + 2] = cast(ubyte)((b & 0x3F) | 0x80); |
| 418 |
offset += 3; |
| 419 |
} |
| 420 |
else if (b <= 0x1FFFFF) |
| 421 |
{ |
| 422 |
this.data[offset + 0] = cast(ubyte)((b >> 18) | 0xF0); |
| 423 |
this.data[offset + 1] = cast(ubyte)(((b >> 12) & 0x3F) | 0x80); |
| 424 |
this.data[offset + 2] = cast(ubyte)(((b >> 6) & 0x3F) | 0x80); |
| 425 |
this.data[offset + 3] = cast(ubyte)((b & 0x3F) | 0x80); |
| 426 |
offset += 4; |
| 427 |
} |
| 428 |
else |
| 429 |
assert(0); |
| 430 |
} |
| 431 |
|
| 432 |
extern (C++) void prependbyte(uint b) pure nothrow @trusted |
| 433 |
{ |
| 434 |
reserve(1); |
| 435 |
memmove(data.ptr + 1, data.ptr, offset); |
| 436 |
data[0] = cast(ubyte)b; |
| 437 |
offset++; |
| 438 |
} |
| 439 |
|
| 440 |
extern (C++) void writewchar(uint w) pure nothrow @safe |
| 441 |
{ |
| 442 |
version (Windows) |
| 443 |
{ |
| 444 |
writeword(w); |
| 445 |
} |
| 446 |
else |
| 447 |
{ |
| 448 |
write4(w); |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
extern (C++) void writeword(uint w) pure nothrow @trusted |
| 453 |
{ |
| 454 |
version (Windows) |
| 455 |
{ |
| 456 |
uint newline = 0x0A0D; |
| 457 |
} |
| 458 |
else |
| 459 |
{ |
| 460 |
uint newline = '\n'; |
| 461 |
} |
| 462 |
if (doindent && !notlinehead && w != newline) |
| 463 |
indent(); |
| 464 |
|
| 465 |
reserve(2); |
| 466 |
*cast(ushort*)(this.data.ptr + offset) = cast(ushort)w; |
| 467 |
offset += 2; |
| 468 |
} |
| 469 |
|
| 470 |
extern (C++) void writeUTF16(uint w) pure nothrow @trusted |
| 471 |
{ |
| 472 |
reserve(4); |
| 473 |
if (w <= 0xFFFF) |
| 474 |
{ |
| 475 |
*cast(ushort*)(this.data.ptr + offset) = cast(ushort)w; |
| 476 |
offset += 2; |
| 477 |
} |
| 478 |
else if (w <= 0x10FFFF) |
| 479 |
{ |
| 480 |
*cast(ushort*)(this.data.ptr + offset) = cast(ushort)((w >> 10) + 0xD7C0); |
| 481 |
*cast(ushort*)(this.data.ptr + offset + 2) = cast(ushort)((w & 0x3FF) | 0xDC00); |
| 482 |
offset += 4; |
| 483 |
} |
| 484 |
else |
| 485 |
assert(0); |
| 486 |
} |
| 487 |
|
| 488 |
extern (C++) void write4(uint w) pure nothrow @trusted |
| 489 |
{ |
| 490 |
version (Windows) |
| 491 |
{ |
| 492 |
bool notnewline = w != 0x000A000D; |
| 493 |
} |
| 494 |
else |
| 495 |
{ |
| 496 |
bool notnewline = true; |
| 497 |
} |
| 498 |
if (doindent && !notlinehead && notnewline) |
| 499 |
indent(); |
| 500 |
reserve(4); |
| 501 |
*cast(uint*)(this.data.ptr + offset) = w; |
| 502 |
offset += 4; |
| 503 |
} |
| 504 |
|
| 505 |
extern (C++) void write(const OutBuffer* buf) pure nothrow @trusted |
| 506 |
{ |
| 507 |
if (buf) |
| 508 |
{ |
| 509 |
reserve(buf.offset); |
| 510 |
memcpy(data.ptr + offset, buf.data.ptr, buf.offset); |
| 511 |
offset += buf.offset; |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
extern (C++) void fill0(size_t nbytes) pure nothrow @trusted |
| 516 |
{ |
| 517 |
reserve(nbytes); |
| 518 |
memset(data.ptr + offset, 0, nbytes); |
| 519 |
offset += nbytes; |
| 520 |
} |
| 521 |
|
| 522 |
/** |
| 523 |
* Allocate space, but leave it uninitialized. |
| 524 |
* Params: |
| 525 |
* nbytes = amount to allocate |
| 526 |
* Returns: |
| 527 |
* slice of the allocated space to be filled in |
| 528 |
*/ |
| 529 |
extern (D) char[] allocate(size_t nbytes) pure nothrow @safe |
| 530 |
{ |
| 531 |
reserve(nbytes); |
| 532 |
offset += nbytes; |
| 533 |
return cast(char[])data[offset - nbytes .. offset]; |
| 534 |
} |
| 535 |
|
| 536 |
extern (C++) void vprintf(const(char)* format, va_list args) nothrow @system |
| 537 |
{ |
| 538 |
int count; |
| 539 |
if (doindent && !notlinehead) |
| 540 |
indent(); |
| 541 |
uint psize = 128; |
| 542 |
for (;;) |
| 543 |
{ |
| 544 |
reserve(psize); |
| 545 |
va_list va; |
| 546 |
va_copy(va, args); |
| 547 |
/* |
| 548 |
The functions vprintf(), vfprintf(), vsprintf(), vsnprintf() |
| 549 |
are equivalent to the functions printf(), fprintf(), sprintf(), |
| 550 |
snprintf(), respectively, except that they are called with a |
| 551 |
va_list instead of a variable number of arguments. These |
| 552 |
functions do not call the va_end macro. Consequently, the value |
| 553 |
of ap is undefined after the call. The application should call |
| 554 |
va_end(ap) itself afterwards. |
| 555 |
*/ |
| 556 |
count = vsnprintf(cast(char*)data.ptr + offset, psize, format, va); |
| 557 |
va_end(va); |
| 558 |
if (count == -1) // snn.lib and older libcmt.lib return -1 if buffer too small |
| 559 |
psize *= 2; |
| 560 |
else if (count >= psize) |
| 561 |
psize = count + 1; |
| 562 |
else |
| 563 |
break; |
| 564 |
} |
| 565 |
offset += count; |
| 566 |
// if (mem.isGCEnabled) |
| 567 |
memset(data.ptr + offset, 0xff, psize - count); |
| 568 |
} |
| 569 |
|
| 570 |
static if (__VERSION__ < 2092) |
| 571 |
{ |
| 572 |
extern (C++) void printf(const(char)* format, ...) nothrow @system |
| 573 |
{ |
| 574 |
va_list ap; |
| 575 |
va_start(ap, format); |
| 576 |
vprintf(format, ap); |
| 577 |
va_end(ap); |
| 578 |
} |
| 579 |
} |
| 580 |
else |
| 581 |
{ |
| 582 |
pragma(printf) extern (C++) void printf(const(char)* format, ...) nothrow @system |
| 583 |
{ |
| 584 |
va_list ap; |
| 585 |
va_start(ap, format); |
| 586 |
vprintf(format, ap); |
| 587 |
va_end(ap); |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
/************************************** |
| 592 |
* Convert `u` to a string and append it to the buffer. |
| 593 |
* Params: |
| 594 |
* u = integral value to append |
| 595 |
*/ |
| 596 |
extern (C++) void print(ulong u) pure nothrow @safe |
| 597 |
{ |
| 598 |
UnsignedStringBuf buf = void; |
| 599 |
writestring(unsignedToTempString(u, buf)); |
| 600 |
} |
| 601 |
|
| 602 |
extern (C++) void bracket(char left, char right) pure nothrow @trusted |
| 603 |
{ |
| 604 |
reserve(2); |
| 605 |
memmove(data.ptr + 1, data.ptr, offset); |
| 606 |
data[0] = left; |
| 607 |
data[offset + 1] = right; |
| 608 |
offset += 2; |
| 609 |
} |
| 610 |
|
| 611 |
/****************** |
| 612 |
* Insert left at i, and right at j. |
| 613 |
* Return index just past right. |
| 614 |
*/ |
| 615 |
extern (C++) size_t bracket(size_t i, const(char)* left, size_t j, const(char)* right) pure nothrow @system |
| 616 |
{ |
| 617 |
size_t leftlen = strlen(left); |
| 618 |
size_t rightlen = strlen(right); |
| 619 |
reserve(leftlen + rightlen); |
| 620 |
insert(i, left, leftlen); |
| 621 |
insert(j + leftlen, right, rightlen); |
| 622 |
return j + leftlen + rightlen; |
| 623 |
} |
| 624 |
|
| 625 |
extern (C++) void spread(size_t offset, size_t nbytes) pure nothrow @system |
| 626 |
{ |
| 627 |
reserve(nbytes); |
| 628 |
memmove(data.ptr + offset + nbytes, data.ptr + offset, this.offset - offset); |
| 629 |
this.offset += nbytes; |
| 630 |
} |
| 631 |
|
| 632 |
/**************************************** |
| 633 |
* Returns: offset + nbytes |
| 634 |
*/ |
| 635 |
extern (C++) size_t insert(size_t offset, const(void)* p, size_t nbytes) pure nothrow @system |
| 636 |
{ |
| 637 |
spread(offset, nbytes); |
| 638 |
memmove(data.ptr + offset, p, nbytes); |
| 639 |
return offset + nbytes; |
| 640 |
} |
| 641 |
|
| 642 |
size_t insert(size_t offset, const(char)[] s) pure nothrow @system |
| 643 |
{ |
| 644 |
return insert(offset, s.ptr, s.length); |
| 645 |
} |
| 646 |
|
| 647 |
extern (C++) void remove(size_t offset, size_t nbytes) pure nothrow @nogc @system |
| 648 |
{ |
| 649 |
memmove(data.ptr + offset, data.ptr + offset + nbytes, this.offset - (offset + nbytes)); |
| 650 |
this.offset -= nbytes; |
| 651 |
} |
| 652 |
|
| 653 |
/** |
| 654 |
* Returns: |
| 655 |
* a non-owning const slice of the buffer contents |
| 656 |
*/ |
| 657 |
extern (D) const(char)[] opSlice() const pure nothrow @nogc @safe |
| 658 |
{ |
| 659 |
return cast(const(char)[])data[0 .. offset]; |
| 660 |
} |
| 661 |
|
| 662 |
extern (D) const(char)[] opSlice(size_t lwr, size_t upr) const pure nothrow @nogc @safe |
| 663 |
{ |
| 664 |
return cast(const(char)[])data[lwr .. upr]; |
| 665 |
} |
| 666 |
|
| 667 |
extern (D) char opIndex(size_t i) const pure nothrow @nogc @safe |
| 668 |
{ |
| 669 |
return cast(char)data[i]; |
| 670 |
} |
| 671 |
|
| 672 |
alias opDollar = length; |
| 673 |
|
| 674 |
/*********************************** |
| 675 |
* Extract the data as a slice and take ownership of it. |
| 676 |
* |
| 677 |
* When `true` is passed as an argument, this function behaves |
| 678 |
* like `dmd.utils.toDString(thisbuffer.extractChars())`. |
| 679 |
* |
| 680 |
* Params: |
| 681 |
* nullTerminate = When `true`, the data will be `null` terminated. |
| 682 |
* This is useful to call C functions or store |
| 683 |
* the result in `Strings`. Defaults to `false`. |
| 684 |
*/ |
| 685 |
extern (D) char[] extractSlice(bool nullTerminate = false) pure nothrow |
| 686 |
{ |
| 687 |
const length = offset; |
| 688 |
if (!nullTerminate) |
| 689 |
return extractData()[0 .. length]; |
| 690 |
// There's already a terminating `'\0'` |
| 691 |
if (length && data[length - 1] == '\0') |
| 692 |
return extractData()[0 .. length - 1]; |
| 693 |
writeByte(0); |
| 694 |
return extractData()[0 .. length]; |
| 695 |
} |
| 696 |
|
| 697 |
extern (D) byte[] extractUbyteSlice(bool nullTerminate = false) pure nothrow |
| 698 |
{ |
| 699 |
return cast(byte[]) extractSlice(nullTerminate); |
| 700 |
} |
| 701 |
|
| 702 |
// Append terminating null if necessary and get view of internal buffer |
| 703 |
extern (C++) char* peekChars() pure nothrow |
| 704 |
{ |
| 705 |
if (!offset || data[offset - 1] != '\0') |
| 706 |
{ |
| 707 |
writeByte(0); |
| 708 |
offset--; // allow appending more |
| 709 |
} |
| 710 |
return cast(char*)data.ptr; |
| 711 |
} |
| 712 |
|
| 713 |
// Peek at slice of data without taking ownership |
| 714 |
extern (D) ubyte[] peekSlice() pure nothrow |
| 715 |
{ |
| 716 |
return data[0 .. offset]; |
| 717 |
} |
| 718 |
|
| 719 |
// Append terminating null if necessary and take ownership of data |
| 720 |
extern (C++) char* extractChars() pure nothrow @safe |
| 721 |
{ |
| 722 |
if (!offset || data[offset - 1] != '\0') |
| 723 |
writeByte(0); |
| 724 |
return extractData(); |
| 725 |
} |
| 726 |
|
| 727 |
void writesLEB128(int value) pure nothrow @safe |
| 728 |
{ |
| 729 |
while (1) |
| 730 |
{ |
| 731 |
ubyte b = value & 0x7F; |
| 732 |
|
| 733 |
value >>= 7; // arithmetic right shift |
| 734 |
if ((value == 0 && !(b & 0x40)) || |
| 735 |
(value == -1 && (b & 0x40))) |
| 736 |
{ |
| 737 |
writeByte(b); |
| 738 |
break; |
| 739 |
} |
| 740 |
writeByte(b | 0x80); |
| 741 |
} |
| 742 |
} |
| 743 |
|
| 744 |
void writeuLEB128(uint value) pure nothrow @safe |
| 745 |
{ |
| 746 |
do |
| 747 |
{ |
| 748 |
ubyte b = value & 0x7F; |
| 749 |
|
| 750 |
value >>= 7; |
| 751 |
if (value) |
| 752 |
b |= 0x80; |
| 753 |
writeByte(b); |
| 754 |
} while (value); |
| 755 |
} |
| 756 |
|
| 757 |
/** |
| 758 |
* Write an array as a string of hexadecimal digits |
| 759 |
* Params: |
| 760 |
* data = bytes to write |
| 761 |
* upperCase = whether to upper case hex digits A-F |
| 762 |
*/ |
| 763 |
void writeHexString(scope const(ubyte)[] data, bool upperCase) pure nothrow @safe |
| 764 |
{ |
| 765 |
auto slice = this.allocate(2 * data.length); |
| 766 |
const a = upperCase ? 'A' : 'a'; |
| 767 |
foreach (i, c; data) |
| 768 |
{ |
| 769 |
char hi = (c >> 4) & 0xF; |
| 770 |
slice[i * 2] = cast(char)(hi < 10 ? hi + '0' : hi - 10 + a); |
| 771 |
char lo = c & 0xF; |
| 772 |
slice[i * 2 + 1] = cast(char)(lo < 10 ? lo + '0' : lo - 10 + a); |
| 773 |
} |
| 774 |
} |
| 775 |
|
| 776 |
/** |
| 777 |
Destructively saves the contents of `this` to `filename`. As an |
| 778 |
optimization, if the file already has identical contents with the buffer, |
| 779 |
no copying is done. This is because on SSD drives reading is often much |
| 780 |
faster than writing and because there's a high likelihood an identical |
| 781 |
file is written during the build process. |
| 782 |
|
| 783 |
Params: |
| 784 |
filename = the name of the file to receive the contents |
| 785 |
|
| 786 |
Returns: `true` iff the operation succeeded. |
| 787 |
*/ |
| 788 |
extern(D) bool moveToFile(const char[] filename) @system |
| 789 |
{ |
| 790 |
bool result = true; |
| 791 |
const filenameZ = (filename ~ "\0").ptr; |
| 792 |
const bool identical = this[] == FileMapping!(const ubyte)(filenameZ)[]; |
| 793 |
|
| 794 |
if (fileMapping && fileMapping.active) |
| 795 |
{ |
| 796 |
// Defer to corresponding functions in FileMapping. |
| 797 |
if (identical) |
| 798 |
{ |
| 799 |
result = fileMapping.discard(); |
| 800 |
} |
| 801 |
else |
| 802 |
{ |
| 803 |
// Resize to fit to get rid of the slack bytes at the end |
| 804 |
fileMapping.resize(offset); |
| 805 |
result = fileMapping.moveToFile(filenameZ); |
| 806 |
} |
| 807 |
// Can't call destroy() here because the file mapping is already closed. |
| 808 |
data = null; |
| 809 |
offset = 0; |
| 810 |
} |
| 811 |
else |
| 812 |
{ |
| 813 |
if (!identical) |
| 814 |
writeFile(filenameZ, this[]); |
| 815 |
destroy(); |
| 816 |
} |
| 817 |
|
| 818 |
return identical |
| 819 |
? result && touchFile(filenameZ) |
| 820 |
: result; |
| 821 |
} |
| 822 |
} |
| 823 |
|
| 824 |
/****** copied from core.internal.string *************/ |
| 825 |
|
| 826 |
private: |
| 827 |
|
| 828 |
alias UnsignedStringBuf = char[20]; |
| 829 |
|
| 830 |
char[] unsignedToTempString(ulong value, return scope char[] buf, uint radix = 10) @safe pure nothrow @nogc |
| 831 |
{ |
| 832 |
size_t i = buf.length; |
| 833 |
do |
| 834 |
{ |
| 835 |
if (value < radix) |
| 836 |
{ |
| 837 |
ubyte x = cast(ubyte)value; |
| 838 |
buf[--i] = cast(char)((x < 10) ? x + '0' : x - 10 + 'a'); |
| 839 |
break; |
| 840 |
} |
| 841 |
else |
| 842 |
{ |
| 843 |
ubyte x = cast(ubyte)(value % radix); |
| 844 |
value /= radix; |
| 845 |
buf[--i] = cast(char)((x < 10) ? x + '0' : x - 10 + 'a'); |
| 846 |
} |
| 847 |
} while (value); |
| 848 |
return buf[i .. $]; |
| 849 |
} |
| 850 |
|
| 851 |
/************* unit tests **************************************************/ |
| 852 |
|
| 853 |
unittest |
| 854 |
{ |
| 855 |
OutBuffer buf; |
| 856 |
buf.printf("betty"); |
| 857 |
buf.insert(1, "xx".ptr, 2); |
| 858 |
buf.insert(3, "yy"); |
| 859 |
buf.remove(4, 1); |
| 860 |
buf.bracket('(', ')'); |
| 861 |
const char[] s = buf[]; |
| 862 |
assert(s == "(bxxyetty)"); |
| 863 |
buf.destroy(); |
| 864 |
} |
| 865 |
|
| 866 |
unittest |
| 867 |
{ |
| 868 |
OutBuffer buf; |
| 869 |
buf.writestring("abc".ptr); |
| 870 |
buf.prependstring("def"); |
| 871 |
buf.prependbyte('x'); |
| 872 |
OutBuffer buf2; |
| 873 |
buf2.writestring("mmm"); |
| 874 |
buf.write(&buf2); |
| 875 |
char[] s = buf.extractSlice(); |
| 876 |
assert(s == "xdefabcmmm"); |
| 877 |
} |
| 878 |
|
| 879 |
unittest |
| 880 |
{ |
| 881 |
OutBuffer buf; |
| 882 |
buf.writeByte('a'); |
| 883 |
char[] s = buf.extractSlice(); |
| 884 |
assert(s == "a"); |
| 885 |
|
| 886 |
buf.writeByte('b'); |
| 887 |
char[] t = buf.extractSlice(); |
| 888 |
assert(t == "b"); |
| 889 |
} |
| 890 |
|
| 891 |
unittest |
| 892 |
{ |
| 893 |
OutBuffer buf; |
| 894 |
char* p = buf.peekChars(); |
| 895 |
assert(*p == 0); |
| 896 |
|
| 897 |
buf.writeByte('s'); |
| 898 |
char* q = buf.peekChars(); |
| 899 |
assert(strcmp(q, "s") == 0); |
| 900 |
} |
| 901 |
|
| 902 |
unittest |
| 903 |
{ |
| 904 |
char[10] buf; |
| 905 |
char[] s = unsignedToTempString(278, buf[], 10); |
| 906 |
assert(s == "278"); |
| 907 |
|
| 908 |
s = unsignedToTempString(1, buf[], 10); |
| 909 |
assert(s == "1"); |
| 910 |
|
| 911 |
s = unsignedToTempString(8, buf[], 2); |
| 912 |
assert(s == "1000"); |
| 913 |
|
| 914 |
s = unsignedToTempString(29, buf[], 16); |
| 915 |
assert(s == "1d"); |
| 916 |
} |
| 917 |
|
| 918 |
unittest |
| 919 |
{ |
| 920 |
OutBuffer buf; |
| 921 |
buf.writeUTF8(0x0000_0011); |
| 922 |
buf.writeUTF8(0x0000_0111); |
| 923 |
buf.writeUTF8(0x0000_1111); |
| 924 |
buf.writeUTF8(0x0001_1111); |
| 925 |
buf.writeUTF8(0x0010_0000); |
| 926 |
assert(buf[] == "\x11\U00000111\U00001111\U00011111\U00100000"); |
| 927 |
|
| 928 |
buf.reset(); |
| 929 |
buf.writeUTF16(0x0000_0011); |
| 930 |
buf.writeUTF16(0x0010_FFFF); |
| 931 |
assert(buf[] == cast(string) "\u0011\U0010FFFF"w); |
| 932 |
} |
| 933 |
|
| 934 |
unittest |
| 935 |
{ |
| 936 |
OutBuffer buf; |
| 937 |
buf.doindent = true; |
| 938 |
|
| 939 |
const(char)[] s = "abc"; |
| 940 |
buf.writestring(s); |
| 941 |
buf.level += 1; |
| 942 |
buf.indent(); |
| 943 |
buf.writestring("abs"); |
| 944 |
|
| 945 |
assert(buf[] == "abc\tabs"); |
| 946 |
|
| 947 |
buf.setsize(4); |
| 948 |
assert(buf.length == 4); |
| 949 |
} |
| 950 |
|
| 951 |
unittest |
| 952 |
{ |
| 953 |
OutBuffer buf; |
| 954 |
|
| 955 |
buf.writenl(); |
| 956 |
buf.writestring("abc \t "); |
| 957 |
buf.writenl(); // strips trailing whitespace |
| 958 |
buf.writenl(); // doesn't strip previous newline |
| 959 |
|
| 960 |
version(Windows) |
| 961 |
assert(buf[] == "\r\nabc\r\n\r\n"); |
| 962 |
else |
| 963 |
assert(buf[] == "\nabc\n\n"); |
| 964 |
} |
| 965 |
|
| 966 |
unittest |
| 967 |
{ |
| 968 |
OutBuffer buf; |
| 969 |
buf.writeHexString([0xAA, 0xBB], false); |
| 970 |
buf.writeHexString([0xCC], true); |
| 971 |
assert(buf[] == "aabbCC"); |
| 972 |
} |