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 {
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<usize>(this), <usize>length << 1);
}
if (len > padLen) {
let count = (len - 1) / padLen;
let base = count * padLen;
let rest = len - base;
memory.repeat(out + (<usize>length << 1), changetype<usize>(padString), <usize>padString.length << 1, count);
if (rest) {
memory.copy(out + ((<usize>base + <usize>length) << 1), changetype<usize>(padString), <usize>rest << 1);
var thisSize = <usize>this.length << 1;
var targetSize = <usize>targetLength << 1;
var padSize = <usize>padString.length << 1;
if (targetSize < thisSize || !padSize) return this;
var appendSize = targetSize - thisSize;
var out = ALLOC(targetSize);
memory.copy(out, changetype<usize>(this), thisSize);
if (appendSize > padSize) {
let repeatCount = (appendSize - 2) / padSize;
let restBase = repeatCount * padSize;
let restSize = appendSize - restBase;
memory.repeat(out + thisSize, changetype<usize>(padString), padSize, repeatCount);
if (restSize) {
memory.copy(out + thisSize + restBase, changetype<usize>(padString), restSize);
}
} 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);
}

View File

@ -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.tee $6
local.get $0
local.get $3
i32.const 1
i32.shl
local.get $5
call $~lib/internal/memory/memmove
end
local.get $6
local.get $5
i32.gt_s
if
local.get $3
i32.const 1
i32.shl
local.get $1
local.get $5
i32.sub
local.tee $3
local.get $4
i32.gt_u
if
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.get $3
i32.const 2
i32.sub
local.get $4
i32.div_u
local.tee $0
i32.sub
local.tee $4
if
local.get $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 $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

View File

@ -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
local.get $3
i32.lt_s
local.tee $5
if (result i32)
local.get $5
else
i32.const 1
i32.shl
local.set $5
local.get $4
local.get $3
i32.lt_u
local.tee $6
if (result i32)
local.get $6
else
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.get $4
call $~lib/runtime/ALLOC
local.set $8
local.get $3
i32.const 1
i32.shl
local.set $9
local.get $5
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
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 $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
i32.gt_u
if
local.get $7
local.get $8
local.get $3
i32.add
i32.const 1
i32.shl
i32.add
i32.const 2
i32.sub
local.get $5
i32.div_u
local.set $10
local.get $2
local.set $11
local.get $5
i32.const 1
i32.shl
local.set $12
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.add
local.get $2
local.get $5
local.get $10
call $~lib/runtime/memory.repeat
local.get $6
if
local.get $8
local.get $3
i32.add
local.get $9
i32.add
local.set $11
local.get $2
local.set $12
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<String>|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