Use the new int_conv instead of the old lexical_cast.

This commit is contained in:
King_DuckZ 2018-10-13 12:34:56 +01:00
parent a70cbd736d
commit 39cfbada85
8 changed files with 120 additions and 30 deletions

View File

@ -0,0 +1,95 @@
/* Copyright 2016-2018, Michele Santullo
* This file is part of "incredis".
*
* "incredis" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "incredis" is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "incredis". If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef id08E89EF0951741388A48A79E769C646C
#define id08E89EF0951741388A48A79E769C646C
#include <string>
#include <stdexcept>
#include "duckhandy/implem/int_conv.hpp"
namespace redis {
namespace implem {
struct AsciiTranslator {
static const constexpr bool BehavesLikeASCII = true;
static const constexpr char FirstDigit = '0';
static const constexpr char FirstLetter = 'a';
static const constexpr char Plus = '+';
static const constexpr char Minus = '-';
static constexpr char to_digit (unsigned int num) {
return (num <= 9 ? static_cast<char>(num + '0') : static_cast<char>(num + 'a'));
}
static constexpr int from_digit (char digit) {
if (digit >= '0' and digit <= '9')
return digit - '0';
else if (digit >= 'a' and digit <= 'z')
return digit - 'a';
else if (digit >= 'A' and digit <= 'Z')
return digit - 'A';
else
throw std::domain_error(
std::string("Can't convert invalid character '") +
digit + "' (" +
std::to_string(static_cast<int>(digit)) +
") to a number"
);
}
};
template <typename T, typename F>
struct IntConv;
template <typename F>
struct IntConv<std::enable_if_t<std::is_integral_v<F>, std::string>, F> {
static std::string conv (const F& in) {
auto retval = dhandy::int_to_ary<F, 10, AsciiTranslator>(in);
return std::string(retval.begin(), retval.end() - 1);
}
};
template <typename T>
struct IntConv<T, std::enable_if_t<std::is_integral_v<T>, std::string>> {
static T conv (const std::string& in) {
return dhandy::ary_to_int<T, char, 10, AsciiTranslator>(in.data(), in.data() + in.size());
}
};
template <typename T>
struct IntConv<T, std::enable_if_t<std::is_integral_v<T>, std::string_view>> {
static T conv (const std::string_view& in) {
return dhandy::ary_to_int<T, char, 10, AsciiTranslator>(in.data(), in.data() + in.size());
}
};
} //namespace implem
template <typename To, typename From>
inline To int_conv (const From& from) {
return implem::IntConv<To, From>::conv(from);
}
template <typename From>
constexpr inline auto int_to_ary_hex (From f) {
return dhandy::int_to_ary<From, 16>(f);
}
template <typename From>
constexpr inline auto int_to_ary_dec (From f) {
return dhandy::int_to_ary<From, 10>(f);
}
} //namespace redis
#endif

View File

@ -19,7 +19,7 @@
#define id5B30CDA57F894CD6888093B64F9433DA
#include "batch.hpp"
#include "duckhandy/lexical_cast.hpp"
#include "incredis/int_conv.hpp"
#include "duckhandy/sequence_bt.hpp"
#include <boost/utility/string_view.hpp>
#include <tuple>
@ -73,7 +73,7 @@ namespace redis {
parBatch.run(
"EVALSHA",
m_sha1,
dhandy::lexical_cast<std::string>(sizeof...(Keys)),
int_conv<std::string>(sizeof...(Keys)),
std::get<KeyIndices>(parKeys)...,
std::get<ValueIndices>(parValues)...
);

@ -1 +1 @@
Subproject commit 7ceaf01a376e9184027850104dac4f3e3fc6b755
Subproject commit 4eb42094093a3eb899d6e8169959dfa8985f3cb6

View File

@ -17,7 +17,7 @@
#include "incredis.hpp"
#include "duckhandy/compatibility.h"
#include "duckhandy/lexical_cast.hpp"
#include "incredis/int_conv.hpp"
#include <cassert>
#include <ciso646>
@ -102,13 +102,12 @@ namespace redis {
}
RedisInt IncRedis::hincrby (boost::string_view parKey, boost::string_view parField, int parInc) {
const auto inc = dhandy::lexical_cast<std::string>(parInc);
auto reply = m_command.run("HINCRBY", parKey, parField, inc);
auto reply = m_command.run("HINCRBY", parKey, parField, int_to_ary_dec(parInc).to<boost::string_view>());
return get_integer(reply);
}
auto IncRedis::srandmember (boost::string_view parKey, int parCount) -> opt_string_list {
return optional_string_list(m_command.run("SRANDMEMBER", parKey, dhandy::lexical_cast<std::string>(parCount)));
return optional_string_list(m_command.run("SRANDMEMBER", parKey, int_to_ary_dec(parCount).to<boost::string_view>()));
}
auto IncRedis::srandmember (boost::string_view parKey) -> opt_string {
@ -142,7 +141,7 @@ namespace redis {
}
bool IncRedis::expire (boost::string_view parKey, RedisInt parTTL) {
const auto ret = redis::get<RedisInt>(m_command.run("EXPIRE", parKey, dhandy::lexical_cast<std::string>(parTTL)));
const auto ret = redis::get<RedisInt>(m_command.run("EXPIRE", parKey, int_to_ary_dec(parTTL).to<boost::string_view>()));
return (ret == 1 ? true : false);
}

View File

@ -16,7 +16,7 @@
*/
#include "incredis_batch.hpp"
#include "duckhandy/lexical_cast.hpp"
#include "incredis/int_conv.hpp"
#include <sstream>
#include <utility>
#include <ciso646>
@ -45,7 +45,7 @@ namespace redis {
}
IncRedisBatch& IncRedisBatch::select (int parIndex) {
m_batch.run("SELECT", dhandy::lexical_cast<std::string>(parIndex));
m_batch.run("SELECT", int_to_ary_dec(parIndex).to<boost::string_view>());
return *this;
}
@ -60,12 +60,12 @@ namespace redis {
}
IncRedisBatch& IncRedisBatch::hincrby (boost::string_view parKey, boost::string_view parField, int parInc) {
m_batch.run("HINCRBY", parKey, parField, dhandy::lexical_cast<std::string>(parInc));
m_batch.run("HINCRBY", parKey, parField, int_to_ary_dec(parInc).to<boost::string_view>());
return *this;
}
IncRedisBatch& IncRedisBatch::srandmember (boost::string_view parKey, int parCount) {
m_batch.run("SRANDMEMBER", parKey, dhandy::lexical_cast<std::string>(parCount));
m_batch.run("SRANDMEMBER", parKey, int_to_ary_dec(parCount).to<boost::string_view>());
return *this;
}

