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