| 1 |
// Copyright(C) 1999-2020, 2022, 2023 National Technology & Engineering Solutions |
| 2 |
// of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with |
| 3 |
// NTESS, the U.S. Government retains certain rights in this software. |
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
// ####################### Start Clang Header Tool Managed Headers ######################## |
| 8 |
// clang-format off |
| 9 |
#include <cctype> // for toupper |
| 10 |
#include <cstddef> // for size_t |
| 11 |
#include <algorithm> // for remove, etc |
| 12 |
#include <iterator> // for insert_iterator |
| 13 |
#include <set> // for set |
| 14 |
#include <string> // for basic_string, etc |
| 15 |
#include <utility> // for pair |
| 16 |
#include <vector> // for vector |
| 17 |
#include <sstream> // for ostringstream |
| 18 |
#include <iostream> |
| 19 |
#include <stdexcept> |
| 20 |
#include <numeric> |
| 21 |
|
| 22 |
// clang-format on |
| 23 |
// ####################### End Clang Header Tool Managed Headers ######################## |
| 24 |
namespace Iotm { |
| 25 |
namespace text_mesh { |
| 26 |
|
| 27 |
template <class EXCEPTION> void handle_error(const std::ostringstream &message) |
| 28 |
{ |
| 29 |
throw EXCEPTION((message).str()); |
| 30 |
} |
| 31 |
|
| 32 |
inline void default_error_handler(const std::ostringstream &message) |
| 33 |
{ |
| 34 |
handle_error<std::logic_error>(message); |
| 35 |
} |
| 36 |
|
| 37 |
template <class ForwardIt, class T> |
| 38 |
ForwardIt bound_search(ForwardIt first, ForwardIt last, const T &value) |
| 39 |
{ |
| 40 |
first = std::lower_bound(first, last, value); |
| 41 |
if (!(first == last) && !(value < *first)) |
| 42 |
return first; |
| 43 |
|
| 44 |
return last; |
| 45 |
} |
| 46 |
|
| 47 |
template <class ForwardIt, class T, class Compare> |
| 48 |
ForwardIt bound_search(ForwardIt first, ForwardIt last, const T &value, Compare comp) |
| 49 |
{ |
| 50 |
first = std::lower_bound(first, last, value, comp); |
| 51 |
if (!(first == last) && !(comp(value, *first))) |
| 52 |
return first; |
| 53 |
|
| 54 |
return last; |
| 55 |
} |
| 56 |
|
| 57 |
inline std::string strip_whitespace(const std::string &inpt) |
| 58 |
{ |
| 59 |
auto start_it = inpt.begin(); |
| 60 |
auto end_it = inpt.rbegin(); |
| 61 |
while (std::isspace(*start_it)) |
| 62 |
++start_it; |
| 63 |
while (std::isspace(*end_it)) |
| 64 |
++end_it; |
| 65 |
return std::string(start_it, end_it.base()); |
| 66 |
} |
| 67 |
|
| 68 |
inline std::vector<std::string> get_tokens(const std::string &str, |
| 69 |
const std::string &separators) |
| 70 |
{ |
| 71 |
std::vector<std::string> tokens; |
| 72 |
auto first = std::begin(str); |
| 73 |
while (first != std::end(str)) { |
| 74 |
const auto second = |
| 75 |
std::find_first_of(first, std::end(str), std::begin(separators), std::end(separators)); |
| 76 |
if (first != second) { |
| 77 |
std::string token = strip_whitespace(std::string(first, second)); |
| 78 |
tokens.emplace_back(token); |
| 79 |
} |
| 80 |
if (second == std::end(str)) { |
| 81 |
break; |
| 82 |
} |
| 83 |
first = std::next(second); |
| 84 |
} |
| 85 |
return tokens; |
| 86 |
} |
| 87 |
|
| 88 |
inline void convert_to_upper_case(std::string &str) |
| 89 |
{ |
| 90 |
std::transform(str.begin(), str.end(), str.begin(), ::toupper); |
| 91 |
} |
| 92 |
|
| 93 |
inline void convert_to_lower_case(std::string &str) |
| 94 |
{ |
| 95 |
std::transform(str.begin(), str.end(), str.begin(), ::tolower); |
| 96 |
} |
| 97 |
|
| 98 |
inline bool is_positive_number(const std::string &str) |
| 99 |
{ |
| 100 |
for (char const &c : str) { |
| 101 |
if (std::isdigit(c) == 0) |
| 102 |
return false; |
| 103 |
} |
| 104 |
return true; |
| 105 |
} |
| 106 |
|
| 107 |
template <typename T> std::set<T> transform_to_set(const std::vector<T> &dataAsVector) |
| 108 |
{ |
| 109 |
std::set<T> dataAsSet; |
| 110 |
|
| 111 |
for (const T &data : dataAsVector) { |
| 112 |
dataAsSet.insert(data); |
| 113 |
} |
| 114 |
|
| 115 |
return dataAsSet; |
| 116 |
} |
| 117 |
|
| 118 |
inline std::pair<unsigned, bool> get_id_from_part_name(const std::string &name, |
| 119 |
const std::string &prefix) |
| 120 |
{ |
| 121 |
const unsigned prefixLength = prefix.length(); |
| 122 |
|
| 123 |
if (name.length() < prefixLength + 1) |
| 124 |
return std::make_pair(0, false); |
| 125 |
|
| 126 |
const std::string namePrefix = name.substr(0, prefixLength); |
| 127 |
const std::string nameSuffix = name.substr(prefixLength); |
| 128 |
|
| 129 |
if (strcasecmp(namePrefix.c_str(), prefix.c_str()) != 0) |
| 130 |
return std::make_pair(0, false); |
| 131 |
|
| 132 |
unsigned id; |
| 133 |
std::istringstream nameSuffixStream(nameSuffix); |
| 134 |
nameSuffixStream >> id; |
| 135 |
if (nameSuffixStream.fail()) { |
| 136 |
return std::make_pair(0, false); |
| 137 |
} |
| 138 |
return std::make_pair(id, true); |
| 139 |
} |
| 140 |
|
| 141 |
} // namespace text_mesh |
| 142 |
} // namespace Iotm |