v8  3.14.5(node0.10.28)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
utils.cc
Go to the documentation of this file.
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 "../include/v8stdint.h"
30 #include "checks.h"
31 #include "utils.h"
32 
33 namespace v8 {
34 namespace internal {
35 
36 
38  buffer_ = Vector<char>::New(size);
39  position_ = 0;
40 }
41 
42 
43 void SimpleStringBuilder::AddString(const char* s) {
44  AddSubstring(s, StrLength(s));
45 }
46 
47 
48 void SimpleStringBuilder::AddSubstring(const char* s, int n) {
50  ASSERT(static_cast<size_t>(n) <= strlen(s));
51  memcpy(&buffer_[position_], s, n * kCharSize);
52  position_ += n;
53 }
54 
55 
56 void SimpleStringBuilder::AddPadding(char c, int count) {
57  for (int i = 0; i < count; i++) {
58  AddCharacter(c);
59  }
60 }
61 
62 
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 
83  buffer_[position_] = '\0';
84  // Make sure nobody managed to add a 0-character to the
85  // buffer while building the string.
86  ASSERT(strlen(buffer_.start()) == static_cast<size_t>(position_));
87  position_ = -1;
89  return buffer_.start();
90 }
91 
92 
94  switch (divisor) {
95  case 3: return DivMagicNumberFor3;
96  case 5: return DivMagicNumberFor5;
97  case 7: return DivMagicNumberFor7;
98  case 9: return DivMagicNumberFor9;
99  case 11: return DivMagicNumberFor11;
100  case 25: return DivMagicNumberFor25;
101  case 125: return DivMagicNumberFor125;
102  case 625: return DivMagicNumberFor625;
103  default: return InvalidDivMagicNumber;
104  }
105 }
106 
107 } } // namespace v8::internal
const DivMagicNumbers DivMagicNumberFor625
Definition: utils.h:109
const DivMagicNumbers DivMagicNumberFor(int32_t divisor)
Definition: utils.cc:93
void AddString(const char *s)
Definition: utils.cc:43
const DivMagicNumbers DivMagicNumberFor3
Definition: utils.h:102
const DivMagicNumbers DivMagicNumberFor5
Definition: utils.h:103
const DivMagicNumbers DivMagicNumberFor9
Definition: utils.h:105
int int32_t
Definition: unicode.cc:47
#define ASSERT(condition)
Definition: checks.h:270
int length() const
Definition: utils.h:384
const DivMagicNumbers DivMagicNumberFor125
Definition: utils.h:108
static Vector< T > New(int length)
Definition: utils.h:370
int StrLength(const char *string)
Definition: utils.h:234
const DivMagicNumbers DivMagicNumberFor11
Definition: utils.h:106
void AddSubstring(const char *s, int n)
Definition: utils.cc:48
const DivMagicNumbers DivMagicNumberFor25
Definition: utils.h:107
void AddDecimalInteger(int value)
Definition: utils.cc:63
void AddPadding(char c, int count)
Definition: utils.cc:56
const DivMagicNumbers InvalidDivMagicNumber
Definition: utils.h:101
const int kCharSize
Definition: globals.h:215
const DivMagicNumbers DivMagicNumberFor7
Definition: utils.h:104