Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(449)

Unified Diff: pkg/dart_messages/bin/message_id.dart

Issue 1514333002: Add dart_messages package. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Add the converted shared_messages.json. Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/dart_messages/bin/json_converter.dart ('k') | pkg/dart_messages/lib/shared_messages.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/dart_messages/bin/message_id.dart
diff --git a/pkg/dart_messages/bin/message_id.dart b/pkg/dart_messages/bin/message_id.dart
new file mode 100644
index 0000000000000000000000000000000000000000..2d7ed5387cfb9c349b1b6f4e82d91fe8384c8eb7
--- /dev/null
+++ b/pkg/dart_messages/bin/message_id.dart
@@ -0,0 +1,35 @@
+// Copyright (c) 2015, 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.
+
+import 'dart:math' as math;
+
+import '../lib/shared_messages.dart' as shared_messages;
+
+math.Random random = new math.Random();
+
+const idLength = 6;
+final $A = "A".codeUnitAt(0);
+final $Z = "Z".codeUnitAt(0);
+
+String computeId() {
+ List charCodes = [];
+ for (int i = 0; i < idLength; i++) {
+ charCodes.add($A + random.nextInt($Z - $A));
+ }
+ return new String.fromCharCodes(charCodes);
+}
+
+/// Computes a random message ID that hasn't been used before.
+void main() {
+ var usedIds =
+ shared_messages.MESSAGES.values.map((entry) => entry['id']).toSet();
+
+ print("${usedIds.length} existing ids");
+
+ var newId;
+ do {
+ newId = computeId();
+ } while (!usedIds.contains(newId));
+ print("Available id: $newId");
+}
« no previous file with comments | « pkg/dart_messages/bin/json_converter.dart ('k') | pkg/dart_messages/lib/shared_messages.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698