| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 import 'dart:math' as math; | 5 import 'dart:math' as math; |
| 6 | 6 |
| 7 import '../lib/shared_messages.dart' as shared_messages; | 7 import '../lib/shared_messages.dart' as shared_messages; |
| 8 | 8 |
| 9 math.Random random = new math.Random(); | 9 math.Random random = new math.Random(); |
| 10 | 10 |
| 11 const idLength = 6; | 11 const idLength = 6; |
| 12 final $A = "A".codeUnitAt(0); | 12 final $A = "A".codeUnitAt(0); |
| 13 final $Z = "Z".codeUnitAt(0); | 13 final $Z = "Z".codeUnitAt(0); |
| 14 | 14 |
| 15 String computeId() { | 15 String computeId() { |
| 16 List charCodes = []; | 16 List charCodes = []; |
| 17 for (int i = 0; i < idLength; i++) { | 17 for (int i = 0; i < idLength; i++) { |
| 18 charCodes.add($A + random.nextInt($Z - $A)); | 18 charCodes.add($A + random.nextInt($Z - $A)); |
| 19 } | 19 } |
| 20 return new String.fromCharCodes(charCodes); | 20 return new String.fromCharCodes(charCodes); |
| 21 } | 21 } |
| 22 | 22 |
| 23 /// Computes a random message ID that hasn't been used before. | 23 /// Computes a random message ID that hasn't been used before. |
| 24 void main() { | 24 void main() { |
| 25 var usedIds = | 25 var usedIds = |
| 26 shared_messages.MESSAGES.values.map((entry) => entry['id']).toSet(); | 26 shared_messages.MESSAGES.values.map((entry) => entry.id).toSet(); |
| 27 | 27 |
| 28 print("${usedIds.length} existing ids"); | 28 print("${usedIds.length} existing ids"); |
| 29 | 29 |
| 30 var newId; | 30 var newId; |
| 31 do { | 31 do { |
| 32 newId = computeId(); | 32 newId = computeId(); |
| 33 } while (!usedIds.contains(newId)); | 33 } while (!usedIds.contains(newId)); |
| 34 print("Available id: $newId"); | 34 print("Available id: $newId"); |
| 35 } | 35 } |
| OLD | NEW |