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

Side by Side Diff: runtime/vm/object.cc

Issue 11412258: Use the code point iterator when computing 16-bit string hash codes. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 #include "vm/object.h" 5 #include "vm/object.h"
6 6
7 #include "include/dart_api.h" 7 #include "include/dart_api.h"
8 #include "platform/assert.h" 8 #include "platform/assert.h"
9 #include "vm/assembler.h" 9 #include "vm/assembler.h"
10 #include "vm/bigint_operations.h" 10 #include "vm/bigint_operations.h"
(...skipping 9975 matching lines...) Expand 10 before | Expand all | Expand 10 after
9986 this->SetHash(result); 9986 this->SetHash(result);
9987 return result; 9987 return result;
9988 } 9988 }
9989 9989
9990 9990
9991 intptr_t String::Hash(const String& str, intptr_t begin_index, intptr_t len) { 9991 intptr_t String::Hash(const String& str, intptr_t begin_index, intptr_t len) {
9992 ASSERT(begin_index >= 0); 9992 ASSERT(begin_index >= 0);
9993 ASSERT(len >= 0); 9993 ASSERT(len >= 0);
9994 ASSERT((begin_index + len) <= str.Length()); 9994 ASSERT((begin_index + len) <= str.Length());
9995 StringHasher hasher; 9995 StringHasher hasher;
9996 for (intptr_t i = 0; i < len; i++) { 9996 CodePointIterator it(str, begin_index, len);
9997 hasher.Add(str.CharAt(begin_index + i)); 9997 while (it.Next()) {
9998 hasher.Add(it.Current());
9998 } 9999 }
9999 return hasher.Finalize(String::kHashBits); 10000 return hasher.Finalize(String::kHashBits);
10000 } 10001 }
10001 10002
10002 10003
10003 template<typename T> 10004 template<typename T>
10004 static intptr_t HashImpl(const T* characters, intptr_t len) { 10005 static intptr_t HashImpl(const T* characters, intptr_t len) {
10005 ASSERT(len >= 0); 10006 ASSERT(len >= 0);
10006 StringHasher hasher; 10007 StringHasher hasher;
10007 for (intptr_t i = 0; i < len; i++) { 10008 for (intptr_t i = 0; i < len; i++) {
10008 hasher.Add(characters[i]); 10009 hasher.Add(characters[i]);
10009 } 10010 }
10010 return hasher.Finalize(String::kHashBits); 10011 return hasher.Finalize(String::kHashBits);
10011 } 10012 }
10012 10013
10013 10014
10014 intptr_t String::Hash(const uint8_t* characters, intptr_t len) { 10015 intptr_t String::Hash(const uint8_t* characters, intptr_t len) {
10015 return HashImpl(characters, len); 10016 return HashImpl(characters, len);
10016 } 10017 }
10017 10018
10018 10019
10019 intptr_t String::Hash(const uint16_t* characters, intptr_t len) { 10020 intptr_t String::Hash(const uint16_t* characters, intptr_t len) {
10020 return HashImpl(characters, len); 10021 StringHasher hasher;
10022 intptr_t i = 0;
10023 while (i < len) {
10024 hasher.Add(Utf16::Next(characters, &i, len));
10025 }
10026 return hasher.Finalize(String::kHashBits);
10021 } 10027 }
10022 10028
10023 10029
10024 intptr_t String::Hash(const int32_t* characters, intptr_t len) { 10030 intptr_t String::Hash(const int32_t* characters, intptr_t len) {
10025 return HashImpl(characters, len); 10031 return HashImpl(characters, len);
10026 } 10032 }
10027 10033
10028 10034
10029 int32_t String::CharAt(intptr_t index) const { 10035 int32_t String::CharAt(intptr_t index) const {
10030 intptr_t class_id = raw()->GetClassId(); 10036 intptr_t class_id = raw()->GetClassId();
(...skipping 604 matching lines...) Expand 10 before | Expand all | Expand 10 after
10635 10641
10636 10642
10637 RawString* String::ToLowerCase(const String& str, Heap::Space space) { 10643 RawString* String::ToLowerCase(const String& str, Heap::Space space) {
10638 // TODO(cshapiro): create a fast-path for OneByteString instances. 10644 // TODO(cshapiro): create a fast-path for OneByteString instances.
10639 return Transform(CaseMapping::ToLower, str, space); 10645 return Transform(CaseMapping::ToLower, str, space);
10640 } 10646 }
10641 10647
10642 10648
10643 bool String::CodePointIterator::Next() { 10649 bool String::CodePointIterator::Next() {
10644 ASSERT(index_ >= -1); 10650 ASSERT(index_ >= -1);
10645 ASSERT(index_ < str_.Length()); 10651 intptr_t length = Utf16::Length(ch_);
10646 int d = Utf16::Length(ch_); 10652 if (index_ < (end_ - length)) {
10647 if (index_ == (str_.Length() - d)) { 10653 index_ += length;
10648 return false; 10654 ch_ = str_.CharAt(index_);
10655 if (Utf16::IsLeadSurrogate(ch_) && (index_ < (end_ - 1))) {
10656 int32_t ch2 = str_.CharAt(index_ + 1);
10657 if (Utf16::IsTrailSurrogate(ch2)) {
10658 ch_ = Utf16::Decode(ch_, ch2);
10659 }
10660 }
10661 return true;
10662 } else {
siva 2012/11/30 02:00:19 is this else necessary? it could just be: } in
cshapiro 2012/11/30 02:28:22 Strictly speaking, it is not. I will remove it.
10663 index_ = end_;
10649 } 10664 }
10650 index_ += d; 10665 return false;
10651 ch_ = str_.CharAt(index_);
10652 if (Utf16::IsLeadSurrogate(ch_) && (index_ != (str_.Length() - 1))) {
10653 int32_t ch2 = str_.CharAt(index_ + 1);
10654 if (Utf16::IsTrailSurrogate(ch2)) {
10655 ch_ = Utf16::Decode(ch_, ch2);
10656 }
10657 }
10658 return true;
10659 } 10666 }
10660 10667
10661 10668
10662 RawOneByteString* OneByteString::EscapeSpecialCharacters(const String& str, 10669 RawOneByteString* OneByteString::EscapeSpecialCharacters(const String& str,
10663 bool raw_str) { 10670 bool raw_str) {
10664 intptr_t len = str.Length(); 10671 intptr_t len = str.Length();
10665 if (len > 0) { 10672 if (len > 0) {
10666 intptr_t num_escapes = 0; 10673 intptr_t num_escapes = 0;
10667 intptr_t index = 0; 10674 intptr_t index = 0;
10668 for (intptr_t i = 0; i < len; i++) { 10675 for (intptr_t i = 0; i < len; i++) {
(...skipping 1541 matching lines...) Expand 10 before | Expand all | Expand 10 after
12210 } 12217 }
12211 return result.raw(); 12218 return result.raw();
12212 } 12219 }
12213 12220
12214 12221
12215 const char* WeakProperty::ToCString() const { 12222 const char* WeakProperty::ToCString() const {
12216 return "_WeakProperty"; 12223 return "_WeakProperty";
12217 } 12224 }
12218 12225
12219 } // namespace dart 12226 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698