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

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

Issue 9113043: Implement Double.{toString, toStringAsExponential, toStringAsPrecision} (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Don't use NULL and rebase. Created 8 years, 10 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
OLDNEW
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 "vm/object.h" 5 #include "vm/object.h"
6 6
7 #include "platform/assert.h" 7 #include "platform/assert.h"
8 #include "vm/assembler.h" 8 #include "vm/assembler.h"
9 #include "vm/bigint_operations.h" 9 #include "vm/bigint_operations.h"
10 #include "vm/bootstrap.h" 10 #include "vm/bootstrap.h"
11 #include "vm/code_generator.h" 11 #include "vm/code_generator.h"
12 #include "vm/code_index_table.h" 12 #include "vm/code_index_table.h"
13 #include "vm/code_patcher.h" 13 #include "vm/code_patcher.h"
14 #include "vm/compiler.h" 14 #include "vm/compiler.h"
15 #include "vm/compiler_stats.h" 15 #include "vm/compiler_stats.h"
16 #include "vm/class_finalizer.h" 16 #include "vm/class_finalizer.h"
17 #include "vm/dart.h" 17 #include "vm/dart.h"
18 #include "vm/dart_api_state.h" 18 #include "vm/dart_api_state.h"
19 #include "vm/dart_entry.h" 19 #include "vm/dart_entry.h"
20 #include "vm/debuginfo.h" 20 #include "vm/debuginfo.h"
21 #include "vm/double_conversion.h"
21 #include "vm/exceptions.h" 22 #include "vm/exceptions.h"
22 #include "vm/growable_array.h" 23 #include "vm/growable_array.h"
23 #include "vm/heap.h" 24 #include "vm/heap.h"
24 #include "vm/ic_data.h" 25 #include "vm/ic_data.h"
25 #include "vm/object_store.h" 26 #include "vm/object_store.h"
26 #include "vm/parser.h" 27 #include "vm/parser.h"
27 #include "vm/runtime_entry.h" 28 #include "vm/runtime_entry.h"
28 #include "vm/scopes.h" 29 #include "vm/scopes.h"
29 #include "vm/timer.h" 30 #include "vm/timer.h"
30 #include "vm/unicode.h" 31 #include "vm/unicode.h"
(...skipping 6047 matching lines...) Expand 10 before | Expand all | Expand 10 after
6078 } 6079 }
6079 6080
6080 6081
6081 const char* Double::ToCString() const { 6082 const char* Double::ToCString() const {
6082 if (isnan(value())) { 6083 if (isnan(value())) {
6083 return "NaN"; 6084 return "NaN";
6084 } 6085 }
6085 if (isinf(value())) { 6086 if (isinf(value())) {
6086 return value() < 0 ? "-Infinity" : "Infinity"; 6087 return value() < 0 ? "-Infinity" : "Infinity";
6087 } 6088 }
6088 const char* kFormat = "%f"; 6089 const int kBufferSize = 128;
6089 // Calculate the size of the string. 6090 char buffer[kBufferSize];
Ivan Posva 2012/02/27 14:47:14 Please avoid stack allocated buffers that are bein
floitsch 2012/02/27 19:55:40 Done.
6090 intptr_t len = OS::SNPrint(NULL, 0, kFormat, value()) + 1; 6091 int len;
6092 bool status = DoubleToCString(value(), buffer, kBufferSize, &len);
6093 ASSERT(status);
6094
6091 char* chars = reinterpret_cast<char*>( 6095 char* chars = reinterpret_cast<char*>(
6092 Isolate::Current()->current_zone()->Allocate(len)); 6096 Isolate::Current()->current_zone()->Allocate(len));
6093 OS::SNPrint(chars, len, kFormat, value()); 6097 memmove(chars, buffer, len);
6094 // Eliminate trailing 0s, but leave one digit after '.'.
6095 // 'chars' is null terminated.
6096 for (intptr_t i = len - 2; i >= 1; i--) {
6097 if ((chars[i] == '0') && (chars[i - 1] != '.')) {
6098 chars[i] = '\0';
6099 } else {
6100 break;
6101 }
6102 }
6103 return chars; 6098 return chars;
6104 } 6099 }
6105 6100
6106 6101
6107 bool Bigint::Equals(const Instance& other) const { 6102 bool Bigint::Equals(const Instance& other) const {
6108 if (this->raw() == other.raw()) { 6103 if (this->raw() == other.raw()) {
6109 // Both handles point to the same raw instance. 6104 // Both handles point to the same raw instance.
6110 return true; 6105 return true;
6111 } 6106 }
6112 6107
(...skipping 1924 matching lines...) Expand 10 before | Expand all | Expand 10 after
8037 const String& str = String::Handle(pattern()); 8032 const String& str = String::Handle(pattern());
8038 const char* format = "JSRegExp: pattern=%s flags=%s"; 8033 const char* format = "JSRegExp: pattern=%s flags=%s";
8039 intptr_t len = OS::SNPrint(NULL, 0, format, str.ToCString(), Flags()); 8034 intptr_t len = OS::SNPrint(NULL, 0, format, str.ToCString(), Flags());
8040 char* chars = reinterpret_cast<char*>( 8035 char* chars = reinterpret_cast<char*>(
8041 Isolate::Current()->current_zone()->Allocate(len + 1)); 8036 Isolate::Current()->current_zone()->Allocate(len + 1));
8042 OS::SNPrint(chars, (len + 1), format, str.ToCString(), Flags()); 8037 OS::SNPrint(chars, (len + 1), format, str.ToCString(), Flags());
8043 return chars; 8038 return chars;
8044 } 8039 }
8045 8040
8046 } // namespace dart 8041 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698