47 #include <boost/algorithm/string/classification.hpp> 48 #include <boost/algorithm/string/split.hpp> 52 void add_escape(std::string& ioString,
const std::string& to_be_escaped)
54 for(std::string::size_type lPos = 0; lPos != ioString.size(); lPos++)
56 if(to_be_escaped.find(ioString[lPos]) != std::string::npos)
59 escaped_char[0] =
'\\';
60 escaped_char[1] = ioString.at(lPos);
61 escaped_char[2] =
'\0';
62 ioString.replace(lPos, 1, escaped_char);
70 for(std::string::size_type lPos = 0; lPos != ioString.size(); lPos++)
72 if(ioString.at(lPos) ==
'\\')
74 if(ioString.at(lPos + 1) ==
'\\')
76 ioString.replace(lPos, 2,
"\\");
78 else if(ioString.at(lPos + 1) ==
'n')
80 ioString.replace(lPos, 2,
"\n");
82 else if(ioString.at(lPos + 1) ==
't')
84 ioString.replace(lPos, 2,
"\t");
93 std::vector<std::string> splitted =
SplitString(value,
" \n\t\r");
95 for(
auto& i : splitted)
97 if(!first and i.size())
112 std::unique_ptr<char, void (*)(void*)> res(abi::__cxa_demangle(input.data(),
nullptr,
nullptr, &status), std::free);
113 return status == 0 ? std::string(res.get()) :
"";
118 auto z_pos = signature.find(
'Z');
119 if(z_pos != std::string::npos)
121 const char* z_start = signature.data() + z_pos + 1;
123 auto z_len = std::strtoul(z_start, &z_end, 10);
126 return signature.substr(0, z_pos + 1) + std::to_string(new_fname.size()) + new_fname +
127 signature.substr(static_cast<size_t>(
std::distance(signature.data(),
static_cast<const char*
>(z_end))) +
136 auto z_pos = signature.find(
'Z');
137 if(z_pos != std::string::npos)
139 const char* z_start = signature.data() + z_pos + 1;
141 auto z_len = std::strtoul(z_start, &z_end, 10);
144 return signature.substr(0, z_pos + 1) + std::to_string(prefix.size() + z_len) + prefix +
145 signature.substr(static_cast<size_t>(
std::distance(signature.data(),
static_cast<const char*
>(z_end))));
148 return prefix + signature;
159 str[0] =
static_cast<char>(toupper(str[0]));
163 static const std::regex
fixed_def(
"a[cp]_(u)?fixed<\\s*(\\d+)\\s*,\\s*(\\d+),?\\s*(\\w+)?[^>]*>[^\\d-]*");
167 #define FD_GROUP_SIGN 4 172 std::string trimmed_value = C_value;
173 THROW_ASSERT(C_value !=
"",
"Empty string for binary conversion");
175 bool is_signed, is_fixed;
179 unsigned_type = !is_signed;
180 trimmed_value = trimmed_value.substr(trimmed_value.find(
'>') + 1);
191 const auto is_match =
193 std::regex_search(C_value.c_str(), what,
fixed_def);
195 const auto w = std::stoul(
197 const auto d = std::stoul(
202 THROW_ASSERT(d < w,
"Decimal part should be smaller then total length");
203 const long double val = strtold(what[0].second,
nullptr) * powl(2, w - d);
206 is_signed &= val < 0;
207 trimmed_value.clear();
208 while(trimmed_value.size() < w)
210 trimmed_value = ((fixp & 1) ?
"1" :
"0") + trimmed_value;
213 while(trimmed_value.size() < precision)
215 trimmed_value = (is_signed ? trimmed_value.front() :
'0') + trimmed_value;
220 long long int ll_value;
221 if(trimmed_value[0] ==
'\"')
223 trimmed_value = trimmed_value.substr(1);
224 trimmed_value = trimmed_value.substr(0, trimmed_value.find(
'\"'));
225 if(trimmed_value[0] ==
'0' && trimmed_value[1] ==
'b')
227 trimmed_value = trimmed_value.substr(2);
229 else if(trimmed_value[0] ==
'0' && (trimmed_value[1] ==
'x' || trimmed_value[1] ==
'o'))
231 bool is_hex = trimmed_value[1] ==
'x';
232 std::string initial_string = trimmed_value.substr(2);
234 std::string hexTable[16] = {
"0000",
"0001",
"0010",
"0011",
"0100",
"0101",
"0110",
"0111",
235 "1000",
"1001",
"1010",
"1011",
"1100",
"1101",
"1110",
"1111"};
236 std::string octTable[16] = {
"000",
"001",
"010",
"011",
"100",
"101",
"110",
"111"};
237 for(
char curChar : initial_string)
242 if(curChar >=
'0' && curChar <=
'9')
246 else if(curChar >=
'A' && curChar <=
'F')
248 off = curChar -
'A' + 10;
250 else if(curChar >=
'a' && curChar <=
'f')
252 off = curChar -
'a' + 10;
261 if(curChar >=
'0' && curChar <=
'8')
270 trimmed_value = trimmed_value + (is_hex ? hexTable[off] : octTable[off]);
278 while(trimmed_value.size() < precision)
280 trimmed_value =
"0" + trimmed_value;
282 while(trimmed_value.size() > precision)
284 trimmed_value = trimmed_value.substr(1);
286 return trimmed_value;
288 else if(trimmed_value[0] ==
'\'')
290 trimmed_value = trimmed_value.substr(1);
291 THROW_ASSERT(trimmed_value.find(
'\'') != std::string::npos,
"unxpected case");
292 trimmed_value = trimmed_value.substr(0, trimmed_value.find(
'\''));
293 if(trimmed_value[0] ==
'\\')
295 ll_value = std::stoll(trimmed_value.substr(1));
299 ll_value = boost::lexical_cast<
char>(trimmed_value);
302 else if(unsigned_type)
304 std::string::size_type sz = 0;
305 unsigned long long ull = std::stoull(trimmed_value, &sz, 0);
306 ll_value =
static_cast<long long int>(ull);
310 std::string::size_type sz = 0;
311 ll_value = std::stoll(trimmed_value, &sz, 0);
313 auto ull_value =
static_cast<unsigned long long int>(ll_value);
317 for(
unsigned int ind = 0; ind < precision; ind++)
319 trimmed_value = trimmed_value + (((1LLU << (precision - ind - 1)) & ull_value) ?
'1' :
'0');
324 for(
unsigned int ind = 0; ind < (precision - 64); ind++)
326 trimmed_value = trimmed_value +
'0';
328 for(
unsigned int ind = 0; ind < 64; ind++)
330 trimmed_value = trimmed_value + (((1LLU << (64 - ind - 1)) & ull_value) ?
'1' :
'0');
334 return trimmed_value;
337 static const std::regex
fixp_val(
"(\\d+\\.?\\d*)");
342 if(std::regex_search(fp_typename.c_str(), what,
fixed_def))
344 const auto w = std::stoul(
346 const auto d = std::stoul(
348 THROW_ASSERT(d < w,
"Decimal part should be smaller then total length");
349 std::sregex_token_iterator fix_val_it(FP_vector.begin(), FP_vector.end(),
fixp_val), end;
350 std::string new_vector =
"{";
351 while(fix_val_it != end)
353 const long double val = strtold(fix_val_it->str().c_str(),
nullptr) * powl(2, w - d);
355 const auto fixp =
static_cast<long long>(val);
356 new_vector +=
"{{{" +
STR(fixp) +
"}}}, ";
359 new_vector.erase(new_vector.size() - 2, 2);
367 #ifndef __clang_analyzer__
372 #ifndef __clang_analyzer__
377 std::vector<std::string> ret_value;
378 #ifndef __clang_analyzer__ 379 boost::algorithm::split(ret_value,
input, boost::algorithm::is_any_of(separators));
388 unsigned long long ll;
394 char* endptr =
nullptr;
404 else if(num ==
"-__Inf")
408 else if(num ==
"__Nan")
412 else if(num ==
"-__Nan")
414 u.f = -(0.0f / 0.0f);
418 u.f = strtof(num.c_str(), &endptr);
421 for(
unsigned int ind = 0; ind < precision; ind++)
423 res = res + (((1
U << (precision - ind - 1)) & u.i) ?
'1' :
'0');
433 else if(num ==
"-__Inf")
437 else if(num ==
"__Nan")
441 else if(num ==
"-__Nan")
447 u.d = strtod(num.c_str(), &endptr);
450 for(
unsigned int ind = 0; ind < precision; ind++)
452 res = res + (((1LLU << (precision - ind - 1)) & u.ll) ?
'1' :
'0');
457 throw std::string(
"not supported precision ") +
STR(precision);
466 unsigned long long ll;
471 char* endptr =
nullptr;
481 else if(num ==
"-__Inf")
485 else if(num ==
"__Nan")
489 else if(num ==
"-__Nan")
491 u.f = -(0.0f / 0.0f);
495 u.f = strtof(num.c_str(), &endptr);
505 else if(num ==
"-__Inf")
509 else if(num ==
"__Nan")
513 else if(num ==
"-__Nan")
519 u.d = strtod(num.c_str(), &endptr);
524 throw std::string(
"not supported precision ") +
STR(precision);
529 static const std::regex
ac_type_def(
"a[cp]_(u)?(\\w+)<\\s*(\\d+)\\s*,?\\s*(\\d+)?,?\\s*(\\w+)?[^>]*>");
533 #define AC_GROUP_SIGN 4 535 unsigned long long ac_type_bitwidth(
const std::string& intType,
bool& is_signed,
bool& is_fixed)
540 if(std::regex_search(intType.c_str(), what,
ac_type_def))
542 auto w = std::stoull(
549 .find(
"fixed") != std::string::npos;
std::string convert_fp_to_string(std::string num, unsigned long long precision)
convert a real number stored in a string into a string of bits with a given precision ...
struct definition of the real_type tree node.
void remove_escaped(std::string &ioString)
Function converting all the escaped characters in the associated character.
std::string capitalize(const std::string &str)
static const std::regex ac_type_def("a[cp]_(u)?(\+)<\*(\+)\*,?\*(\+)?,?\*(\+)?[^>]*>")
const std::vector< std::string > SplitString(const std::string &input, const std::string &separators)
Function which splits a string into tokens.
exceptions managed by PandA
std::string ConvertInBinary(const std::string &C_value, unsigned long long precision, const bool real_type, bool unsigned_type)
Convert a string storing a number in decimal format into a string in binary format.
static const std::regex fixp_val("(\+\?\*)")
void add_escape(std::string &ioString, const std::string &to_be_escaped)
Header include.
#define STR(s)
Macro which performs a lexical_cast to a string.
Auxiliary methods for manipulating string.
std::string cxa_rename_mangled(const std::string &signature, const std::string &new_fname)
#define THROW_ERROR(str_expr)
helper function used to throw an error in a standard way
std::string TrimSpaces(const std::string &value)
static const std::regex fixed_def("a[cp]_(u)?fixed<\*(\+)\*,\*(\+),?\*(\+)?[^>]*>[^\-]*")
TYPE distance(TYPE position_x[nAtoms], TYPE position_y[nAtoms], TYPE position_z[nAtoms], int i, int j)
unsigned long long ac_type_bitwidth(const std::string &intType, bool &is_signed, bool &is_fixed)
std::string FixedPointReinterpret(const std::string &FP_vector, const std::string &fp_typename)
std::string cxa_prefix_mangled(const std::string &signature, const std::string &prefix)
std::string cxa_demangle(const std::string &input)
unsigned long long convert_fp_to_bits(std::string num, unsigned long long precision)
convert a real number stored in a string into bits with a given precision
#define THROW_ASSERT(cond, str_expr)
helper function used to check an assert and if needed to throw an error in a standard way ...