diff options
| author | Even Rouault <even.rouault@spatialys.com> | 2018-12-08 22:57:07 +0100 |
|---|---|---|
| committer | Even Rouault <even.rouault@spatialys.com> | 2018-12-08 23:35:41 +0100 |
| commit | cf21ff22f1c68a49199099dd027eadefc8301381 (patch) | |
| tree | ccf789b7160ddec29d29e96e58b4b100b0fdf1ad /src | |
| parent | 56d4e2a564f5b2ab50a6ab63a8c1da31445879bf (diff) | |
| download | PROJ-cf21ff22f1c68a49199099dd027eadefc8301381.tar.gz PROJ-cf21ff22f1c68a49199099dd027eadefc8301381.zip | |
Speed-up toString(int) and toString(double)
Diffstat (limited to 'src')
| -rw-r--r-- | src/internal.cpp | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/internal.cpp b/src/internal.cpp index ecb724c2..c43605d1 100644 --- a/src/internal.cpp +++ b/src/internal.cpp @@ -45,6 +45,8 @@ #include <sstream> // std::istringstream and std::ostringstream #include <string> +#include "sqlite3.h" + NS_PROJ_START namespace internal { @@ -296,6 +298,12 @@ std::vector<std::string> split(const std::string &str, char separator) { // --------------------------------------------------------------------------- +#ifdef _WIN32 + +// For some reason, sqlite3_snprintf() in the sqlite3 builds used on AppVeyor +// doesn't round identically to the Unix builds, and thus breaks a number of +// unit test. So to avoid this, use the stdlib formatting + std::string toString(int val) { std::ostringstream buffer; buffer.imbue(std::locale::classic()); @@ -319,6 +327,31 @@ std::string toString(double val, int precision) { return str; } +#else + +std::string toString(int val) { + // use sqlite3 API that is slighly faster than std::ostringstream + // with forcing the C locale. sqlite3_snprintf() emulates a C locale. + constexpr int BUF_SIZE = 16; + char szBuffer[BUF_SIZE]; + sqlite3_snprintf(BUF_SIZE, szBuffer, "%d", val); + return szBuffer; +} + +std::string toString(double val, int precision) { + // use sqlite3 API that is slighly faster than std::ostringstream + // with forcing the C locale. sqlite3_snprintf() emulates a C locale. + constexpr int BUF_SIZE = 32; + char szBuffer[BUF_SIZE]; + sqlite3_snprintf(BUF_SIZE, szBuffer, "%.*g", precision, val); + if (precision == 15 && strstr(szBuffer, "9999999999")) { + sqlite3_snprintf(BUF_SIZE, szBuffer, "%.14g", val); + } + return szBuffer; +} + +#endif + // --------------------------------------------------------------------------- std::string concat(const char *a, const std::string &b) { |
