From 4ff3f8dc51a6ac39f7c23d7610e62a6319c20bdf Mon Sep 17 00:00:00 2001 From: Ferdinand Majerech Date: Tue, 22 Jul 2014 03:41:46 +0200 Subject: [PATCH] Optimized updateBuffer() for UTF-16/UTF-32 --- source/dyaml/reader.d | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/source/dyaml/reader.d b/source/dyaml/reader.d index aa75189..5f35f4e 100644 --- a/source/dyaml/reader.d +++ b/source/dyaml/reader.d @@ -494,30 +494,26 @@ struct UTFBlockDecoder(size_t bufferSize_) if (bufferSize_ % 2 == 0) { case UTFEncoding.UTF_8: const bytes = min(bufferSize_ - rawUsed_, input_.length); - // Current length of valid data in rawBuffer8_. - const rawLength = rawUsed_ + bytes; rawBuffer8_[rawUsed_ .. rawUsed_ + bytes] = cast(char[])input_[0 .. bytes]; input_ = input_[bytes .. $]; + // Current length of valid data in rawBuffer8_. + const rawLength = rawUsed_ + bytes; decodeRawBuffer(rawBuffer8_, rawLength); break; case UTFEncoding.UTF_16: const words = min((bufferSize_ / 2) - rawUsed_, input_.length / 2); + const bytes = 2 * words; + rawBuffer16_[rawUsed_ .. rawUsed_ + words] = cast(wchar[])input_[0 .. bytes]; + input_ = input_[bytes .. $]; // Current length of valid data in rawBuffer16_. const rawLength = rawUsed_ + words; - foreach(c; rawUsed_ .. rawLength) - { - rawBuffer16_[c] = *cast(wchar*)input_.ptr; - input_ = input_[2 .. $]; - } decodeRawBuffer(rawBuffer16_, rawLength); break; case UTFEncoding.UTF_32: const chars = min(bufferSize_ / 4, input_.length / 4); - foreach(c; 0 .. chars) - { - decodedSpace_[c] = *cast(dchar*)input_.ptr; - input_ = input_[4 .. $]; - } + const bytes = 4 * chars; + decodedSpace_[0 .. chars] = cast(dchar[])input_[0 .. bytes]; + input_ = input_[bytes .. $]; decoded_ = decodedSpace_[0 .. chars]; break; }