Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(354)

Side by Side Diff: runtime/vm/json_stream.cc

Issue 558853004: Preserve the contents of Dart strings with unmatched surrogate halfs by avoiding a UTF16 -> UTF8 (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: sync and build Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/vm/json_stream.h ('k') | runtime/vm/json_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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/assert.h" 5 #include "platform/assert.h"
6 6
7 #include "vm/dart_entry.h" 7 #include "vm/dart_entry.h"
8 #include "vm/debugger.h" 8 #include "vm/debugger.h"
9 #include "vm/json_stream.h" 9 #include "vm/json_stream.h"
10 #include "vm/message.h" 10 #include "vm/message.h"
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 210
211 211
212 void JSONStream::PrintValue(const char* s) { 212 void JSONStream::PrintValue(const char* s) {
213 PrintCommaIfNeeded(); 213 PrintCommaIfNeeded();
214 buffer_.AddChar('"'); 214 buffer_.AddChar('"');
215 AddEscapedUTF8String(s); 215 AddEscapedUTF8String(s);
216 buffer_.AddChar('"'); 216 buffer_.AddChar('"');
217 } 217 }
218 218
219 219
220 void JSONStream::PrintValue(const char* s, intptr_t len) { 220 bool JSONStream::PrintValueStr(const String& s, intptr_t limit) {
221 PrintCommaIfNeeded(); 221 PrintCommaIfNeeded();
222 buffer_.AddChar('"'); 222 buffer_.AddChar('"');
223 AddEscapedUTF8String(s, len); 223 bool did_truncate = AddDartString(s, limit);
224 buffer_.AddChar('"'); 224 buffer_.AddChar('"');
225 return did_truncate;
225 } 226 }
226 227
227 228
228 void JSONStream::PrintValueNoEscape(const char* s) { 229 void JSONStream::PrintValueNoEscape(const char* s) {
229 PrintCommaIfNeeded(); 230 PrintCommaIfNeeded();
230 buffer_.Printf("%s", s); 231 buffer_.Printf("%s", s);
231 } 232 }
232 233
233 234
234 void JSONStream::PrintfValue(const char* format, ...) { 235 void JSONStream::PrintfValue(const char* format, ...) {
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 PrintValue(d); 304 PrintValue(d);
304 } 305 }
305 306
306 307
307 void JSONStream::PrintProperty(const char* name, const char* s) { 308 void JSONStream::PrintProperty(const char* name, const char* s) {
308 PrintPropertyName(name); 309 PrintPropertyName(name);
309 PrintValue(s); 310 PrintValue(s);
310 } 311 }
311 312
312 313
313 void JSONStream::PrintProperty(const char* name, const char* s, intptr_t len) { 314 bool JSONStream::PrintPropertyStr(const char* name,
315 const String& s,
316 intptr_t limit) {
314 PrintPropertyName(name); 317 PrintPropertyName(name);
315 PrintValue(s, len); 318 return PrintValueStr(s, limit);
316 } 319 }
317 320
318 321
319 void JSONStream::PrintPropertyNoEscape(const char* name, const char* s) { 322 void JSONStream::PrintPropertyNoEscape(const char* name, const char* s) {
320 PrintPropertyName(name); 323 PrintPropertyName(name);
321 PrintValueNoEscape(s); 324 PrintValueNoEscape(s);
322 } 325 }
323 326
324 327
325 void JSONStream::PrintProperty(const char* name, const DebuggerEvent* event) { 328 void JSONStream::PrintProperty(const char* name, const DebuggerEvent* event) {
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
434 if (s == NULL) { 437 if (s == NULL) {
435 return; 438 return;
436 } 439 }
437 const uint8_t* s8 = reinterpret_cast<const uint8_t*>(s); 440 const uint8_t* s8 = reinterpret_cast<const uint8_t*>(s);
438 intptr_t i = 0; 441 intptr_t i = 0;
439 for (; i < len; ) { 442 for (; i < len; ) {
440 // Extract next UTF8 character. 443 // Extract next UTF8 character.
441 int32_t ch = 0; 444 int32_t ch = 0;
442 int32_t ch_len = Utf8::Decode(&s8[i], len - i, &ch); 445 int32_t ch_len = Utf8::Decode(&s8[i], len - i, &ch);
443 ASSERT(ch_len != 0); 446 ASSERT(ch_len != 0);
444 buffer_.AddEscapedChar(ch); 447 buffer_.EscapeAndAddCodeUnit(ch);
445 // Move i forward. 448 // Move i forward.
446 i += ch_len; 449 i += ch_len;
447 } 450 }
448 ASSERT(i == len); 451 ASSERT(i == len);
449 } 452 }
450 453
451 454
455 bool JSONStream::AddDartString(const String& s, intptr_t limit) {
456 bool did_truncate = false;
457 intptr_t length = s.Length();
458 if (limit == -1) {
459 limit = length;
460 }
461 if (length <= limit) {
462 limit = length;
463 } else {
464 did_truncate = true;
465 }
466
467 for (intptr_t i = 0; i < limit; i++) {
468 intptr_t code_unit = s.CharAt(i);
469 buffer_.EscapeAndAddCodeUnit(code_unit);
470 }
471 return did_truncate;
472 }
473
474
452 JSONObject::JSONObject(const JSONArray* arr) : stream_(arr->stream_) { 475 JSONObject::JSONObject(const JSONArray* arr) : stream_(arr->stream_) {
453 stream_->OpenObject(); 476 stream_->OpenObject();
454 } 477 }
455 478
456 479
457 void JSONObject::AddPropertyF(const char* name, 480 void JSONObject::AddPropertyF(const char* name,
458 const char* format, ...) const { 481 const char* format, ...) const {
459 stream_->PrintPropertyName(name); 482 stream_->PrintPropertyName(name);
460 va_list args; 483 va_list args;
461 va_start(args, format); 484 va_start(args, format);
(...skipping 22 matching lines...) Expand all
484 intptr_t len2 = OS::VSNPrint(p, len+1, format, args); 507 intptr_t len2 = OS::VSNPrint(p, len+1, format, args);
485 va_end(args); 508 va_end(args);
486 ASSERT(len == len2); 509 ASSERT(len == len2);
487 stream_->buffer_.AddChar('"'); 510 stream_->buffer_.AddChar('"');
488 stream_->AddEscapedUTF8String(p); 511 stream_->AddEscapedUTF8String(p);
489 stream_->buffer_.AddChar('"'); 512 stream_->buffer_.AddChar('"');
490 free(p); 513 free(p);
491 } 514 }
492 515
493 } // namespace dart 516 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/json_stream.h ('k') | runtime/vm/json_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698