OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 28 matching lines...) Expand all Loading... |
39 position_ = 0; | 39 position_ = 0; |
40 } | 40 } |
41 | 41 |
42 | 42 |
43 void SimpleStringBuilder::AddString(const char* s) { | 43 void SimpleStringBuilder::AddString(const char* s) { |
44 AddSubstring(s, StrLength(s)); | 44 AddSubstring(s, StrLength(s)); |
45 } | 45 } |
46 | 46 |
47 | 47 |
48 void SimpleStringBuilder::AddSubstring(const char* s, int n) { | 48 void SimpleStringBuilder::AddSubstring(const char* s, int n) { |
49 ASSERT(!is_finalized() && position_ + n < buffer_.length()); | 49 ASSERT(!is_finalized() && position_ + n <= buffer_.length()); |
50 ASSERT(static_cast<size_t>(n) <= strlen(s)); | 50 ASSERT(static_cast<size_t>(n) <= strlen(s)); |
51 memcpy(&buffer_[position_], s, n * kCharSize); | 51 memcpy(&buffer_[position_], s, n * kCharSize); |
52 position_ += n; | 52 position_ += n; |
53 } | 53 } |
54 | 54 |
55 | 55 |
56 void SimpleStringBuilder::AddPadding(char c, int count) { | 56 void SimpleStringBuilder::AddPadding(char c, int count) { |
57 for (int i = 0; i < count; i++) { | 57 for (int i = 0; i < count; i++) { |
58 AddCharacter(c); | 58 AddCharacter(c); |
59 } | 59 } |
(...skipping 12 matching lines...) Expand all Loading... |
72 } | 72 } |
73 position_ += digits; | 73 position_ += digits; |
74 for (int i = 1; i <= digits; i++) { | 74 for (int i = 1; i <= digits; i++) { |
75 buffer_[position_ - i] = '0' + static_cast<char>(number % 10); | 75 buffer_[position_ - i] = '0' + static_cast<char>(number % 10); |
76 number /= 10; | 76 number /= 10; |
77 } | 77 } |
78 } | 78 } |
79 | 79 |
80 | 80 |
81 char* SimpleStringBuilder::Finalize() { | 81 char* SimpleStringBuilder::Finalize() { |
82 ASSERT(!is_finalized() && position_ < buffer_.length()); | 82 ASSERT(!is_finalized() && position_ <= buffer_.length()); |
| 83 // If there is no space for null termination, overwrite last character. |
| 84 if (position_ == buffer_.length()) { |
| 85 position_--; |
| 86 // Print ellipsis. |
| 87 for (int i = 3; i > 0 && position_ > i; --i) buffer_[position_ - i] = '.'; |
| 88 } |
83 buffer_[position_] = '\0'; | 89 buffer_[position_] = '\0'; |
84 // Make sure nobody managed to add a 0-character to the | 90 // Make sure nobody managed to add a 0-character to the |
85 // buffer while building the string. | 91 // buffer while building the string. |
86 ASSERT(strlen(buffer_.start()) == static_cast<size_t>(position_)); | 92 ASSERT(strlen(buffer_.start()) == static_cast<size_t>(position_)); |
87 position_ = -1; | 93 position_ = -1; |
88 ASSERT(is_finalized()); | 94 ASSERT(is_finalized()); |
89 return buffer_.start(); | 95 return buffer_.start(); |
90 } | 96 } |
91 | 97 |
92 | 98 |
93 const DivMagicNumbers DivMagicNumberFor(int32_t divisor) { | 99 const DivMagicNumbers DivMagicNumberFor(int32_t divisor) { |
94 switch (divisor) { | 100 switch (divisor) { |
95 case 3: return DivMagicNumberFor3; | 101 case 3: return DivMagicNumberFor3; |
96 case 5: return DivMagicNumberFor5; | 102 case 5: return DivMagicNumberFor5; |
97 case 7: return DivMagicNumberFor7; | 103 case 7: return DivMagicNumberFor7; |
98 case 9: return DivMagicNumberFor9; | 104 case 9: return DivMagicNumberFor9; |
99 case 11: return DivMagicNumberFor11; | 105 case 11: return DivMagicNumberFor11; |
100 case 25: return DivMagicNumberFor25; | 106 case 25: return DivMagicNumberFor25; |
101 case 125: return DivMagicNumberFor125; | 107 case 125: return DivMagicNumberFor125; |
102 case 625: return DivMagicNumberFor625; | 108 case 625: return DivMagicNumberFor625; |
103 default: return InvalidDivMagicNumber; | 109 default: return InvalidDivMagicNumber; |
104 } | 110 } |
105 } | 111 } |
106 | 112 |
107 } } // namespace v8::internal | 113 } } // namespace v8::internal |
OLD | NEW |