jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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> |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 29 | #include "../include/v8stdint.h" |
| 30 | #include "checks.h" |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 31 | #include "platform.h" |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 32 | #include "utils.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 33 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 34 | namespace v8 { |
| 35 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 36 | |
| 37 | |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 38 | SimpleStringBuilder::SimpleStringBuilder(int size) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 39 | buffer_ = Vector<char>::New(size); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 40 | position_ = 0; |
| 41 | } |
| 42 | |
| 43 | |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 44 | void SimpleStringBuilder::AddString(const char* s) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 45 | AddSubstring(s, StrLength(s)); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 49 | void SimpleStringBuilder::AddSubstring(const char* s, int n) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 50 | ASSERT(!is_finalized() && position_ + n <= buffer_.length()); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 51 | ASSERT(static_cast<size_t>(n) <= strlen(s)); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 52 | OS::MemCopy(&buffer_[position_], s, n * kCharSize); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 53 | position_ += n; |
| 54 | } |
| 55 | |
| 56 | |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 57 | void SimpleStringBuilder::AddPadding(char c, int count) { |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 58 | for (int i = 0; i < count; i++) { |
| 59 | AddCharacter(c); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 64 | void SimpleStringBuilder::AddDecimalInteger(int32_t value) { |
| 65 | uint32_t number = static_cast<uint32_t>(value); |
| 66 | if (value < 0) { |
| 67 | AddCharacter('-'); |
| 68 | number = static_cast<uint32_t>(-value); |
| 69 | } |
| 70 | int digits = 1; |
| 71 | for (uint32_t factor = 10; digits < 10; digits++, factor *= 10) { |
| 72 | if (factor > number) break; |
| 73 | } |
| 74 | position_ += digits; |
| 75 | for (int i = 1; i <= digits; i++) { |
| 76 | buffer_[position_ - i] = '0' + static_cast<char>(number % 10); |
| 77 | number /= 10; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | |
| 82 | char* SimpleStringBuilder::Finalize() { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 83 | ASSERT(!is_finalized() && position_ <= buffer_.length()); |
| 84 | // If there is no space for null termination, overwrite last character. |
| 85 | if (position_ == buffer_.length()) { |
| 86 | position_--; |
| 87 | // Print ellipsis. |
| 88 | for (int i = 3; i > 0 && position_ > i; --i) buffer_[position_ - i] = '.'; |
| 89 | } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 90 | buffer_[position_] = '\0'; |
| 91 | // Make sure nobody managed to add a 0-character to the |
| 92 | // buffer while building the string. |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 93 | ASSERT(strlen(buffer_.start()) == static_cast<size_t>(position_)); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 94 | position_ = -1; |
| 95 | ASSERT(is_finalized()); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 96 | return buffer_.start(); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 97 | } |
| 98 | |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 99 | |
| 100 | const DivMagicNumbers DivMagicNumberFor(int32_t divisor) { |
| 101 | switch (divisor) { |
| 102 | case 3: return DivMagicNumberFor3; |
| 103 | case 5: return DivMagicNumberFor5; |
| 104 | case 7: return DivMagicNumberFor7; |
| 105 | case 9: return DivMagicNumberFor9; |
| 106 | case 11: return DivMagicNumberFor11; |
| 107 | case 25: return DivMagicNumberFor25; |
| 108 | case 125: return DivMagicNumberFor125; |
| 109 | case 625: return DivMagicNumberFor625; |
| 110 | default: return InvalidDivMagicNumber; |
| 111 | } |
| 112 | } |
| 113 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 114 | } } // namespace v8::internal |