std::string_view 的使用
This commit is contained in:
@@ -19,6 +19,7 @@
|
||||
#include <thread>
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
#include <string_view>
|
||||
using namespace std;
|
||||
|
||||
rx_data_cbk_t s_cbk;
|
||||
@@ -1552,7 +1553,7 @@ struct Global_Read_Buffer {
|
||||
std::swap(ret, buffer);
|
||||
return ret;
|
||||
}
|
||||
void append(std::string buf) {
|
||||
void append(std::string_view buf) {
|
||||
std::lock_guard g(mtx);
|
||||
buffer.append(buf);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
#include "Json.h"
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
std::function<void(const char*, const std::string&, const char*, int)> Json::handle_error = [](const char* function_name, const std::string& reason, const char* file, int line) {
|
||||
#include <string_view>
|
||||
std::function<void(const char*, std::string_view, const char*, int)> Json::handle_error = [](const char* function_name, std::string_view reason, const char* file, int line) {
|
||||
std::ostringstream oss;
|
||||
oss << "Json::" << function_name << " 原因:" << reason << std::endl;
|
||||
oss << "位置:" << file << ":" << line << std::endl;
|
||||
@@ -28,7 +29,7 @@ Json::operator std::string() const {
|
||||
return const_cast<Json*>(this)->to_json_string();
|
||||
}
|
||||
|
||||
std::string Json::get_string(const std::string& key) const {
|
||||
std::string Json::get_string(std::string_view key) const {
|
||||
const Json* that = get(key);
|
||||
if (!that) {
|
||||
HANDLE_ERROR(key + " not found! " + to_json_string())
|
||||
@@ -44,7 +45,7 @@ bool Json::bool_val() const {
|
||||
return (val == "true");
|
||||
}
|
||||
|
||||
const std::string& Json::get_inside_string(const std::string& key) const {
|
||||
const std::string& Json::get_inside_string(std::string_view key) const {
|
||||
const Json* that = get(key);
|
||||
if (!that) {
|
||||
HANDLE_ERROR(key + " not found!")
|
||||
@@ -53,16 +54,16 @@ const std::string& Json::get_inside_string(const std::string& key) const {
|
||||
return that->val;
|
||||
}
|
||||
|
||||
Json::Json(std::string key, const char* val) : key(std::move(key)), val(std::string(val)), valueType(String) {
|
||||
Json::Json(std::string_view key, const char* val) : key(std::move(key)), val(std::string(val)), valueType(String) {
|
||||
}
|
||||
|
||||
Json::Json(std::string key, std::string val) : key(std::move(key)), val(std::move(val)), valueType(String) {
|
||||
Json::Json(std::string_view key, std::string_view val) : key(std::move(key)), val(std::move(val)), valueType(String) {
|
||||
}
|
||||
|
||||
Json::Json(std::string key, bool val) : key(std::move(key)), val(val ? "true" : "false"), valueType(Bool) {
|
||||
Json::Json(std::string_view key, bool val) : key(std::move(key)), val(val ? "true" : "false"), valueType(Bool) {
|
||||
}
|
||||
|
||||
Json::Json(std::string key, const Json& children) :
|
||||
Json::Json(std::string_view key, const Json& children) :
|
||||
key(std::move(key)), val(children.val), valueType(children.valueType), children(children.children) {}
|
||||
|
||||
|
||||
@@ -102,7 +103,7 @@ void Json::prepend(const Json& json) {
|
||||
Json::Json(const char* val) : val(std::string(val)), valueType(String) {
|
||||
}
|
||||
|
||||
Json::Json(std::string val) : val(std::move(val)), valueType(String) {
|
||||
Json::Json(std::string_view val) : val(std::move(val)), valueType(String) {
|
||||
}
|
||||
|
||||
Json::Json(const int& val) : val(std::to_string(val)), valueType(Number) {
|
||||
@@ -114,7 +115,7 @@ Json::Json(const double& val) : val(std::to_string(val)), valueType(Number) {
|
||||
Json::Json(const bool& val) : val(val ? "true" : "false"), valueType(Bool) {
|
||||
}
|
||||
|
||||
const Json* Json::get(const std::string& key) const {
|
||||
const Json* Json::get(std::string_view key) const {
|
||||
for (const Json& child : children) {
|
||||
if (child.key == key) {
|
||||
return &child;
|
||||
@@ -132,7 +133,7 @@ Json Json::object(const std::vector<Json>& children) {
|
||||
return object("", children);
|
||||
}
|
||||
|
||||
Json Json::array(std::string key, const std::vector<Json>& children) {
|
||||
Json Json::array(std::string_view key, const std::vector<Json>& children) {
|
||||
Json ret;
|
||||
ret.key = std::move(key);
|
||||
ret.valueType = Array;
|
||||
@@ -140,7 +141,7 @@ Json Json::array(std::string key, const std::vector<Json>& children) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
Json Json::object(std::string key, const std::vector<Json>& children) {
|
||||
Json Json::object(std::string_view key, const std::vector<Json>& children) {
|
||||
Json ret;
|
||||
ret.key = std::move(key);
|
||||
ret.valueType = Object;
|
||||
@@ -148,7 +149,7 @@ Json Json::object(std::string key, const std::vector<Json>& children) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::string Json::get_indent(const int& indent, const std::string& indentStr) {
|
||||
std::string Json::get_indent(const int& indent, std::string_view indentStr) {
|
||||
std::string ret;
|
||||
for (int i = 0; i < indent; ++i) {
|
||||
ret.append(indentStr);
|
||||
@@ -156,8 +157,8 @@ std::string Json::get_indent(const int& indent, const std::string& indentStr) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
void Json::append_value_string(std::string& ret, const int& indent, bool isEnd, const std::string& object_split,
|
||||
const std::string& array_split, const std::string& indentStr, JsonType fatherType) const {
|
||||
void Json::append_value_string(std::string& ret, const int& indent, bool isEnd, std::string_view object_split,
|
||||
std::string_view array_split, std::string_view indentStr, JsonType fatherType) const {
|
||||
ret.append(get_indent(indent, indentStr));
|
||||
if (fatherType == Object) ret.append("\"" + key + "\"" + ": ");
|
||||
const std::string split = fatherType == Array ? array_split : object_split;
|
||||
@@ -215,8 +216,8 @@ void handleBlank(std::string& s) {
|
||||
}
|
||||
}
|
||||
|
||||
std::string Json::to_json_string(const std::string& object_split, const std::string& array_split,
|
||||
const std::string& indentStr, const int& indent) const {
|
||||
std::string Json::to_json_string(std::string_view object_split, std::string_view array_split,
|
||||
std::string_view indentStr, const int& indent) const {
|
||||
if (valueType == Bool) return val;
|
||||
if (valueType == String) return '\"' + val + '\"';
|
||||
if (valueType == Number) return val;
|
||||
@@ -235,7 +236,7 @@ std::string Json::to_json_string(const std::string& object_split, const std::str
|
||||
Json::Json(std::nullptr_t) : valueType(Null), val("null") {
|
||||
}
|
||||
|
||||
Json::Json(std::string key, std::nullptr_t) : key(std::move(key)), valueType(Null), val("null") {
|
||||
Json::Json(std::string_view key, std::nullptr_t) : key(std::move(key)), valueType(Null), val("null") {
|
||||
}
|
||||
|
||||
Json::Json() = default;
|
||||
@@ -283,7 +284,7 @@ std::string parse_number(std::string& s, size_t& i) {
|
||||
|
||||
|
||||
std::optional<Json> parse_json_EX(std::string& s, size_t& i);
|
||||
std::optional<Json> parse_json(const std::string& s) {
|
||||
std::optional<Json> parse_json(std::string_view s) {
|
||||
auto& t = const_cast<std::string&>(s);
|
||||
size_t i = 0;
|
||||
return parse_json_EX(t, i);
|
||||
@@ -396,7 +397,7 @@ std::optional<Json> parse_array(std::string& s, size_t& i) {
|
||||
return ret;
|
||||
}
|
||||
std::optional<Json> parse_json_file(std::string& fileName) {
|
||||
static auto readFile = [](const std::string& path) -> std::string {
|
||||
static auto readFile = [](std::string_view path) -> std::string {
|
||||
std::ifstream infile(path, std::ios::binary | std::ios::ate);
|
||||
if (!infile.is_open()) {
|
||||
HANDLE_ERROR("找不到文件路径!" + path)
|
||||
|
||||
@@ -7,8 +7,9 @@
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <string_view>
|
||||
#define Json_GET(type, name) \
|
||||
[[nodiscard]] type get_##name(const std::string &key) const { \
|
||||
[[nodiscard]] type get_##name(std::string_view key) const { \
|
||||
const Json *that = get(key); \
|
||||
if (!that) { \
|
||||
std::cout << "get" #type " error json not found! key:" << key \
|
||||
@@ -18,7 +19,7 @@
|
||||
return that->name##_val(); \
|
||||
}
|
||||
|
||||
template <typename T_Number> T_Number to_number(const std::string &val) {
|
||||
template <typename T_Number> T_Number to_number(std::string_view val) {
|
||||
if constexpr (std::is_integral<T_Number>::value) {
|
||||
if constexpr (std::is_unsigned<T_Number>::value) {
|
||||
return static_cast<T_Number>(std::stoull(val));
|
||||
@@ -33,25 +34,25 @@ template <typename T_Number> T_Number to_number(const std::string &val) {
|
||||
|
||||
class Json {
|
||||
public:
|
||||
static std::function<void(const char *, const std::string &, const char *,
|
||||
static std::function<void(const char *, std::string_view, const char *,
|
||||
int)>
|
||||
handle_error;
|
||||
explicit operator std::string() const;
|
||||
friend std::ostream &operator<<(std::ostream &os, const Json &obj);
|
||||
[[nodiscard]] const Json *get(const std::string &key) const;
|
||||
[[nodiscard]] std::string get_string(const std::string &key) const;
|
||||
[[nodiscard]] const Json *get(std::string_view key) const;
|
||||
[[nodiscard]] std::string get_string(std::string_view key) const;
|
||||
[[nodiscard]] const std::string &
|
||||
get_inside_string(const std::string &key) const;
|
||||
get_inside_string(std::string_view key) const;
|
||||
// 键值对
|
||||
Json(std::string key, const char *charArrayVal);
|
||||
Json(std::string key, std::nullptr_t);
|
||||
Json(std::string key, std::string stringVal);
|
||||
Json(std::string key, bool boolVal);
|
||||
Json(std::string key, const Json &body);
|
||||
Json(std::string_view key, const char *charArrayVal);
|
||||
Json(std::string_view key, std::nullptr_t);
|
||||
Json(std::string_view key, std::string_view stringVal);
|
||||
Json(std::string_view key, bool boolVal);
|
||||
Json(std::string_view key, const Json &body);
|
||||
|
||||
[[nodiscard]] bool bool_val() const;
|
||||
Json_GET(bool, bool) template <typename T_Number>
|
||||
T_Number get_number(const std::string &key) const {
|
||||
T_Number get_number(std::string_view key) const {
|
||||
const Json *that = get(key);
|
||||
if (!that) {
|
||||
std::cout << "json not found! key:" << key
|
||||
@@ -70,7 +71,7 @@ public:
|
||||
// }
|
||||
|
||||
template <typename T_Number>
|
||||
Json(std::string key, const T_Number &intVal,
|
||||
Json(std::string_view key, const T_Number &intVal,
|
||||
typename std::enable_if<
|
||||
!std::is_same<T_Number, bool>::value &&
|
||||
!std::is_same<T_Number, std::atomic<bool>>::value>::type * = nullptr)
|
||||
@@ -79,14 +80,14 @@ public:
|
||||
void append_list(const std::vector<Json> &arr);
|
||||
void append(const Json &json);
|
||||
void prepend(const Json &json);
|
||||
static Json array(std::string key, const std::vector<Json> &arr);
|
||||
static Json object(std::string key, const std::vector<Json> &children);
|
||||
static Json array(std::string_view key, const std::vector<Json> &arr);
|
||||
static Json object(std::string_view key, const std::vector<Json> &children);
|
||||
static Json array(const std::vector<Json> &arr = {});
|
||||
static Json object(const std::vector<Json> &children = {});
|
||||
// 不加explicit 让他允许隐式类型转换
|
||||
Json(std::nullptr_t); // 只能填写 nullptr
|
||||
Json(const char *charArrayVal);
|
||||
Json(std::string stringVal);
|
||||
Json(std::string_view stringVal);
|
||||
Json(const int &intVal);
|
||||
Json(const double &doubleVal);
|
||||
Json(const bool &boolVal);
|
||||
@@ -96,23 +97,23 @@ public:
|
||||
std::string key, val;
|
||||
std::vector<Json> children;
|
||||
[[nodiscard]] std::string
|
||||
to_json_string(const std::string &object_split = "\n",
|
||||
const std::string &array_split = "\n",
|
||||
const std::string &indentStr = " ",
|
||||
to_json_string(std::string_view object_split = "\n",
|
||||
std::string_view array_split = "\n",
|
||||
std::string_view indentStr = " ",
|
||||
const int &indent = 0) const;
|
||||
Json();
|
||||
|
||||
private:
|
||||
static std::string get_indent(const int &indent,
|
||||
const std::string &indentStr);
|
||||
std::string_view indentStr);
|
||||
void append_value_string(std::string &ret, const int &indent, bool isEnd,
|
||||
const std::string &object_split,
|
||||
const std::string &array_split,
|
||||
const std::string &indentStr,
|
||||
std::string_view object_split,
|
||||
std::string_view array_split,
|
||||
std::string_view indentStr,
|
||||
JsonType fatherType) const;
|
||||
};
|
||||
|
||||
std::optional<Json> parse_json(const std::string &s);
|
||||
std::optional<Json> parse_json(std::string_view s);
|
||||
std::optional<Json> parse_json_file(std::string &fileName);
|
||||
std::optional<Json> parse_json_file(std::string &&fileName);
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <unistd.h>
|
||||
#include <string_view>
|
||||
|
||||
rx_data_cbk_t s_cbk;
|
||||
|
||||
@@ -1335,7 +1336,7 @@ struct Global_Read_Buffer {
|
||||
std::swap(ret, buffer);
|
||||
return ret;
|
||||
}
|
||||
void append(std::string buf) {
|
||||
void append(std::string_view buf) {
|
||||
std::lock_guard g(mtx);
|
||||
buffer.append(buf);
|
||||
}
|
||||
|
||||
@@ -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