std::string_view 的使用
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#include <regex>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string_view>
|
||||
// 2025-01-02 05:01:50 [info] 1a480118 9607f81e f020 3a24 67761dbd 1a9ecbf8
|
||||
// 5655c75b 00601307
|
||||
#include <algorithm>
|
||||
@@ -95,8 +96,8 @@ const bool is_big_endian = []() {
|
||||
auto *p = reinterpret_cast<uint8_t *>(&x);
|
||||
return p[0] == 0x01; // 如果 p[0] 为 0x01,说明是大端字节序
|
||||
}();
|
||||
std::vector<std::string> split(const std::string &str,
|
||||
const std::string &delimiter) {
|
||||
std::vector<std::string> split(std::string_view str,
|
||||
std::string_view delimiter) {
|
||||
std::vector<std::string> tokens;
|
||||
size_t start = 0;
|
||||
size_t end = str.find(delimiter);
|
||||
@@ -110,7 +111,7 @@ std::vector<std::string> split(const std::string &str,
|
||||
tokens.push_back(str.substr(start)); // Add the last token
|
||||
return tokens;
|
||||
}
|
||||
std::string bin2mem(const std::string &bin) {
|
||||
std::string bin2mem(std::string_view bin) {
|
||||
const size_t len = bin.size();
|
||||
if (len % 8 != 0) {
|
||||
throw std::invalid_argument("binary string length must be a multiple of 8");
|
||||
@@ -395,7 +396,7 @@ TEST(Bi222, xor_hex_Ex) {
|
||||
#endif
|
||||
|
||||
// 将字节序从大端转换到当前平台字节序,并复制到目标内存中
|
||||
void big_endian_2_platform(const std::string &big_endian, char *dest) {
|
||||
void big_endian_2_platform(std::string_view big_endian, char *dest) {
|
||||
// 先复制原始数据
|
||||
std::string result = big_endian;
|
||||
// 如果当前平台是小端,反转数据
|
||||
@@ -405,14 +406,14 @@ void big_endian_2_platform(const std::string &big_endian, char *dest) {
|
||||
// 将转换后的数据拷贝到目标内存中
|
||||
std::memcpy(dest, result.data(), result.size());
|
||||
}
|
||||
std::string big_endian_2_platform(const std::string &big_endian) {
|
||||
std::string big_endian_2_platform(std::string_view big_endian) {
|
||||
std::string result;
|
||||
result.resize(big_endian.size());
|
||||
big_endian_2_platform(big_endian, result.data());
|
||||
return result;
|
||||
}
|
||||
// 将字节序从小端转换到当前平台字节序,并复制到目标内存中
|
||||
void little_endian_2_platform(const std::string &little_endian, char *dest) {
|
||||
void little_endian_2_platform(std::string_view little_endian, char *dest) {
|
||||
// 先复制原始数据
|
||||
std::string result = little_endian;
|
||||
// 如果当前平台是大端,反转数据
|
||||
@@ -422,7 +423,7 @@ void little_endian_2_platform(const std::string &little_endian, char *dest) {
|
||||
// 将转换后的数据拷贝到目标内存中
|
||||
std::memcpy(dest, result.data(), result.size());
|
||||
}
|
||||
std::string little_endian_2_platform(const std::string &little_endian) {
|
||||
std::string little_endian_2_platform(std::string_view little_endian) {
|
||||
std::string result;
|
||||
result.resize(little_endian.size());
|
||||
big_endian_2_platform(little_endian, result.data());
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef BitSet_H
|
||||
#define BitSet_H
|
||||
#include <bitset>
|
||||
#include <string_view>
|
||||
#ifdef __linux__
|
||||
#include <cstring>
|
||||
#endif
|
||||
@@ -70,11 +71,11 @@ extern const bool is_big_endian;
|
||||
void big_endian_2_platform(void *dest, const char *d, size_t size);
|
||||
void little_endian_2_platform(void *dest, const char *d, size_t size);
|
||||
|
||||
void big_endian_2_platform(const std::string &big_endian, char *dest);
|
||||
void little_endian_2_platform(const std::string &little_endian, char *dest);
|
||||
void big_endian_2_platform(std::string_view big_endian, char *dest);
|
||||
void little_endian_2_platform(std::string_view little_endian, char *dest);
|
||||
|
||||
std::string big_endian_2_platform(const std::string &big_endian);
|
||||
std::string little_endian_2_platform(const std::string &little_endian);
|
||||
std::string big_endian_2_platform(std::string_view big_endian);
|
||||
std::string little_endian_2_platform(std::string_view little_endian);
|
||||
std::string platform_2_big_endian(void *memory, int size);
|
||||
std::string platform_2_little_endian(void *memory, int size);
|
||||
|
||||
@@ -180,7 +181,7 @@ void mem2hex_Ex2(void *dest, void *mem, size_t num, bool uppercase,
|
||||
const char *middle,
|
||||
size_t middle_len = std::numeric_limits<size_t>::max());
|
||||
Base STR_Type mem2hex(const STR_Type &mem, bool uppercase = true,
|
||||
const std::string &middle = "",
|
||||
std::string_view middle = "",
|
||||
std::pmr::memory_resource *resource = nullptr) {
|
||||
auto size = cacl_type<STR_Type>::size(mem);
|
||||
auto ret =
|
||||
@@ -209,7 +210,7 @@ template <int start, int len, typename Enum> Enum get_bin(std::string &msg) {
|
||||
return static_cast<Enum>(
|
||||
std::bitset<len>(msg.substr(start, len)).to_ullong());
|
||||
}
|
||||
template <int start, int len> int get_bin(const std::string &msg) {
|
||||
template <int start, int len> int get_bin(std::string_view msg) {
|
||||
return std::bitset<len>(msg.substr(start, len)).to_ullong();
|
||||
}
|
||||
template <int start, int len>
|
||||
@@ -217,7 +218,7 @@ void set_bin(std::string &msg, unsigned long long n) {
|
||||
msg.replace(start, len, std::bitset<len>(n).to_string());
|
||||
}
|
||||
template <int start, int len>
|
||||
void set_bin(std::string &msg, const std::string &value) {
|
||||
void set_bin(std::string &msg, std::string_view value) {
|
||||
if (value.size() != len) {
|
||||
std::cout << "len == " << len << " bits.size() == " << value.size()
|
||||
<< std::endl;
|
||||
@@ -231,13 +232,13 @@ template <int len> std::string to_bin(unsigned long long n) {
|
||||
template <int num> std::string to_bin(double n) {
|
||||
return std::bitset<num>(static_cast<unsigned long long>(n)).to_string();
|
||||
}
|
||||
template <typename T> T bin2(const std::string &binstr) {
|
||||
template <typename T> T bin2(std::string_view binstr) {
|
||||
return std::stoull(binstr, nullptr, 2);
|
||||
}
|
||||
template <typename T> unsigned long long hex2(const std::string &hexstr) {
|
||||
template <typename T> unsigned long long hex2(std::string_view hexstr) {
|
||||
return std::stoull(hexstr, nullptr, 16);
|
||||
}
|
||||
template <> inline int bin2<int>(const std::string &binstr) {
|
||||
template <> inline int bin2<int>(std::string_view binstr) {
|
||||
int bit_limit = sizeof(int) * 8;
|
||||
if (binstr.size() > bit_limit) {
|
||||
std::cout << "Bitset.h inline int bin2<int> error! Binary string exceeds "
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <cassert>
|
||||
#include <chrono>
|
||||
#include <string_view>
|
||||
|
||||
using namespace yC;
|
||||
|
||||
@@ -22,7 +23,7 @@ MLAT_timestamp::MLAT_timestamp() {
|
||||
parse_hhmmss();
|
||||
}
|
||||
|
||||
MLAT_timestamp::MLAT_timestamp(const std::string &memory_6) : memory(memory_6) {
|
||||
MLAT_timestamp::MLAT_timestamp(std::string_view memory_6) : memory(memory_6) {
|
||||
assert(memory_6.size() == 6);
|
||||
auto msg_bin = hex2bin(mem2hex(memory));
|
||||
daysec = std::bitset<18>(msg_bin.substr(0, 18)).to_ulong();
|
||||
@@ -55,7 +56,7 @@ void MLAT_timestamp::parse_hhmmss() {
|
||||
mm = (daysec / 60) % 60;
|
||||
ss = daysec % 60;
|
||||
}
|
||||
std::string packet_to_escape_format(std::string t) {
|
||||
std::string packet_to_escape_format(std::string_view t) {
|
||||
std::string result = "\x1a";
|
||||
for (size_t i = 1; i < t.size(); ++i) {
|
||||
result.push_back(t[i]); // 插入当前字符
|
||||
@@ -66,7 +67,7 @@ std::string packet_to_escape_format(std::string t) {
|
||||
return result;
|
||||
}
|
||||
void Binary_Format_handle_buffer(
|
||||
std::string &buffer, const std::string &data,
|
||||
std::string &buffer, std::string_view data,
|
||||
const std::function<void(std::string &)> &callback) {
|
||||
size_t i = 0;
|
||||
while (i < data.size()) {
|
||||
@@ -99,7 +100,7 @@ void Binary_Format_handle_buffer(
|
||||
// 9 + 7
|
||||
// 9 + 14
|
||||
std::string mode_s_msg_packet_to_server_packet(std::uint32_t id,
|
||||
std::string packet) {
|
||||
std::string_view packet) {
|
||||
std::string id_code = yC::platform_2_big_endian((char *)&id, sizeof(id));
|
||||
// assert(id_code.size() == 4, "id.size != 4");
|
||||
packet.insert(1, id_code.c_str(), 4);
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
#ifndef DATAOUTPUTFORMATS_H
|
||||
#define DATAOUTPUTFORMATS_H
|
||||
#include "base_format.h"
|
||||
#include <string_view>
|
||||
using namespace yC;
|
||||
|
||||
// AVR format
|
||||
// *02E99619FACDAE;
|
||||
// *8D3C5EE69901BD9540078D37335F;
|
||||
// *7700;
|
||||
inline std::string create_AVR_format(const std::string &msg_hex) {
|
||||
inline std::string create_AVR_format(std::string_view msg_hex) {
|
||||
return "*" + msg_hex + ";";
|
||||
}
|
||||
|
||||
@@ -16,7 +17,7 @@ inline std::string create_AVR_format(const std::string &msg_hex) {
|
||||
// @016CE3671C747700;
|
||||
|
||||
// @5500068BA46CB402c18d34fc8000;
|
||||
inline std::string create_MLAT_AVR_format(const std::string &msg_hex,
|
||||
inline std::string create_MLAT_AVR_format(std::string_view msg_hex,
|
||||
MLAT_timestamp *timestamp) {
|
||||
// std::string signal_level_str =
|
||||
// bin2hex(std::bitset<8>(signal_level).to_string()); std::cout << "signal:"
|
||||
@@ -32,7 +33,7 @@ inline std::string create_MLAT_AVR_format(const std::string &msg_hex,
|
||||
// configuration settings, time stamp error ticks as int8_t (1 tick is 15ns)
|
||||
// (message "4" not on Mode-S Beast classic) <esc><esc>: true 0x1a <esc> is
|
||||
// 0x1a, and "1", "2" and "3" are 0x31, 0x32 and 0x33
|
||||
inline std::string create_Binary_Format_memory(const std::string &msg_hex,
|
||||
inline std::string create_Binary_Format_memory(std::string_view msg_hex,
|
||||
char signal_level,
|
||||
MLAT_timestamp *timestamp) {
|
||||
std::string type;
|
||||
@@ -72,9 +73,9 @@ inline std::string create_Binary_Format_memory(const std::string &msg_hex,
|
||||
}
|
||||
|
||||
// 把要传递的信息转化成转义字符 二进制==>转义二进制
|
||||
std::string packet_to_escape_format(std::string t);
|
||||
std::string packet_to_escape_format(std::string_view t);
|
||||
|
||||
inline std::string unescape_packet(std::string t) {
|
||||
inline std::string unescape_packet(std::string_view t) {
|
||||
std::string result;
|
||||
for (size_t i = 0; i < t.size(); ++i) {
|
||||
if (t[i] == '\x1a' && i + 1 < t.size() && t[i + 1] == '\x1a') {
|
||||
@@ -117,7 +118,7 @@ inline std::string create_Binary_Format_4(MLAT_timestamp timestamp,
|
||||
struct TCXO_Clock {
|
||||
unsigned long long times;
|
||||
|
||||
explicit TCXO_Clock(const std::string &hex) {
|
||||
explicit TCXO_Clock(std::string_view hex) {
|
||||
times = std::bitset<48>(hex2bin(hex)).to_ullong();
|
||||
}
|
||||
|
||||
@@ -325,7 +326,7 @@ struct HULC_Status_Message : HULC_Message {
|
||||
return ret;
|
||||
}
|
||||
|
||||
static double decodeLatitudeFromBAM(const std::string &bigdian4) {
|
||||
static double decodeLatitudeFromBAM(std::string_view bigdian4) {
|
||||
std::uint32_t value;
|
||||
yC::big_endian_2_platform(bigdian4, (char *)&value);
|
||||
// BAM 格式解码公式
|
||||
@@ -349,18 +350,18 @@ struct HULC_Status_Message : HULC_Message {
|
||||
}
|
||||
};
|
||||
|
||||
static void hulc_big_endian_set(const std::string &msg, void *field, int start,
|
||||
static void hulc_big_endian_set(std::string_view msg, void *field, int start,
|
||||
int len) {
|
||||
auto str = msg.substr(start, len);
|
||||
yC::big_endian_2_platform(str, (char *)field);
|
||||
}
|
||||
|
||||
static void hulc_set2(const std::string &msg, void *field, int start, int len) {
|
||||
static void hulc_set2(std::string_view msg, void *field, int start, int len) {
|
||||
auto it = msg.substr(start, len);
|
||||
std::memcpy(field, it.data(), len);
|
||||
}
|
||||
// msg 是文本二进制字符串 转义后的 1a34
|
||||
static HULC_Status_Message create_HULC_Status_Message(const std::string &msg) {
|
||||
static HULC_Status_Message create_HULC_Status_Message(std::string_view msg) {
|
||||
HULC_Status_Message ret{};
|
||||
hulc_big_endian_set(msg, &ret.SerNum, 4, 4);
|
||||
hulc_big_endian_set(msg, &ret.Flags, 8, 2);
|
||||
@@ -380,9 +381,9 @@ struct HULC_Reply_to_command : HULC_Message {
|
||||
};
|
||||
|
||||
// 0x1A 0x48 0x01 0x18 SerNum Flags I.U. xTime Lat Lon Alt Sat HDOP
|
||||
inline std::uint8_t get_id(const std::string &msg) { return msg[2]; }
|
||||
inline std::uint8_t get_id(std::string_view msg) { return msg[2]; }
|
||||
|
||||
inline std::uint8_t get_len(const std::string &msg) { return msg[3]; }
|
||||
inline std::uint8_t get_len(std::string_view msg) { return msg[3]; }
|
||||
|
||||
// Type Payload Size Description
|
||||
// 0x31 4 bytes Mode-A/C raw data
|
||||
@@ -400,7 +401,7 @@ inline std::uint8_t get_len(const std::string &msg) { return msg[3]; }
|
||||
// During Transmission: 1A 32 27 1A 1A E8 57 F0 1A 1A 6C
|
||||
// After Un-Escaping: 1A 32 27 1A E8 57 F0 1A 6C
|
||||
void Binary_Format_handle_buffer(
|
||||
std::string &buffer, const std::string &data,
|
||||
std::string &buffer, std::string_view data,
|
||||
const std::function<void(std::string &)> &callback);
|
||||
/* Command Message (Host ==> Device)
|
||||
The command-message has a simple ASCII based structure. Each message starts with
|
||||
@@ -414,7 +415,7 @@ in the respective protocol section.
|
||||
*/
|
||||
|
||||
std::string mode_s_msg_packet_to_server_packet(std::uint32_t id,
|
||||
std::string packet);
|
||||
std::string_view packet);
|
||||
|
||||
// 1a34 radarcape 状态帧消息
|
||||
// https://wiki.jetvision.de/wiki/Radarcape:Version_History#Status_Frame_0x34_contents_were_extended_with_GPS_and_UTC_information
|
||||
@@ -532,7 +533,7 @@ struct Radarcape_STATUS_Message {
|
||||
// 消息5字节
|
||||
//
|
||||
static Radarcape_STATUS_Message
|
||||
create_Radarcape_STATUS_Message(const std::string &msg_5) {
|
||||
create_Radarcape_STATUS_Message(std::string_view msg_5) {
|
||||
std::string msg = yC::big_endian_2_platform(msg_5.substr(2));
|
||||
Radarcape_STATUS_Message ret{};
|
||||
std::memcpy(&ret, (void *)(msg.data()), 3);
|
||||
|
||||
@@ -22,11 +22,12 @@
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <string_view>
|
||||
|
||||
// 1纳秒是 10 -9次方秒
|
||||
struct MLAT_timestamp {
|
||||
explicit MLAT_timestamp();
|
||||
explicit MLAT_timestamp(const std::string &memory_6);
|
||||
explicit MLAT_timestamp(std::string_view memory_6);
|
||||
MLAT_timestamp(std::uint32_t daysec, std::uint32_t nanosec);
|
||||
std::uint32_t daysec{}; // 当天的秒数 高18位
|
||||
std::uint32_t nanosec{}; // 当天的纳秒数 低30位
|
||||
|
||||
@@ -3,8 +3,9 @@
|
||||
#include "magic_enum/magic_enum.hpp"
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string_view>
|
||||
|
||||
template <typename EnumType> EnumType to_enum(const std::string &str) {
|
||||
template <typename EnumType> EnumType to_enum(std::string_view str) {
|
||||
auto opt = magic_enum::enum_cast<EnumType>(str);
|
||||
if (opt.has_value()) {
|
||||
return opt.value(); // 返回枚举值
|
||||
@@ -15,7 +16,7 @@ template <typename EnumType> EnumType to_enum(const std::string &str) {
|
||||
}
|
||||
|
||||
template <typename EnumType>
|
||||
std::optional<EnumType> to_enum2(const std::string &str) {
|
||||
std::optional<EnumType> to_enum2(std::string_view str) {
|
||||
auto opt = magic_enum::enum_cast<EnumType>(str);
|
||||
if (opt.has_value()) {
|
||||
return opt.value(); // 返回枚举值
|
||||
@@ -65,7 +66,7 @@ template <typename Enum> std::vector<Enum> split_flags(Enum flags) {
|
||||
|
||||
inline std::string to_string(const char *val) { return std::string(val); }
|
||||
|
||||
template <> inline std::string to_string(std::string val) { return val; }
|
||||
template <> inline std::string to_string(std::string_view val) { return val; }
|
||||
template <> inline std::string to_string(bool val) {
|
||||
return val ? "true" : "false";
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@
|
||||
|
||||
#include "magic_enum.hpp"
|
||||
#include "magic_enum_flags.hpp"
|
||||
#include <string_view>
|
||||
|
||||
#if !defined(MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT)
|
||||
#define MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT 1
|
||||
|
||||
Reference in New Issue
Block a user