}
// Slow case.
- i::StringInputBuffer& write_input_buffer = *isolate->write_input_buffer();
+ i::StringCharacterStream stream(*str, isolate->write_iterator());
isolate->string_tracker()->RecordWrite(str);
- write_input_buffer.Reset(0, *str);
int len = str->length();
// Encode the first K - 3 bytes directly into the buffer since we
// know there's room for them. If no capacity is given we copy all
int nchars = 0;
int previous = unibrow::Utf16::kNoPreviousCharacter;
for (i = 0; i < len && (capacity == -1 || pos < fast_end); i++) {
- i::uc32 c = write_input_buffer.GetNext();
+ i::uc32 c = stream.GetNext();
int written = unibrow::Utf8::Encode(buffer + pos, c, previous);
pos += written;
nchars++;
// buffer.
char intermediate[unibrow::Utf8::kMaxEncodedSize];
for (; i < len && pos < capacity; i++) {
- i::uc32 c = write_input_buffer.GetNext();
+ i::uc32 c = stream.GetNext();
if (unibrow::Utf16::IsTrailSurrogate(c) &&
unibrow::Utf16::IsLeadSurrogate(previous)) {
// We can't use the intermediate buffer here because the encoding
}
if (str->IsOneByteRepresentation()) {
- // WriteToFlat is faster than using the StringInputBuffer.
+ // WriteToFlat is faster than using the StringCharacterStream.
if (length == -1) length = str->length() + 1;
int len = i::Min(length, str->length() - start);
i::String::WriteToFlat(*str, buffer, start, start + len);
return len;
}
- i::StringInputBuffer& write_input_buffer = *isolate->write_input_buffer();
int end = length;
if ((length == -1) || (length > str->length() - start)) {
end = str->length() - start;
}
if (end < 0) return 0;
- write_input_buffer.Reset(start, *str);
+ i::StringCharacterStream write_stream(*str, isolate->write_iterator(), start);
int i;
for (i = 0; i < end; i++) {
- char c = static_cast<char>(write_input_buffer.GetNext());
+ char c = static_cast<char>(write_stream.GetNext());
if (c == '\0' && !(options & PRESERVE_ASCII_NULL)) c = ' ';
buffer[i] = c;
}
isolate->string_tracker()->RecordWrite(str);
if (options & HINT_MANY_WRITES_EXPECTED) {
// Flatten the string for efficiency. This applies whether we are
- // using StringInputBuffer or Get(i) to access the characters.
+ // using StringCharacterStream or Get(i) to access the characters.
FlattenString(str);
}
int end = start + length;
Handle<Script> script = info->script();
if (!script->IsUndefined() && !script->source()->IsUndefined()) {
PrintF("--- Raw source ---\n");
- StringInputBuffer stream(String::cast(script->source()));
- stream.Seek(function->start_position());
+ ConsStringIteratorOp op;
+ StringCharacterStream stream(String::cast(script->source()),
+ &op,
+ function->start_position());
// fun->end_position() points to the last character in the stream. We
// need to compensate by adding one to calculate the length.
int source_len =
function->end_position() - function->start_position() + 1;
for (int i = 0; i < source_len; i++) {
- if (stream.has_more()) PrintF("%c", stream.GetNext());
+ if (stream.HasMore()) PrintF("%c", stream.GetNext());
}
PrintF("\n\n");
}
free_list_(0),
preallocated_storage_preallocated_(false),
inner_pointer_to_code_cache_(NULL),
- write_input_buffer_(NULL),
+ write_iterator_(NULL),
global_handles_(NULL),
context_switcher_(NULL),
thread_manager_(NULL),
bootstrapper_ = NULL;
delete inner_pointer_to_code_cache_;
inner_pointer_to_code_cache_ = NULL;
- delete write_input_buffer_;
- write_input_buffer_ = NULL;
+ delete write_iterator_;
+ write_iterator_ = NULL;
delete context_switcher_;
context_switcher_ = NULL;
descriptor_lookup_cache_ = new DescriptorLookupCache();
unicode_cache_ = new UnicodeCache();
inner_pointer_to_code_cache_ = new InnerPointerToCodeCache(this);
- write_input_buffer_ = new StringInputBuffer();
+ write_iterator_ = new ConsStringIteratorOp();
global_handles_ = new GlobalHandles(this);
bootstrapper_ = new Bootstrapper();
handle_scope_implementer_ = new HandleScopeImplementer(this);
class RegExpStack;
class SaveContext;
class UnicodeCache;
-class StringInputBuffer;
+class ConsStringIteratorOp;
class StringTracker;
class StubCache;
class ThreadManager;
return inner_pointer_to_code_cache_;
}
- StringInputBuffer* write_input_buffer() { return write_input_buffer_; }
+ ConsStringIteratorOp* write_iterator() { return write_iterator_; }
GlobalHandles* global_handles() { return global_handles_; }
return &jsregexp_canonrange_;
}
- StringInputBuffer* objects_string_compare_buffer_a() {
- return &objects_string_compare_buffer_a_;
+ ConsStringIteratorOp* objects_string_compare_iterator_a() {
+ return &objects_string_compare_iterator_a_;
}
- StringInputBuffer* objects_string_compare_buffer_b() {
- return &objects_string_compare_buffer_b_;
+ ConsStringIteratorOp* objects_string_compare_iterator_b() {
+ return &objects_string_compare_iterator_b_;
}
- StaticResource<StringInputBuffer>* objects_string_input_buffer() {
- return &objects_string_input_buffer_;
+ StaticResource<ConsStringIteratorOp>* objects_string_iterator() {
+ return &objects_string_iterator_;
}
RuntimeState* runtime_state() { return &runtime_state_; }
PreallocatedStorage free_list_;
bool preallocated_storage_preallocated_;
InnerPointerToCodeCache* inner_pointer_to_code_cache_;
- StringInputBuffer* write_input_buffer_;
+ ConsStringIteratorOp* write_iterator_;
GlobalHandles* global_handles_;
ContextSwitcher* context_switcher_;
ThreadManager* thread_manager_;
StringTracker* string_tracker_;
unibrow::Mapping<unibrow::Ecma262UnCanonicalize> jsregexp_uncanonicalize_;
unibrow::Mapping<unibrow::CanonicalizationRange> jsregexp_canonrange_;
- StringInputBuffer objects_string_compare_buffer_a_;
- StringInputBuffer objects_string_compare_buffer_b_;
- StaticResource<StringInputBuffer> objects_string_input_buffer_;
+ ConsStringIteratorOp objects_string_compare_iterator_a_;
+ ConsStringIteratorOp objects_string_compare_iterator_b_;
+ StaticResource<ConsStringIteratorOp> objects_string_iterator_;
unibrow::Mapping<unibrow::Ecma262Canonicalize>
regexp_macro_assembler_canonicalize_;
RegExpStack* regexp_stack_;
uint16_t StringCharacterStream::GetNext() {
- ASSERT((buffer8_ == NULL && end_ == NULL) || buffer8_ < end_);
+ ASSERT(buffer8_ != NULL && end_ != NULL);
+ // Advance cursor if needed.
+ // TODO(dcarney): Ensure uses of the api call HasMore first and avoid this.
+ if (buffer8_ == end_) HasMore();
+ ASSERT(buffer8_ < end_);
return is_one_byte_ ? *buffer8_++ : *buffer16_++;
}
-StringCharacterStream::StringCharacterStream(
- String* string, unsigned offset, ConsStringIteratorOp* op)
+StringCharacterStream::StringCharacterStream(String* string,
+ ConsStringIteratorOp* op,
+ unsigned offset)
: is_one_byte_(false),
- buffer8_(NULL),
- end_(NULL),
op_(op) {
- op->Reset();
+ Reset(string, offset);
+}
+
+
+void StringCharacterStream::Reset(String* string, unsigned offset) {
+ op_->Reset();
+ buffer8_ = NULL;
+ end_ = NULL;
int32_t type = string->map()->instance_type();
unsigned length = string->length();
- String::Visit(string, offset, *this, *op, type, length);
+ String::Visit(string, offset, *this, *op_, type, length);
}
return;
}
- StringInputBuffer buf(this);
+ ConsStringIteratorOp op;
+ StringCharacterStream stream(this, &op);
bool truncated = false;
if (len > kMaxShortPrintLength) {
}
bool ascii = true;
for (int i = 0; i < len; i++) {
- int c = buf.GetNext();
+ uint16_t c = stream.GetNext();
if (c < 32 || c >= 127) {
ascii = false;
}
}
- buf.Reset(this);
+ stream.Reset(this);
if (ascii) {
accumulator->Add("<String[%u]: ", length());
for (int i = 0; i < len; i++) {
- accumulator->Put(buf.GetNext());
+ accumulator->Put(stream.GetNext());
}
accumulator->Put('>');
} else {
// characters and that backslashes are therefore escaped.
accumulator->Add("<String[%u]\\: ", length());
for (int i = 0; i < len; i++) {
- int c = buf.GetNext();
+ uint16_t c = stream.GetNext();
if (c == '\n') {
accumulator->Add("\\n");
} else if (c == '\r') {
}
-static bool IsIdentifier(UnicodeCache* cache,
- unibrow::CharacterStream* buffer) {
+static bool IsIdentifier(UnicodeCache* cache, String* string) {
// Checks whether the buffer contains an identifier (no escape).
- if (!buffer->has_more()) return false;
- if (!cache->IsIdentifierStart(buffer->GetNext())) {
+ if (string->length() == 0) return false;
+ ConsStringIteratorOp op;
+ StringCharacterStream stream(string, &op);
+ if (!cache->IsIdentifierStart(stream.GetNext())) {
return false;
}
- while (buffer->has_more()) {
- if (!cache->IsIdentifierPart(buffer->GetNext())) {
+ while (stream.HasMore()) {
+ if (!cache->IsIdentifierPart(stream.GetNext())) {
return false;
}
}
// hidden symbols) and is not a real identifier.
// Normalize the object if it will have too many fast properties.
Isolate* isolate = GetHeap()->isolate();
- StringInputBuffer buffer(name);
- if ((!IsIdentifier(isolate->unicode_cache(), &buffer)
+ if ((!IsIdentifier(isolate->unicode_cache(), name)
&& name != isolate->heap()->hidden_symbol()) ||
(map()->unused_property_fields() == 0 &&
TooManyFastProperties(properties()->length(), store_mode))) {
if (length < 0) length = kMaxInt - offset;
// Compute the size of the UTF-8 string. Start at the specified offset.
- Access<StringInputBuffer> buffer(
- heap->isolate()->objects_string_input_buffer());
- buffer->Reset(offset, this);
+ Access<ConsStringIteratorOp> op(
+ heap->isolate()->objects_string_iterator());
+ StringCharacterStream stream(this, op.value(), offset);
int character_position = offset;
int utf8_bytes = 0;
int last = unibrow::Utf16::kNoPreviousCharacter;
- while (buffer->has_more() && character_position++ < offset + length) {
- uint16_t character = buffer->GetNext();
+ while (stream.HasMore() && character_position++ < offset + length) {
+ uint16_t character = stream.GetNext();
utf8_bytes += unibrow::Utf8::Length(character, last);
last = character;
}
char* result = NewArray<char>(utf8_bytes + 1);
// Convert the UTF-16 string to a UTF-8 buffer. Start at the specified offset.
- buffer->Rewind();
- buffer->Seek(offset);
+ stream.Reset(this, offset);
character_position = offset;
int utf8_byte_position = 0;
last = unibrow::Utf16::kNoPreviousCharacter;
- while (buffer->has_more() && character_position++ < offset + length) {
- uint16_t character = buffer->GetNext();
+ while (stream.HasMore() && character_position++ < offset + length) {
+ uint16_t character = stream.GetNext();
if (allow_nulls == DISALLOW_NULLS && character == 0) {
character = ' ';
}
}
Heap* heap = GetHeap();
- Access<StringInputBuffer> buffer(
- heap->isolate()->objects_string_input_buffer());
- buffer->Reset(this);
+ Access<ConsStringIteratorOp> op(
+ heap->isolate()->objects_string_iterator());
+ StringCharacterStream stream(this, op.value());
uc16* result = NewArray<uc16>(length() + 1);
int i = 0;
- while (buffer->has_more()) {
- uint16_t character = buffer->GetNext();
+ while (stream.HasMore()) {
+ uint16_t character = stream.GetNext();
result[i++] = character;
}
result[i] = 0;
}
-template <typename IteratorA, typename IteratorB>
-static inline bool CompareStringContents(IteratorA* ia, IteratorB* ib) {
- // General slow case check. We know that the ia and ib iterators
- // have the same length.
- while (ia->has_more()) {
- uint32_t ca = ia->GetNext();
- uint32_t cb = ib->GetNext();
- ASSERT(ca <= unibrow::Utf16::kMaxNonSurrogateCharCode);
- ASSERT(cb <= unibrow::Utf16::kMaxNonSurrogateCharCode);
- if (ca != cb)
- return false;
- }
- return true;
-}
-
-
// Compares the contents of two strings by reading and comparing
// int-sized blocks of characters.
template <typename Char>
-static inline bool CompareRawStringContents(Vector<Char> a, Vector<Char> b) {
- int length = a.length();
- ASSERT_EQ(length, b.length());
- const Char* pa = a.start();
- const Char* pb = b.start();
+static inline bool CompareRawStringContents(const Char* const a,
+ const Char* const b,
+ int length) {
int i = 0;
#ifndef V8_HOST_CAN_READ_UNALIGNED
// If this architecture isn't comfortable reading unaligned ints
// then we have to check that the strings are aligned before
// comparing them blockwise.
const int kAlignmentMask = sizeof(uint32_t) - 1; // NOLINT
- uint32_t pa_addr = reinterpret_cast<uint32_t>(pa);
- uint32_t pb_addr = reinterpret_cast<uint32_t>(pb);
+ uint32_t pa_addr = reinterpret_cast<uint32_t>(a);
+ uint32_t pb_addr = reinterpret_cast<uint32_t>(b);
if (((pa_addr & kAlignmentMask) | (pb_addr & kAlignmentMask)) == 0) {
#endif
const int kStepSize = sizeof(int) / sizeof(Char); // NOLINT
int endpoint = length - kStepSize;
// Compare blocks until we reach near the end of the string.
for (; i <= endpoint; i += kStepSize) {
- uint32_t wa = *reinterpret_cast<const uint32_t*>(pa + i);
- uint32_t wb = *reinterpret_cast<const uint32_t*>(pb + i);
+ uint32_t wa = *reinterpret_cast<const uint32_t*>(a + i);
+ uint32_t wb = *reinterpret_cast<const uint32_t*>(b + i);
if (wa != wb) {
return false;
}
}
-template <typename IteratorA>
-static inline bool CompareStringContentsPartial(Isolate* isolate,
- IteratorA* ia,
- String* b) {
- String::FlatContent content = b->GetFlatContent();
- if (content.IsFlat()) {
- if (content.IsAscii()) {
- VectorIterator<char> ib(content.ToAsciiVector());
- return CompareStringContents(ia, &ib);
- } else {
- VectorIterator<uc16> ib(content.ToUC16Vector());
- return CompareStringContents(ia, &ib);
+template<typename Chars1, typename Chars2>
+class RawStringComparator : public AllStatic {
+ public:
+ static inline bool compare(const Chars1* a, const Chars2* b, int len) {
+ ASSERT(sizeof(Chars1) != sizeof(Chars2));
+ for (int i = 0; i < len; i++) {
+ if (a[i] != b[i]) {
+ return false;
+ }
}
- } else {
- isolate->objects_string_compare_buffer_b()->Reset(0, b);
- return CompareStringContents(ia,
- isolate->objects_string_compare_buffer_b());
+ return true;
}
-}
+};
+
+
+template<>
+class RawStringComparator<uint16_t, uint16_t> {
+ public:
+ static inline bool compare(const uint16_t* a, const uint16_t* b, int len) {
+ return CompareRawStringContents(a, b, len);
+ }
+};
+
+
+template<>
+class RawStringComparator<uint8_t, uint8_t> {
+ public:
+ static inline bool compare(const uint8_t* a, const uint8_t* b, int len) {
+ return CompareRawStringContents(a, b, len);
+ }
+};
+
+
+class StringComparator {
+ class State {
+ public:
+ explicit inline State(ConsStringIteratorOp* op)
+ : op_(op) {}
+
+ inline void Init(String* string, unsigned len) {
+ op_->Reset();
+ int32_t type = string->map()->instance_type();
+ String::Visit(string, 0, *this, *op_, type, len);
+ }
+
+ inline void VisitOneByteString(const uint8_t* chars, unsigned length) {
+ is_one_byte_ = true;
+ buffer8_ = chars;
+ length_ = length;
+ }
+
+ inline void VisitTwoByteString(const uint16_t* chars, unsigned length) {
+ is_one_byte_ = false;
+ buffer16_ = chars;
+ length_ = length;
+ }
+
+ void Advance(unsigned consumed) {
+ ASSERT(consumed <= length_);
+ // Still in buffer.
+ if (length_ != consumed) {
+ if (is_one_byte_) {
+ buffer8_ += consumed;
+ } else {
+ buffer16_ += consumed;
+ }
+ length_ -= consumed;
+ return;
+ }
+ // Advance state.
+ ASSERT(op_->HasMore());
+ int32_t type;
+ unsigned length;
+ String* next = op_->ContinueOperation(&type, &length);
+ ASSERT(next != NULL);
+ ConsStringNullOp null_op;
+ String::Visit(next, 0, *this, null_op, type, length);
+ }
+
+ ConsStringIteratorOp* const op_;
+ bool is_one_byte_;
+ unsigned offset_;
+ unsigned length_;
+ union {
+ const uint8_t* buffer8_;
+ const uint16_t* buffer16_;
+ };
+ DISALLOW_IMPLICIT_CONSTRUCTORS(State);
+ };
+
+ public:
+ inline StringComparator(ConsStringIteratorOp* op_1,
+ ConsStringIteratorOp* op_2)
+ : state_1_(op_1),
+ state_2_(op_2) {
+ }
+
+ template<typename Chars1, typename Chars2>
+ static inline bool Equals(State* state_1, State* state_2, unsigned to_check) {
+ const Chars1* a = reinterpret_cast<const Chars1*>(state_1->buffer8_);
+ const Chars2* b = reinterpret_cast<const Chars2*>(state_2->buffer8_);
+ return RawStringComparator<Chars1, Chars2>::compare(a, b, to_check);
+ }
+
+ bool Equals(unsigned length, String* string_1, String* string_2) {
+ ASSERT(length != 0);
+ state_1_.Init(string_1, length);
+ state_2_.Init(string_2, length);
+ while (true) {
+ unsigned to_check = Min(state_1_.length_, state_2_.length_);
+ ASSERT(to_check > 0 && to_check <= length);
+ bool is_equal;
+ if (state_1_.is_one_byte_) {
+ if (state_2_.is_one_byte_) {
+ is_equal = Equals<uint8_t, uint8_t>(&state_1_, &state_2_, to_check);
+ } else {
+ is_equal = Equals<uint8_t, uint16_t>(&state_1_, &state_2_, to_check);
+ }
+ } else {
+ if (state_2_.is_one_byte_) {
+ is_equal = Equals<uint16_t, uint8_t>(&state_1_, &state_2_, to_check);
+ } else {
+ is_equal = Equals<uint16_t, uint16_t>(&state_1_, &state_2_, to_check);
+ }
+ }
+ // Looping done.
+ if (!is_equal) return false;
+ length -= to_check;
+ // Exit condition. Strings are equal.
+ if (length == 0) return true;
+ state_1_.Advance(to_check);
+ state_2_.Advance(to_check);
+ }
+ }
+
+ private:
+ State state_1_;
+ State state_2_;
+ DISALLOW_IMPLICIT_CONSTRUCTORS(StringComparator);
+};
bool String::SlowEquals(String* other) {
String* lhs = this->TryFlattenGetString();
String* rhs = other->TryFlattenGetString();
+ // TODO(dcarney): Compare all types of flat strings with a Visitor.
if (StringShape(lhs).IsSequentialAscii() &&
StringShape(rhs).IsSequentialAscii()) {
const char* str1 = SeqOneByteString::cast(lhs)->GetChars();
const char* str2 = SeqOneByteString::cast(rhs)->GetChars();
- return CompareRawStringContents(Vector<const char>(str1, len),
- Vector<const char>(str2, len));
+ return CompareRawStringContents(str1, str2, len);
}
Isolate* isolate = GetIsolate();
- String::FlatContent lhs_content = lhs->GetFlatContent();
- String::FlatContent rhs_content = rhs->GetFlatContent();
- if (lhs_content.IsFlat()) {
- if (lhs_content.IsAscii()) {
- Vector<const char> vec1 = lhs_content.ToAsciiVector();
- if (rhs_content.IsFlat()) {
- if (rhs_content.IsAscii()) {
- Vector<const char> vec2 = rhs_content.ToAsciiVector();
- return CompareRawStringContents(vec1, vec2);
- } else {
- VectorIterator<char> buf1(vec1);
- VectorIterator<uc16> ib(rhs_content.ToUC16Vector());
- return CompareStringContents(&buf1, &ib);
- }
- } else {
- VectorIterator<char> buf1(vec1);
- isolate->objects_string_compare_buffer_b()->Reset(0, rhs);
- return CompareStringContents(&buf1,
- isolate->objects_string_compare_buffer_b());
- }
- } else {
- Vector<const uc16> vec1 = lhs_content.ToUC16Vector();
- if (rhs_content.IsFlat()) {
- if (rhs_content.IsAscii()) {
- VectorIterator<uc16> buf1(vec1);
- VectorIterator<char> ib(rhs_content.ToAsciiVector());
- return CompareStringContents(&buf1, &ib);
- } else {
- Vector<const uc16> vec2(rhs_content.ToUC16Vector());
- return CompareRawStringContents(vec1, vec2);
- }
- } else {
- VectorIterator<uc16> buf1(vec1);
- isolate->objects_string_compare_buffer_b()->Reset(0, rhs);
- return CompareStringContents(&buf1,
- isolate->objects_string_compare_buffer_b());
- }
- }
- } else {
- isolate->objects_string_compare_buffer_a()->Reset(0, lhs);
- return CompareStringContentsPartial(isolate,
- isolate->objects_string_compare_buffer_a(), rhs);
- }
+ StringComparator comparator(isolate->objects_string_compare_iterator_a(),
+ isolate->objects_string_compare_iterator_b());
+
+ return comparator.Equals(static_cast<unsigned>(len), lhs, rhs);
}
}
-bool String::ComputeArrayIndex(unibrow::CharacterStream* buffer,
- uint32_t* index,
- int length) {
+bool String::ComputeArrayIndex(uint32_t* index) {
+ int length = this->length();
if (length == 0 || length > kMaxArrayIndexSize) return false;
- uc32 ch = buffer->GetNext();
+ ConsStringIteratorOp op;
+ StringCharacterStream stream(this, &op);
+ uint16_t ch = stream.GetNext();
// If the string begins with a '0' character, it must only consist
// of it to be a legal array index.
int d = ch - '0';
if (d < 0 || d > 9) return false;
uint32_t result = d;
- while (buffer->has_more()) {
- d = buffer->GetNext() - '0';
+ while (stream.HasMore()) {
+ d = stream.GetNext() - '0';
if (d < 0 || d > 9) return false;
// Check that the new result is below the 32 bit limit.
if (result > 429496729U - ((d > 5) ? 1 : 0)) return false;
*index = (kArrayIndexHashMask & field) >> kHashShift;
return true;
} else {
- StringInputBuffer buffer(this);
- return ComputeArrayIndex(&buffer, index, length());
+ return ComputeArrayIndex(index);
}
}
// Returns a hash value used for the property table
inline uint32_t Hash();
- static bool ComputeArrayIndex(unibrow::CharacterStream* buffer,
- uint32_t* index,
- int length);
+ bool ComputeArrayIndex(uint32_t* index);
// Externalization.
bool MakeExternal(v8::String::ExternalStringResource* resource);
class StringCharacterStream {
public:
inline StringCharacterStream(String* string,
- unsigned offset,
- ConsStringIteratorOp* op);
+ ConsStringIteratorOp* op,
+ unsigned offset = 0);
inline uint16_t GetNext();
inline bool HasMore();
- inline void Reset(String* string, unsigned offset, ConsStringIteratorOp* op);
+ inline void Reset(String* string, unsigned offset = 0);
inline void VisitOneByteString(const uint8_t* chars, unsigned length);
inline void VisitTwoByteString(const uint16_t* chars, unsigned length);
str1->TryFlatten();
str2->TryFlatten();
- StringInputBuffer& buf1 =
- *isolate->runtime_state()->string_locale_compare_buf1();
- StringInputBuffer& buf2 =
- *isolate->runtime_state()->string_locale_compare_buf2();
-
- buf1.Reset(str1);
- buf2.Reset(str2);
+ ConsStringIteratorOp* op1 =
+ isolate->runtime_state()->string_locale_compare_it1();
+ ConsStringIteratorOp* op2 =
+ isolate->runtime_state()->string_locale_compare_it2();
+ // TODO(dcarney) Can do array compares here more efficiently.
+ StringCharacterStream stream1(str1, op1);
+ StringCharacterStream stream2(str2, op2);
for (int i = 0; i < end; i++) {
- uint16_t char1 = buf1.GetNext();
- uint16_t char2 = buf2.GetNext();
+ uint16_t char1 = stream1.GetNext();
+ uint16_t char2 = stream2.GetNext();
if (char1 != char2) return Smi::FromInt(char1 - char2);
}
int escaped_length = 0;
int length = source->length();
{
- Access<StringInputBuffer> buffer(
- isolate->runtime_state()->string_input_buffer());
- buffer->Reset(source);
- while (buffer->has_more()) {
- uint16_t character = buffer->GetNext();
+ Access<ConsStringIteratorOp> op(
+ isolate->runtime_state()->string_iterator());
+ StringCharacterStream stream(source, op.value());
+ while (stream.HasMore()) {
+ uint16_t character = stream.GetNext();
if (character >= 256) {
escaped_length += 6;
} else if (IsNotEscaped(character)) {
String* destination = String::cast(o);
int dest_position = 0;
- Access<StringInputBuffer> buffer(
- isolate->runtime_state()->string_input_buffer());
- buffer->Rewind();
- while (buffer->has_more()) {
- uint16_t chr = buffer->GetNext();
+ Access<ConsStringIteratorOp> op(
+ isolate->runtime_state()->string_iterator());
+ StringCharacterStream stream(source, op.value());
+ while (stream.HasMore()) {
+ uint16_t chr = stream.GetNext();
if (chr >= 256) {
destination->Set(dest_position, '%');
destination->Set(dest_position+1, 'u');
// Convert all characters to upper case, assuming that they will fit
// in the buffer
- Access<StringInputBuffer> buffer(
- isolate->runtime_state()->string_input_buffer());
- buffer->Reset(s);
+ Access<ConsStringIteratorOp> op(
+ isolate->runtime_state()->string_iterator());
+ StringCharacterStream stream(s, op.value());
unibrow::uchar chars[Converter::kMaxWidth];
// We can assume that the string is not empty
- uc32 current = buffer->GetNext();
+ uc32 current = stream.GetNext();
for (int i = 0; i < length;) {
- bool has_next = buffer->has_more();
- uc32 next = has_next ? buffer->GetNext() : 0;
+ bool has_next = stream.HasMore();
+ uc32 next = has_next ? stream.GetNext() : 0;
int char_length = mapping->get(current, next, chars);
if (char_length == 0) {
// The case conversion of this character is the character itself.
if (next_length == 0) next_length = 1;
}
int current_length = i + char_length + next_length;
- while (buffer->has_more()) {
- current = buffer->GetNext();
+ while (stream.HasMore()) {
+ current = stream.GetNext();
// NOTE: we use 0 as the next character here because, while
// the next character may affect what a character converts to,
// it does not in any case affect the length of what it convert
}
-static Object* StringInputBufferCompare(RuntimeState* state,
+static Object* StringCharacterStreamCompare(RuntimeState* state,
String* x,
String* y) {
- StringInputBuffer& bufx = *state->string_input_buffer_compare_bufx();
- StringInputBuffer& bufy = *state->string_input_buffer_compare_bufy();
- bufx.Reset(x);
- bufy.Reset(y);
- while (bufx.has_more() && bufy.has_more()) {
- int d = bufx.GetNext() - bufy.GetNext();
+ StringCharacterStream stream_x(x, state->string_iterator_compare_x());
+ StringCharacterStream stream_y(y, state->string_iterator_compare_y());
+ while (stream_x.HasMore() && stream_y.HasMore()) {
+ int d = stream_x.GetNext() - stream_y.GetNext();
if (d < 0) return Smi::FromInt(LESS);
else if (d > 0) return Smi::FromInt(GREATER);
}
// x is (non-trivial) prefix of y:
- if (bufy.has_more()) return Smi::FromInt(LESS);
+ if (stream_y.HasMore()) return Smi::FromInt(LESS);
// y is prefix of x:
- return Smi::FromInt(bufx.has_more() ? GREATER : EQUAL);
+ return Smi::FromInt(stream_x.HasMore() ? GREATER : EQUAL);
}
result = (r < 0) ? Smi::FromInt(LESS) : Smi::FromInt(GREATER);
}
ASSERT(result ==
- StringInputBufferCompare(Isolate::Current()->runtime_state(), x, y));
+ StringCharacterStreamCompare(Isolate::Current()->runtime_state(), x, y));
return result;
}
}
return (x->IsFlat() && y->IsFlat()) ? FlatStringCompare(x, y)
- : StringInputBufferCompare(isolate->runtime_state(), x, y);
+ : StringCharacterStreamCompare(isolate->runtime_state(), x, y);
}
ASSERT(args.length() == 1);
CONVERT_ARG_CHECKED(String, string, 0);
- StringInputBuffer buffer(string);
- while (buffer.has_more()) {
- uint16_t character = buffer.GetNext();
+ ConsStringIteratorOp op;
+ StringCharacterStream stream(string, &op);
+ while (stream.HasMore()) {
+ uint16_t character = stream.GetNext();
PrintF("%c", character);
}
return string;
class RuntimeState {
public:
- StaticResource<StringInputBuffer>* string_input_buffer() {
- return &string_input_buffer_;
+ StaticResource<ConsStringIteratorOp>* string_iterator() {
+ return &string_iterator_;
}
unibrow::Mapping<unibrow::ToUppercase, 128>* to_upper_mapping() {
return &to_upper_mapping_;
unibrow::Mapping<unibrow::ToLowercase, 128>* to_lower_mapping() {
return &to_lower_mapping_;
}
- StringInputBuffer* string_input_buffer_compare_bufx() {
- return &string_input_buffer_compare_bufx_;
+ ConsStringIteratorOp* string_iterator_compare_x() {
+ return &string_iterator_compare_x_;
}
- StringInputBuffer* string_input_buffer_compare_bufy() {
- return &string_input_buffer_compare_bufy_;
+ ConsStringIteratorOp* string_iterator_compare_y() {
+ return &string_iterator_compare_y_;
}
- StringInputBuffer* string_locale_compare_buf1() {
- return &string_locale_compare_buf1_;
+ ConsStringIteratorOp* string_locale_compare_it1() {
+ return &string_locale_compare_it1_;
}
- StringInputBuffer* string_locale_compare_buf2() {
- return &string_locale_compare_buf2_;
+ ConsStringIteratorOp* string_locale_compare_it2() {
+ return &string_locale_compare_it2_;
}
private:
RuntimeState() {}
// Non-reentrant string buffer for efficient general use in the runtime.
- StaticResource<StringInputBuffer> string_input_buffer_;
+ StaticResource<ConsStringIteratorOp> string_iterator_;
unibrow::Mapping<unibrow::ToUppercase, 128> to_upper_mapping_;
unibrow::Mapping<unibrow::ToLowercase, 128> to_lower_mapping_;
- StringInputBuffer string_input_buffer_compare_bufx_;
- StringInputBuffer string_input_buffer_compare_bufy_;
- StringInputBuffer string_locale_compare_buf1_;
- StringInputBuffer string_locale_compare_buf2_;
+ ConsStringIteratorOp string_iterator_compare_x_;
+ ConsStringIteratorOp string_iterator_compare_y_;
+ ConsStringIteratorOp string_locale_compare_it1_;
+ ConsStringIteratorOp string_locale_compare_it2_;
friend class Isolate;
friend class Runtime;
bool StringStream::Put(String* str, int start, int end) {
- StringInputBuffer name_buffer(str);
- name_buffer.Seek(start);
- for (int i = start; i < end && name_buffer.has_more(); i++) {
- int c = name_buffer.GetNext();
+ ConsStringIteratorOp op;
+ StringCharacterStream stream(str, &op, start);
+ for (int i = start; i < end && stream.HasMore(); i++) {
+ uint16_t c = stream.GetNext();
if (c >= 127 || c < 32) {
c = '?';
}
namespace {
-// C++-style iterator adaptor for StringInputBuffer
+// C++-style iterator adaptor for StringCharacterStream
// (unlike C++ iterators the end-marker has different type).
-class StringInputBufferIterator {
+class StringCharacterStreamIterator {
public:
class EndMarker {};
- explicit StringInputBufferIterator(StringInputBuffer* buffer);
+ explicit StringCharacterStreamIterator(StringCharacterStream* stream);
- int operator*() const;
+ uint16_t operator*() const;
void operator++();
bool operator==(EndMarker const&) const { return end_; }
bool operator!=(EndMarker const& m) const { return !end_; }
private:
- StringInputBuffer* const buffer_;
- int current_;
+ StringCharacterStream* const stream_;
+ uint16_t current_;
bool end_;
};
-StringInputBufferIterator::StringInputBufferIterator(
- StringInputBuffer* buffer) : buffer_(buffer) {
+StringCharacterStreamIterator::StringCharacterStreamIterator(
+ StringCharacterStream* stream) : stream_(stream) {
++(*this);
}
-int StringInputBufferIterator::operator*() const {
+uint16_t StringCharacterStreamIterator::operator*() const {
return current_;
}
-void StringInputBufferIterator::operator++() {
- end_ = !buffer_->has_more();
+void StringCharacterStreamIterator::operator++() {
+ end_ = !stream_->HasMore();
if (!end_) {
- current_ = buffer_->GetNext();
+ current_ = stream_->GetNext();
}
}
} // End anonymous namespace.
double StringToDouble(UnicodeCache* unicode_cache,
String* str, int flags, double empty_string_val) {
StringShape shape(str);
+ // TODO(dcarney): Use a Visitor here.
if (shape.IsSequentialAscii()) {
const char* begin = SeqOneByteString::cast(str)->GetChars();
const char* end = begin + str->length();
return InternalStringToDouble(unicode_cache, begin, end, flags,
empty_string_val);
} else {
- StringInputBuffer buffer(str);
+ ConsStringIteratorOp op;
+ StringCharacterStream stream(str, &op);
return InternalStringToDouble(unicode_cache,
- StringInputBufferIterator(&buffer),
- StringInputBufferIterator::EndMarker(),
+ StringCharacterStreamIterator(&stream),
+ StringCharacterStreamIterator::EndMarker(),
flags,
empty_string_val);
}
String* str,
int radix) {
StringShape shape(str);
+ // TODO(dcarney): Use a Visitor here.
if (shape.IsSequentialAscii()) {
const char* begin = SeqOneByteString::cast(str)->GetChars();
const char* end = begin + str->length();
const uc16* end = begin + str->length();
return InternalStringToInt(unicode_cache, begin, end, radix);
} else {
- StringInputBuffer buffer(str);
+ ConsStringIteratorOp op;
+ StringCharacterStream stream(str, &op);
return InternalStringToInt(unicode_cache,
- StringInputBufferIterator(&buffer),
- StringInputBufferIterator::EndMarker(),
+ StringCharacterStreamIterator(&stream),
+ StringCharacterStreamIterator::EndMarker(),
radix);
}
}
void VerifyConsString(Handle<String> root, ConsStringGenerationData* data) {
// Verify basic data.
CHECK(root->IsConsString());
- CHECK((unsigned)root->length() == data->stats_.chars_);
+ CHECK(static_cast<unsigned>(root->length()) == data->stats_.chars_);
// Recursive verify.
ConsStringStats stats;
AccumulateStats(ConsString::cast(*root), &stats);
static void Traverse(Handle<String> s1, Handle<String> s2) {
int i = 0;
buffer.Reset(*s1);
- StringCharacterStream character_stream_1(*s1, 0, &cons_string_iterator_op_1);
- StringCharacterStream character_stream_2(*s2, 0, &cons_string_iterator_op_2);
+ StringCharacterStream character_stream_1(*s1, &cons_string_iterator_op_1);
+ StringCharacterStream character_stream_2(*s2, &cons_string_iterator_op_2);
StringInputBuffer buffer2(*s2);
while (buffer.has_more()) {
CHECK(buffer2.has_more());
int i = 0;
buffer.Reset(*s1);
StringInputBuffer buffer2(*s2);
- StringCharacterStream character_stream_1(*s1, 0, &cons_string_iterator_op_1);
- StringCharacterStream character_stream_2(*s2, 0, &cons_string_iterator_op_2);
+ StringCharacterStream character_stream_1(*s1, &cons_string_iterator_op_1);
+ StringCharacterStream character_stream_2(*s2, &cons_string_iterator_op_2);
while (buffer.has_more() && i < chars) {
CHECK(buffer2.has_more());
CHECK(character_stream_1.HasMore());
// Want to test the offset == length case.
if (offset > length) offset = length;
StringCharacterStream flat_stream(
- flat_string, (unsigned) offset, &cons_string_iterator_op_1);
+ flat_string, &cons_string_iterator_op_1, static_cast<unsigned>(offset));
StringCharacterStream cons_stream(
- cons_string, (unsigned) offset, &cons_string_iterator_op_2);
+ cons_string, &cons_string_iterator_op_2, static_cast<unsigned>(offset));
for (int i = offset; i < length; i++) {
uint16_t c = flat_string->Get(i);
CHECK(flat_stream.HasMore());