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

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

Issue 8333001: Implement case mapping using the Unicode default case mapping algorithm. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address review comments. Created 9 years, 2 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/vm/object.h ('k') | runtime/vm/unicode.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 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 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 "vm/assembler.h" 7 #include "vm/assembler.h"
8 #include "vm/assert.h" 8 #include "vm/assert.h"
9 #include "vm/bigint_operations.h" 9 #include "vm/bigint_operations.h"
10 #include "vm/bootstrap.h" 10 #include "vm/bootstrap.h"
(...skipping 5703 matching lines...) Expand 10 before | Expand all | Expand 10 after
5714 const char* String::ToCString() const { 5714 const char* String::ToCString() const {
5715 intptr_t len = Utf8::Length(*this); 5715 intptr_t len = Utf8::Length(*this);
5716 Zone* zone = Isolate::Current()->current_zone(); 5716 Zone* zone = Isolate::Current()->current_zone();
5717 char* result = reinterpret_cast<char*>(zone->Allocate(len + 1)); 5717 char* result = reinterpret_cast<char*>(zone->Allocate(len + 1));
5718 Utf8::Encode(*this, result, len); 5718 Utf8::Encode(*this, result, len);
5719 result[len] = 0; 5719 result[len] = 0;
5720 return result; 5720 return result;
5721 } 5721 }
5722 5722
5723 5723
5724 RawString* String::Transform(int32_t (*mapping)(int32_t ch),
5725 const String& str,
5726 Heap::Space space) {
5727 ASSERT(!str.IsNull());
5728 bool has_mapping = false;
5729 int32_t dst_max = 0;
5730 intptr_t len = str.Length();
5731 // TODO(cshapiro): assume a transform is required, rollback if not.
5732 for (intptr_t i = 0; i < len; ++i) {
5733 int32_t src = str.CharAt(i);
5734 int32_t dst = mapping(src);
5735 if (src != dst) {
5736 has_mapping = true;
5737 }
5738 dst_max = Utils::Maximum(dst_max, dst);
5739 }
5740 if (!has_mapping) {
5741 return str.raw();
5742 }
5743 if (dst_max <= 0xFF) {
5744 return OneByteString::Transform(mapping, str, space);
5745 }
5746 if (dst_max <= 0xFFFF) {
5747 return TwoByteString::Transform(mapping, str, space);
5748 }
5749 ASSERT(dst_max > 0xFFFF);
5750 return FourByteString::Transform(mapping, str, space);
5751 }
5752
5753
5754 RawString* String::ToUpperCase(const String& str, Heap::Space space) {
5755 // TODO(cshapiro): create a fast-path for OneByteString instances.
5756 return Transform(CaseMapping::ToUpper, str, space);
5757 }
5758
5759
5760 RawString* String::ToLowerCase(const String& str, Heap::Space space) {
5761 // TODO(cshapiro): create a fast-path for OneByteString instances.
5762 return Transform(CaseMapping::ToLower, str, space);
5763 }
5764
5765
5724 RawOneByteString* OneByteString::New(intptr_t len, 5766 RawOneByteString* OneByteString::New(intptr_t len,
5725 Heap::Space space) { 5767 Heap::Space space) {
5726 Isolate* isolate = Isolate::Current(); 5768 Isolate* isolate = Isolate::Current();
5727 5769
5728 const Class& cls = 5770 const Class& cls =
5729 Class::Handle(isolate->object_store()->one_byte_string_class()); 5771 Class::Handle(isolate->object_store()->one_byte_string_class());
5730 OneByteString& result = OneByteString::Handle(); 5772 OneByteString& result = OneByteString::Handle();
5731 { 5773 {
5732 RawObject* raw = Object::Allocate(cls, 5774 RawObject* raw = Object::Allocate(cls,
5733 OneByteString::InstanceSize(len), 5775 OneByteString::InstanceSize(len),
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
5816 OneByteString& result = OneByteString::Handle(); 5858 OneByteString& result = OneByteString::Handle();
5817 if (length <= (str.Length() - begin_index)) { 5859 if (length <= (str.Length() - begin_index)) {
5818 result ^= OneByteString::New(length, space); 5860 result ^= OneByteString::New(length, space);
5819 String::Copy(result, 0, str, begin_index, length); 5861 String::Copy(result, 0, str, begin_index, length);
5820 } 5862 }
5821 // TODO(5418937): return a non-null object on error. 5863 // TODO(5418937): return a non-null object on error.
5822 return result.raw(); 5864 return result.raw();
5823 } 5865 }
5824 5866
5825 5867
5868 RawOneByteString* OneByteString::Transform(int32_t (*mapping)(int32_t ch),
5869 const String& str,
5870 Heap::Space space) {
5871 ASSERT(!str.IsNull());
5872 intptr_t len = str.Length();
5873 const OneByteString& onestr =
Ivan Posva 2011/10/21 14:03:37 onestr -> result?
cshapiro 2011/10/21 20:29:08 Yes, that would be more consistent. Renamed.
5874 OneByteString::Handle(OneByteString::New(len, space));
5875 for (intptr_t i = 0; i < len; ++i) {
5876 int32_t ch = mapping(str.CharAt(i));
5877 ASSERT(ch >= 0 && ch <= 0xFF);
5878 *onestr.CharAddr(i) = ch;
5879 }
5880 return onestr.raw();
5881 }
5882
5883
5826 const char* OneByteString::ToCString() const { 5884 const char* OneByteString::ToCString() const {
5827 return String::ToCString(); 5885 return String::ToCString();
5828 } 5886 }
5829 5887
5830 5888
5831 RawTwoByteString* TwoByteString::New(intptr_t len, 5889 RawTwoByteString* TwoByteString::New(intptr_t len,
5832 Heap::Space space) { 5890 Heap::Space space) {
5833 Isolate* isolate = Isolate::Current(); 5891 Isolate* isolate = Isolate::Current();
5834 5892
5835 const Class& cls = 5893 const Class& cls =
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
5915 TwoByteString& result = TwoByteString::Handle(); 5973 TwoByteString& result = TwoByteString::Handle();
5916 if (length <= (str.Length() - begin_index)) { 5974 if (length <= (str.Length() - begin_index)) {
5917 result ^= TwoByteString::New(length, space); 5975 result ^= TwoByteString::New(length, space);
5918 String::Copy(result, 0, str, begin_index, length); 5976 String::Copy(result, 0, str, begin_index, length);
5919 } 5977 }
5920 // TODO(5418937): return a non-null object on error. 5978 // TODO(5418937): return a non-null object on error.
5921 return result.raw(); 5979 return result.raw();
5922 } 5980 }
5923 5981
5924 5982
5983 RawTwoByteString* TwoByteString::Transform(int32_t (*mapping)(int32_t ch),
5984 const String& str,
5985 Heap::Space space) {
5986 ASSERT(!str.IsNull());
5987 intptr_t len = str.Length();
5988 const TwoByteString& twostr =
Ivan Posva 2011/10/21 14:03:37 ditto
cshapiro 2011/10/21 20:29:08 Done.
5989 TwoByteString::Handle(TwoByteString::New(len, space));
5990 for (intptr_t i = 0; i < len; ++i) {
5991 int32_t ch = mapping(str.CharAt(i));
5992 ASSERT(ch >= 0 && ch <= 0xFFFF);
5993 *twostr.CharAddr(i) = ch;
5994 }
5995 return twostr.raw();
5996 }
5997
5998
5925 const char* TwoByteString::ToCString() const { 5999 const char* TwoByteString::ToCString() const {
5926 return String::ToCString(); 6000 return String::ToCString();
5927 } 6001 }
5928 6002
5929 6003
5930
5931 RawFourByteString* FourByteString::New(intptr_t len, 6004 RawFourByteString* FourByteString::New(intptr_t len,
5932 Heap::Space space) { 6005 Heap::Space space) {
5933 Isolate* isolate = Isolate::Current(); 6006 Isolate* isolate = Isolate::Current();
5934 6007
5935 const Class& cls = 6008 const Class& cls =
5936 Class::Handle(isolate->object_store()->four_byte_string_class()); 6009 Class::Handle(isolate->object_store()->four_byte_string_class());
5937 FourByteString& result = FourByteString::Handle(); 6010 FourByteString& result = FourByteString::Handle();
5938 { 6011 {
5939 RawObject* raw = Object::Allocate(cls, 6012 RawObject* raw = Object::Allocate(cls,
5940 FourByteString::InstanceSize(len), 6013 FourByteString::InstanceSize(len),
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
6007 FourByteString& result = FourByteString::Handle(); 6080 FourByteString& result = FourByteString::Handle();
6008 if (length <= (str.Length() - begin_index)) { 6081 if (length <= (str.Length() - begin_index)) {
6009 result ^= FourByteString::New(length, space); 6082 result ^= FourByteString::New(length, space);
6010 String::Copy(result, 0, str, begin_index, length); 6083 String::Copy(result, 0, str, begin_index, length);
6011 } 6084 }
6012 // TODO(5418937): return a non-null object on error. 6085 // TODO(5418937): return a non-null object on error.
6013 return result.raw(); 6086 return result.raw();
6014 } 6087 }
6015 6088
6016 6089
6090 RawFourByteString* FourByteString::Transform(int32_t (*mapping)(int32_t ch),
6091 const String& str,
6092 Heap::Space space) {
6093 ASSERT(!str.IsNull());
6094 intptr_t len = str.Length();
6095 const FourByteString& fourstr =
Ivan Posva 2011/10/21 14:03:37 ditto
cshapiro 2011/10/21 20:29:08 Done.
6096 FourByteString::Handle(FourByteString::New(len, space));
6097 for (intptr_t i = 0; i < len; ++i) {
6098 int32_t ch = mapping(str.CharAt(i));
6099 ASSERT(ch >= 0 && ch <= 0x10FFFF);
6100 *fourstr.CharAddr(i) = ch;
6101 }
6102 return fourstr.raw();
6103 }
6104
6105
6017 const char* FourByteString::ToCString() const { 6106 const char* FourByteString::ToCString() const {
6018 return String::ToCString(); 6107 return String::ToCString();
6019 } 6108 }
6020 6109
6021 6110
6022 RawBool* Bool::True() { 6111 RawBool* Bool::True() {
6023 return Isolate::Current()->object_store()->true_value(); 6112 return Isolate::Current()->object_store()->true_value();
6024 } 6113 }
6025 6114
6026 6115
(...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after
6445 const String& str = String::Handle(pattern()); 6534 const String& str = String::Handle(pattern());
6446 const char* format = "JSRegExp: pattern=%s flags=%s"; 6535 const char* format = "JSRegExp: pattern=%s flags=%s";
6447 intptr_t len = OS::SNPrint(NULL, 0, format, str.ToCString(), Flags()); 6536 intptr_t len = OS::SNPrint(NULL, 0, format, str.ToCString(), Flags());
6448 char* chars = reinterpret_cast<char*>( 6537 char* chars = reinterpret_cast<char*>(
6449 Isolate::Current()->current_zone()->Allocate(len + 1)); 6538 Isolate::Current()->current_zone()->Allocate(len + 1));
6450 OS::SNPrint(chars, (len + 1), format, str.ToCString(), Flags()); 6539 OS::SNPrint(chars, (len + 1), format, str.ToCString(), Flags());
6451 return chars; 6540 return chars;
6452 } 6541 }
6453 6542
6454 } // namespace dart 6543 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/object.h ('k') | runtime/vm/unicode.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698