jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include <stdarg.h> |
| 29 | #include <limits.h> |
| 30 | |
| 31 | #include "v8.h" |
| 32 | |
| 33 | #include "conversions-inl.h" |
| 34 | #include "v8conversions.h" |
| 35 | #include "dtoa.h" |
| 36 | #include "factory.h" |
| 37 | #include "scanner-base.h" |
| 38 | #include "strtod.h" |
| 39 | |
| 40 | namespace v8 { |
| 41 | namespace internal { |
| 42 | |
| 43 | namespace { |
| 44 | |
| 45 | // C++-style iterator adaptor for StringInputBuffer |
| 46 | // (unlike C++ iterators the end-marker has different type). |
| 47 | class StringInputBufferIterator { |
| 48 | public: |
| 49 | class EndMarker {}; |
| 50 | |
| 51 | explicit StringInputBufferIterator(StringInputBuffer* buffer); |
| 52 | |
| 53 | int operator*() const; |
| 54 | void operator++(); |
| 55 | bool operator==(EndMarker const&) const { return end_; } |
| 56 | bool operator!=(EndMarker const& m) const { return !end_; } |
| 57 | |
| 58 | private: |
| 59 | StringInputBuffer* const buffer_; |
| 60 | int current_; |
| 61 | bool end_; |
| 62 | }; |
| 63 | |
| 64 | |
| 65 | StringInputBufferIterator::StringInputBufferIterator( |
| 66 | StringInputBuffer* buffer) : buffer_(buffer) { |
| 67 | ++(*this); |
| 68 | } |
| 69 | |
| 70 | int StringInputBufferIterator::operator*() const { |
| 71 | return current_; |
| 72 | } |
| 73 | |
| 74 | |
| 75 | void StringInputBufferIterator::operator++() { |
| 76 | end_ = !buffer_->has_more(); |
| 77 | if (!end_) { |
| 78 | current_ = buffer_->GetNext(); |
| 79 | } |
| 80 | } |
| 81 | } // End anonymous namespace. |
| 82 | |
| 83 | |
| 84 | double StringToDouble(UnicodeCache* unicode_cache, |
| 85 | String* str, int flags, double empty_string_val) { |
| 86 | StringShape shape(str); |
| 87 | if (shape.IsSequentialAscii()) { |
| 88 | const char* begin = SeqAsciiString::cast(str)->GetChars(); |
| 89 | const char* end = begin + str->length(); |
| 90 | return InternalStringToDouble(unicode_cache, begin, end, flags, |
| 91 | empty_string_val); |
| 92 | } else if (shape.IsSequentialTwoByte()) { |
| 93 | const uc16* begin = SeqTwoByteString::cast(str)->GetChars(); |
| 94 | const uc16* end = begin + str->length(); |
| 95 | return InternalStringToDouble(unicode_cache, begin, end, flags, |
| 96 | empty_string_val); |
| 97 | } else { |
| 98 | StringInputBuffer buffer(str); |
| 99 | return InternalStringToDouble(unicode_cache, |
| 100 | StringInputBufferIterator(&buffer), |
| 101 | StringInputBufferIterator::EndMarker(), |
| 102 | flags, |
| 103 | empty_string_val); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | |
| 108 | double StringToInt(UnicodeCache* unicode_cache, |
| 109 | String* str, |
| 110 | int radix) { |
| 111 | StringShape shape(str); |
| 112 | if (shape.IsSequentialAscii()) { |
| 113 | const char* begin = SeqAsciiString::cast(str)->GetChars(); |
| 114 | const char* end = begin + str->length(); |
| 115 | return InternalStringToInt(unicode_cache, begin, end, radix); |
| 116 | } else if (shape.IsSequentialTwoByte()) { |
| 117 | const uc16* begin = SeqTwoByteString::cast(str)->GetChars(); |
| 118 | const uc16* end = begin + str->length(); |
| 119 | return InternalStringToInt(unicode_cache, begin, end, radix); |
| 120 | } else { |
| 121 | StringInputBuffer buffer(str); |
| 122 | return InternalStringToInt(unicode_cache, |
| 123 | StringInputBufferIterator(&buffer), |
| 124 | StringInputBufferIterator::EndMarker(), |
| 125 | radix); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | } } // namespace v8::internal |