This repository has been archived by the owner on Jun 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from dart-lang/devoncarew_uuid
replace the uuid package with our own implementation
- Loading branch information
Showing
6 changed files
with
125 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
/** | ||
* A UUID generator library. | ||
*/ | ||
library usage.uuid; | ||
|
||
import 'dart:math' show Random; | ||
|
||
/** | ||
* A UUID generator. This will generate unique IDs in the format: | ||
* | ||
* f47ac10b-58cc-4372-a567-0e02b2c3d479 | ||
* | ||
* The generated uuids are 128 bit numbers encoded in a specific string format. | ||
* | ||
* For more information, see | ||
* http://en.wikipedia.org/wiki/Universally_unique_identifier. | ||
*/ | ||
class Uuid { | ||
Random _random = new Random(); | ||
|
||
/** | ||
* Generate a version 4 (random) uuid. This is a uuid scheme that only uses | ||
* random numbers as the source of the generated uuid. | ||
*/ | ||
String generateV4() { | ||
// Generate xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx / 8-4-4-4-12. | ||
int special = 8 + _random.nextInt(4); | ||
|
||
return | ||
'${_bitsDigits(16, 4)}${_bitsDigits(16, 4)}-' | ||
'${_bitsDigits(16, 4)}-' | ||
'4${_bitsDigits(12, 3)}-' | ||
'${_printDigits(special, 1)}${_bitsDigits(12, 3)}-' | ||
'${_bitsDigits(16, 4)}${_bitsDigits(16, 4)}${_bitsDigits(16, 4)}'; | ||
} | ||
|
||
String _bitsDigits(int bitCount, int digitCount) => | ||
_printDigits(_generateBits(bitCount), digitCount); | ||
|
||
int _generateBits(int bitCount) => _random.nextInt(1 << bitCount); | ||
|
||
String _printDigits(int value, int count) => | ||
value.toRadixString(16).padLeft(count, '0'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
library usage.uuid_test; | ||
|
||
import 'package:unittest/unittest.dart'; | ||
import 'package:usage/src/uuid.dart'; | ||
|
||
void defineTests() { | ||
group('uuid', () { | ||
// xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx | ||
test('simple', () { | ||
Uuid uuid = new Uuid(); | ||
String result = uuid.generateV4(); | ||
expect(result.length, 36); | ||
expect(result[8], '-'); | ||
expect(result[13], '-'); | ||
expect(result[18], '-'); | ||
expect(result[23], '-'); | ||
}); | ||
|
||
test('can parse', () { | ||
Uuid uuid = new Uuid(); | ||
String result = uuid.generateV4(); | ||
expect(int.parse(result.substring(0, 8), radix: 16), isNotNull); | ||
expect(int.parse(result.substring(9, 13), radix: 16), isNotNull); | ||
expect(int.parse(result.substring(14, 18), radix: 16), isNotNull); | ||
expect(int.parse(result.substring(19, 23), radix: 16), isNotNull); | ||
expect(int.parse(result.substring(24, 36), radix: 16), isNotNull); | ||
}); | ||
|
||
test('special bits', () { | ||
Uuid uuid = new Uuid(); | ||
String result = uuid.generateV4(); | ||
expect(result[14], '4'); | ||
expect(result[19].toLowerCase(), isIn('89ab')); | ||
|
||
result = uuid.generateV4(); | ||
expect(result[19].toLowerCase(), isIn('89ab')); | ||
|
||
result = uuid.generateV4(); | ||
expect(result[19].toLowerCase(), isIn('89ab')); | ||
}); | ||
|
||
test('is pretty random', () { | ||
Set set = new Set(); | ||
|
||
Uuid uuid = new Uuid(); | ||
for (int i = 0; i < 64; i++) { | ||
String val = uuid.generateV4(); | ||
expect(set, isNot(contains(val))); | ||
set.add(val); | ||
} | ||
|
||
uuid = new Uuid(); | ||
for (int i = 0; i < 64; i++) { | ||
String val = uuid.generateV4(); | ||
expect(set, isNot(contains(val))); | ||
set.add(val); | ||
} | ||
|
||
uuid = new Uuid(); | ||
for (int i = 0; i < 64; i++) { | ||
String val = uuid.generateV4(); | ||
expect(set, isNot(contains(val))); | ||
set.add(val); | ||
} | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters