| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /// A UUID generator library. |
| 6 * A UUID generator library. | 6 library uuid; |
| 7 */ | |
| 8 library usage.uuid; | |
| 9 | 7 |
| 10 import 'dart:math' show Random; | 8 import 'dart:math' show Random; |
| 11 | 9 |
| 12 /** | 10 /// A UUID generator. |
| 13 * A UUID generator. This will generate unique IDs in the format: | 11 /// |
| 14 * | 12 /// This will generate unique IDs in the format: |
| 15 * f47ac10b-58cc-4372-a567-0e02b2c3d479 | 13 /// |
| 16 * | 14 /// f47ac10b-58cc-4372-a567-0e02b2c3d479 |
| 17 * The generated uuids are 128 bit numbers encoded in a specific string format. | 15 /// |
| 18 * | 16 /// The generated uuids are 128 bit numbers encoded in a specific string format. |
| 19 * For more information, see | 17 /// For more information, see |
| 20 * http://en.wikipedia.org/wiki/Universally_unique_identifier. | 18 /// [en.wikipedia.org/wiki/Universally_unique_identifier](http://en.wikipedia.or
g/wiki/Universally_unique_identifier). |
| 21 */ | |
| 22 class Uuid { | 19 class Uuid { |
| 23 Random _random = new Random(); | 20 final Random _random = new Random(); |
| 24 | 21 |
| 25 /** | 22 /// Generate a version 4 (random) uuid. This is a uuid scheme that only uses |
| 26 * Generate a version 4 (random) uuid. This is a uuid scheme that only uses | 23 /// random numbers as the source of the generated uuid. |
| 27 * random numbers as the source of the generated uuid. | |
| 28 */ | |
| 29 String generateV4() { | 24 String generateV4() { |
| 30 // Generate xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx / 8-4-4-4-12. | 25 // Generate xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx / 8-4-4-4-12. |
| 31 int special = 8 + _random.nextInt(4); | 26 int special = 8 + _random.nextInt(4); |
| 32 | 27 |
| 33 return | 28 return '${_bitsDigits(16, 4)}${_bitsDigits(16, 4)}-' |
| 34 '${_bitsDigits(16, 4)}${_bitsDigits(16, 4)}-' | |
| 35 '${_bitsDigits(16, 4)}-' | 29 '${_bitsDigits(16, 4)}-' |
| 36 '4${_bitsDigits(12, 3)}-' | 30 '4${_bitsDigits(12, 3)}-' |
| 37 '${_printDigits(special, 1)}${_bitsDigits(12, 3)}-' | 31 '${_printDigits(special, 1)}${_bitsDigits(12, 3)}-' |
| 38 '${_bitsDigits(16, 4)}${_bitsDigits(16, 4)}${_bitsDigits(16, 4)}'; | 32 '${_bitsDigits(16, 4)}${_bitsDigits(16, 4)}${_bitsDigits(16, 4)}'; |
| 39 } | 33 } |
| 40 | 34 |
| 41 String _bitsDigits(int bitCount, int digitCount) => | 35 String _bitsDigits(int bitCount, int digitCount) => |
| 42 _printDigits(_generateBits(bitCount), digitCount); | 36 _printDigits(_generateBits(bitCount), digitCount); |
| 43 | 37 |
| 44 int _generateBits(int bitCount) => _random.nextInt(1 << bitCount); | 38 int _generateBits(int bitCount) => _random.nextInt(1 << bitCount); |
| 45 | 39 |
| 46 String _printDigits(int value, int count) => | 40 String _printDigits(int value, int count) => |
| 47 value.toRadixString(16).padLeft(count, '0'); | 41 value.toRadixString(16).padLeft(count, '0'); |
| 48 } | 42 } |
| OLD | NEW |