| 1 |
#include <pbcopper/utility/StringUtils.h> |
| 2 |
|
| 3 |
#include <sstream> |
| 4 |
#include <stdexcept> |
| 5 |
|
| 6 |
#include <cctype> |
| 7 |
#include <cstddef> |
| 8 |
#include <cstdlib> |
| 9 |
|
| 10 |
namespace PacBio { |
| 11 |
namespace Utility { |
| 12 |
|
| 13 |
std::string Join(const std::vector<std::string>& input, const std::string& separator) |
| 14 |
{ |
| 15 |
// determine total joined length |
| 16 |
size_t totalLen = 0; |
| 17 |
for (const auto& s : input) { |
| 18 |
totalLen += s.size(); |
| 19 |
} |
| 20 |
if (!input.empty()) { |
| 21 |
totalLen += separator.size() * (input.size() - 1); // no separator after last string |
| 22 |
} |
| 23 |
|
| 24 |
// join input strings |
| 25 |
std::string result; |
| 26 |
result.reserve(totalLen); |
| 27 |
for (size_t i = 0; i < input.size(); ++i) { |
| 28 |
if (i != 0) { |
| 29 |
result += separator; |
| 30 |
} |
| 31 |
result += input.at(i); |
| 32 |
} |
| 33 |
return result; |
| 34 |
} |
| 35 |
|
| 36 |
std::string Join(const std::vector<std::string>& input, const char separator) |
| 37 |
{ |
| 38 |
return Join(input, std::string(1, separator)); |
| 39 |
} |
| 40 |
|
| 41 |
int64_t ScientificNotationToInt(const std::string& input) |
| 42 |
{ |
| 43 |
return ScientificNotationToDouble(input); |
| 44 |
} |
| 45 |
|
| 46 |
uint64_t ScientificNotationToUInt(const std::string& input) |
| 47 |
{ |
| 48 |
return ScientificNotationToDouble(input); |
| 49 |
} |
| 50 |
|
| 51 |
double ScientificNotationToDouble(const std::string& input) |
| 52 |
{ |
| 53 |
if (input.empty()) { |
| 54 |
throw std::runtime_error{ |
| 55 |
"[pbcopper] string utility ERROR: cannot convert empty string to number."}; |
| 56 |
} |
| 57 |
|
| 58 |
char* end; |
| 59 |
const double value = std::strtod(input.c_str(), &end); |
| 60 |
if (input.c_str() == end) { |
| 61 |
throw std::runtime_error{"[pbcopper] string utility ERROR: cannot convert string '" + |
| 62 |
input + "' to number"}; |
| 63 |
} |
| 64 |
return value; |
| 65 |
} |
| 66 |
|
| 67 |
int64_t SIStringToInt(const std::string& input) |
| 68 |
{ |
| 69 |
if (input.empty()) { |
| 70 |
throw std::runtime_error{ |
| 71 |
"[pbcopper] string utility ERROR: cannot convert empty string to number."}; |
| 72 |
} |
| 73 |
|
| 74 |
const auto suffix = input.back(); |
| 75 |
if (!std::isalpha(suffix)) { |
| 76 |
return std::stoll(input); |
| 77 |
} |
| 78 |
|
| 79 |
int64_t result = std::stoll(input.substr(0, input.size() - 1)); |
| 80 |
switch (suffix) { |
| 81 |
case 'K': |
| 82 |
case 'k': |
| 83 |
result *= 1000; |
| 84 |
break; |
| 85 |
case 'M': |
| 86 |
case 'm': |
| 87 |
result *= (1000 * 1000); |
| 88 |
break; |
| 89 |
case 'G': |
| 90 |
case 'g': |
| 91 |
result *= (1000 * 1000 * 1000); |
| 92 |
break; |
| 93 |
default: |
| 94 |
std::ostringstream msg; |
| 95 |
msg << "[pbcopper] string utility ERROR: cannot convert string '" << input |
| 96 |
<< "' to number, suffix '" << suffix << "' is not a recognized multipler."; |
| 97 |
throw std::runtime_error{msg.str()}; |
| 98 |
} |
| 99 |
return result; |
| 100 |
} |
| 101 |
|
| 102 |
uint64_t SIStringToUInt(const std::string& input) |
| 103 |
{ |
| 104 |
if (input.empty()) { |
| 105 |
throw std::runtime_error{ |
| 106 |
"[pbcopper] string utility ERROR: cannot convert empty string to number."}; |
| 107 |
} |
| 108 |
|
| 109 |
const auto suffix = input.back(); |
| 110 |
if (!std::isalpha(suffix)) { |
| 111 |
return std::stoull(input); |
| 112 |
} |
| 113 |
|
| 114 |
uint64_t result = std::stoull(input.substr(0, input.size() - 1)); |
| 115 |
switch (suffix) { |
| 116 |
case 'K': |
| 117 |
case 'k': |
| 118 |
result *= 1000; |
| 119 |
break; |
| 120 |
case 'M': |
| 121 |
case 'm': |
| 122 |
result *= (1000 * 1000); |
| 123 |
break; |
| 124 |
case 'G': |
| 125 |
case 'g': |
| 126 |
result *= (1000 * 1000 * 1000); |
| 127 |
break; |
| 128 |
default: |
| 129 |
std::ostringstream msg; |
| 130 |
msg << "[pbcopper] string utility ERROR: cannot convert string '" << input |
| 131 |
<< "' to number, suffix '" << suffix << "' is not a recognized multipler."; |
| 132 |
throw std::runtime_error{msg.str()}; |
| 133 |
} |
| 134 |
return result; |
| 135 |
} |
| 136 |
|
| 137 |
double SIStringToDouble(const std::string& input) |
| 138 |
{ |
| 139 |
if (input.empty()) { |
| 140 |
throw std::runtime_error{ |
| 141 |
"[pbcopper] string utility ERROR: cannot convert empty string to number."}; |
| 142 |
} |
| 143 |
|
| 144 |
const auto suffix = input.back(); |
| 145 |
if (!std::isalpha(suffix)) { |
| 146 |
return std::stod(input); |
| 147 |
} |
| 148 |
|
| 149 |
double result = std::stod(input.substr(0, input.size() - 1)); |
| 150 |
switch (suffix) { |
| 151 |
case 'K': |
| 152 |
case 'k': |
| 153 |
result *= 1000.; |
| 154 |
break; |
| 155 |
case 'M': |
| 156 |
case 'm': |
| 157 |
result *= (1000. * 1000.); |
| 158 |
break; |
| 159 |
case 'G': |
| 160 |
case 'g': |
| 161 |
result *= (1000. * 1000. * 1000.); |
| 162 |
break; |
| 163 |
default: |
| 164 |
std::ostringstream msg; |
| 165 |
msg << "[pbcopper] string utility ERROR: cannot convert string '" << input |
| 166 |
<< "' to number, suffix '" << suffix << "' is not a recognized multipler."; |
| 167 |
throw std::runtime_error{msg.str()}; |
| 168 |
} |
| 169 |
return result; |
| 170 |
} |
| 171 |
|
| 172 |
std::vector<std::string> Split(const std::string& line, const char delim) |
| 173 |
{ |
| 174 |
std::vector<std::string> tokens; |
| 175 |
std::istringstream lineStream{line}; |
| 176 |
std::string token; |
| 177 |
while (std::getline(lineStream, token, delim)) { |
| 178 |
tokens.push_back(token); |
| 179 |
} |
| 180 |
return tokens; |
| 181 |
} |
| 182 |
|
| 183 |
std::string WordWrapped(const std::string& input, const size_t maxColumns) |
| 184 |
{ |
| 185 |
std::ostringstream result; |
| 186 |
|
| 187 |
std::string word; |
| 188 |
std::istringstream in{input}; |
| 189 |
|
| 190 |
// grab initial (maybe non-existing) word |
| 191 |
if (in >> word) { |
| 192 |
|
| 193 |
result << word; |
| 194 |
size_t available = maxColumns - word.size(); |
| 195 |
|
| 196 |
// continue until input exhausted |
| 197 |
while (in >> word) { |
| 198 |
|
| 199 |
const size_t wordLength = word.size(); |
| 200 |
|
| 201 |
// line break before word if overhanging |
| 202 |
if (available < (wordLength + 1)) { |
| 203 |
result << '\n' << word; |
| 204 |
available = maxColumns - wordLength; |
| 205 |
} |
| 206 |
// pass through word, with space |
| 207 |
else { |
| 208 |
result << ' ' << word; |
| 209 |
available -= (wordLength + 1); |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
return result.str(); |
| 214 |
} |
| 215 |
|
| 216 |
std::vector<std::string> WordWrappedLines(const std::string& input, const size_t maxColumns) |
| 217 |
{ |
| 218 |
return PacBio::Utility::Split(WordWrapped(input, maxColumns), '\n'); |
| 219 |
} |
| 220 |
|
| 221 |
} // namespace Utility |
| 222 |
} // namespace PacBio |