-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7fae11c
commit f40ccf3
Showing
2 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#ifndef ABS_SCPI_DRIVER_SRC_SCPIUTIL_H | ||
#define ABS_SCPI_DRIVER_SRC_SCPIUTIL_H | ||
|
||
#include <bci/abs/CommonTypes.h> | ||
|
||
#include <ranges> | ||
#include <span> | ||
#include <string_view> | ||
|
||
#include "StringUtil.h" | ||
#include "Util.h" | ||
|
||
namespace bci::abs::scpi { | ||
|
||
template <std::size_t kLen> | ||
constexpr ErrorCode SplitRespFloats(std::string_view resp, | ||
std::span<float, kLen> out) { | ||
resp = util::Trim(resp); | ||
|
||
std::size_t i = 0; | ||
for (const auto val : std::views::split(resp, ',')) { | ||
if (i >= out.size()) { | ||
return ErrorCode::kInvalidResponse; | ||
} | ||
|
||
if (auto v = | ||
util::StrViewToFloat(std::string_view(val.begin(), val.end()))) { | ||
out[i++] = *v; | ||
} else { | ||
return ErrorCode::kInvalidResponse; | ||
} | ||
} | ||
|
||
if (i < out.size()) { | ||
return ErrorCode::kInvalidResponse; | ||
} | ||
|
||
return ErrorCode::kSuccess; | ||
} | ||
|
||
template <std::size_t kLen> | ||
constexpr ErrorCode SplitRespFloats(std::string_view resp, | ||
std::array<float, kLen>& out) { | ||
return SplitRespFloats(resp, std::span{out}); | ||
} | ||
|
||
template <std::size_t kLen> | ||
ErrorCode SplitRespMnemonics(std::string_view resp, | ||
std::array<std::string, kLen>& out) { | ||
resp = util::Trim(resp); | ||
|
||
std::size_t i = 0; | ||
for (const auto val : std::views::split(resp, ',')) { | ||
if (i >= out.size()) { | ||
return ErrorCode::kInvalidResponse; | ||
} | ||
|
||
out[i++] = std::string(val.begin(), val.end()); | ||
} | ||
|
||
if (i < out.size()) { | ||
return ErrorCode::kInvalidResponse; | ||
} | ||
} | ||
|
||
template <std::size_t kLen> | ||
constexpr ErrorCode SplitRespMnemonics( | ||
std::string_view resp, std::array<std::string_view, kLen>& out) { | ||
resp = util::Trim(resp); | ||
|
||
std::size_t i = 0; | ||
for (const auto val : std::views::split(resp, ',')) { | ||
if (i >= out.size()) { | ||
return ErrorCode::kInvalidResponse; | ||
} | ||
|
||
out[i++] = std::string_view(val.begin(), val.end()); | ||
} | ||
|
||
if (i < out.size()) { | ||
return ErrorCode::kInvalidResponse; | ||
} | ||
} | ||
|
||
constexpr Result<float> ParseFloatResponse(std::string_view text) { | ||
if (auto f = util::StrViewToFloat(text)) { | ||
return *f; | ||
} | ||
return util::Err(ErrorCode::kInvalidResponse); | ||
} | ||
|
||
} // namespace bci::abs::scpi | ||
|
||
#endif /* ABS_SCPI_DRIVER_SRC_SCPIUTIL_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#ifndef ABS_SCPI_DRIVER_SRC_STRINGUTIL_H | ||
#define ABS_SCPI_DRIVER_SRC_STRINGUTIL_H | ||
|
||
#include <algorithm> | ||
#include <optional> | ||
#include <string_view> | ||
#include <system_error> | ||
|
||
#include <fast_float/fast_float.h> | ||
|
||
namespace bci::abs::util { | ||
|
||
inline constexpr std::string_view kTrimChars{" \t\v\r\n\0"}; | ||
|
||
inline constexpr std::string_view Trim(std::string_view v) noexcept { | ||
if (v.size() == 0) { | ||
return v; | ||
} | ||
|
||
auto s = v.find_first_not_of(kTrimChars); | ||
v.remove_prefix((std::min)(s, v.size())); | ||
if (v.size() <= 1) { | ||
return v; | ||
} | ||
|
||
s = v.find_last_not_of(kTrimChars); | ||
if (s != v.npos) { | ||
v.remove_suffix(v.size() - s - 1); | ||
} | ||
|
||
return v; | ||
} | ||
|
||
inline constexpr std::optional<float> StrViewToFloat(std::string_view sv) { | ||
if (sv.empty()) { | ||
return std::nullopt; | ||
} | ||
|
||
float f; | ||
auto [ptr, ec] = fast_float::from_chars(sv.data(), sv.data() + sv.size(), f); | ||
if (ec == std::errc()) { | ||
return f; | ||
} else { | ||
return std::nullopt; | ||
} | ||
} | ||
|
||
} // namespace bci::abs::util | ||
|
||
#endif /* ABS_SCPI_DRIVER_SRC_STRINGUTIL_H */ |