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