From 510357f4c7ac96d11c571a1ed4fbb56bc1c4e758 Mon Sep 17 00:00:00 2001 From: Ferdinand Majerech Date: Tue, 29 Jul 2014 14:41:46 +0200 Subject: [PATCH] insert() instead of insertBack() for SliceBuilder. --- source/dyaml/reader.d | 25 ++++++++++++++++++------- source/dyaml/scanner.d | 8 +++----- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/source/dyaml/reader.d b/source/dyaml/reader.d index 19d3370..88d4243 100644 --- a/source/dyaml/reader.d +++ b/source/dyaml/reader.d @@ -714,19 +714,30 @@ public: 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 /// 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(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; - core.stdc.string.memmove(reader_.buffer_.ptr + point + 1, - reader_.buffer_.ptr + point, - backwards * dchar.sizeof); + const point = start_ + position; + const movedLength = end_ - point; + if(movedLength > 0) + { + core.stdc.string.memmove(reader_.buffer_.ptr + point + 1, + reader_.buffer_.ptr + point, + movedLength * dchar.sizeof); + } ++end_; reader_.buffer_[point] = c; } diff --git a/source/dyaml/scanner.d b/source/dyaml/scanner.d index 53c16f8..bb70131 100644 --- a/source/dyaml/scanner.d +++ b/source/dyaml/scanner.d @@ -1248,7 +1248,6 @@ final class Scanner indent += increment - 1; endMark = scanBlockScalarBreaksToSlice(indent); } - size_t endLen = reader_.sliceBuilder.length; // int.max means there's no line break (int.max is outside UTF-32). dchar lineBreak = cast(dchar)int.max; @@ -1269,7 +1268,6 @@ final class Scanner // The line breaks should actually be written _after_ the if() block // below. We work around that by inserting endMark = scanBlockScalarBreaksToSlice(indent); - endLen = reader_.sliceBuilder.length; // This will not run during the last iteration (see the if() vs the // 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 // middle of slice. - if(startLen == endLen) + if(startLen == reader_.sliceBuilder.length) { reader_.sliceBuilder.write(' '); } @@ -1293,7 +1291,7 @@ final class Scanner { // We need to insert in the middle of the slice in case any line // breaks were scanned. - reader_.sliceBuilder.insertBack(lineBreak, endLen - startLen); + reader_.sliceBuilder.insert(lineBreak, startLen); } ////this is Clark Evans's interpretation (also in the spec @@ -1336,7 +1334,7 @@ final class Scanner // be inserted _before_ the other line breaks. 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 // directly write the first line break (as it isn't stripped - chomping