提交 a304998c 编写于 作者: J Johannes Rieken

debt - modernize uuid and remove unused stuff

上级 076c919a
......@@ -3,87 +3,6 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
/**
* Represents a UUID as defined by rfc4122.
*/
export interface UUID {
/**
* @returns the canonical representation in sets of hexadecimal numbers separated by dashes.
*/
asHex(): string;
}
class ValueUUID implements UUID {
constructor(public _value: string) {
// empty
}
public asHex(): string {
return this._value;
}
}
class V4UUID extends ValueUUID {
private static readonly _chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
private static readonly _timeHighBits = ['8', '9', 'a', 'b'];
private static _oneOf(array: string[]): string {
return array[Math.floor(array.length * Math.random())];
}
private static _randomHex(): string {
return V4UUID._oneOf(V4UUID._chars);
}
constructor() {
super([
V4UUID._randomHex(),
V4UUID._randomHex(),
V4UUID._randomHex(),
V4UUID._randomHex(),
V4UUID._randomHex(),
V4UUID._randomHex(),
V4UUID._randomHex(),
V4UUID._randomHex(),
'-',
V4UUID._randomHex(),
V4UUID._randomHex(),
V4UUID._randomHex(),
V4UUID._randomHex(),
'-',
'4',
V4UUID._randomHex(),
V4UUID._randomHex(),
V4UUID._randomHex(),
'-',
V4UUID._oneOf(V4UUID._timeHighBits),
V4UUID._randomHex(),
V4UUID._randomHex(),
V4UUID._randomHex(),
'-',
V4UUID._randomHex(),
V4UUID._randomHex(),
V4UUID._randomHex(),
V4UUID._randomHex(),
V4UUID._randomHex(),
V4UUID._randomHex(),
V4UUID._randomHex(),
V4UUID._randomHex(),
V4UUID._randomHex(),
V4UUID._randomHex(),
V4UUID._randomHex(),
V4UUID._randomHex(),
].join(''));
}
}
export function v4(): UUID {
return new V4UUID();
}
const _UUIDPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
......@@ -91,18 +10,52 @@ export function isUUID(value: string): boolean {
return _UUIDPattern.test(value);
}
/**
* Parses a UUID that is of the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
* @param value A uuid string.
*/
export function parse(value: string): UUID {
if (!isUUID(value)) {
throw new Error('invalid uuid');
}
return new ValueUUID(value);
// prep-work
const _data = new Uint8Array(16);
const _hex: string[] = [];
for (let i = 0; i < 256; i++) {
_hex.push(i.toString(16).padStart(2, '0'));
}
const _fillRandomValues = typeof crypto === 'object'
? crypto.getRandomValues.bind(crypto)
: function (bucket: Uint8Array): Uint8Array { // todo@jrieken node nodejs use `crypto#randomBytes`, see: https://nodejs.org/docs/latest/api/crypto.html#crypto_crypto_randombytes_size_callback
for (let i = 0; i < bucket.length; i++) {
bucket[i] = Math.floor(Math.random() * 256);
}
return bucket;
};
export function generateUuid(): string {
return v4().asHex();
// get data
_fillRandomValues(_data);
// set version bits
_data[6] = (_data[6] & 0x0f) | 0x40;
_data[8] = (_data[8] & 0x3f) | 0x80;
// print as string
let i = 0;
let result = '';
result += _hex[_data[i++]];
result += _hex[_data[i++]];
result += _hex[_data[i++]];
result += _hex[_data[i++]];
result += '-';
result += _hex[_data[i++]];
result += _hex[_data[i++]];
result += '-';
result += _hex[_data[i++]];
result += _hex[_data[i++]];
result += '-';
result += _hex[_data[i++]];
result += _hex[_data[i++]];
result += '-';
result += _hex[_data[i++]];
result += _hex[_data[i++]];
result += _hex[_data[i++]];
result += _hex[_data[i++]];
result += _hex[_data[i++]];
result += _hex[_data[i++]];
return result;
}
......@@ -7,16 +7,17 @@ import * as uuid from 'vs/base/common/uuid';
suite('UUID', () => {
test('generation', () => {
const asHex = uuid.v4().asHex();
const asHex = uuid.generateUuid();
assert.equal(asHex.length, 36);
assert.equal(asHex[14], '4');
assert.ok(asHex[19] === '8' || asHex[19] === '9' || asHex[19] === 'a' || asHex[19] === 'b');
});
test('parse', () => {
const id = uuid.v4();
const asHext = id.asHex();
const id2 = uuid.parse(asHext);
assert.equal(id.asHex(), id2.asHex());
test('self-check', function () {
const t1 = Date.now();
while (Date.now() - t1 < 50) {
const value = uuid.generateUuid();
assert.ok(uuid.isUUID(value));
}
});
});
......@@ -514,11 +514,7 @@ function assertProblemMatcher(actual: string | ProblemMatcher, expected: string
}
if (typeof actual !== 'string' && typeof expected !== 'string') {
if (expected.owner === ProblemMatcherBuilder.DEFAULT_UUID) {
try {
UUID.parse(actual.owner);
} catch (err) {
assert.fail(actual.owner, 'Owner must be a UUID');
}
assert.ok(UUID.isUUID(actual.owner), 'Owner must be a UUID');
} else {
assert.strictEqual(actual.owner, expected.owner);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册