blob: 89ef4c6e3e389397e79e55b55efcd4b557260475 [file] [log] [blame]
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001// Copyright 2011 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// 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.orgddda9e82011-07-06 11:27:02 +000029#include "../include/v8stdint.h"
30#include "checks.h"
31#include "utils.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000032
kasperl@chromium.org71affb52009-05-26 05:44:31 +000033namespace v8 {
34namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000035
36
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +000037SimpleStringBuilder::SimpleStringBuilder(int size) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000038 buffer_ = Vector<char>::New(size);
kasper.lund7276f142008-07-30 08:49:36 +000039 position_ = 0;
40}
41
42
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +000043void SimpleStringBuilder::AddString(const char* s) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +000044 AddSubstring(s, StrLength(s));
kasper.lund7276f142008-07-30 08:49:36 +000045}
46
47
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +000048void SimpleStringBuilder::AddSubstring(const char* s, int n) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000049 ASSERT(!is_finalized() && position_ + n < buffer_.length());
kasper.lund7276f142008-07-30 08:49:36 +000050 ASSERT(static_cast<size_t>(n) <= strlen(s));
51 memcpy(&buffer_[position_], s, n * kCharSize);
52 position_ += n;
53}
54
55
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +000056void SimpleStringBuilder::AddPadding(char c, int count) {
kasper.lund7276f142008-07-30 08:49:36 +000057 for (int i = 0; i < count; i++) {
58 AddCharacter(c);
59 }
60}
61
62
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +000063void SimpleStringBuilder::AddDecimalInteger(int32_t value) {
64 uint32_t number = static_cast<uint32_t>(value);
65 if (value < 0) {
66 AddCharacter('-');
67 number = static_cast<uint32_t>(-value);
68 }
69 int digits = 1;
70 for (uint32_t factor = 10; digits < 10; digits++, factor *= 10) {
71 if (factor > number) break;
72 }
73 position_ += digits;
74 for (int i = 1; i <= digits; i++) {
75 buffer_[position_ - i] = '0' + static_cast<char>(number % 10);
76 number /= 10;
77 }
78}
79
80
81char* SimpleStringBuilder::Finalize() {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000082 ASSERT(!is_finalized() && position_ < buffer_.length());
kasper.lund7276f142008-07-30 08:49:36 +000083 buffer_[position_] = '\0';
84 // Make sure nobody managed to add a 0-character to the
85 // buffer while building the string.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000086 ASSERT(strlen(buffer_.start()) == static_cast<size_t>(position_));
kasper.lund7276f142008-07-30 08:49:36 +000087 position_ = -1;
88 ASSERT(is_finalized());
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000089 return buffer_.start();
kasper.lund7276f142008-07-30 08:49:36 +000090}
91
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000092} } // namespace v8::internal