| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011, 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 //String.prototype.get$length = function() { | |
| 6 // return this.length; | |
| 7 //} | |
| 8 | |
| 9 // TODO(jimhug): Unify with code from compiler/lib/implementation. | |
| 10 class StringImplementation implements String native "String" { | |
| 11 | |
| 12 String operator[](int index) native; | |
| 13 | |
| 14 int charCodeAt(int index) native; | |
| 15 | |
| 16 final int length; //native since it's on a native type. | |
| 17 | |
| 18 bool operator ==(var other) native; | |
| 19 | |
| 20 bool endsWith(String other) native ''' | |
| 21 'use strict'; | |
| 22 if (other.length > this.length) return false; | |
| 23 return other == this.substring(this.length - other.length);'''; | |
| 24 | |
| 25 bool startsWith(String other) native ''' | |
| 26 'use strict'; | |
| 27 if (other.length > this.length) return false; | |
| 28 return other == this.substring(0, other.length);'''; | |
| 29 | |
| 30 int indexOf(String other, [int start]) native; | |
| 31 int lastIndexOf(String other, [int start]) native; | |
| 32 | |
| 33 bool isEmpty() => length == 0; | |
| 34 | |
| 35 String concat(String other) native; | |
| 36 | |
| 37 String operator +(Object obj) native { obj.toString(); } | |
| 38 | |
| 39 String substring(int startIndex, [int endIndex = null]) native; | |
| 40 | |
| 41 String trim() native; | |
| 42 | |
| 43 // TODO(jmesserly): should support pattern too. | |
| 44 bool contains(Pattern pattern, [int startIndex]) native | |
| 45 "'use strict'; return this.indexOf(pattern, startIndex) >= 0;"; | |
| 46 | |
| 47 String _replaceFirst(String from, String to) native | |
| 48 "'use strict';return this.replace(from, to);"; | |
| 49 | |
| 50 String _replaceRegExp(RegExp from, String to) native | |
| 51 "'use strict';return this.replace(from.re, to);"; | |
| 52 | |
| 53 String replaceFirst(Pattern from, String to) { | |
| 54 if (from is String) return _replaceFirst(from, to); | |
| 55 if (from is RegExp) return _replaceRegExp(from, to); | |
| 56 for (var match in from.allMatches(this)) { | |
| 57 // We just care about the first match | |
| 58 return substring(0, match.start()) + to + substring(match.end()); | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 String _replaceAll(String from, String to) native @""" | |
| 63 'use strict'; | |
| 64 from = new RegExp(from.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'g'); | |
| 65 to = to.replace(/\$/g, '$$$$'); // Escape sequences are fun! | |
| 66 return this.replace(from, to);"""; | |
| 67 | |
| 68 String replaceAll(Pattern from, String to) { | |
| 69 if (from is String) return _replaceAll(from, to); | |
| 70 if (from is RegExp) return _replaceRegExp(from.dynamic._global, to); | |
| 71 var buffer = new StringBuffer(); | |
| 72 var lastMatchEnd = 0; | |
| 73 for (var match in from.allMatches(this)) { | |
| 74 buffer.add(substring(lastMatchEnd, match.start())); | |
| 75 buffer.add(to); | |
| 76 lastMatchEnd = match.end(); | |
| 77 } | |
| 78 buffer.add(substring(lastMatchEnd)); | |
| 79 } | |
| 80 | |
| 81 // TODO(jimhug): Get correct reified generic list here. | |
| 82 List<String> split(Pattern pattern) { | |
| 83 if (pattern is String) return _split(pattern); | |
| 84 if (pattern is RegExp) return _splitRegExp(pattern); | |
| 85 throw "String.split(Pattern) unimplemented."; | |
| 86 } | |
| 87 | |
| 88 List<String> _split(String pattern) native | |
| 89 "'use strict'; return this.split(pattern);"; | |
| 90 | |
| 91 List<String> _splitRegExp(RegExp pattern) native | |
| 92 "'use strict'; return this.split(pattern.re);"; | |
| 93 | |
| 94 Iterable<Match> allMatches(String str) { | |
| 95 throw "String.allMatches(String str) unimplemented."; | |
| 96 } | |
| 97 /* | |
| 98 Iterable<Match> allMatches(String str) { | |
| 99 List<Match> result = []; | |
| 100 if (this.isEmpty()) return result; | |
| 101 int length = this.length; | |
| 102 | |
| 103 int ix = 0; | |
| 104 while (ix < str.length) { | |
| 105 int foundIx = str.indexOf(this, ix); | |
| 106 if (foundIx < 0) break; | |
| 107 // Call "toString" to coerce the "this" back to a primitive string. | |
| 108 result.add(new _StringMatch(foundIx, str, this.toString())); | |
| 109 ix = foundIx + length; | |
| 110 } | |
| 111 return result; | |
| 112 } | |
| 113 */ | |
| 114 | |
| 115 List<String> splitChars() => split(''); | |
| 116 | |
| 117 List<int> charCodes() { | |
| 118 int len = length; | |
| 119 List<int> result = new List<int>(len); | |
| 120 for (int i = 0; i < len; i++) { | |
| 121 result[i] = charCodeAt(i); | |
| 122 } | |
| 123 return result; | |
| 124 } | |
| 125 | |
| 126 String toLowerCase() native; | |
| 127 String toUpperCase() native; | |
| 128 | |
| 129 // TODO(jmesserly): we might want to optimize this further. | |
| 130 // This is the [Jenkins hash function][1], but with masking to keep the | |
| 131 // hash in the Smi range. I did some simple microbenchmarks to verify that | |
| 132 // this performs adequately on the standard words list. Letting it spill over | |
| 133 // into doubles and truncating at the end was ~2x worse, letting it box was | |
| 134 // ~70x worse. | |
| 135 // | |
| 136 // [1]: http://en.wikipedia.org/wiki/Jenkins_hash_function | |
| 137 int hashCode() native ''' | |
| 138 'use strict'; | |
| 139 var hash = 0; | |
| 140 for (var i = 0; i < this.length; i++) { | |
| 141 hash = 0x1fffffff & (hash + this.charCodeAt(i)); | |
| 142 hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10)); | |
| 143 hash ^= hash >> 6; | |
| 144 } | |
| 145 | |
| 146 hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3)); | |
| 147 hash ^= hash >> 11; | |
| 148 return 0x1fffffff & (hash + ((0x00003fff & hash) << 15));'''; | |
| 149 | |
| 150 int compareTo(String other) native | |
| 151 "'use strict'; return this == other ? 0 : this < other ? -1 : 1;"; | |
| 152 } | |
| 153 | |
| 154 /* | |
| 155 class _StringMatch implements Match { | |
| 156 const _StringMatch(int this._start, | |
| 157 String this.str, | |
| 158 String this.pattern); | |
| 159 | |
| 160 int start() => _start; | |
| 161 int end() => _start + pattern.length; | |
| 162 String operator[](int g) => group(g); | |
| 163 int groupCount() => 0; | |
| 164 | |
| 165 String group(int group) { | |
| 166 if (group != 0) { | |
| 167 throw new IndexOutOfRangeException(group); | |
| 168 } | |
| 169 return pattern; | |
| 170 } | |
| 171 | |
| 172 List<String> groups(List<int> groups) { | |
| 173 List<String> result = new List<String>(); | |
| 174 for (int g in groups) { | |
| 175 result.add(group(g)); | |
| 176 } | |
| 177 return result; | |
| 178 } | |
| 179 | |
| 180 final int _start; | |
| 181 final String str; | |
| 182 final String pattern; | |
| 183 } | |
| 184 */ | |
| OLD | NEW |