OLD | NEW |
---|---|
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/factory.h" | 5 #include "src/factory.h" |
6 | 6 |
7 #include "src/accessors.h" | 7 #include "src/accessors.h" |
8 #include "src/allocation-site-scopes.h" | 8 #include "src/allocation-site-scopes.h" |
9 #include "src/base/bits.h" | 9 #include "src/base/bits.h" |
10 #include "src/bootstrapper.h" | 10 #include "src/bootstrapper.h" |
(...skipping 288 matching lines...) Loading... | |
299 uint16_t* data = result->GetChars(); | 299 uint16_t* data = result->GetChars(); |
300 const char* ascii_data = string.start(); | 300 const char* ascii_data = string.start(); |
301 for (int i = 0; i < non_ascii_start; i++) { | 301 for (int i = 0; i < non_ascii_start; i++) { |
302 *data++ = *ascii_data++; | 302 *data++ = *ascii_data++; |
303 } | 303 } |
304 // Now write the remainder. | 304 // Now write the remainder. |
305 decoder->WriteUtf16(data, utf16_length); | 305 decoder->WriteUtf16(data, utf16_length); |
306 return result; | 306 return result; |
307 } | 307 } |
308 | 308 |
309 MaybeHandle<String> Factory::NewStringFromUtf8SubString( | |
310 Handle<SeqOneByteString> str, int begin, int length, | |
311 PretenureFlag pretenure) { | |
312 // Check for ASCII first since this is the common case. | |
313 const char* start = reinterpret_cast<const char*>(str->GetChars() + begin); | |
314 int non_ascii_start = String::NonAsciiStart(start, length); | |
315 if (non_ascii_start >= length) { | |
316 // If the string is ASCII, we can just make a substring. | |
317 // TODO(v8): the pretenure flag is ignored in this case. | |
318 return NewSubString(str, begin, begin + length); | |
319 } | |
320 | |
321 // Non-ASCII and we need to decode. | |
322 Access<UnicodeCache::Utf8Decoder> decoder( | |
323 isolate()->unicode_cache()->utf8_decoder()); | |
324 decoder->Reset(start + non_ascii_start, length - non_ascii_start); | |
325 int utf16_length = static_cast<int>(decoder->Utf16Length()); | |
326 DCHECK(utf16_length > 0); | |
327 // Allocate string. | |
328 Handle<SeqTwoByteString> result; | |
329 ASSIGN_RETURN_ON_EXCEPTION( | |
330 isolate(), result, | |
331 NewRawTwoByteString(non_ascii_start + utf16_length, pretenure), String); | |
332 | |
333 // Reset the decoder, because the original {str} may have moved. | |
334 const char* ascii_data = | |
335 reinterpret_cast<const char*>(str->GetChars() + begin); | |
336 decoder->Reset(ascii_data + non_ascii_start, length - non_ascii_start); | |
337 // Copy ASCII portion. | |
338 uint16_t* data = result->GetChars(); | |
339 for (int i = 0; i < non_ascii_start; i++) { | |
Mircea Trofin
2016/10/19 05:28:55
can't this be a condition on ascii_data not hittin
titzer
2016/10/19 09:54:40
This is essentially a copy of the above code, so f
| |
340 *data++ = *ascii_data++; | |
341 } | |
342 // Now write the remainder. | |
343 decoder->WriteUtf16(data, utf16_length); | |
344 return result; | |
345 } | |
346 | |
309 MaybeHandle<String> Factory::NewStringFromTwoByte(const uc16* string, | 347 MaybeHandle<String> Factory::NewStringFromTwoByte(const uc16* string, |
310 int length, | 348 int length, |
311 PretenureFlag pretenure) { | 349 PretenureFlag pretenure) { |
312 if (String::IsOneByte(string, length)) { | 350 if (String::IsOneByte(string, length)) { |
313 if (length == 1) return LookupSingleCharacterStringFromCode(string[0]); | 351 if (length == 1) return LookupSingleCharacterStringFromCode(string[0]); |
314 Handle<SeqOneByteString> result; | 352 Handle<SeqOneByteString> result; |
315 ASSIGN_RETURN_ON_EXCEPTION( | 353 ASSIGN_RETURN_ON_EXCEPTION( |
316 isolate(), | 354 isolate(), |
317 result, | 355 result, |
318 NewRawOneByteString(length, pretenure), | 356 NewRawOneByteString(length, pretenure), |
(...skipping 2351 matching lines...) Loading... | |
2670 Handle<JSFixedArrayIterator>::cast(NewJSObjectFromMap(map)); | 2708 Handle<JSFixedArrayIterator>::cast(NewJSObjectFromMap(map)); |
2671 iterator->set_initial_next(*next); | 2709 iterator->set_initial_next(*next); |
2672 iterator->set_array(*array); | 2710 iterator->set_array(*array); |
2673 iterator->set_index(0); | 2711 iterator->set_index(0); |
2674 iterator->InObjectPropertyAtPut(JSFixedArrayIterator::kNextIndex, *next); | 2712 iterator->InObjectPropertyAtPut(JSFixedArrayIterator::kNextIndex, *next); |
2675 return iterator; | 2713 return iterator; |
2676 } | 2714 } |
2677 | 2715 |
2678 } // namespace internal | 2716 } // namespace internal |
2679 } // namespace v8 | 2717 } // namespace v8 |
OLD | NEW |