std::string_view 的使用
This commit is contained in:
+27
-25
@@ -6,9 +6,9 @@
|
||||
#define HANDLE_ERROR(reason) \
|
||||
JSON::handle_error(__func__, reason, __FILE__, __LINE__);
|
||||
namespace Psc {
|
||||
std::function<void(const char *, const std::string&, const char *, int)>
|
||||
std::function<void(const char *, std::string_view, const char *, int)>
|
||||
JSON::handle_error = [](const char *function_name,
|
||||
const std::string& reason, const char *file,
|
||||
std::string_view reason, const char *file,
|
||||
int line) {
|
||||
std::ostringstream oss;
|
||||
oss << "JSON::" << function_name << " 原因:" << reason << std::endl;
|
||||
@@ -34,7 +34,7 @@ JSON::operator std::string() const {
|
||||
}
|
||||
PSC_DEFINE_TRIPLE_API_CONST_METHOD_FROM_BOOL(std::string, JSON, get_string,
|
||||
PSC_JSON_KEY_PARAMS)
|
||||
bool JSON::get_string_ec(const std::string& key, std::string& out,
|
||||
bool JSON::get_string_ec(std::string_view key, std::string& out,
|
||||
std::error_code& ec) const noexcept {
|
||||
const JSON *that = get(key);
|
||||
if (!that) {
|
||||
@@ -56,7 +56,7 @@ bool JSON::bool_val_ec(bool& out, std::error_code& ec) const noexcept {
|
||||
}
|
||||
PSC_DEFINE_TRIPLE_API_CONST_METHOD_FROM_BOOL(bool, JSON, get_bool,
|
||||
PSC_JSON_KEY_PARAMS)
|
||||
bool JSON::get_bool_ec(const std::string& key, bool& out,
|
||||
bool JSON::get_bool_ec(std::string_view key, bool& out,
|
||||
std::error_code& ec) const noexcept {
|
||||
const Psc::JSON *that = get(key);
|
||||
if (!that) {
|
||||
@@ -65,7 +65,7 @@ bool JSON::get_bool_ec(const std::string& key, bool& out,
|
||||
}
|
||||
return that->bool_val_ec(out, ec);
|
||||
}
|
||||
JSON::JSON(std::string key, const JSON& children) : key(std::move(key)), val(children.val), valueType(children.valueType),
|
||||
JSON::JSON(std::string_view key, const JSON& children) : key(key), val(children.val), valueType(children.valueType),
|
||||
children(children.children) {}
|
||||
void JSON::append_list(const std::vector<JSON>& arr) {
|
||||
#ifdef _DEBUG
|
||||
@@ -99,7 +99,7 @@ void JSON::prepend(const JSON& json) {
|
||||
#endif
|
||||
children.insert(children.begin(), json);
|
||||
}
|
||||
bool JSON::has(const std::string& _key) const {
|
||||
bool JSON::has(std::string_view _key) const {
|
||||
for (const JSON& child : children) {
|
||||
if (child.key == _key) {
|
||||
return true;
|
||||
@@ -107,7 +107,7 @@ bool JSON::has(const std::string& _key) const {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
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;
|
||||
@@ -122,21 +122,21 @@ JSON JSON::array(const std::vector<JSON>& children) {
|
||||
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.key = std::string(key);
|
||||
ret.valueType = Array;
|
||||
ret.children = 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.key = std::string(key);
|
||||
ret.valueType = Object;
|
||||
ret.children = 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);
|
||||
@@ -144,13 +144,13 @@ 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,
|
||||
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;
|
||||
const std::string split = std::string(fatherType == Array ? array_split : object_split);
|
||||
if (valueType == String) {
|
||||
ret.append("\"" + val + "\"");
|
||||
goto end;
|
||||
@@ -164,7 +164,8 @@ void JSON::append_value_string(std::string& ret, const int& indent, bool isEnd,
|
||||
goto end;
|
||||
}
|
||||
if (valueType == Object) {
|
||||
ret.append("{" + object_split);
|
||||
ret.append("{");
|
||||
ret.append(object_split);
|
||||
size_t n = children.size();
|
||||
if (n) {
|
||||
for (size_t i = 0; i < n - 1; ++i) {
|
||||
@@ -178,7 +179,8 @@ void JSON::append_value_string(std::string& ret, const int& indent, bool isEnd,
|
||||
goto end;
|
||||
}
|
||||
if (valueType == Array) {
|
||||
ret.append("[" + array_split);
|
||||
ret.append("[");
|
||||
ret.append(array_split);
|
||||
size_t n = children.size();
|
||||
if (n) {
|
||||
for (size_t i = 0; i < n - 1; ++i) {
|
||||
@@ -207,9 +209,9 @@ 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,
|
||||
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 + '\"';
|
||||
@@ -317,9 +319,9 @@ bool parse_number(std::string& s, size_t& i, std::string& out,
|
||||
bool parse_json_EX(std::string& s, size_t& i, JSON& out,
|
||||
std::error_code& ec) noexcept;
|
||||
PSC_DEFINE_TRIPLE_API_FROM_BOOL(JSON, parse_json, PSC_JSON_PARSE_PARAMS)
|
||||
bool parse_json_ec(const std::string& text, JSON& out,
|
||||
bool parse_json_ec(std::string_view text, JSON& out,
|
||||
std::error_code& ec) noexcept {
|
||||
auto& t = const_cast<std::string&>(text);
|
||||
auto t = std::string(text);
|
||||
size_t i = 0;
|
||||
return parse_json_EX(t, i, out, ec);
|
||||
}
|
||||
@@ -445,9 +447,9 @@ bool parse_array(std::string& s, size_t& i, JSON& out,
|
||||
return true;
|
||||
}
|
||||
PSC_DEFINE_TRIPLE_API_FROM_BOOL(JSON, parse_json_file, PSC_JSON_FILE_PARAMS)
|
||||
bool parse_json_file_ec(const std::string& file_name, JSON& out,
|
||||
bool parse_json_file_ec(std::string_view file_name, JSON& out,
|
||||
std::error_code& ec) noexcept {
|
||||
auto readFile = [](const std::string& path, std::string& content,
|
||||
auto readFile = [](std::string_view path, std::string& content,
|
||||
std::error_code& ec) noexcept {
|
||||
std::ifstream infile(path, std::ios::binary | std::ios::ate);
|
||||
if (!infile.is_open()) {
|
||||
|
||||
Reference in New Issue
Block a user