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

Unified Diff: dart/runtime/lib/symbol_patch.dart

Issue 14142003: Add Symbol class. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Implement hashCode and equals Created 7 years, 8 months 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
Index: dart/runtime/lib/symbol_patch.dart
diff --git a/dart/runtime/lib/symbol_patch.dart b/dart/runtime/lib/symbol_patch.dart
new file mode 100644
index 0000000000000000000000000000000000000000..bf4184e7823109402ee6d6fbd828a28d2d08bfcf
--- /dev/null
+++ b/dart/runtime/lib/symbol_patch.dart
@@ -0,0 +1,35 @@
+// Copyright (c) 2013, 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.
+
+patch class Symbol {
+ final String _name;
+ /* patch */ const Symbol(String name)
+ : this._name = _validate(name);
+
+
+ static final RegExp _validationPattern =
+ new RegExp(r'^[a-zA-Z$][a-zA-Z$0-9_]*=?');
+
+ static _validate(String name) {
+ if (name is! String) throw new ArgumentError('name must be a String');
+ if (name.isEmpty) return;
+ if (name.startsWith('_')) {
+ throw new ArgumentError('"$name" is a private identifier');
+ }
+ if (!_validationPattern.hasMatch(name)) {
+ throw new ArgumentError(
+ '"$name" is not an identifier or an empty String');
+ }
+ return name;
+ }
+
+ /* patch */ bool operator ==(other) {
+ return _name == _name;
kasperl 2013/04/11 11:37:00 Shouldn't this be checking that other is a Symbol
ahe 2013/04/11 11:47:50 Done.
+ }
+
+ /* patch */ int get hashCode {
+ const arbitraryPrime = 664597;
+ return 0x1fffffff & (arbitraryPrime * _name.hashCode);
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698