OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 #include "platform/text_buffer.h" |
| 6 |
| 7 #include "platform/assert.h" |
| 8 #include "platform/globals.h" |
| 9 #include "platform/utils.h" |
| 10 #include "vm/os.h" |
| 11 |
| 12 namespace dart { |
| 13 |
| 14 TextBuffer::TextBuffer(intptr_t buf_size) { |
| 15 ASSERT(buf_size > 0); |
| 16 buf_ = reinterpret_cast<char*>(malloc(buf_size)); |
| 17 buf_size_ = buf_size; |
| 18 Clear(); |
| 19 } |
| 20 |
| 21 |
| 22 TextBuffer::~TextBuffer() { |
| 23 free(buf_); |
| 24 buf_ = NULL; |
| 25 } |
| 26 |
| 27 |
| 28 void TextBuffer::Clear() { |
| 29 msg_len_ = 0; |
| 30 buf_[0] = '\0'; |
| 31 } |
| 32 |
| 33 |
| 34 const char* TextBuffer::Steal() { |
| 35 const char* r = buf_; |
| 36 buf_ = NULL; |
| 37 buf_size_ = 0; |
| 38 msg_len_ = 0; |
| 39 return r; |
| 40 } |
| 41 |
| 42 |
| 43 void TextBuffer::AddChar(char ch) { |
| 44 EnsureCapacity(sizeof(ch)); |
| 45 buf_[msg_len_] = ch; |
| 46 msg_len_++; |
| 47 buf_[msg_len_] = '\0'; |
| 48 } |
| 49 |
| 50 |
| 51 intptr_t TextBuffer::Printf(const char* format, ...) { |
| 52 va_list args; |
| 53 va_start(args, format); |
| 54 intptr_t remaining = buf_size_ - msg_len_; |
| 55 ASSERT(remaining >= 0); |
| 56 intptr_t len = OS::VSNPrint(buf_ + msg_len_, remaining, format, args); |
| 57 va_end(args); |
| 58 if (len >= remaining) { |
| 59 EnsureCapacity(len); |
| 60 remaining = buf_size_ - msg_len_; |
| 61 ASSERT(remaining > len); |
| 62 va_list args2; |
| 63 va_start(args2, format); |
| 64 intptr_t len2 = OS::VSNPrint(buf_ + msg_len_, remaining, format, args2); |
| 65 va_end(args2); |
| 66 ASSERT(len == len2); |
| 67 } |
| 68 msg_len_ += len; |
| 69 buf_[msg_len_] = '\0'; |
| 70 return len; |
| 71 } |
| 72 |
| 73 |
| 74 // Write a UTF-16 code unit so it can be read by a JSON parser in a string |
| 75 // literal. Use escape sequences for characters other than printable ASCII. |
| 76 void TextBuffer::EscapeAndAddCodeUnit(uint32_t codeunit) { |
| 77 switch (codeunit) { |
| 78 case '"': |
| 79 Printf("%s", "\\\""); |
| 80 break; |
| 81 case '\\': |
| 82 Printf("%s", "\\\\"); |
| 83 break; |
| 84 case '/': |
| 85 Printf("%s", "\\/"); |
| 86 break; |
| 87 case '\b': |
| 88 Printf("%s", "\\b"); |
| 89 break; |
| 90 case '\f': |
| 91 Printf("%s", "\\f"); |
| 92 break; |
| 93 case '\n': |
| 94 Printf("%s", "\\n"); |
| 95 break; |
| 96 case '\r': |
| 97 Printf("%s", "\\r"); |
| 98 break; |
| 99 case '\t': |
| 100 Printf("%s", "\\t"); |
| 101 break; |
| 102 default: |
| 103 if (codeunit < 0x20) { |
| 104 // Encode character as \u00HH. |
| 105 uint32_t digit2 = (codeunit >> 4) & 0xf; |
| 106 uint32_t digit3 = (codeunit & 0xf); |
| 107 Printf("\\u00%c%c", |
| 108 digit2 > 9 ? 'A' + (digit2 - 10) : '0' + digit2, |
| 109 digit3 > 9 ? 'A' + (digit3 - 10) : '0' + digit3); |
| 110 } else if (codeunit > 127) { |
| 111 // Encode character as \uHHHH. |
| 112 uint32_t digit0 = (codeunit >> 12) & 0xf; |
| 113 uint32_t digit1 = (codeunit >> 8) & 0xf; |
| 114 uint32_t digit2 = (codeunit >> 4) & 0xf; |
| 115 uint32_t digit3 = (codeunit & 0xf); |
| 116 Printf("\\u%c%c%c%c", |
| 117 digit0 > 9 ? 'A' + (digit0 - 10) : '0' + digit0, |
| 118 digit1 > 9 ? 'A' + (digit1 - 10) : '0' + digit1, |
| 119 digit2 > 9 ? 'A' + (digit2 - 10) : '0' + digit2, |
| 120 digit3 > 9 ? 'A' + (digit3 - 10) : '0' + digit3); |
| 121 } else { |
| 122 AddChar(codeunit); |
| 123 } |
| 124 } |
| 125 } |
| 126 |
| 127 |
| 128 void TextBuffer::AddString(const char* s) { |
| 129 Printf("%s", s); |
| 130 } |
| 131 |
| 132 |
| 133 void TextBuffer::AddEscapedString(const char* s) { |
| 134 intptr_t len = strlen(s); |
| 135 for (int i = 0; i < len; i++) { |
| 136 EscapeAndAddCodeUnit(s[i]); |
| 137 } |
| 138 } |
| 139 |
| 140 |
| 141 void TextBuffer::EnsureCapacity(intptr_t len) { |
| 142 intptr_t remaining = buf_size_ - msg_len_; |
| 143 if (remaining <= len) { |
| 144 const int kBufferSpareCapacity = 64; // Somewhat arbitrary. |
| 145 // TODO(turnidge): do we need to guard against overflow or other |
| 146 // security issues here? Text buffers are used by the debugger |
| 147 // to send user-controlled data (e.g. values of string variables) to |
| 148 // the debugger front-end. |
| 149 intptr_t new_size = buf_size_ + len + kBufferSpareCapacity; |
| 150 char* new_buf = reinterpret_cast<char*>(realloc(buf_, new_size)); |
| 151 ASSERT(new_buf != NULL); |
| 152 buf_ = new_buf; |
| 153 buf_size_ = new_size; |
| 154 } |
| 155 } |
| 156 |
| 157 } // namespace dart |
OLD | NEW |