src/utilities/core/StringHelpers.hpp
| 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 | #ifndef UTILITIES_CORE_STRINGHELPERS_HPP | ||
| 7 | #define UTILITIES_CORE_STRINGHELPERS_HPP | ||
| 8 | |||
| 9 | #include "../UtilitiesAPI.hpp" | ||
| 10 | |||
| 11 | #include "./Enum.hpp" | ||
| 12 | |||
| 13 | #include <boost/regex.hpp> | ||
| 14 | |||
| 15 | #include <string> | ||
| 16 | #include <sstream> | ||
| 17 | #include <cstdint> | ||
| 18 | |||
| 19 | namespace openstudio { | ||
| 20 | |||
| 21 | // clang-format off | ||
| 22 | |||
| 23 | /** \class DocumentFormat | ||
| 24 | * \brief Document output formats supported by OpenStudio. | ||
| 25 | * \details See the OPENSTUDIO_ENUM documentation in utilities/core/Enum.hpp. The actual | ||
| 26 | * macro call is: | ||
| 27 | * \code | ||
| 28 | OPENSTUDIO_ENUM(DocumentFormat, | ||
| 29 | ((COUT)) | ||
| 30 | ((LaTeX)) | ||
| 31 | ((XHTML)) | ||
| 32 | ); | ||
| 33 | * \endcode */ | ||
| 34 | ✗ | OPENSTUDIO_ENUM(DocumentFormat, | |
| 35 | ((COUT)) | ||
| 36 | ((LaTeX)) | ||
| 37 | ((XHTML)) | ||
| 38 | ); | ||
| 39 | |||
| 40 | // clang-format on | ||
| 41 | |||
| 42 | /** Replace all non-letters and digits with spaces, and then remove spaces by to make camel | ||
| 43 | * case. */ | ||
| 44 | UTILITIES_API std::string toCamelCase(const std::string& s); | ||
| 45 | |||
| 46 | /** Make camel case then upcase the first letter. */ | ||
| 47 | UTILITIES_API std::string toUpperCamelCase(const std::string& s); | ||
| 48 | |||
| 49 | /** Make camel case then downcase the first letter. */ | ||
| 50 | UTILITIES_API std::string toLowerCamelCase(const std::string& s); | ||
| 51 | |||
| 52 | /** Make underscore case. */ | ||
| 53 | UTILITIES_API std::string toUnderscoreCase(const std::string& s); | ||
| 54 | |||
| 55 | /** Convert IDD object and field names to something reasonable for computers. */ | ||
| 56 | UTILITIES_API std::string convertIddName(const std::string& s); | ||
| 57 | |||
| 58 | /** Convert IDD object name to default name for IDF objects of this type. */ | ||
| 59 | UTILITIES_API std::string iddObjectNameToIdfObjectName(const std::string& s); | ||
| 60 | |||
| 61 | /** Prints value with numFractionalDigits, inserting commas to separate into | ||
| 62 | * thousands, millions, etc. if desired. */ | ||
| 63 | UTILITIES_API std::string toNeatString(double value, unsigned numFractionalDigits = 0, bool applyCommas = true); | ||
| 64 | |||
| 65 | /** Prints value in numSigFigs, inserting commas to separate into thousands, | ||
| 66 | millions, etc. if desired. */ | ||
| 67 | UTILITIES_API std::string toNeatStringBySigFigs(double value, unsigned numSigFigs = 3, bool applyCommas = true); | ||
| 68 | |||
| 69 | /** Returns the number of digits past the decimal point in str. */ | ||
| 70 | UTILITIES_API unsigned numFractionalDigits(const std::string& str); | ||
| 71 | |||
| 72 | /** Returns the number of significant digits past the decimal point in value, given numSigFigs. | ||
| 73 | * Throws if numSigFigs == 0. */ | ||
| 74 | UTILITIES_API unsigned numFractionalDigits(double value, unsigned numSigFigs); | ||
| 75 | |||
| 76 | /** Returns the minimum and maximum number of significant digits past the decimal | ||
| 77 | * point in values, given numSigFigs. Throws if numSigFigs == 0. */ | ||
| 78 | UTILITIES_API std::pair<unsigned, unsigned> numFractionalDigits(const std::vector<double>& values, unsigned numSigFigs); | ||
| 79 | |||
| 80 | /** Rounds value to a number of significant figures (rather than to a particular | ||
| 81 | * decimal place). Throws if numSigFigs == 0. */ | ||
| 82 | UTILITIES_API double toNumSigFigs(double value, unsigned numSigFigs); | ||
| 83 | |||
| 84 | UTILITIES_API boost::regex superscript(); | ||
| 85 | |||
| 86 | UTILITIES_API boost::regex subscript(); | ||
| 87 | |||
| 88 | UTILITIES_API std::string formatText(const std::string& str, DocumentFormat fmt); | ||
| 89 | |||
| 90 | /** Ensures that underscores will remain as underscores (not changed to subscripts). */ | ||
| 91 | UTILITIES_API std::string formatUnderscore(const std::string& str); | ||
| 92 | |||
| 93 | UTILITIES_API std::string formatSuperAndSubscripts(const std::string& str, DocumentFormat fmt); | ||
| 94 | |||
| 95 | /** Applies formatSuperAndSubscripts, and replaces '*' with small dot. Does nothing for fmt == | ||
| 96 | * DocumentFormat::COUT. */ | ||
| 97 | UTILITIES_API std::string formatUnitString(const std::string& str, DocumentFormat fmt); | ||
| 98 | |||
| 99 | /** Split a string into a vector of strings given a delimiter character. An empty string results | ||
| 100 | * in an empty vector, strings that begin or end with the delimiter will result in a vector | ||
| 101 | * beginning or ending with an empty string, and non-empty strings with no delimiters returns | ||
| 102 | * a vector with the input string as the only element. */ | ||
| 103 | UTILITIES_API std::vector<std::string> splitString(const std::string& string, char delimiter); | ||
| 104 | |||
| 105 | /** This takes the line of an EMS Program or Subroutine, and will split it into tokens, | ||
| 106 | * Before returning, it filters out the reserved keywords (IF, WHILE, RETURN, RUN, etc) and any function (starts with '@') | ||
| 107 | * As a result, what is returned is the only "words" that could be potential objects for UUID substitution. | ||
| 108 | * Note: JM 2018-08-16: The second parameters defaults to the operators currently accepted as of 8.9.0. | ||
| 109 | * The reserved keywords are hardcoded per the same 8.9.0 version. | ||
| 110 | */ | ||
| 111 | UTILITIES_API std::vector<std::string> splitEMSLineToTokens(const std::string& line, const std::string& delimiters = " +-*/^=<>&|"); | ||
| 112 | |||
| 113 | UTILITIES_API std::string replace(std::string input, const std::string& before, const std::string& after); | ||
| 114 | |||
| 115 | enum struct FloatFormat | ||
| 116 | { | ||
| 117 | fixed, | ||
| 118 | general, | ||
| 119 | general_capital | ||
| 120 | }; | ||
| 121 | |||
| 122 | namespace string_conversions { | ||
| 123 | /** Converts a number into a string, replicating the behavior of QString::number. | ||
| 124 | * This is necessary because QString does *not* respect the locale, and stringstring | ||
| 125 | * and std::string::to_string *do* respect the locale | ||
| 126 | */ | ||
| 127 | UTILITIES_API std::string number(std::int32_t, int base = 10); | ||
| 128 | UTILITIES_API std::string number(std::uint32_t, int base = 10); | ||
| 129 | UTILITIES_API std::string number(std::int64_t, int base = 10); | ||
| 130 | UTILITIES_API std::string number(std::uint64_t, int base = 10); | ||
| 131 | UTILITIES_API std::string number(double, FloatFormat format = FloatFormat::general, int precision = 6); | ||
| 132 | |||
| 133 | template <typename DesiredType, typename InputType> | ||
| 134 | 2445 | boost::optional<DesiredType> to_no_throw(const InputType& inp) { | |
| 135 |
2/2boost::optional<double> openstudio::string_conversions::to_no_throw<double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&):
✓ Branch 1 taken 1276 times.
boost::optional<int> openstudio::string_conversions::to_no_throw<int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&):
✓ Branch 1 taken 1169 times.
|
2445 | std::stringstream ss; |
| 136 |
4/4boost::optional<double> openstudio::string_conversions::to_no_throw<double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&):
✓ Branch 1 taken 1276 times.
✓ Branch 4 taken 1276 times.
boost::optional<int> openstudio::string_conversions::to_no_throw<int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&):
✓ Branch 1 taken 1169 times.
✓ Branch 4 taken 1169 times.
|
2445 | ss.imbue(std::locale::classic()); |
| 137 |
2/2boost::optional<double> openstudio::string_conversions::to_no_throw<double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&):
✓ Branch 1 taken 1276 times.
boost::optional<int> openstudio::string_conversions::to_no_throw<int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&):
✓ Branch 1 taken 1169 times.
|
2445 | ss.str(inp); |
| 138 | DesiredType o; | ||
| 139 |
2/2boost::optional<double> openstudio::string_conversions::to_no_throw<double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&):
✓ Branch 1 taken 1276 times.
boost::optional<int> openstudio::string_conversions::to_no_throw<int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&):
✓ Branch 1 taken 1169 times.
|
2445 | ss >> o; |
| 140 |
14/23boost::optional<double> openstudio::string_conversions::to_no_throw<double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&):
✓ Branch 1 taken 1276 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 430 times.
✓ Branch 4 taken 846 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 430 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 430 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 846 times.
✓ Branch 11 taken 430 times.
boost::optional<int> openstudio::string_conversions::to_no_throw<int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&):
✓ Branch 1 taken 1169 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 1166 times.
✓ Branch 6 taken 3 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 3 times.
✓ Branch 10 taken 1166 times.
✓ Branch 11 taken 3 times.
|
2445 | if (!ss.eof() || ss.fail()) { |
| 141 | 2012 | return {}; | |
| 142 | } | ||
| 143 |
2/2boost::optional<double> openstudio::string_conversions::to_no_throw<double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&):
✓ Branch 1 taken 430 times.
boost::optional<int> openstudio::string_conversions::to_no_throw<int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&):
✓ Branch 1 taken 3 times.
|
433 | return o; |
| 144 | 2445 | } | |
| 145 | |||
| 146 | template <typename DesiredType, typename InputType> | ||
| 147 | 107 | DesiredType to(const InputType& inp) { | |
| 148 |
1/1✓ Branch 1 taken 107 times.
|
107 | const auto result = to_no_throw<DesiredType>(inp); |
| 149 |
1/2✓ Branch 0 taken 107 times.
✗ Branch 1 not taken.
|
107 | if (result) { |
| 150 |
1/1✓ Branch 1 taken 107 times.
|
214 | return result.value(); |
| 151 | } else { | ||
| 152 | ✗ | throw std::bad_cast{}; | |
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | template <typename DesiredType, typename InputType> | ||
| 157 | bool is_valid(const InputType& input) { | ||
| 158 | try { | ||
| 159 | to<DesiredType>(input); | ||
| 160 | return true; | ||
| 161 | } catch (const std::bad_cast&) { | ||
| 162 | return false; | ||
| 163 | } | ||
| 164 | } | ||
| 165 | |||
| 166 | template <typename DesiredType, typename InputType, typename DestinationType> | ||
| 167 | bool assign_if_valid(const InputType& input, DestinationType& dest) { | ||
| 168 | if (is_valid<DesiredType>(input)) { | ||
| 169 | dest = input; | ||
| 170 | return true; | ||
| 171 | } else { | ||
| 172 | return false; | ||
| 173 | } | ||
| 174 | } | ||
| 175 | } // namespace string_conversions | ||
| 176 | } // namespace openstudio | ||
| 177 | |||
| 178 | #endif // UTILITIES_CORE_STRINGHELPERS_HPP | ||
| 179 |