Skip to content

Commit fdbc413

Browse files
authored
turn OutBuffer into an output range (dlang#21425)
1 parent ecca304 commit fdbc413

2 files changed

Lines changed: 22 additions & 17 deletions

File tree

compiler/src/dmd/common/outbuffer.d

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,11 @@ struct OutBuffer
230230
offset += len;
231231
}
232232

233+
alias put = write; // transition to output range which uses put()
234+
233235
extern (C++) void write(const(void)* data, size_t nbytes) pure nothrow @system
234236
{
235-
write(data[0 .. nbytes]);
237+
put(data[0 .. nbytes]);
236238
}
237239

238240
void write(scope const(void)[] buf) pure nothrow @trusted
@@ -262,23 +264,23 @@ struct OutBuffer
262264
void write16(int v) nothrow
263265
{
264266
auto u = cast(ushort) v;
265-
write(&u, u.sizeof);
267+
put(&u, u.sizeof);
266268
}
267269

268270
/**
269271
* Writes a 32 bit int.
270272
*/
271273
void write32(int v) nothrow @trusted
272274
{
273-
write(&v, v.sizeof);
275+
put(&v, v.sizeof);
274276
}
275277

276278
/**
277279
* Writes a 64 bit int.
278280
*/
279281
@trusted void write64(long v) nothrow
280282
{
281-
write(&v, v.sizeof);
283+
put(&v, v.sizeof);
282284
}
283285

284286
/// Buffer will NOT be zero-terminated
@@ -287,19 +289,19 @@ struct OutBuffer
287289
if (!s)
288290
return;
289291
import core.stdc.string : strlen;
290-
write(s[0 .. strlen(s)]);
292+
put(s[0 .. strlen(s)]);
291293
}
292294

293295
/// ditto
294296
void writestring(scope const(char)[] s) pure nothrow @safe
295297
{
296-
write(s);
298+
put(s);
297299
}
298300

299301
/// ditto
300302
void writestring(scope string s) pure nothrow @safe
301303
{
302-
write(s);
304+
put(s);
303305
}
304306

305307
/// Buffer will NOT be zero-terminated, followed by newline
@@ -313,13 +315,13 @@ struct OutBuffer
313315
*/
314316
void writeStringz(const(char)* s) pure nothrow @system
315317
{
316-
write(s[0 .. strlen(s)+1]);
318+
put(s[0 .. strlen(s)+1]);
317319
}
318320

319321
/// ditto
320322
void writeStringz(const(char)[] s) pure nothrow @safe
321323
{
322-
write(s);
324+
put(s);
323325
writeByte(0);
324326
}
325327

@@ -502,14 +504,17 @@ struct OutBuffer
502504
offset += 4;
503505
}
504506

505-
extern (C++) void write(const OutBuffer* buf) pure nothrow @trusted
507+
extern (C++) void write(scope const OutBuffer* buf) pure nothrow @trusted
506508
{
507509
if (buf)
508-
{
509-
reserve(buf.offset);
510-
memcpy(data.ptr + offset, buf.data.ptr, buf.offset);
511-
offset += buf.offset;
512-
}
510+
put(*buf);
511+
}
512+
513+
void write(ref scope const OutBuffer buf) pure nothrow @trusted
514+
{
515+
reserve(buf.offset);
516+
memcpy(data.ptr + offset, buf.data.ptr, buf.offset);
517+
offset += buf.offset;
513518
}
514519

515520
extern (C++) void fill0(size_t nbytes) pure nothrow @trusted
@@ -871,7 +876,7 @@ unittest
871876
buf.prependbyte('x');
872877
OutBuffer buf2;
873878
buf2.writestring("mmm");
874-
buf.write(&buf2);
879+
buf.put(&buf2);
875880
char[] s = buf.extractSlice();
876881
assert(s == "xdefabcmmm");
877882
}

compiler/src/dmd/errors.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -999,7 +999,7 @@ private void colorHighlightCode(ref OutBuffer buf)
999999
res.writeByte(HIGHLIGHT.Default);
10001000
//printf("res = '%.*s'\n", cast(int)buf.length, buf[].ptr);
10011001
buf.setsize(0);
1002-
buf.write(&res);
1002+
buf.put(res);
10031003
--nested;
10041004
}
10051005

0 commit comments

Comments
 (0)