Skip to content

fix: Fix wrong string comparision when sizes are differ #1583

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions std/assembly/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ import { Array } from "./array";
var rightLength = right.length;
if (!rightLength) return true;
// @ts-ignore: string <-> String
return compareImpl(left, 0, right, 0, min(leftLength, rightLength)) > 0;
var res = compareImpl(left, 0, right, 0, min(leftLength, rightLength));
return res ? res > 0 : leftLength > rightLength;
}

@operator(">=") private static __gte(left: String, right: String): bool {
Expand All @@ -136,7 +137,8 @@ import { Array } from "./array";
var leftLength = left.length;
if (!leftLength) return true;
// @ts-ignore: string <-> String
return compareImpl(left, 0, right, 0, min(leftLength, rightLength)) < 0;
var res = compareImpl(left, 0, right, 0, min(leftLength, rightLength));
return res ? res < 0 : leftLength < rightLength;
}

@operator("<=") private static __lte(left: String, right: String): bool {
Expand Down
5 changes: 3 additions & 2 deletions std/assembly/util/sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ export function COMPARATOR<T>(): (a: T, b: T) => i32 {
if (a === b || a === null || b === null) return 0;
var alen = changetype<string>(a).length;
var blen = changetype<string>(b).length;
if (!alen && !blen) return 0;
if (!(alen | blen)) return 0;
if (!alen) return -1;
if (!blen) return 1;
return compareImpl(changetype<string>(a), 0, changetype<string>(b), 0, <usize>min(alen, blen));
let res = compareImpl(changetype<string>(a), 0, changetype<string>(b), 0, <usize>min(alen, blen));
return res ? res : alen - blen;
};
} else {
return (a: T, b: T): i32 => (i32(a > b) - i32(a < b));
Expand Down
13 changes: 10 additions & 3 deletions tests/compiler/std/array.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -6315,6 +6315,7 @@
(func $~lib/util/sort/COMPARATOR<~lib/string/String|null>~anonymous|0 (param $0 i32) (param $1 i32) (result i32)
(local $2 i32)
(local $3 i32)
(local $4 i32)
local.get $0
call $~lib/rt/pure/__retain
local.set $0
Expand All @@ -6333,23 +6334,22 @@
select
select
br_if $folding-inner0
i32.const 0
local.get $1
i32.const 20
i32.sub
i32.load offset=16
i32.const 1
i32.shr_u
local.tee $2
i32.eqz
local.get $0
i32.const 20
i32.sub
i32.load offset=16
i32.const 1
i32.shr_u
local.tee $3
select
i32.or
i32.eqz
br_if $folding-inner0
local.get $3
i32.eqz
Expand Down Expand Up @@ -6380,10 +6380,17 @@
i32.gt_s
select
call $~lib/util/string/compareImpl
local.set $4
local.get $0
call $~lib/rt/pure/__release
local.get $1
call $~lib/rt/pure/__release
local.get $4
local.get $3
local.get $2
i32.sub
local.get $4
select
return
end
local.get $0
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/std/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1067,4 +1067,4 @@ export class ArrayU32 extends Array<u32> {}
export class ArrayU8 extends Array<u8> {}
export class ArrayStr extends Array<string> {}
// TODO:
// export class ArrayArrayI32 extends Array<Array<i32>> {}
// export class ArrayArrayI32 extends Array<Array<i32>> {}
57 changes: 36 additions & 21 deletions tests/compiler/std/array.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -11778,13 +11778,9 @@
call $~lib/string/String#get:length
local.set $4
local.get $3
local.get $4
i32.or
i32.eqz
if (result i32)
local.get $4
i32.eqz
else
i32.const 0
end
if
i32.const 0
local.set $2
Expand Down Expand Up @@ -11833,11 +11829,20 @@
select
call $~lib/util/string/compareImpl
local.set $2
local.get $2
if (result i32)
local.get $2
else
local.get $3
local.get $4
i32.sub
end
local.set $5
local.get $0
call $~lib/rt/pure/__release
local.get $1
call $~lib/rt/pure/__release
local.get $2
local.get $5
)
(func $std/array/assertSorted<~lib/string/String|null>@varargs (param $0 i32) (param $1 i32)
(local $2 i32)
Expand Down Expand Up @@ -12830,13 +12835,9 @@
call $~lib/string/String#get:length
local.set $4
local.get $3
local.get $4
i32.or
i32.eqz
if (result i32)
local.get $4
i32.eqz
else
i32.const 0
end
if
i32.const 0
local.set $2
Expand Down Expand Up @@ -12885,11 +12886,20 @@
select
call $~lib/util/string/compareImpl
local.set $2
local.get $2
if (result i32)
local.get $2
else
local.get $3
local.get $4
i32.sub
end
local.set $5
local.get $0
call $~lib/rt/pure/__release
local.get $1
call $~lib/rt/pure/__release
local.get $2
local.get $5
)
(func $std/array/assertSorted<~lib/string/String>@varargs (param $0 i32) (param $1 i32)
(local $2 i32)
Expand Down Expand Up @@ -32588,13 +32598,9 @@
call $~lib/string/String#get:length
local.set $4
local.get $3
local.get $4
i32.or
i32.eqz
if (result i32)
local.get $4
i32.eqz
else
i32.const 0
end
if
i32.const 0
local.set $2
Expand Down Expand Up @@ -32643,11 +32649,20 @@
select
call $~lib/util/string/compareImpl
local.set $2
local.get $2
if (result i32)
local.get $2
else
local.get $3
local.get $4
i32.sub
end
local.set $5
local.get $0
call $~lib/rt/pure/__release
local.get $1
call $~lib/rt/pure/__release
local.get $2
local.get $5
)
(func $~lib/array/Array<~lib/string/String>#sort@varargs (param $0 i32) (param $1 i32) (result i32)
(local $2 i32)
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/std/string-encoding.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2591,7 +2591,7 @@
if
i32.const 0
i32.const 1504
i32.const 740
i32.const 742
i32.const 7
call $~lib/builtins/abort
unreachable
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/std/string-encoding.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -4388,7 +4388,7 @@
if
i32.const 0
i32.const 480
i32.const 740
i32.const 742
i32.const 7
call $~lib/builtins/abort
unreachable
Expand Down
Loading