Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "platform/text_buffer.h" | 5 #include "platform/text_buffer.h" |
| 6 | 6 |
| 7 #include "platform/assert.h" | 7 #include "platform/assert.h" |
| 8 #include "platform/globals.h" | 8 #include "platform/globals.h" |
| 9 #include "platform/utils.h" | 9 #include "platform/utils.h" |
| 10 #include "vm/os.h" | 10 #include "vm/os.h" |
| 11 #include "vm/unicode.h" | |
| 11 | 12 |
| 12 namespace dart { | 13 namespace dart { |
| 13 | 14 |
| 14 TextBuffer::TextBuffer(intptr_t buf_size) { | 15 TextBuffer::TextBuffer(intptr_t buf_size) { |
| 15 ASSERT(buf_size > 0); | 16 ASSERT(buf_size > 0); |
| 16 buf_ = reinterpret_cast<char*>(malloc(buf_size)); | 17 buf_ = reinterpret_cast<char*>(malloc(buf_size)); |
| 17 buf_size_ = buf_size; | 18 buf_size_ = buf_size; |
| 18 Clear(); | 19 Clear(); |
| 19 } | 20 } |
| 20 | 21 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 71 va_start(args2, format); | 72 va_start(args2, format); |
| 72 intptr_t len2 = OS::VSNPrint(buf_ + msg_len_, remaining, format, args2); | 73 intptr_t len2 = OS::VSNPrint(buf_ + msg_len_, remaining, format, args2); |
| 73 va_end(args2); | 74 va_end(args2); |
| 74 ASSERT(len == len2); | 75 ASSERT(len == len2); |
| 75 } | 76 } |
| 76 msg_len_ += len; | 77 msg_len_ += len; |
| 77 buf_[msg_len_] = '\0'; | 78 buf_[msg_len_] = '\0'; |
| 78 return len; | 79 return len; |
| 79 } | 80 } |
| 80 | 81 |
| 81 | 82 // Write a UTF-32 code unit so it can be read by a JSON parser in a string |
| 82 // Write a UTF-16 code unit so it can be read by a JSON parser in a string | 83 // literal. Use official encoding from JSON specification. http://json.org/ |
| 83 // literal. Use escape sequences for characters other than printable ASCII. | |
| 84 void TextBuffer::EscapeAndAddCodeUnit(uint32_t codeunit) { | 84 void TextBuffer::EscapeAndAddCodeUnit(uint32_t codeunit) { |
|
rmacnak
2016/06/21 16:56:56
Perhaps this should be split into EscapeAndAddUTF1
cbernaschina
2016/06/21 21:24:43
Done.
| |
| 85 switch (codeunit) { | 85 switch (codeunit) { |
| 86 case '"': | 86 case '"': |
| 87 Printf("%s", "\\\""); | 87 Printf("%s", "\\\""); |
| 88 break; | 88 break; |
| 89 case '\\': | 89 case '\\': |
| 90 Printf("%s", "\\\\"); | 90 Printf("%s", "\\\\"); |
| 91 break; | 91 break; |
| 92 case '/': | 92 case '/': |
| 93 Printf("%s", "\\/"); | 93 Printf("%s", "\\/"); |
| 94 break; | 94 break; |
| 95 case '\b': | 95 case '\b': |
| 96 Printf("%s", "\\b"); | 96 Printf("%s", "\\b"); |
| 97 break; | 97 break; |
| 98 case '\f': | 98 case '\f': |
| 99 Printf("%s", "\\f"); | 99 Printf("%s", "\\f"); |
| 100 break; | 100 break; |
| 101 case '\n': | 101 case '\n': |
| 102 Printf("%s", "\\n"); | 102 Printf("%s", "\\n"); |
| 103 break; | 103 break; |
| 104 case '\r': | 104 case '\r': |
| 105 Printf("%s", "\\r"); | 105 Printf("%s", "\\r"); |
| 106 break; | 106 break; |
| 107 case '\t': | 107 case '\t': |
| 108 Printf("%s", "\\t"); | 108 Printf("%s", "\\t"); |
| 109 break; | 109 break; |
| 110 default: | 110 default: |
| 111 if (codeunit < 0x20) { | 111 char encoded[6]; |
| 112 // Encode character as \u00HH. | 112 intptr_t length = Utf8::Length(codeunit); |
| 113 uint32_t digit2 = (codeunit >> 4) & 0xf; | 113 Utf8::Encode(codeunit, encoded); |
| 114 uint32_t digit3 = (codeunit & 0xf); | 114 AddRaw(reinterpret_cast<uint8_t*>(encoded), length); |
| 115 Printf("\\u00%c%c", | |
| 116 digit2 > 9 ? 'A' + (digit2 - 10) : '0' + digit2, | |
| 117 digit3 > 9 ? 'A' + (digit3 - 10) : '0' + digit3); | |
| 118 } else if (codeunit > 127) { | |
| 119 // Encode character as \uHHHH. | |
| 120 uint32_t digit0 = (codeunit >> 12) & 0xf; | |
| 121 uint32_t digit1 = (codeunit >> 8) & 0xf; | |
| 122 uint32_t digit2 = (codeunit >> 4) & 0xf; | |
| 123 uint32_t digit3 = (codeunit & 0xf); | |
| 124 Printf("\\u%c%c%c%c", | |
| 125 digit0 > 9 ? 'A' + (digit0 - 10) : '0' + digit0, | |
| 126 digit1 > 9 ? 'A' + (digit1 - 10) : '0' + digit1, | |
| 127 digit2 > 9 ? 'A' + (digit2 - 10) : '0' + digit2, | |
| 128 digit3 > 9 ? 'A' + (digit3 - 10) : '0' + digit3); | |
| 129 } else { | |
| 130 AddChar(codeunit); | |
| 131 } | |
| 132 } | 115 } |
| 133 } | 116 } |
| 134 | 117 |
| 135 | 118 |
| 136 void TextBuffer::AddString(const char* s) { | 119 void TextBuffer::AddString(const char* s) { |
| 137 Printf("%s", s); | 120 Printf("%s", s); |
| 138 } | 121 } |
| 139 | 122 |
| 140 | 123 |
| 141 void TextBuffer::AddEscapedString(const char* s) { | 124 void TextBuffer::AddEscapedString(const char* s) { |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 156 // the debugger front-end. | 139 // the debugger front-end. |
| 157 intptr_t new_size = buf_size_ + len + kBufferSpareCapacity; | 140 intptr_t new_size = buf_size_ + len + kBufferSpareCapacity; |
| 158 char* new_buf = reinterpret_cast<char*>(realloc(buf_, new_size)); | 141 char* new_buf = reinterpret_cast<char*>(realloc(buf_, new_size)); |
| 159 ASSERT(new_buf != NULL); | 142 ASSERT(new_buf != NULL); |
| 160 buf_ = new_buf; | 143 buf_ = new_buf; |
| 161 buf_size_ = new_size; | 144 buf_size_ = new_size; |
| 162 } | 145 } |
| 163 } | 146 } |
| 164 | 147 |
| 165 } // namespace dart | 148 } // namespace dart |
| OLD | NEW |