View File

@ -16,7 +16,7 @@
*/
#include "reply.hpp"
#include "duckhandy/lexical_cast.hpp"
#include "incredis/int_conv.hpp"
#include <boost/variant/get.hpp>
namespace redis {
@ -35,14 +35,12 @@ namespace redis {
}
RedisInt get_integer_autoconv_if_str (const Reply &parReply) {
using dhandy::lexical_cast;
const auto type = parReply.which();
switch (type) {
case RedisVariantType_Integer:
return get_integer(parReply);
case RedisVariantType_String:
return lexical_cast<RedisInt>(get_string(parReply));
return int_conv<RedisInt>(get_string(parReply));
default:
assert(false);
return 0;

View File

@ -16,7 +16,7 @@
*/
#include "scan_iterator.hpp"
#include "duckhandy/lexical_cast.hpp"
#include "incredis/int_conv.hpp"
#include "command.hpp"
#include <cassert>
#include <ciso646>
@ -42,8 +42,8 @@ namespace redis {
}
Reply ScanIteratorBaseClass::run (const char* parCommand, RedisInt parScanContext, std::size_t parCount) {
const auto scan_context = dhandy::lexical_cast<std::string>(parScanContext);
const auto count_hint = dhandy::lexical_cast<std::string>(parCount);
const auto scan_context = int_conv<std::string>(parScanContext);
const auto count_hint = int_conv<std::string>(parCount);
if (m_match_pattern.empty())
return m_command->run(parCommand, scan_context, "COUNT", count_hint);
else
@ -51,8 +51,8 @@ namespace redis {
}
Reply ScanIteratorBaseClass::run (const char* parCommand, const boost::string_view& parParameter, RedisInt parScanContext, std::size_t parCount) {
const auto scan_context = dhandy::lexical_cast<std::string>(parScanContext);
const auto count_hint = dhandy::lexical_cast<std::string>(parCount);
const auto scan_context = int_conv<std::string>(parScanContext);
const auto count_hint = int_conv<std::string>(parCount);
if (m_match_pattern.empty())
return m_command->run(parCommand, parParameter, scan_context, "COUNT", count_hint);
else

View File

@ -16,7 +16,7 @@
*/
#include "script_manager.hpp"
#include "duckhandy/lexical_cast.hpp"
#include "incredis/int_conv.hpp"
#include "command.hpp"
#include <cassert>
#if defined(MAKE_SHA1_WITH_CRYPTOPP)
@ -51,8 +51,6 @@ namespace redis {
if (parScript.empty())
return boost::string_view();
using dhandy::lexical_cast;
static_assert(20 == CryptoPP::SHA1::DIGESTSIZE, "Unexpected SHA1 digest size");
static_assert(sizeof(LuaScriptHash) >= CryptoPP::SHA1::DIGESTSIZE, "Wrong SHA1 struct size");
static_assert(Sha1Array().size() == CryptoPP::SHA1::DIGESTSIZE * 2, "Wrong array size");
@ -60,13 +58,13 @@ namespace redis {
LuaScriptHash digest;
CryptoPP::SHA1().CalculateDigest(digest.raw_bytes, reinterpret_cast<const uint8_t*>(parScript.data()), parScript.size());
//TODO: change when lexical_cast will support arrays
auto sha1_str_parta = lexical_cast<std::string, dhandy::tags::hexl>(__builtin_bswap64(digest.part_a));
auto sha1_str_partb = lexical_cast<std::string, dhandy::tags::hexl>(__builtin_bswap64(digest.part_b));
auto sha1_str_partc = lexical_cast<std::string, dhandy::tags::hexl>(__builtin_bswap32(digest.part_c));
auto sha1_str_parta = int_to_ary_hex(__builtin_bswap64(digest.part_a));
auto sha1_str_partb = int_to_ary_hex(__builtin_bswap64(digest.part_b));
auto sha1_str_partc = int_to_ary_hex(__builtin_bswap32(digest.part_c));
const std::string sha1_str =
std::string(sizeof(digest.part_a) * 2 - sha1_str_parta.size(), '0') + sha1_str_parta +
std::string(sizeof(digest.part_b) * 2 - sha1_str_partb.size(), '0') + sha1_str_partb +
std::string(sizeof(digest.part_c) * 2 - sha1_str_partc.size(), '0') + sha1_str_partc
std::string(sizeof(digest.part_a) * 2 - sha1_str_parta.size() + 1, '0') + sha1_str_parta +
std::string(sizeof(digest.part_b) * 2 - sha1_str_partb.size() + 1, '0') + sha1_str_partb +
std::string(sizeof(digest.part_c) * 2 - sha1_str_partc.size() + 1, '0') + sha1_str_partc
;
Sha1Array sha1_array;
assert(sha1_str.size() == sha1_array.size());