insert() instead of insertBack() for SliceBuilder.

This commit is contained in:
Ferdinand Majerech 2014-07-29 14:41:46 +02:00
parent 239152f793
commit 510357f4c7
2 changed files with 21 additions and 12 deletions

View file

@ -714,19 +714,30 @@ public:
reader_.buffer_[end_++] = c; reader_.buffer_[end_++] = c;
} }
/// Insert a character into the slice, backwards characters before the end. /// Insert a character to a specified position in the slice.
/// ///
/// Enlarges the slice by 1 char. Note that the slice can only extend up to the /// Enlarges the slice by 1 char. Note that the slice can only extend up to the
/// current position in the Reader buffer. /// current position in the Reader buffer.
void insertBack(dchar c, size_t backwards) @system pure nothrow @nogc ///
/// Params:
///
/// c = The character to insert.
/// position = Position to insert the character at in code units, not code points.
/// Must be less than slice length(); a previously returned length()
/// can be used.
void insert(const dchar c, const size_t position) @system pure nothrow @nogc
{ {
assert(inProgress, "insertBack called without begin"); assert(inProgress, "insertBack called without begin");
assert(end_ - backwards >= start_, "Trying to insert in front of the slice"); assert(start_ + position <= end_, "Trying to insert after the end of the slice");
const point = end_ - backwards; const point = start_ + position;
core.stdc.string.memmove(reader_.buffer_.ptr + point + 1, const movedLength = end_ - point;
reader_.buffer_.ptr + point, if(movedLength > 0)
backwards * dchar.sizeof); {
core.stdc.string.memmove(reader_.buffer_.ptr + point + 1,
reader_.buffer_.ptr + point,
movedLength * dchar.sizeof);
}
++end_; ++end_;
reader_.buffer_[point] = c; reader_.buffer_[point] = c;
} }

View file

@ -1248,7 +1248,6 @@ final class Scanner
indent += increment - 1; indent += increment - 1;
endMark = scanBlockScalarBreaksToSlice(indent); endMark = scanBlockScalarBreaksToSlice(indent);
} }
size_t endLen = reader_.sliceBuilder.length;
// int.max means there's no line break (int.max is outside UTF-32). // int.max means there's no line break (int.max is outside UTF-32).
dchar lineBreak = cast(dchar)int.max; dchar lineBreak = cast(dchar)int.max;
@ -1269,7 +1268,6 @@ final class Scanner
// The line breaks should actually be written _after_ the if() block // The line breaks should actually be written _after_ the if() block
// below. We work around that by inserting // below. We work around that by inserting
endMark = scanBlockScalarBreaksToSlice(indent); endMark = scanBlockScalarBreaksToSlice(indent);
endLen = reader_.sliceBuilder.length;
// This will not run during the last iteration (see the if() vs the // This will not run during the last iteration (see the if() vs the
// while()), hence breaksTransaction rollback (which happens after this // while()), hence breaksTransaction rollback (which happens after this
@ -1284,7 +1282,7 @@ final class Scanner
{ {
// No breaks were scanned; no need to insert the space in the // No breaks were scanned; no need to insert the space in the
// middle of slice. // middle of slice.
if(startLen == endLen) if(startLen == reader_.sliceBuilder.length)
{ {
reader_.sliceBuilder.write(' '); reader_.sliceBuilder.write(' ');
} }
@ -1293,7 +1291,7 @@ final class Scanner
{ {
// We need to insert in the middle of the slice in case any line // We need to insert in the middle of the slice in case any line
// breaks were scanned. // breaks were scanned.
reader_.sliceBuilder.insertBack(lineBreak, endLen - startLen); reader_.sliceBuilder.insert(lineBreak, startLen);
} }
////this is Clark Evans's interpretation (also in the spec ////this is Clark Evans's interpretation (also in the spec
@ -1336,7 +1334,7 @@ final class Scanner
// be inserted _before_ the other line breaks. // be inserted _before_ the other line breaks.
if(chomping == Chomping.Keep) if(chomping == Chomping.Keep)
{ {
reader_.sliceBuilder.insertBack(lineBreak, endLen - startLen); reader_.sliceBuilder.insert(lineBreak, startLen);
} }
// If chomping is not Keep, breaksTransaction was cancelled so we can // If chomping is not Keep, breaksTransaction was cancelled so we can
// directly write the first line break (as it isn't stripped - chomping // directly write the first line break (as it isn't stripped - chomping