From ce82e5458ee878358244324ad116694b672c9fd2 Mon Sep 17 00:00:00 2001 From: dcode Date: Mon, 11 Mar 2019 01:16:05 +0100 Subject: [PATCH] unshiftify padEnd --- std/assembly/string.ts | 31 +++-- tests/compiler/std/string.optimized.wat | 116 ++++++++----------- tests/compiler/std/string.untouched.wat | 147 +++++++++++------------- 3 files changed, 133 insertions(+), 161 deletions(-) diff --git a/std/assembly/string.ts b/std/assembly/string.ts index 2a3a71f9..5703c411 100644 --- a/std/assembly/string.ts +++ b/std/assembly/string.ts @@ -329,24 +329,23 @@ export class String extends StringBase { padEnd(targetLength: i32, padString: String = changetype(" ")): String { assert(this !== null); - var length = this.length; - var padLen = padString.length; - if (targetLength < length || !padLen) return this; - var len = targetLength - length; - var out = ALLOC(targetLength << 1); - if (length) { - memory.copy(out, changetype(this), length << 1); - } - if (len > padLen) { - let count = (len - 1) / padLen; - let base = count * padLen; - let rest = len - base; - memory.repeat(out + (length << 1), changetype(padString), padString.length << 1, count); - if (rest) { - memory.copy(out + ((base + length) << 1), changetype(padString), rest << 1); + var thisSize = this.length << 1; + var targetSize = targetLength << 1; + var padSize = padString.length << 1; + if (targetSize < thisSize || !padSize) return this; + var appendSize = targetSize - thisSize; + var out = ALLOC(targetSize); + memory.copy(out, changetype(this), thisSize); + if (appendSize > padSize) { + let repeatCount = (appendSize - 2) / padSize; + let restBase = repeatCount * padSize; + let restSize = appendSize - restBase; + memory.repeat(out + thisSize, changetype(padString), padSize, repeatCount); + if (restSize) { + memory.copy(out + thisSize + restBase, changetype(padString), restSize); } } else { - memory.copy(out + (length << 1), changetype(padString), len << 1); + memory.copy(out + thisSize, changetype(padString), appendSize); } return REGISTER(out); } diff --git a/tests/compiler/std/string.optimized.wat b/tests/compiler/std/string.optimized.wat index 4f9252e6..eed04311 100644 --- a/tests/compiler/std/string.optimized.wat +++ b/tests/compiler/std/string.optimized.wat @@ -2170,21 +2170,28 @@ i32.load offset=4 i32.const 1 i32.shr_u - local.set $5 + i32.const 1 + i32.shl + local.set $4 local.get $1 + i32.const 1 + i32.shl + local.tee $1 local.get $0 i32.const 8 i32.sub i32.load offset=4 i32.const 1 i32.shr_u + i32.const 1 + i32.shl + local.tee $5 + i32.lt_u local.tee $3 - i32.lt_s - local.tee $4 if (result i32) - local.get $4 + local.get $3 else - local.get $5 + local.get $4 i32.eqz end if @@ -2192,86 +2199,61 @@ return end local.get $1 - local.get $3 - i32.sub - local.set $6 - local.get $1 - i32.const 1 - i32.shl call $~lib/runtime/ALLOC - local.set $1 - local.get $3 - if - local.get $1 - local.get $0 - local.get $3 - i32.const 1 - i32.shl - call $~lib/internal/memory/memmove - end - local.get $6 + local.tee $6 + local.get $0 local.get $5 - i32.gt_s + call $~lib/internal/memory/memmove + local.get $1 + local.get $5 + i32.sub + local.tee $3 + local.get $4 + i32.gt_u if - local.get $3 - i32.const 1 - i32.shl - local.get $1 + local.get $5 + local.get $6 i32.add local.get $2 - local.get $2 - i32.const 8 - i32.sub - i32.load offset=4 - i32.const 1 - i32.shr_u - i32.const 1 - i32.shl - local.get $6 - i32.const 1 - i32.sub - local.get $5 - i32.div_s - local.tee $4 - call $~lib/runtime/memory.repeat - local.get $6 local.get $4 - local.get $5 - i32.mul - local.tee $0 + local.get $3 + i32.const 2 i32.sub - local.tee $4 + local.get $4 + i32.div_u + local.tee $0 + call $~lib/runtime/memory.repeat + local.get $3 + local.get $0 + local.get $4 + i32.mul + local.tee $1 + i32.sub + local.tee $3 if - local.get $0 - local.get $3 + local.get $5 + local.get $6 i32.add - i32.const 1 - i32.shl local.get $1 i32.add local.get $2 - local.get $4 - i32.const 1 - i32.shl + local.get $3 call $~lib/internal/memory/memmove end else - local.get $3 - i32.const 1 - i32.shl - local.get $1 + local.get $5 + local.get $6 i32.add local.get $2 - local.get $6 - i32.const 1 - i32.shl + local.get $3 call $~lib/internal/memory/memmove end - local.get $1 + local.get $6 + local.tee $0 call $~lib/runtime/unref i32.const 1 i32.store - local.get $1 + local.get $0 ) (func $~lib/string/String#padEnd|trampoline (; 19 ;) (type $FUNCSIG$iii) (param $0 i32) (param $1 i32) (result i32) (local $2 i32) @@ -2745,7 +2727,7 @@ if i32.const 0 i32.const 96 - i32.const 597 + i32.const 596 i32.const 10 call $~lib/env/abort unreachable @@ -3029,7 +3011,7 @@ if i32.const 0 i32.const 96 - i32.const 355 + i32.const 354 i32.const 4 call $~lib/env/abort unreachable @@ -3059,7 +3041,7 @@ if i32.const 0 i32.const 96 - i32.const 360 + i32.const 359 i32.const 6 call $~lib/env/abort unreachable @@ -3420,7 +3402,7 @@ if i32.const 0 i32.const 96 - i32.const 382 + i32.const 381 i32.const 4 call $~lib/env/abort unreachable diff --git a/tests/compiler/std/string.untouched.wat b/tests/compiler/std/string.untouched.wat index 4a5fee69..b80e56b1 100644 --- a/tests/compiler/std/string.untouched.wat +++ b/tests/compiler/std/string.untouched.wat @@ -2736,6 +2736,7 @@ (local $10 i32) (local $11 i32) (local $12 i32) + (local $13 i32) local.get $0 i32.const 0 i32.ne @@ -2750,125 +2751,115 @@ end local.get $0 call $~lib/runtime/StringBase#get:length + i32.const 1 + i32.shl local.set $3 + local.get $1 + i32.const 1 + i32.shl + local.set $4 local.get $2 call $~lib/runtime/StringBase#get:length - local.set $4 - local.get $1 + i32.const 1 + i32.shl + local.set $5 + local.get $4 local.get $3 - i32.lt_s - local.tee $5 + i32.lt_u + local.tee $6 if (result i32) - local.get $5 + local.get $6 else - local.get $4 + local.get $5 i32.eqz end if local.get $0 return end - local.get $1 + local.get $4 local.get $3 i32.sub - local.set $6 - local.get $1 - i32.const 1 - i32.shl - call $~lib/runtime/ALLOC local.set $7 - local.get $3 - if - local.get $7 - local.set $5 - local.get $0 - local.set $8 - local.get $3 - i32.const 1 - i32.shl - local.set $9 - local.get $5 + local.get $4 + call $~lib/runtime/ALLOC + local.set $8 + block $~lib/runtime/memory.copy|inlined.4 local.get $8 + local.set $6 + local.get $0 + local.set $9 + local.get $3 + local.set $10 + local.get $6 local.get $9 + local.get $10 call $~lib/internal/memory/memmove end - local.get $6 - local.get $4 - i32.gt_s + local.get $7 + local.get $5 + i32.gt_u if - local.get $6 - i32.const 1 - i32.sub - local.get $4 - i32.div_s - local.set $9 - local.get $9 - local.get $4 - i32.mul - local.set $8 - local.get $6 - local.get $8 - i32.sub - local.set $5 local.get $7 + i32.const 2 + i32.sub + local.get $5 + i32.div_u + local.set $10 + local.get $10 + local.get $5 + i32.mul + local.set $9 + local.get $7 + local.get $9 + i32.sub + local.set $6 + local.get $8 local.get $3 - i32.const 1 - i32.shl i32.add local.get $2 - local.get $2 - call $~lib/runtime/StringBase#get:length - i32.const 1 - i32.shl - local.get $9 - call $~lib/runtime/memory.repeat local.get $5 + local.get $10 + call $~lib/runtime/memory.repeat + local.get $6 if - local.get $7 local.get $8 local.get $3 i32.add - i32.const 1 - i32.shl + local.get $9 i32.add - local.set $10 - local.get $2 local.set $11 - local.get $5 - i32.const 1 - i32.shl + local.get $2 local.set $12 - local.get $10 + local.get $6 + local.set $13 local.get $11 local.get $12 + local.get $13 call $~lib/internal/memory/memmove end else - local.get $7 - local.get $3 - i32.const 1 - i32.shl - i32.add - local.set $5 - local.get $2 - local.set $8 - local.get $6 - i32.const 1 - i32.shl - local.set $9 - local.get $5 local.get $8 + local.get $3 + i32.add + local.set $6 + local.get $2 + local.set $9 + local.get $7 + local.set $10 + local.get $6 local.get $9 + local.get $10 call $~lib/internal/memory/memmove end block $~lib/runtime/REGISTER|inlined.3 (result i32) - local.get $7 - local.set $9 - local.get $9 + local.get $8 + local.set $10 + local.get $10 call $~lib/runtime/unref i32.const 1 i32.store - local.get $9 + local.get $10 end ) (func $~lib/string/String#padEnd|trampoline (; 23 ;) (type $FUNCSIG$iiii) (param $0 i32) (param $1 i32) (param $2 i32) (result i32) @@ -3436,7 +3427,7 @@ if i32.const 0 i32.const 96 - i32.const 597 + i32.const 596 i32.const 10 call $~lib/env/abort unreachable @@ -3755,7 +3746,7 @@ if i32.const 0 i32.const 96 - i32.const 355 + i32.const 354 i32.const 4 call $~lib/env/abort unreachable @@ -3783,7 +3774,7 @@ if i32.const 0 i32.const 96 - i32.const 360 + i32.const 359 i32.const 6 call $~lib/env/abort unreachable @@ -4298,7 +4289,7 @@ if i32.const 0 i32.const 96 - i32.const 382 + i32.const 381 i32.const 4 call $~lib/env/abort unreachable