| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 part of dart._collection.dev; | 5 part of dart._collection.dev; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Implementation of [core.Symbol]. This class uses the same name as | 8 * Implementation of [core.Symbol]. This class uses the same name as |
| 9 * a core class so a user can't tell the difference. | 9 * a core class so a user can't tell the difference. |
| 10 * | 10 * |
| 11 * The purpose of this class is to hide [_name] from user code, but | 11 * The purpose of this class is to hide [_name] from user code, but |
| 12 * make it accessible to Dart platform code via the static method | 12 * make it accessible to Dart platform code via the static method |
| 13 * [getName]. | 13 * [getName]. |
| 14 */ | 14 */ |
| 15 class Symbol implements core.Symbol { | 15 class Symbol implements core.Symbol { |
| 16 final String _name; | 16 final String _name; |
| 17 | 17 |
| 18 static final RegExp validationPattern = |
| 19 new RegExp(r'^(?:[a-zA-Z$][a-zA-Z$0-9_]*\.)*(?:[a-zA-Z$][a-zA-Z$0-9_]*=?|' |
| 20 r'-|' |
| 21 r'unary-|' |
| 22 r'\[\]=|' |
| 23 r'~|' |
| 24 r'==|' |
| 25 r'\[\]|' |
| 26 r'\*|' |
| 27 r'/|' |
| 28 r'%|' |
| 29 r'~/|' |
| 30 r'\+|' |
| 31 r'<<|' |
| 32 r'>>|' |
| 33 r'>=|' |
| 34 r'>|' |
| 35 r'<=|' |
| 36 r'<|' |
| 37 r'&|' |
| 38 r'\^|' |
| 39 r'\|' |
| 40 r')$'); |
| 41 |
| 18 external const Symbol(String name); | 42 external const Symbol(String name); |
| 19 | 43 |
| 20 /** | 44 /** |
| 21 * Platform-private method used by the mirror system to create | 45 * Platform-private method used by the mirror system to create |
| 22 * otherwise invalid names. | 46 * otherwise invalid names. |
| 23 */ | 47 */ |
| 24 const Symbol.unvalidated(this._name); | 48 const Symbol.unvalidated(this._name); |
| 25 | 49 |
| 50 // This is called by dart2js. |
| 51 Symbol.validated(String name) |
| 52 : this._name = validate(name); |
| 53 |
| 26 bool operator ==(other) => other is Symbol && _name == other._name; | 54 bool operator ==(other) => other is Symbol && _name == other._name; |
| 27 | 55 |
| 28 int get hashCode { | 56 int get hashCode { |
| 29 const arbitraryPrime = 664597; | 57 const arbitraryPrime = 664597; |
| 30 return 0x1fffffff & (arbitraryPrime * _name.hashCode); | 58 return 0x1fffffff & (arbitraryPrime * _name.hashCode); |
| 31 } | 59 } |
| 32 | 60 |
| 33 /// Platform-private accessor which cannot be called from user libraries. | 61 /// Platform-private accessor which cannot be called from user libraries. |
| 34 static String getName(Symbol symbol) => symbol._name; | 62 static String getName(Symbol symbol) => symbol._name; |
| 63 |
| 64 static String validate(String name) { |
| 65 if (name.isEmpty) return name; |
| 66 if (name.startsWith('_')) { |
| 67 throw new ArgumentError('"$name" is a private identifier'); |
| 68 } |
| 69 if (!validationPattern.hasMatch(name)) { |
| 70 throw new ArgumentError( |
| 71 '"$name" is not an identifier or an empty String'); |
| 72 } |
| 73 return name; |
| 74 } |
| 35 } | 75 } |
| OLD | NEW |