OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 | 50 |
51 | 51 |
52 NoAllocationStringAllocator::NoAllocationStringAllocator(char* memory, | 52 NoAllocationStringAllocator::NoAllocationStringAllocator(char* memory, |
53 unsigned size) { | 53 unsigned size) { |
54 size_ = size; | 54 size_ = size; |
55 space_ = memory; | 55 space_ = memory; |
56 } | 56 } |
57 | 57 |
58 | 58 |
59 bool StringStream::Put(char c) { | 59 bool StringStream::Put(char c) { |
60 if (space() == 0) return false; | 60 if (full()) return false; |
61 if (length_ >= capacity_ - 1) { | 61 ASSERT(length_ < capacity_); |
| 62 // Since the trailing '\0' is not accounted for in length_ fullness is |
| 63 // indicated by a difference of 1 between length_ and capacity_. Thus when |
| 64 // reaching a difference of 2 we need to grow the buffer. |
| 65 if (length_ == capacity_ - 2) { // |
62 unsigned new_capacity = capacity_; | 66 unsigned new_capacity = capacity_; |
63 char* new_buffer = allocator_->grow(&new_capacity); | 67 char* new_buffer = allocator_->grow(&new_capacity); |
64 if (new_capacity > capacity_) { | 68 if (new_capacity > capacity_) { |
65 capacity_ = new_capacity; | 69 capacity_ = new_capacity; |
66 buffer_ = new_buffer; | 70 buffer_ = new_buffer; |
67 } else { | 71 } else { |
68 // Indicate truncation with dots. | 72 // Reached the end of the available buffer. |
69 memset(cursor(), '.', space()); | 73 ASSERT(capacity_ >= 5); |
70 length_ = capacity_; | 74 length_ = capacity_ - 1; // Indicate fullness of the stream. |
71 buffer_[length_ - 2] = '\n'; | 75 buffer_[length_ - 4] = '.'; |
72 buffer_[length_ - 1] = '\0'; | 76 buffer_[length_ - 3] = '.'; |
| 77 buffer_[length_ - 2] = '.'; |
| 78 buffer_[length_ - 1] = '\n'; |
| 79 buffer_[length_] = '\0'; |
73 return false; | 80 return false; |
74 } | 81 } |
75 } | 82 } |
76 buffer_[length_] = c; | 83 buffer_[length_] = c; |
77 buffer_[length_ + 1] = '\0'; | 84 buffer_[length_ + 1] = '\0'; |
78 length_++; | 85 length_++; |
79 return true; | 86 return true; |
80 } | 87 } |
81 | 88 |
82 | 89 |
83 // A control character is one that configures a format element. For | 90 // A control character is one that configures a format element. For |
84 // instance, in %.5s, .5 are control characters. | 91 // instance, in %.5s, .5 are control characters. |
85 static bool IsControlChar(char c) { | 92 static bool IsControlChar(char c) { |
86 switch (c) { | 93 switch (c) { |
87 case '0': case '1': case '2': case '3': case '4': case '5': | 94 case '0': case '1': case '2': case '3': case '4': case '5': |
88 case '6': case '7': case '8': case '9': case '.': case '-': | 95 case '6': case '7': case '8': case '9': case '.': case '-': |
89 return true; | 96 return true; |
90 default: | 97 default: |
91 return false; | 98 return false; |
92 } | 99 } |
93 } | 100 } |
94 | 101 |
95 | 102 |
96 void StringStream::Add(Vector<const char> format, Vector<FmtElm> elms) { | 103 void StringStream::Add(Vector<const char> format, Vector<FmtElm> elms) { |
97 // If we already ran out of space then return immediately. | 104 // If we already ran out of space then return immediately. |
98 if (space() == 0) | 105 if (full()) return; |
99 return; | |
100 int offset = 0; | 106 int offset = 0; |
101 int elm = 0; | 107 int elm = 0; |
102 while (offset < format.length()) { | 108 while (offset < format.length()) { |
103 if (format[offset] != '%' || elm == elms.length()) { | 109 if (format[offset] != '%' || elm == elms.length()) { |
104 Put(format[offset]); | 110 Put(format[offset]); |
105 offset++; | 111 offset++; |
106 continue; | 112 continue; |
107 } | 113 } |
108 // Read this formatting directive into a temporary buffer | 114 // Read this formatting directive into a temporary buffer |
109 EmbeddedVector<char, 24> temp; | 115 EmbeddedVector<char, 24> temp; |
(...skipping 447 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
557 return space_; | 563 return space_; |
558 } | 564 } |
559 memcpy(new_space, space_, *bytes); | 565 memcpy(new_space, space_, *bytes); |
560 *bytes = new_bytes; | 566 *bytes = new_bytes; |
561 DeleteArray(space_); | 567 DeleteArray(space_); |
562 space_ = new_space; | 568 space_ = new_space; |
563 return new_space; | 569 return new_space; |
564 } | 570 } |
565 | 571 |
566 | 572 |
| 573 // Only grow once to the maximum allowable size. |
567 char* NoAllocationStringAllocator::grow(unsigned* bytes) { | 574 char* NoAllocationStringAllocator::grow(unsigned* bytes) { |
568 unsigned new_bytes = *bytes * 2; | 575 ASSERT(size_ >= *bytes); |
569 if (new_bytes > size_) { | 576 *bytes = size_; |
570 new_bytes = size_; | |
571 } | |
572 *bytes = new_bytes; | |
573 return space_; | 577 return space_; |
574 } | 578 } |
575 | 579 |
576 | 580 |
577 } } // namespace v8::internal | 581 } } // namespace v8::internal |
OLD | NEW |