SimpleTemplate::SimpleTemplate(boost::string_ref text) { static char const rawMarker[] = "@@"; static boost::string_ref const marker(rawMarker, sizeof(rawMarker) - 1); auto beg = text.find(marker); while (beg != boost::string_ref::npos) { m_literals.emplace_back(text.data(), beg); text.remove_prefix(beg + marker.size()); auto end = text.find(marker); if (end == boost::string_ref::npos) { std::string& lit = m_literals.back(); lit.reserve(lit.size() + marker.size() + text.size()); lit.append(marker.data(), marker.size()); lit.append(text.data(), text.size()); assert(m_literals.size() == m_insertionKeys.size() + 1); return; } m_insertionKeys.emplace_back(text.data(), end); text.remove_prefix(end + marker.size()); beg = text.find(marker); } m_literals.emplace_back(text.data(), text.size()); assert(m_literals.size() == m_insertionKeys.size() + 1); }
double CInfixExpressionCalculator::ParseDouble(boost::string_ref &ref) { SkipSpaces(ref); double value = 0; bool parsedAny = false; while (!ref.empty() && std::isdigit(ref[0])) { parsedAny = true; const int digit = ref[0] - '0'; value = value * 10.0 + double(digit); ref.remove_prefix(1); } if (!parsedAny) { return std::numeric_limits<double>::quiet_NaN(); } if (ref.empty() || (ref[0] != '.')) { return value; } ref.remove_prefix(1); double factor = 1.0; while (!ref.empty() && std::isdigit(ref[0])) { const int digit = ref[0] - '0'; factor *= 0.1; value += factor * double(digit); ref.remove_prefix(1); } return value; }
double CInfixExpressionCalculator::ParseExprSum(boost::string_ref &ref) { double value = ParseExprMul(ref); while (true) { SkipSpaces(ref); if (!ref.empty() && ref[0] == '+') { ref.remove_prefix(1); value += ParseExprMul(ref); } else if (!ref.empty() && ref[0] == '-') { ref.remove_prefix(1); value -= ParseExprMul(ref); } else { break; } } return value; }
void CInfixExpressionCalculator::SkipSpaces(boost::string_ref &ref) { size_t i = 0; while (i < ref.size() && std::isspace(ref[i])) ++i; ref.remove_prefix(i); }
inline boost::string_ref remove_leading_spaces(boost::string_ref string, const char* spaces = " \t\r\n") { auto pos = string.find_first_not_of(spaces); if (pos == std::string::npos) pos = string.size(); string.remove_prefix(pos); return string; }