unshiftify padEnd

This commit is contained in:
dcode 2019-03-11 01:16:05 +01:00
parent f076826e59
commit ce82e5458e
3 changed files with 133 additions and 161 deletions

View File

@ -329,24 +329,23 @@ export class String extends StringBase {
padEnd(targetLength: i32, padString: String = changetype<String>(" ")): String { padEnd(targetLength: i32, padString: String = changetype<String>(" ")): String {
assert(this !== null); assert(this !== null);
var length = this.length; var thisSize = <usize>this.length << 1;
var padLen = padString.length; var targetSize = <usize>targetLength << 1;
if (targetLength < length || !padLen) return this; var padSize = <usize>padString.length << 1;
var len = targetLength - length; if (targetSize < thisSize || !padSize) return this;
var out = ALLOC(targetLength << 1); var appendSize = targetSize - thisSize;
if (length) { var out = ALLOC(targetSize);
memory.copy(out, changetype<usize>(this), <usize>length << 1); memory.copy(out, changetype<usize>(this), thisSize);
} if (appendSize > padSize) {
if (len > padLen) { let repeatCount = (appendSize - 2) / padSize;
let count = (len - 1) / padLen; let restBase = repeatCount * padSize;
let base = count * padLen; let restSize = appendSize - restBase;
let rest = len - base; memory.repeat(out + thisSize, changetype<usize>(padString), padSize, repeatCount);
memory.repeat(out + (<usize>length << 1), changetype<usize>(padString), <usize>padString.length << 1, count); if (restSize) {
if (rest) { memory.copy(out + thisSize + restBase, changetype<usize>(padString), restSize);
memory.copy(out + ((<usize>base + <usize>length) << 1), changetype<usize>(padString), <usize>rest << 1);
} }
} else { } else {
memory.copy(out + (<usize>length << 1), changetype<usize>(padString), <usize>len << 1); memory.copy(out + thisSize, changetype<usize>(padString), appendSize);
} }
return REGISTER<String>(out); return REGISTER<String>(out);
} }

View File

@ -2170,21 +2170,28 @@
i32.load offset=4 i32.load offset=4
i32.const 1 i32.const 1
i32.shr_u i32.shr_u
local.set $5 i32.const 1
i32.shl
local.set $4
local.get $1 local.get $1
i32.const 1
i32.shl
local.tee $1
local.get $0 local.get $0
i32.const 8 i32.const 8
i32.sub i32.sub
i32.load offset=4 i32.load offset=4
i32.const 1 i32.const 1
i32.shr_u i32.shr_u
i32.const 1
i32.shl
local.tee $5
i32.lt_u
local.tee $3 local.tee $3
i32.lt_s
local.tee $4
if (result i32) if (result i32)
local.get $4 local.get $3
else else
local.get $5 local.get $4
i32.eqz i32.eqz
end end
if if
@ -2192,86 +2199,61 @@
return return
end end
local.get $1 local.get $1
local.get $3
i32.sub
local.set $6
local.get $1
i32.const 1
i32.shl
call $~lib/runtime/ALLOC call $~lib/runtime/ALLOC
local.set $1 local.tee $6
local.get $3 local.get $0
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.get $5 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 if
local.get $3 local.get $5
i32.const 1 local.get $6
i32.shl
local.get $1
i32.add i32.add
local.get $2 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 $4
local.get $5 local.get $3
i32.mul i32.const 2
local.tee $0
i32.sub 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 if
local.get $0 local.get $5
local.get $3 local.get $6
i32.add i32.add
i32.const 1
i32.shl
local.get $1 local.get $1
i32.add i32.add
local.get $2 local.get $2
local.get $4 local.get $3
i32.const 1
i32.shl
call $~lib/internal/memory/memmove call $~lib/internal/memory/memmove
end end
else else
local.get $3 local.get $5
i32.const 1 local.get $6
i32.shl
local.get $1
i32.add i32.add
local.get $2 local.get $2
local.get $6 local.get $3
i32.const 1
i32.shl
call $~lib/internal/memory/memmove call $~lib/internal/memory/memmove
end end
local.get $1 local.get $6
local.tee $0
call $~lib/runtime/unref call $~lib/runtime/unref
i32.const 1 i32.const 1
i32.store 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) (func $~lib/string/String#padEnd|trampoline (; 19 ;) (type $FUNCSIG$iii) (param $0 i32) (param $1 i32) (result i32)
(local $2 i32) (local $2 i32)
@ -2745,7 +2727,7 @@
if if
i32.const 0 i32.const 0
i32.const 96 i32.const 96
i32.const 597 i32.const 596
i32.const 10 i32.const 10
call $~lib/env/abort call $~lib/env/abort
unreachable unreachable
@ -3029,7 +3011,7 @@
if if
i32.const 0 i32.const 0
i32.const 96 i32.const 96
i32.const 355 i32.const 354
i32.const 4 i32.const 4
call $~lib/env/abort call $~lib/env/abort
unreachable unreachable
@ -3059,7 +3041,7 @@
if if
i32.const 0 i32.const 0
i32.const 96 i32.const 96
i32.const 360 i32.const 359
i32.const 6 i32.const 6
call $~lib/env/abort call $~lib/env/abort
unreachable unreachable
@ -3420,7 +3402,7 @@
if if
i32.const 0 i32.const 0
i32.const 96 i32.const 96
i32.const 382 i32.const 381
i32.const 4 i32.const 4
call $~lib/env/abort call $~lib/env/abort
unreachable unreachable

View File

@ -2736,6 +2736,7 @@
(local $10 i32) (local $10 i32)
(local $11 i32) (local $11 i32)
(local $12 i32) (local $12 i32)
(local $13 i32)
local.get $0 local.get $0
i32.const 0 i32.const 0
i32.ne i32.ne
@ -2750,125 +2751,115 @@
end end
local.get $0 local.get $0
call $~lib/runtime/StringBase#get:length call $~lib/runtime/StringBase#get:length
i32.const 1
i32.shl
local.set $3 local.set $3
local.get $1
i32.const 1
i32.shl
local.set $4
local.get $2 local.get $2
call $~lib/runtime/StringBase#get:length call $~lib/runtime/StringBase#get:length
local.set $4 i32.const 1
local.get $1 i32.shl
local.set $5
local.get $4
local.get $3 local.get $3
i32.lt_s i32.lt_u
local.tee $5 local.tee $6
if (result i32) if (result i32)
local.get $5 local.get $6
else else
local.get $4 local.get $5
i32.eqz i32.eqz
end end
if if
local.get $0 local.get $0
return return
end end
local.get $1 local.get $4
local.get $3 local.get $3
i32.sub i32.sub
local.set $6
local.get $1
i32.const 1
i32.shl
call $~lib/runtime/ALLOC
local.set $7 local.set $7
local.get $3 local.get $4
if call $~lib/runtime/ALLOC
local.get $7 local.set $8
local.set $5 block $~lib/runtime/memory.copy|inlined.4
local.get $0
local.set $8
local.get $3
i32.const 1
i32.shl
local.set $9
local.get $5
local.get $8 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 $9
local.get $10
call $~lib/internal/memory/memmove call $~lib/internal/memory/memmove
end end
local.get $6 local.get $7
local.get $4 local.get $5
i32.gt_s i32.gt_u
if 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 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 local.get $3
i32.const 1
i32.shl
i32.add i32.add
local.get $2 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 $5
local.get $10
call $~lib/runtime/memory.repeat
local.get $6
if if
local.get $7
local.get $8 local.get $8
local.get $3 local.get $3
i32.add i32.add
i32.const 1 local.get $9
i32.shl
i32.add i32.add
local.set $10
local.get $2
local.set $11 local.set $11
local.get $5 local.get $2
i32.const 1
i32.shl
local.set $12 local.set $12
local.get $10 local.get $6
local.set $13
local.get $11 local.get $11
local.get $12 local.get $12
local.get $13
call $~lib/internal/memory/memmove call $~lib/internal/memory/memmove
end end
else 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 $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 $9
local.get $10
call $~lib/internal/memory/memmove call $~lib/internal/memory/memmove
end end
block $~lib/runtime/REGISTER<String>|inlined.3 (result i32) block $~lib/runtime/REGISTER<String>|inlined.3 (result i32)
local.get $7 local.get $8
local.set $9 local.set $10
local.get $9 local.get $10
call $~lib/runtime/unref call $~lib/runtime/unref
i32.const 1 i32.const 1
i32.store i32.store
local.get $9 local.get $10
end end
) )
(func $~lib/string/String#padEnd|trampoline (; 23 ;) (type $FUNCSIG$iiii) (param $0 i32) (param $1 i32) (param $2 i32) (result i32) (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 if
i32.const 0 i32.const 0
i32.const 96 i32.const 96
i32.const 597 i32.const 596
i32.const 10 i32.const 10
call $~lib/env/abort call $~lib/env/abort
unreachable unreachable
@ -3755,7 +3746,7 @@
if if
i32.const 0 i32.const 0
i32.const 96 i32.const 96
i32.const 355 i32.const 354
i32.const 4 i32.const 4
call $~lib/env/abort call $~lib/env/abort
unreachable unreachable
@ -3783,7 +3774,7 @@
if if
i32.const 0 i32.const 0
i32.const 96 i32.const 96
i32.const 360 i32.const 359
i32.const 6 i32.const 6
call $~lib/env/abort call $~lib/env/abort
unreachable unreachable
@ -4298,7 +4289,7 @@
if if
i32.const 0 i32.const 0
i32.const 96 i32.const 96
i32.const 382 i32.const 381
i32.const 4 i32.const 4
call $~lib/env/abort call $~lib/env/abort
unreachable unreachable