Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xxadev
vscode
提交
932a27d9
V
vscode
项目概览
xxadev
/
vscode
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
V
vscode
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
932a27d9
编写于
8月 14, 2020
作者:
M
Martin Aeschlimann
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Unexpected hash collision. Fixes #86584
上级
b3ec3fb4
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
31 addition
and
9 deletion
+31
-9
src/vs/base/common/hash.ts
src/vs/base/common/hash.ts
+11
-6
src/vs/base/test/common/hash.test.ts
src/vs/base/test/common/hash.test.ts
+20
-3
未找到文件。
src/vs/base/common/hash.ts
浏览文件 @
932a27d9
...
...
@@ -8,7 +8,12 @@ import * as strings from 'vs/base/common/strings';
/**
* Return a hash value for an object.
*/
export
function
hash
(
obj
:
any
,
hashVal
=
0
):
number
{
export
function
hash
(
obj
:
any
):
number
{
return
doHash
(
obj
,
0
);
}
export
function
doHash
(
obj
:
any
,
hashVal
:
number
):
number
{
switch
(
typeof
obj
)
{
case
'
object
'
:
if
(
obj
===
null
)
{
...
...
@@ -24,9 +29,9 @@ export function hash(obj: any, hashVal = 0): number {
case
'
number
'
:
return
numberHash
(
obj
,
hashVal
);
case
'
undefined
'
:
return
numberHash
(
0
,
937
);
return
numberHash
(
937
,
hashVal
);
default
:
return
numberHash
(
0
,
617
);
return
numberHash
(
617
,
hashVal
);
}
}
...
...
@@ -48,14 +53,14 @@ export function stringHash(s: string, hashVal: number) {
function
arrayHash
(
arr
:
any
[],
initialHashVal
:
number
):
number
{
initialHashVal
=
numberHash
(
104579
,
initialHashVal
);
return
arr
.
reduce
((
hashVal
,
item
)
=>
h
ash
(
item
,
hashVal
),
initialHashVal
);
return
arr
.
reduce
((
hashVal
,
item
)
=>
doH
ash
(
item
,
hashVal
),
initialHashVal
);
}
function
objectHash
(
obj
:
any
,
initialHashVal
:
number
):
number
{
initialHashVal
=
numberHash
(
181387
,
initialHashVal
);
return
Object
.
keys
(
obj
).
sort
().
reduce
((
hashVal
,
key
)
=>
{
hashVal
=
stringHash
(
key
,
hashVal
);
return
h
ash
(
obj
[
key
],
hashVal
);
return
doH
ash
(
obj
[
key
],
hashVal
);
},
initialHashVal
);
}
...
...
@@ -68,7 +73,7 @@ export class Hasher {
}
hash
(
obj
:
any
):
number
{
this
.
_value
=
h
ash
(
obj
,
this
.
_value
);
this
.
_value
=
doH
ash
(
obj
,
this
.
_value
);
return
this
.
_value
;
}
}
...
...
src/vs/base/test/common/hash.test.ts
浏览文件 @
932a27d9
...
...
@@ -32,12 +32,18 @@ suite('Hash', () => {
assert
.
equal
(
hash
([
1
,
2
,
3
]),
hash
([
1
,
2
,
3
]));
assert
.
equal
(
hash
([
'
foo
'
,
'
bar
'
]),
hash
([
'
foo
'
,
'
bar
'
]));
assert
.
equal
(
hash
([]),
hash
([]));
assert
.
equal
(
hash
([]),
hash
(
new
Array
()));
assert
.
notEqual
(
hash
([
'
foo
'
,
'
bar
'
]),
hash
([
'
bar
'
,
'
foo
'
]));
assert
.
notEqual
(
hash
([
'
foo
'
,
'
bar
'
]),
hash
([
'
bar
'
,
'
foo
'
,
null
]));
assert
.
notEqual
(
hash
([
'
foo
'
,
'
bar
'
,
null
]),
hash
([
'
bar
'
,
'
foo
'
,
null
]));
assert
.
notEqual
(
hash
([
'
foo
'
,
'
bar
'
]),
hash
([
'
bar
'
,
'
foo
'
,
undefined
]));
assert
.
notEqual
(
hash
([
'
foo
'
,
'
bar
'
,
undefined
]),
hash
([
'
bar
'
,
'
foo
'
,
undefined
]));
assert
.
notEqual
(
hash
([
'
foo
'
,
'
bar
'
,
null
]),
hash
([
'
foo
'
,
'
bar
'
,
undefined
]));
});
test
(
'
object
'
,
()
=>
{
assert
.
equal
(
hash
({}),
hash
({}));
assert
.
equal
(
hash
({}),
hash
(
Object
.
create
(
null
)));
assert
.
equal
(
hash
({
'
foo
'
:
'
bar
'
}),
hash
({
'
foo
'
:
'
bar
'
}));
assert
.
equal
(
hash
({
'
foo
'
:
'
bar
'
,
'
foo2
'
:
undefined
}),
hash
({
'
foo2
'
:
undefined
,
'
foo
'
:
'
bar
'
}));
assert
.
notEqual
(
hash
({
'
foo
'
:
'
bar
'
}),
hash
({
'
foo
'
:
'
bar2
'
}));
...
...
@@ -45,14 +51,25 @@ suite('Hash', () => {
});
test
(
'
array - unexpected collision
'
,
function
()
{
this
.
skip
();
const
a
=
hash
([
undefined
,
undefined
,
undefined
,
undefined
,
undefined
]);
const
b
=
hash
([
undefined
,
undefined
,
'
HHHHHH
'
,
[{
line
:
0
,
character
:
0
},
{
line
:
0
,
character
:
0
}],
undefined
]);
// console.log(a);
// console.log(b);
assert
.
notEqual
(
a
,
b
);
});
test
(
'
all different
'
,
()
=>
{
const
candidates
:
any
[]
=
[
null
,
undefined
,
{},
[],
0
,
false
,
true
,
''
,
'
'
,
[
null
],
[
undefined
],
[
undefined
,
undefined
],
{
''
:
undefined
},
{
[
'
'
]:
undefined
}
];
const
hashes
:
number
[]
=
candidates
.
map
(
hash
);
for
(
let
i
=
0
;
i
<
hashes
.
length
;
i
++
)
{
assert
.
equal
(
hashes
[
i
],
hash
(
candidates
[
i
]));
// verify that repeated invocation returns the same hash
for
(
let
k
=
i
+
1
;
k
<
hashes
.
length
;
k
++
)
{
assert
.
notEqual
(
hashes
[
i
],
hashes
[
k
],
`Same hash
${
hashes
[
i
]}
for
${
JSON
.
stringify
(
candidates
[
i
])}
ans
${
JSON
.
stringify
(
candidates
[
k
])}
`
);
}
}
});
function
checkSHA1
(
strings
:
string
[],
expected
:
string
)
{
const
hash
=
new
StringSHA1
();
for
(
const
str
of
strings
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录