src/utilities/core/String.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************************************************** | ||
| 2 | * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC. | ||
| 3 | * See also https://openstudio.net/license | ||
| 4 | ***********************************************************************************************************************/ | ||
| 5 | |||
| 6 | #include "String.hpp" | ||
| 7 | |||
| 8 | #include "Logger.hpp" | ||
| 9 | |||
| 10 | #if (defined(__GNUC__)) | ||
| 11 | # pragma GCC diagnostic push | ||
| 12 | # pragma GCC diagnostic ignored "-Wdeprecated-declarations" | ||
| 13 | #endif | ||
| 14 | #include <codecvt> | ||
| 15 | |||
| 16 | #include <iomanip> | ||
| 17 | |||
| 18 | namespace openstudio { | ||
| 19 | |||
| 20 | /** char* to std::string. */ | ||
| 21 | 40145 | std::string toString(const char* s) { | |
| 22 |
1/1✓ Branch 1 taken 40145 times.
|
120435 | return {s}; |
| 23 | } | ||
| 24 | |||
| 25 | /** string to std::string. */ | ||
| 26 | 831 | std::string toString(const std::string& s) { | |
| 27 | 831 | return s; | |
| 28 | } | ||
| 29 | |||
| 30 | /** wchar_t* to std::string. */ | ||
| 31 | 1 | std::string toString(const wchar_t* w) { | |
| 32 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
|
2 | return toString(std::wstring(w)); |
| 33 | } | ||
| 34 | |||
| 35 | 1277175 | std::string toString(double v) { | |
| 36 | |||
| 37 | 1277175 | std::string result; | |
| 38 | |||
| 39 | // Return Infinity or NaN as strings, getDouble in IdfObject will fail to convert to double which will cause setDouble to fail | ||
| 40 |
2/2✓ Branch 1 taken 10 times.
✓ Branch 2 taken 1277165 times.
|
1277175 | if (std::isinf(v)) { |
| 41 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 6 times.
|
10 | if (v < 0) { |
| 42 |
1/1✓ Branch 1 taken 4 times.
|
4 | result = "-Infinity"; |
| 43 | } else { | ||
| 44 |
1/1✓ Branch 1 taken 6 times.
|
6 | result = "Infinity"; |
| 45 | } | ||
| 46 |
2/2✓ Branch 1 taken 6 times.
✓ Branch 2 taken 1277159 times.
|
1277165 | } else if (std::isnan(v)) { |
| 47 |
1/1✓ Branch 1 taken 6 times.
|
6 | result = "NaN"; |
| 48 | } else { | ||
| 49 | |||
| 50 |
1/1✓ Branch 1 taken 1277159 times.
|
1277159 | std::stringstream ss; |
| 51 |
1/1✓ Branch 3 taken 1277159 times.
|
1277159 | ss << std::setprecision(std::numeric_limits<double>::digits10) << v; |
| 52 |
1/1✓ Branch 1 taken 1277159 times.
|
1277159 | result = ss.str(); |
| 53 | 1277159 | } | |
| 54 | |||
| 55 | 1277175 | return result; | |
| 56 | ✗ | } | |
| 57 | |||
| 58 | ✗ | std::string toString(std::istream& s) { | |
| 59 | // istream -> string code from | ||
| 60 | // http://stackoverflow.com/questions/2602013/read-whole-ascii-file-into-c-stdstring | ||
| 61 | ✗ | std::string contents; | |
| 62 | ✗ | s.seekg(0, std::ios::end); | |
| 63 | ✗ | contents.resize(s.tellg()); | |
| 64 | ✗ | s.seekg(0, std::ios::beg); | |
| 65 | ✗ | s.read(&contents[0], contents.size()); | |
| 66 | ✗ | return contents; | |
| 67 | ✗ | } | |
| 68 | |||
| 69 | /// | ||
| 70 | /// Note that MSVC has linker issues with wchars for some weird reason | ||
| 71 | /// Hence these ifdef workarounds https://stackoverflow.com/questions/32055357/visual-studio-c-2015-stdcodecvt-with-char16-t-or-char32-t | ||
| 72 | /// | ||
| 73 | |||
| 74 | ✗ | std::wstring toWString(const std::string& s) { | |
| 75 | #if _MSC_VER >= 1900 | ||
| 76 | std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> convert; | ||
| 77 | const auto u16_conv = convert.from_bytes(s); | ||
| 78 | return {u16_conv.begin(), u16_conv.end()}; | ||
| 79 | #else | ||
| 80 | ✗ | std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert; | |
| 81 | ✗ | const auto u16_conv = convert.from_bytes(s); | |
| 82 | ✗ | return {u16_conv.begin(), u16_conv.end()}; | |
| 83 | #endif | ||
| 84 | ✗ | } | |
| 85 | |||
| 86 | 2 | std::string toString(const std::wstring& utf16_string) { | |
| 87 | #if _MSC_VER >= 1900 | ||
| 88 | std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> convert; | ||
| 89 | return convert.to_bytes(utf16_string.data(), utf16_string.data() + utf16_string.size()); | ||
| 90 | #else | ||
| 91 |
1/1✓ Branch 1 taken 2 times.
|
2 | std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert; |
| 92 |
1/1✓ Branch 3 taken 2 times.
|
4 | const std::u16string u16string{utf16_string.begin(), utf16_string.end()}; |
| 93 |
1/1✓ Branch 1 taken 2 times.
|
4 | return convert.to_bytes(u16string); |
| 94 | #endif | ||
| 95 | 2 | } | |
| 96 | |||
| 97 | #if (defined(__GNUC__)) | ||
| 98 | # pragma GCC diagnostic pop | ||
| 99 | #endif | ||
| 100 | |||
| 101 | } // namespace openstudio | ||
| 102 |