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) { |
85 switch (codeunit) { | 85 switch (codeunit) { |
86 case '"': | 86 case '"': |
87 Printf("%s", "\\\""); | 87 AddRaw(reinterpret_cast<uint8_t const*>("\\\""), 2); |
88 break; | 88 break; |
89 case '\\': | 89 case '\\': |
90 Printf("%s", "\\\\"); | 90 AddRaw(reinterpret_cast<uint8_t const*>("\\\\"), 2); |
91 break; | 91 break; |
92 case '/': | 92 case '/': |
93 Printf("%s", "\\/"); | 93 AddRaw(reinterpret_cast<uint8_t const*>("\\/"), 2); |
94 break; | 94 break; |
95 case '\b': | 95 case '\b': |
96 Printf("%s", "\\b"); | 96 AddRaw(reinterpret_cast<uint8_t const*>("\\b"), 2); |
97 break; | 97 break; |
98 case '\f': | 98 case '\f': |
99 Printf("%s", "\\f"); | 99 AddRaw(reinterpret_cast<uint8_t const*>("\\f"), 2); |
100 break; | 100 break; |
101 case '\n': | 101 case '\n': |
102 Printf("%s", "\\n"); | 102 AddRaw(reinterpret_cast<uint8_t const*>("\\n"), 2); |
103 break; | 103 break; |
104 case '\r': | 104 case '\r': |
105 Printf("%s", "\\r"); | 105 AddRaw(reinterpret_cast<uint8_t const*>("\\r"), 2); |
106 break; | 106 break; |
107 case '\t': | 107 case '\t': |
108 Printf("%s", "\\t"); | 108 AddRaw(reinterpret_cast<uint8_t const*>("\\t"), 2); |
109 break; | 109 break; |
110 default: | 110 default: |
111 if (codeunit < 0x20) { | 111 if (codeunit < 0x20) { |
112 // Encode character as \u00HH. | 112 EscapeAndAddUTF16CodeUnit(codeunit); |
113 uint32_t digit2 = (codeunit >> 4) & 0xf; | |
114 uint32_t digit3 = (codeunit & 0xf); | |
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 { | 113 } else { |
130 AddChar(codeunit); | 114 char encoded[6]; |
| 115 intptr_t length = Utf8::Length(codeunit); |
| 116 Utf8::Encode(codeunit, encoded); |
| 117 AddRaw(reinterpret_cast<uint8_t const*>(encoded), length); |
131 } | 118 } |
132 } | 119 } |
133 } | 120 } |
134 | 121 |
| 122 // Write an incomplete UTF-16 code unit so it can be read by a JSON parser in a |
| 123 // string literal. |
| 124 void TextBuffer::EscapeAndAddUTF16CodeUnit(uint16_t codeunit) { |
| 125 Printf("\\u%04X", codeunit); |
| 126 } |
| 127 |
135 | 128 |
136 void TextBuffer::AddString(const char* s) { | 129 void TextBuffer::AddString(const char* s) { |
137 Printf("%s", s); | 130 Printf("%s", s); |
138 } | 131 } |
139 | 132 |
140 | 133 |
141 void TextBuffer::AddEscapedString(const char* s) { | 134 void TextBuffer::AddEscapedString(const char* s) { |
142 intptr_t len = strlen(s); | 135 intptr_t len = strlen(s); |
143 for (int i = 0; i < len; i++) { | 136 for (int i = 0; i < len; i++) { |
144 EscapeAndAddCodeUnit(s[i]); | 137 EscapeAndAddCodeUnit(s[i]); |
(...skipping 11 matching lines...) Expand all Loading... |
156 // the debugger front-end. | 149 // the debugger front-end. |
157 intptr_t new_size = buf_size_ + len + kBufferSpareCapacity; | 150 intptr_t new_size = buf_size_ + len + kBufferSpareCapacity; |
158 char* new_buf = reinterpret_cast<char*>(realloc(buf_, new_size)); | 151 char* new_buf = reinterpret_cast<char*>(realloc(buf_, new_size)); |
159 ASSERT(new_buf != NULL); | 152 ASSERT(new_buf != NULL); |
160 buf_ = new_buf; | 153 buf_ = new_buf; |
161 buf_size_ = new_size; | 154 buf_size_ = new_size; |
162 } | 155 } |
163 } | 156 } |
164 | 157 |
165 } // namespace dart | 158 } // namespace dart |
OLD | NEW |