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;
}
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;
}
Example #3
0
	void DatabaseImpl::checkSimpleArgumentFor(const boost::string_ref& key, partNum_t partNum) const
	{
		RAISE_INVALID_ARGUMENT_IF(key.size() > MAX_KEY_SIZE, "key too long");
		RAISE_INVALID_ARGUMENT_IF(key.empty(), "empty key is not allowed");
		RAISE_INVALID_ARGUMENT_IF(partNum > MAX_PARTNUM, "partNum is too large");
		RAISE_INVALID_ARGUMENT_IF(partNum == ALL_PARTS, "partNum ALL_PARTS is not allowed");
		RAISE_INVALID_ARGUMENT_IF(partNum < 0, "negative partNum is not allowed");
	}
Example #4
0
std::pair<std::string, bool>
history::up (const boost::string_ref & current_text)
{
	int next;

	if (this->realpos == HISTORY_SIZE - 1)
	{
		if (this->pos == 0)
			return std::make_pair(std::string{}, false);
	} else
	{
		if (this->pos == this->realpos + 1)
			return std::make_pair(std::string{}, false);
	}

	next = HISTORY_SIZE - 1;
	if (this->pos != 0)
		next = this->pos - 1;

	if (!this->lines[next].empty())
	{
		if
		(
			!current_text.empty() && this->lines[next] != current_text &&
			(this->lines[this->pos].empty() || current_text != this->lines[this->pos]) &&
			(this->lines[this->realpos].empty() || current_text != this->lines[this->pos])
		)
		{
			this->add (current_text);
		}
		
		this->pos = next;
		return std::make_pair(this->lines[this->pos], true);
	}

	return std::make_pair(std::string{}, false);
}
Example #5
0
bool
server::p_raw(const boost::string_ref &raw)
{
	char tbuf[4096];
	if (!raw.empty())
	{
		if (raw.size() < sizeof (tbuf) - 3)
		{
			auto len = snprintf(tbuf, sizeof(tbuf), "%s\r\n", raw.data());
			if (len < 0)
			{
				PrintText(current_sess, _("Unable to send message to server, and error has occurred"));
				return false;
			}
			tcp_send_len(*this, boost::string_ref{ tbuf, static_cast<std::size_t>(len)});
		} else
		{
			tcp_send_len (*this, raw);
			tcp_send_len(*this, boost::string_ref{ "\r\n", 2 });
		}
		return true;
	}
	return false;
}