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

Side by Side Diff: src/objects.cc

Issue 7709024: Replace ToAsciiVector and ToUC16Vector with single function that returns a tagged value. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 9 years, 4 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
OLDNEW
1 // Copyright 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 5020 matching lines...) Expand 10 before | Expand all | Expand 10 after
5031 Access<StringInputBuffer> buffer( 5031 Access<StringInputBuffer> buffer(
5032 heap->isolate()->objects_string_input_buffer()); 5032 heap->isolate()->objects_string_input_buffer());
5033 buffer->Reset(0, this); 5033 buffer->Reset(0, this);
5034 int result = 0; 5034 int result = 0;
5035 while (buffer->has_more()) 5035 while (buffer->has_more())
5036 result += unibrow::Utf8::Length(buffer->GetNext()); 5036 result += unibrow::Utf8::Length(buffer->GetNext());
5037 return result; 5037 return result;
5038 } 5038 }
5039 5039
5040 5040
5041 Vector<const char> String::ToAsciiVector() { 5041 String::FlatContent String::GetFlatContent() {
5042 ASSERT(IsAsciiRepresentation());
5043 ASSERT(IsFlat());
5044
5045 int offset = 0;
5046 int length = this->length(); 5042 int length = this->length();
5047 StringRepresentationTag string_tag = StringShape(this).representation_tag(); 5043 StringShape shape(this);
5048 String* string = this; 5044 String* string = this;
5049 if (string_tag == kConsStringTag) { 5045 if (shape.representation_tag() == kConsStringTag) {
5050 ConsString* cons = ConsString::cast(string); 5046 ConsString* cons = ConsString::cast(string);
5051 ASSERT(cons->second()->length() == 0); 5047 if (cons->second()->length() != 0) {
5048 return FlatContent();
5049 }
5052 string = cons->first(); 5050 string = cons->first();
5053 string_tag = StringShape(string).representation_tag(); 5051 shape = StringShape(string);
5054 } 5052 }
5055 if (string_tag == kSeqStringTag) { 5053 if (shape.encoding_tag() == kAsciiStringTag) {
5056 SeqAsciiString* seq = SeqAsciiString::cast(string); 5054 const char* start;
5057 char* start = seq->GetChars(); 5055 if (shape.representation_tag() == kSeqStringTag) {
5058 return Vector<const char>(start + offset, length); 5056 start = SeqAsciiString::cast(string)->GetChars();
5057 } else {
5058 start = ExternalAsciiString::cast(string)->resource()->data();
5059 }
5060 return FlatContent(Vector<const char>(start, length));
5061 } else {
5062 ASSERT(shape.encoding_tag() == kTwoByteStringTag);
5063 const uc16* start;
5064 if (shape.representation_tag() == kSeqStringTag) {
5065 start = SeqTwoByteString::cast(string)->GetChars();
5066 } else {
5067 start = ExternalTwoByteString::cast(string)->resource()->data();
5068 }
5069 return FlatContent(Vector<const uc16>(start, length));
5059 } 5070 }
5060 ASSERT(string_tag == kExternalStringTag);
5061 ExternalAsciiString* ext = ExternalAsciiString::cast(string);
5062 const char* start = ext->resource()->data();
5063 return Vector<const char>(start + offset, length);
5064 } 5071 }
5065 5072
5066 5073
5067 Vector<const uc16> String::ToUC16Vector() {
5068 ASSERT(IsTwoByteRepresentation());
5069 ASSERT(IsFlat());
5070
5071 int offset = 0;
5072 int length = this->length();
5073 StringRepresentationTag string_tag = StringShape(this).representation_tag();
5074 String* string = this;
5075 if (string_tag == kConsStringTag) {
5076 ConsString* cons = ConsString::cast(string);
5077 ASSERT(cons->second()->length() == 0);
5078 string = cons->first();
5079 string_tag = StringShape(string).representation_tag();
5080 }
5081 if (string_tag == kSeqStringTag) {
5082 SeqTwoByteString* seq = SeqTwoByteString::cast(string);
5083 return Vector<const uc16>(seq->GetChars() + offset, length);
5084 }
5085 ASSERT(string_tag == kExternalStringTag);
5086 ExternalTwoByteString* ext = ExternalTwoByteString::cast(string);
5087 const uc16* start =
5088 reinterpret_cast<const uc16*>(ext->resource()->data());
5089 return Vector<const uc16>(start + offset, length);
5090 }
5091
5092
5093 SmartPointer<char> String::ToCString(AllowNullsFlag allow_nulls, 5074 SmartPointer<char> String::ToCString(AllowNullsFlag allow_nulls,
5094 RobustnessFlag robust_flag, 5075 RobustnessFlag robust_flag,
5095 int offset, 5076 int offset,
5096 int length, 5077 int length,
5097 int* length_return) { 5078 int* length_return) {
5098 if (robust_flag == ROBUST_STRING_TRAVERSAL && !LooksValid()) { 5079 if (robust_flag == ROBUST_STRING_TRAVERSAL && !LooksValid()) {
5099 return SmartPointer<char>(NULL); 5080 return SmartPointer<char>(NULL);
5100 } 5081 }
5101 Heap* heap = GetHeap(); 5082 Heap* heap = GetHeap();
5102 5083
(...skipping 426 matching lines...) Expand 10 before | Expand all | Expand 10 after
5529 str_(0), 5510 str_(0),
5530 is_ascii_(true), 5511 is_ascii_(true),
5531 length_(input.length()), 5512 length_(input.length()),
5532 start_(input.start()) { } 5513 start_(input.start()) { }
5533 5514
5534 5515
5535 void FlatStringReader::PostGarbageCollection() { 5516 void FlatStringReader::PostGarbageCollection() {
5536 if (str_ == NULL) return; 5517 if (str_ == NULL) return;
5537 Handle<String> str(str_); 5518 Handle<String> str(str_);
5538 ASSERT(str->IsFlat()); 5519 ASSERT(str->IsFlat());
5539 is_ascii_ = str->IsAsciiRepresentation(); 5520 String::FlatContent content = str->GetFlatContent();
5521 ASSERT(content.is_flat());
5522 is_ascii_ = content.IsAscii();
5540 if (is_ascii_) { 5523 if (is_ascii_) {
5541 start_ = str->ToAsciiVector().start(); 5524 start_ = content.ToAsciiVector().start();
5542 } else { 5525 } else {
5543 start_ = str->ToUC16Vector().start(); 5526 start_ = content.ToUC16Vector().start();
5544 } 5527 }
5545 } 5528 }
5546 5529
5547 5530
5548 void StringInputBuffer::Seek(unsigned pos) { 5531 void StringInputBuffer::Seek(unsigned pos) {
5549 Reset(pos, input_); 5532 Reset(pos, input_);
5550 } 5533 }
5551 5534
5552 5535
5553 void SafeStringInputBuffer::Seek(unsigned pos) { 5536 void SafeStringInputBuffer::Seek(unsigned pos) {
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after
5853 } 5836 }
5854 } 5837 }
5855 return true; 5838 return true;
5856 } 5839 }
5857 5840
5858 5841
5859 template <typename IteratorA> 5842 template <typename IteratorA>
5860 static inline bool CompareStringContentsPartial(Isolate* isolate, 5843 static inline bool CompareStringContentsPartial(Isolate* isolate,
5861 IteratorA* ia, 5844 IteratorA* ia,
5862 String* b) { 5845 String* b) {
5863 if (b->IsFlat()) { 5846 String::FlatContent content = b->GetFlatContent();
5864 if (b->IsAsciiRepresentation()) { 5847 if (content.IsFlat()) {
5865 VectorIterator<char> ib(b->ToAsciiVector()); 5848 if (content.IsAscii()) {
5849 VectorIterator<char> ib(content.ToAsciiVector());
5866 return CompareStringContents(ia, &ib); 5850 return CompareStringContents(ia, &ib);
5867 } else { 5851 } else {
5868 VectorIterator<uc16> ib(b->ToUC16Vector()); 5852 VectorIterator<uc16> ib(content.ToUC16Vector());
5869 return CompareStringContents(ia, &ib); 5853 return CompareStringContents(ia, &ib);
5870 } 5854 }
5871 } else { 5855 } else {
5872 isolate->objects_string_compare_buffer_b()->Reset(0, b); 5856 isolate->objects_string_compare_buffer_b()->Reset(0, b);
5873 return CompareStringContents(ia, 5857 return CompareStringContents(ia,
5874 isolate->objects_string_compare_buffer_b()); 5858 isolate->objects_string_compare_buffer_b());
5875 } 5859 }
5876 } 5860 }
5877 5861
5878 5862
(...skipping 18 matching lines...) Expand all
5897 5881
5898 if (StringShape(lhs).IsSequentialAscii() && 5882 if (StringShape(lhs).IsSequentialAscii() &&
5899 StringShape(rhs).IsSequentialAscii()) { 5883 StringShape(rhs).IsSequentialAscii()) {
5900 const char* str1 = SeqAsciiString::cast(lhs)->GetChars(); 5884 const char* str1 = SeqAsciiString::cast(lhs)->GetChars();
5901 const char* str2 = SeqAsciiString::cast(rhs)->GetChars(); 5885 const char* str2 = SeqAsciiString::cast(rhs)->GetChars();
5902 return CompareRawStringContents(Vector<const char>(str1, len), 5886 return CompareRawStringContents(Vector<const char>(str1, len),
5903 Vector<const char>(str2, len)); 5887 Vector<const char>(str2, len));
5904 } 5888 }
5905 5889
5906 Isolate* isolate = GetIsolate(); 5890 Isolate* isolate = GetIsolate();
5907 if (lhs->IsFlat()) { 5891 String::FlatContent lhs_content = lhs->GetFlatContent();
5908 if (lhs->IsAsciiRepresentation()) { 5892 String::FlatContent rhs_content = rhs->GetFlatContent();
5909 Vector<const char> vec1 = lhs->ToAsciiVector(); 5893 if (lhs_content.IsFlat()) {
5910 if (rhs->IsFlat()) { 5894 if (lhs_content.IsAscii()) {
5911 if (rhs->IsAsciiRepresentation()) { 5895 Vector<const char> vec1 = lhs_content.ToAsciiVector();
5912 Vector<const char> vec2 = rhs->ToAsciiVector(); 5896 if (rhs_content.IsFlat()) {
5897 if (rhs_content.IsAscii()) {
5898 Vector<const char> vec2 = rhs_content.ToAsciiVector();
5913 return CompareRawStringContents(vec1, vec2); 5899 return CompareRawStringContents(vec1, vec2);
5914 } else { 5900 } else {
5915 VectorIterator<char> buf1(vec1); 5901 VectorIterator<char> buf1(vec1);
5916 VectorIterator<uc16> ib(rhs->ToUC16Vector()); 5902 VectorIterator<uc16> ib(rhs_content.ToUC16Vector());
5917 return CompareStringContents(&buf1, &ib); 5903 return CompareStringContents(&buf1, &ib);
5918 } 5904 }
5919 } else { 5905 } else {
5920 VectorIterator<char> buf1(vec1); 5906 VectorIterator<char> buf1(vec1);
5921 isolate->objects_string_compare_buffer_b()->Reset(0, rhs); 5907 isolate->objects_string_compare_buffer_b()->Reset(0, rhs);
5922 return CompareStringContents(&buf1, 5908 return CompareStringContents(&buf1,
5923 isolate->objects_string_compare_buffer_b()); 5909 isolate->objects_string_compare_buffer_b());
5924 } 5910 }
5925 } else { 5911 } else {
5926 Vector<const uc16> vec1 = lhs->ToUC16Vector(); 5912 Vector<const uc16> vec1 = lhs_content.ToUC16Vector();
5927 if (rhs->IsFlat()) { 5913 if (rhs_content.IsFlat()) {
5928 if (rhs->IsAsciiRepresentation()) { 5914 if (rhs_content.IsAscii()) {
5929 VectorIterator<uc16> buf1(vec1); 5915 VectorIterator<uc16> buf1(vec1);
5930 VectorIterator<char> ib(rhs->ToAsciiVector()); 5916 VectorIterator<char> ib(rhs_content.ToAsciiVector());
5931 return CompareStringContents(&buf1, &ib); 5917 return CompareStringContents(&buf1, &ib);
5932 } else { 5918 } else {
5933 Vector<const uc16> vec2(rhs->ToUC16Vector()); 5919 Vector<const uc16> vec2(rhs_content.ToUC16Vector());
5934 return CompareRawStringContents(vec1, vec2); 5920 return CompareRawStringContents(vec1, vec2);
5935 } 5921 }
5936 } else { 5922 } else {
5937 VectorIterator<uc16> buf1(vec1); 5923 VectorIterator<uc16> buf1(vec1);
5938 isolate->objects_string_compare_buffer_b()->Reset(0, rhs); 5924 isolate->objects_string_compare_buffer_b()->Reset(0, rhs);
5939 return CompareStringContents(&buf1, 5925 return CompareStringContents(&buf1,
5940 isolate->objects_string_compare_buffer_b()); 5926 isolate->objects_string_compare_buffer_b());
5941 } 5927 }
5942 } 5928 }
5943 } else { 5929 } else {
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
5976 uc32 r = decoder->GetNext(); 5962 uc32 r = decoder->GetNext();
5977 if (Get(i) != r) return false; 5963 if (Get(i) != r) return false;
5978 } 5964 }
5979 return i == slen && !decoder->has_more(); 5965 return i == slen && !decoder->has_more();
5980 } 5966 }
5981 5967
5982 5968
5983 bool String::IsAsciiEqualTo(Vector<const char> str) { 5969 bool String::IsAsciiEqualTo(Vector<const char> str) {
5984 int slen = length(); 5970 int slen = length();
5985 if (str.length() != slen) return false; 5971 if (str.length() != slen) return false;
5986 if (IsFlat() && IsAsciiRepresentation()) { 5972 FlatContent content = GetFlatContent();
5987 return CompareChars(ToAsciiVector().start(), str.start(), slen) == 0; 5973 if (content.IsAscii()) {
5974 return CompareChars(content.ToAsciiVector().start(),
5975 str.start(), slen) == 0;
5988 } 5976 }
5989 for (int i = 0; i < slen; i++) { 5977 for (int i = 0; i < slen; i++) {
5990 if (Get(i) != static_cast<uint16_t>(str[i])) return false; 5978 if (Get(i) != static_cast<uint16_t>(str[i])) return false;
5991 } 5979 }
5992 return true; 5980 return true;
5993 } 5981 }
5994 5982
5995 5983
5996 bool String::IsTwoByteEqualTo(Vector<const uc16> str) { 5984 bool String::IsTwoByteEqualTo(Vector<const uc16> str) {
5997 int slen = length(); 5985 int slen = length();
5998 if (str.length() != slen) return false; 5986 if (str.length() != slen) return false;
5999 if (IsFlat() && IsTwoByteRepresentation()) { 5987 FlatContent content = GetFlatContent();
6000 return CompareChars(ToUC16Vector().start(), str.start(), slen) == 0; 5988 if (content.IsTwoByte()) {
5989 return CompareChars(content.ToUC16Vector().start(), str.start(), slen) == 0;
6001 } 5990 }
6002 for (int i = 0; i < slen; i++) { 5991 for (int i = 0; i < slen; i++) {
6003 if (Get(i) != str[i]) return false; 5992 if (Get(i) != str[i]) return false;
6004 } 5993 }
6005 return true; 5994 return true;
6006 } 5995 }
6007 5996
6008 5997
6009 uint32_t String::ComputeAndSetHash() { 5998 uint32_t String::ComputeAndSetHash() {
6010 // Should only be called if hash code has not yet been computed. 5999 // Should only be called if hash code has not yet been computed.
(...skipping 5542 matching lines...) Expand 10 before | Expand all | Expand 10 after
11553 if (break_point_objects()->IsUndefined()) return 0; 11542 if (break_point_objects()->IsUndefined()) return 0;
11554 // Single break point. 11543 // Single break point.
11555 if (!break_point_objects()->IsFixedArray()) return 1; 11544 if (!break_point_objects()->IsFixedArray()) return 1;
11556 // Multiple break points. 11545 // Multiple break points.
11557 return FixedArray::cast(break_point_objects())->length(); 11546 return FixedArray::cast(break_point_objects())->length();
11558 } 11547 }
11559 #endif 11548 #endif
11560 11549
11561 11550
11562 } } // namespace v8::internal 11551 } } // namespace v8::internal
OLDNEW
« src/objects.h ('K') | « src/objects.h ('k') | src/objects-inl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698