Skip to content

Commit

Permalink
Move and rename IntConv
Browse files Browse the repository at this point in the history
  • Loading branch information
hhvrc committed Sep 6, 2024
1 parent 4e1e963 commit 20bbc25
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 49 deletions.
16 changes: 16 additions & 0 deletions include/Convert.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include <cstdint>
#include <string_view>

namespace OpenShock::Convert {
bool ToInt8(std::string_view str, int8_t& val);
bool ToUInt8(std::string_view str, uint8_t& val);
bool ToInt16(std::string_view str, int16_t& val);
bool ToUInt16(std::string_view str, uint16_t& val);
bool ToInt32(std::string_view str, int32_t& val);
bool ToUInt32(std::string_view str, uint32_t& val);
bool ToInt64(std::string_view str, int64_t& val);
bool ToUInt64(std::string_view str, uint64_t& val);
bool ToBool(std::string_view str, bool& val);
} // namespace OpenShock::Convert
15 changes: 0 additions & 15 deletions include/intconv.h

This file was deleted.

36 changes: 27 additions & 9 deletions src/intconv.cpp → src/Convert.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "intconv.h"
#include "Convert.h"

#include <cstdint>
#include <cstring>
#include <limits>
#include <string_view>
#include <type_traits>
Expand Down Expand Up @@ -102,30 +103,47 @@ constexpr bool spanToST(std::string_view str, T& val) {
using namespace OpenShock;

// Specific converters
bool IntConv::stoi8(std::string_view str, int8_t& val) {
bool Convert::ToInt8(std::string_view str, int8_t& val) {
return spanToST(str, val);
}
bool IntConv::stou8(std::string_view str, uint8_t& val) {
bool Convert::ToUInt8(std::string_view str, uint8_t& val) {
return spanToUT(str, val);
}
bool IntConv::stoi16(std::string_view str, int16_t& val) {
bool Convert::ToInt16(std::string_view str, int16_t& val) {
return spanToST(str, val);
}
bool IntConv::stou16(std::string_view str, uint16_t& val) {
bool Convert::ToUInt16(std::string_view str, uint16_t& val) {
return spanToUT(str, val);
}
bool IntConv::stoi32(std::string_view str, int32_t& val) {
bool Convert::ToInt32(std::string_view str, int32_t& val) {
return spanToST(str, val);
}
bool IntConv::stou32(std::string_view str, uint32_t& val) {
bool Convert::ToUInt32(std::string_view str, uint32_t& val) {
return spanToUT(str, val);
}
bool IntConv::stoi64(std::string_view str, int64_t& val) {
bool Convert::ToInt64(std::string_view str, int64_t& val) {
return spanToST(str, val);
}
bool IntConv::stou64(std::string_view str, uint64_t& val) {
bool Convert::ToUInt64(std::string_view str, uint64_t& val) {
return spanToUT(str, val);
}
bool Convert::ToBool(std::string_view str, bool& val) {
if (str.length() > 5) {
return false;
}

if (strncasecmp(str.data(), "true", str.length()) == 0) {
val = true;
return true;
}

if (strncasecmp(str.data(), "false", str.length()) == 0) {
val = false;
return true;
}

return false;
}

static_assert(NumDigits<uint8_t>() == 3, "NumDigits test for uint8_t failed");
static_assert(NumDigits<uint16_t>() == 5, "NumDigits test for uint16_t failed");
Expand Down
26 changes: 3 additions & 23 deletions src/serial/SerialInputHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const char* const TAG = "SerialInputHandler";
#include "config/SerialInputConfig.h"
#include "FormatHelpers.h"
#include "http/HTTPRequestManager.h"
#include "intconv.h"
#include "Convert.h"
#include "Logging.h"
#include "serialization/JsonAPI.h"
#include "serialization/JsonSerial.h"
Expand Down Expand Up @@ -74,27 +74,7 @@ static std::unordered_map<std::string_view, SerialCmdHandler, std::hash_ci, std:
/// @param out Output boolean
/// @return True if the argument is a boolean, false otherwise
bool _tryParseBool(std::string_view str, bool& out) {
if (str.empty()) {
return false;
}

str = OpenShock::StringTrim(str);

if (str.length() > 5) {
return false;
}

if (strncasecmp(str.data(), "true", str.length()) == 0) {
out = true;
return true;
}

if (strncasecmp(str.data(), "false", str.length()) == 0) {
out = false;
return true;
}

return false;
return OpenShock::Convert::ToBool(OpenShock::StringTrim(str), out);
}

void _handleVersionCommand(std::string_view arg) {
Expand Down Expand Up @@ -134,7 +114,7 @@ void _handleRfTxPinCommand(std::string_view arg) {
}

uint8_t pin;
if (!OpenShock::IntConv::stou8(arg, pin)) {
if (!OpenShock::Convert::ToUInt8(arg, pin)) {
SERPR_ERROR("Invalid argument (number invalid or out of range)");
}

Expand Down
4 changes: 2 additions & 2 deletions src/util/IPAddressUtils.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "util/IPAddressUtils.h"

#include "intconv.h"
#include "Convert.h"
#include "util/StringUtils.h"

const char* const TAG = "Util::IPAddressUtils";
Expand All @@ -16,7 +16,7 @@ bool OpenShock::IPV4AddressFromStringView(IPAddress& ip, std::string_view sv) {
}

std::uint8_t octets[4];
if (!IntConv::stou8(parts[0], octets[0]) || !IntConv::stou8(parts[1], octets[1]) || !IntConv::stou8(parts[2], octets[2]) || !IntConv::stou8(parts[3], octets[3])) {
if (!Convert::ToUInt8(parts[0], octets[0]) || !Convert::ToUInt8(parts[1], octets[1]) || !Convert::ToUInt8(parts[2], octets[2]) || !Convert::ToUInt8(parts[3], octets[3])) {
return false;
}

Expand Down

0 comments on commit 20bbc25

Please sign in to comment.