diff options
| author | levlam <levlam@telegram.org> | 2018-02-03 22:40:06 +0300 |
|---|---|---|
| committer | levlam <levlam@telegram.org> | 2018-02-03 22:40:06 +0300 |
| commit | 9d5580f315086fb174c0377311890b1968b1d68f (patch) | |
| tree | 0e653a3579ab38c29b029213f8b4530f0aa7adc2 /tdutils/td/utils/StringBuilder.cpp | |
| parent | 499e64430ba4bc5693b1c3553f679191cc87cb66 (diff) | |
Locale-independent to_double and from_double conversions.
GitOrigin-RevId: 997ec58422c4128857d395fa49c46ee0605afca9
Diffstat (limited to 'tdutils/td/utils/StringBuilder.cpp')
| -rw-r--r-- | tdutils/td/utils/StringBuilder.cpp | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/tdutils/td/utils/StringBuilder.cpp b/tdutils/td/utils/StringBuilder.cpp index 83fcb3c30..a36f5e574 100644 --- a/tdutils/td/utils/StringBuilder.cpp +++ b/tdutils/td/utils/StringBuilder.cpp @@ -6,7 +6,11 @@ // #include "td/utils/StringBuilder.h" +#include "td/utils/misc.h" + #include <cstdio> +#include <locale> +#include <sstream> namespace td { @@ -63,14 +67,24 @@ StringBuilder &StringBuilder::operator<<(double x) { if (unlikely(end_ptr_ < current_ptr_)) { return on_error(); } + + static TD_THREAD_LOCAL std::stringstream *ss; + if (init_thread_local<std::stringstream>(ss)) { + ss->imbue(std::locale::classic()); + } else { + ss->str(std::string()); + ss->clear(); + } + *ss << x; + + int len = narrow_cast<int>(static_cast<std::streamoff>(ss->tellp())); auto left = end_ptr_ + reserved_size - current_ptr_; - int len = std::snprintf(current_ptr_, left, "%lf", x); if (unlikely(len >= left)) { error_flag_ = true; - current_ptr_ += left - 1; - } else { - current_ptr_ += len; + len = left - 1; } + ss->read(current_ptr_, len); + current_ptr_ += len; return *this; } |
