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

Unified Diff: runtime/vm/object.cc

Issue 100833005: Add Instance::ToUserCString. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years 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 side-by-side diff with in-line comments
Download patch
Index: runtime/vm/object.cc
===================================================================
--- runtime/vm/object.cc (revision 30973)
+++ runtime/vm/object.cc (working copy)
@@ -11393,7 +11393,7 @@
}
const Type& type =
Type::Handle(Type::New(cls, type_arguments, Scanner::kDummyTokenIndex));
- const String& type_name = String::Handle(type.Name());
+ const String& type_name = String::Handle(type.UserVisibleName());
// Calculate the size of the string.
intptr_t len = OS::SNPrint(NULL, 0, kFormat, type_name.ToCString()) + 1;
char* chars = Isolate::Current()->current_zone()->Alloc<char>(len);
@@ -11403,6 +11403,17 @@
}
+const char* Instance::ToUserCString() const {
+ if (raw() == Object::sentinel().raw()) {
+ return "<not initialized>";
+ } else if (raw() == Object::transition_sentinel().raw()) {
+ return "<being initialized>";
+ } else {
+ return ToCString();
+ }
+}
+
+
void Instance::PrintToJSONStream(JSONStream* stream, bool ref) const {
ObjectIdRing* ring = Isolate::Current()->object_id_ring();
intptr_t id = ring->GetIdForObject(raw());
@@ -14224,6 +14235,100 @@
}
+static bool IsAsciiPrintChar(intptr_t codePoint) {
+ return codePoint >= ' ' && codePoint <= '~';
+}
+
+
+// Does not null-terminate.
+intptr_t String::EscapedString(char* buffer, int maxLen) const {
+ int pos = 0;
+
+ CodePointIterator cpi(*this);
+ while (cpi.Next()) {
+ int32_t codePoint = cpi.Current();
+ if (IsSpecialCharacter(codePoint)) {
+ if (pos + 2 > maxLen) {
+ return pos;
+ }
+ buffer[pos++] = '\\';
+ buffer[pos++] = SpecialCharacter(codePoint);
+ } else if (IsAsciiPrintChar(codePoint)) {
+ buffer[pos++] = codePoint;
+ } else {
+ if (pos + 6 > maxLen) {
+ return pos;
+ }
+ pos += OS::SNPrint((buffer + pos), (maxLen - pos), "\\u%04x", codePoint);
+ }
+ if (pos == maxLen) {
+ return pos;
+ }
+ }
+ return pos;
+}
+
+
+intptr_t String::EscapedStringLen(intptr_t tooLong) const {
+ intptr_t len = 0;
+
+ CodePointIterator cpi(*this);
+ while (cpi.Next()) {
+ int32_t codePoint = cpi.Current();
+ if (IsSpecialCharacter(codePoint)) {
+ len += 2; // e.g. "\n"
+ } else if (IsAsciiPrintChar(codePoint)) {
+ len += 1;
+ } else {
+ len += 6; // e.g. "\u0000".
+ }
+ if (len > tooLong) {
+ // No point going further.
+ break;
+ }
+ }
+ return len;
+}
+
+
+const char* String::ToUserCString() const {
+ const int maxLen = 40;
+
+ // Compute the needed length for the buffer.
+ const intptr_t escapedLen = EscapedStringLen(maxLen);
+ intptr_t printLen = escapedLen;
+ intptr_t bufferLen = escapedLen + 2; // +2 for quotes.
+ if (bufferLen > maxLen) {
+ bufferLen = maxLen; // Truncate.
+ printLen = maxLen - 5; // -2 for quotes, -3 for elipsis.
+ }
+
+ // Allocate the buffer.
+ Zone* zone = Isolate::Current()->current_zone();
+ char* buffer = zone->Alloc<char>(bufferLen + 1);
+
+ // Leading quote.
+ intptr_t pos = 0;
+ buffer[pos++] = '\"';
+
+ // Print escaped string.
+ pos += EscapedString((buffer + pos), printLen);
+
+ // Trailing quote.
+ buffer[pos++] = '\"';
+
+ if (printLen < escapedLen) {
+ buffer[pos++] = '.';
+ buffer[pos++] = '.';
+ buffer[pos++] = '.';
+ }
+ ASSERT(pos <= bufferLen);
+ buffer[pos++] = '\0';
+
+ return buffer;
+}
+
+
void String::PrintToJSONStream(JSONStream* stream, bool ref) const {
Instance::PrintToJSONStream(stream, ref);
}
@@ -15272,7 +15377,7 @@
if (IsNull()) {
return "_GrowableList NULL";
}
- const char* format = "_GrowableList len:%" Pd "";
+ const char* format = "Instance(length:%" Pd ") of '_GrowableList'";
intptr_t len = OS::SNPrint(NULL, 0, format, Length()) + 1;
char* chars = Isolate::Current()->current_zone()->Alloc<char>(len);
OS::SNPrint(chars, len, format, Length());
« runtime/vm/object.h ('K') | « runtime/vm/object.h ('k') | runtime/vm/object_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698