diff options
| -rw-r--r-- | libjsonpp/jsonFlexLexer.cpp | 11 | ||||
| -rw-r--r-- | libjsonpp/jsonFlexLexer.h | 6 | ||||
| -rw-r--r-- | libjsonpp/jsonValueFlexLexer.cpp | 20 | ||||
| -rw-r--r-- | libjsonpp/jsonValueFlexLexer.h | 6 | ||||
| -rw-r--r-- | libjsonpp/jsonpp.cpp | 12 | ||||
| -rw-r--r-- | libjsonpp/jsonpp.h | 8 | ||||
| -rw-r--r-- | libjsonpp/parse.cpp | 24 | ||||
| -rw-r--r-- | libjsonpp/serialize.cpp | 48 | ||||
| -rw-r--r-- | libjsonpp/testEncoding.cpp | 32 | ||||
| -rw-r--r-- | libjsonpp/testParse.cpp | 11 | ||||
| -rw-r--r-- | libjsonpp/testSerialise.cpp | 20 | ||||
| -rw-r--r-- | libjsonpp/ustring_wrap.cpp | 1 | ||||
| -rw-r--r-- | libjsonpp/ustring_wrap.h | 10 | 
13 files changed, 111 insertions, 98 deletions
diff --git a/libjsonpp/jsonFlexLexer.cpp b/libjsonpp/jsonFlexLexer.cpp index b58c82f..e1f5006 100644 --- a/libjsonpp/jsonFlexLexer.cpp +++ b/libjsonpp/jsonFlexLexer.cpp @@ -1,9 +1,10 @@ -#include "jsonValueFlexLexer.h" +#include "jsonValueFlexLexer.h" // IWYU pragma: keep +#include <format>  #include <glibmm/convert.h>  namespace json { -	jsonFlexLexer::jsonFlexLexer(std::istream & in, std::string enc) : -		yyFlexLexer(&in, nullptr), encoding(enc != utf8 ? std::move(enc) : std::string()) +	jsonFlexLexer::jsonFlexLexer(std::istream & input, std::string enc) : +		yyFlexLexer(&input, nullptr), encoding(enc != utf8 ? std::move(enc) : std::string())  	{  		yy_push_state(0);  	} @@ -23,8 +24,8 @@ namespace json {  		throw ParseError(msg, 0, 0);  	} -	ParseError::ParseError(const char * at, int l, int s) : -		std::invalid_argument(Glib::ustring::compose("Parse error at or near %1 (line %2, state %3)", at, l, s)) +	ParseError::ParseError(const char * atOrNear, int line, int state) : +		std::invalid_argument(std::format("Parse error at or near {} (line {}, state {})", atOrNear, line, state))  	{  	}  } diff --git a/libjsonpp/jsonFlexLexer.h b/libjsonpp/jsonFlexLexer.h index eab59a6..3f497f9 100644 --- a/libjsonpp/jsonFlexLexer.h +++ b/libjsonpp/jsonFlexLexer.h @@ -10,6 +10,7 @@  namespace json {  #pragma GCC visibility push(default) +  	class ParseError : public std::invalid_argument {  	public:  		ParseError(const char *, int, int); @@ -28,8 +29,8 @@ namespace json {  		virtual void PushBoolean(bool) = 0;  		virtual void PushNumber(double) = 0;  		virtual void PushNull() = 0; -		virtual void PushText(std::string &&) = 0; -		virtual void PushKey(std::string &&) = 0; +		virtual void PushText(std::string) = 0; +		virtual void PushKey(std::string) = 0;  		virtual void EndArray() = 0;  		virtual void EndObject() = 0; @@ -40,6 +41,7 @@ namespace json {  		std::string buf;  		const std::string encoding;  	}; +  #pragma GCC visibility pop  } diff --git a/libjsonpp/jsonValueFlexLexer.cpp b/libjsonpp/jsonValueFlexLexer.cpp index 68b0f5d..fe66390 100644 --- a/libjsonpp/jsonValueFlexLexer.cpp +++ b/libjsonpp/jsonValueFlexLexer.cpp @@ -1,11 +1,11 @@  #include "jsonValueFlexLexer.h"  namespace json { -	jsonValueFlexLexer::jsonValueFlexLexer(std::istream & in, std::string enc, Value & v) : -		jsonFlexLexer(in, std::move(enc)) +	jsonValueFlexLexer::jsonValueFlexLexer(std::istream & input, std::string enc, Value & val) : +		jsonFlexLexer(input, std::move(enc))  	{ -		acceptValues.push([&v](auto && value) { -			return &(v = std::forward<Value>(value)); +		acceptValues.emplace([&val](auto && value) { +			return &(val = std::forward<Value>(value));  		});  	} @@ -13,7 +13,7 @@ namespace json {  	jsonValueFlexLexer::BeginObject()  	{  		auto object = std::get_if<Object>(acceptValues.top()(Object())); -		acceptValues.push([object, this](auto && value) { +		acceptValues.emplace([object, this](auto && value) {  			return &object->emplace(std::move(name), std::forward<Value>(value)).first->second;  		});  	} @@ -22,7 +22,7 @@ namespace json {  	jsonValueFlexLexer::BeginArray()  	{  		auto array = std::get_if<Array>(acceptValues.top()(Array())); -		acceptValues.push([array](auto && value) { +		acceptValues.emplace([array](auto && value) {  			return &array->emplace_back(std::forward<Value>(value));  		});  	} @@ -46,15 +46,15 @@ namespace json {  	}  	void -	jsonValueFlexLexer::PushText(std::string && value) +	jsonValueFlexLexer::PushText(std::string value)  	{ -		acceptValues.top()(std::forward<std::string>(value)); +		acceptValues.top()(std::move(value));  	}  	void -	jsonValueFlexLexer::PushKey(std::string && value) +	jsonValueFlexLexer::PushKey(std::string value)  	{ -		name = std::forward<std::string>(value); +		name = std::move(value);  	}  	void diff --git a/libjsonpp/jsonValueFlexLexer.h b/libjsonpp/jsonValueFlexLexer.h index 964674c..fafb50d 100644 --- a/libjsonpp/jsonValueFlexLexer.h +++ b/libjsonpp/jsonValueFlexLexer.h @@ -9,7 +9,7 @@  namespace json {  	class jsonValueFlexLexer : public jsonFlexLexer {  	public: -		jsonValueFlexLexer(std::istream &, std::string enc, Value & v); +		jsonValueFlexLexer(std::istream &, std::string enc, Value &);  		void BeginObject() override;  		void BeginArray() override; @@ -17,8 +17,8 @@ namespace json {  		void PushBoolean(bool) override;  		void PushNumber(double) override;  		void PushNull() override; -		void PushText(std::string &&) override; -		void PushKey(std::string &&) override; +		void PushText(std::string) override; +		void PushKey(std::string) override;  		void EndArray() override;  		void EndObject() override; diff --git a/libjsonpp/jsonpp.cpp b/libjsonpp/jsonpp.cpp index 66f7524..2bcad7a 100644 --- a/libjsonpp/jsonpp.cpp +++ b/libjsonpp/jsonpp.cpp @@ -4,10 +4,10 @@ namespace json {  	const std::string null("null");  	const std::string utf8("utf-8"); -	static_assert(std::is_move_constructible<Value>::value); -	static_assert(std::is_nothrow_move_constructible<Object>::value); -	static_assert(std::is_nothrow_move_constructible<Array>::value); -	static_assert(std::is_move_assignable<Value>::value); -	static_assert(std::is_nothrow_move_assignable<Object>::value); -	static_assert(std::is_nothrow_move_assignable<Array>::value); +	static_assert(std::is_move_constructible_v<Value>); +	static_assert(std::is_nothrow_move_constructible_v<Object>); +	static_assert(std::is_nothrow_move_constructible_v<Array>); +	static_assert(std::is_move_assignable_v<Value>); +	static_assert(std::is_nothrow_move_assignable_v<Object>); +	static_assert(std::is_nothrow_move_assignable_v<Array>);  } diff --git a/libjsonpp/jsonpp.h b/libjsonpp/jsonpp.h index 7e84285..f6665a7 100644 --- a/libjsonpp/jsonpp.h +++ b/libjsonpp/jsonpp.h @@ -1,8 +1,8 @@  #ifndef JSONPP_H  #define JSONPP_H -#include "jsonFlexLexer.h" -#include "ustring_wrap.h" +#include "jsonFlexLexer.h" // IWYU pragma: keep +#include <glibmm/ustring.h>  #include <map>  #include <variant>  #include <vector> @@ -15,17 +15,21 @@ namespace json {  	using Number = double;  	using Boolean = bool;  #pragma GCC visibility push(default) +  	class Null { };  	class Object;  	class Array;  	using Value = std::variant<Null, String, Number, Object, Array, Boolean>;  	using M = std::map<std::string, Value, std::less<>>; +  	// NOLINTNEXTLINE(misc-no-recursion)  	class Object : public M {  	public:  		using M::M;  	}; +  	using A = std::vector<Value>; +  	// NOLINTNEXTLINE(misc-no-recursion)  	class Array : public A {  	public: diff --git a/libjsonpp/parse.cpp b/libjsonpp/parse.cpp index 6a4c4f1..70a6d92 100644 --- a/libjsonpp/parse.cpp +++ b/libjsonpp/parse.cpp @@ -3,32 +3,32 @@  namespace json {  	Value -	parseValue(std::istream & s) +	parseValue(std::istream & input)  	{ -		return parseValue(s, std::string()); +		return parseValue(input, std::string());  	}  	Value -	parseValue(std::istream & s, const std::string & enc) +	parseValue(std::istream & input, const std::string & enc)  	{ -		Value v; -		jsonValueFlexLexer jfl(s, enc, v); +		Value val; +		jsonValueFlexLexer jfl(input, enc, val);  		while (jfl.yylex()) { } -		return v; +		return val;  	}  	Value -	parseValue(const Glib::ustring & s) +	parseValue(const Glib::ustring & str)  	{ -		std::stringstream stream(s); +		std::stringstream stream(str);  		return parseValue(stream);  	}  	Value -	parseValue(Glib::ustring::const_iterator & s) +	parseValue(Glib::ustring::const_iterator & strIter)  	{ -		Glib::ustring::const_iterator start = s; -		while (*s++) { } -		return parseValue(Glib::ustring(start, --s)); +		Glib::ustring::const_iterator start = strIter; +		while (*strIter++) { } +		return parseValue(Glib::ustring(start, --strIter));  	}  } diff --git a/libjsonpp/serialize.cpp b/libjsonpp/serialize.cpp index c757d40..ec7dd5c 100644 --- a/libjsonpp/serialize.cpp +++ b/libjsonpp/serialize.cpp @@ -14,22 +14,26 @@ namespace json {  			  << std::setfill('0') // for String \uNNNN  					;  		} +  		void  		// NOLINTNEXTLINE(misc-no-recursion) -		operator()(const Value & v) const +		operator()(const Value & val) const  		{ -			std::visit(*this, v); +			std::visit(*this, val);  		} +  		void  		operator()(const std::string & str) const  		{  			(*this)(str, e);  		} +  		void  		operator()(const String & str) const  		{  			(*this)(str, e);  		} +  		void  		operator()(const String & str, const std::string & enc) const  		{ @@ -40,6 +44,7 @@ namespace json {  				serializeString(str);  			}  		} +  		void  		serializeString(const String & str) const  		{ @@ -53,12 +58,12 @@ namespace json {  					s << str.raw();  					break;  				} -				else if (start != i) { +				if (start != i) {  					s << Glib::ustring(start, i).raw();  				}  				while (i != str.end() && (*i < 32 || *i == '/' || *i == '"' || *i == '\\')) { -					const gunichar & c = *i; -					switch (c) { +					const gunichar & chr = *i; +					switch (chr) {  						case '\f':  							s << "\\f";  							break; @@ -84,7 +89,7 @@ namespace json {  							s << "\\\"";  							break;  						default: -							s << "\\u" << std::setw(4) << std::hex << c << std::setw(1); +							s << "\\u" << std::setw(4) << std::hex << chr << std::setw(1);  							break;  					}  					i++; @@ -92,48 +97,53 @@ namespace json {  			}  			s << '"';  		} +  		void  		operator()(const Number & n) const  		{  			s << std::dec << n;  		} +  		void  		// NOLINTNEXTLINE(misc-no-recursion) -		operator()(const Array & a) const +		operator()(const Array & array) const  		{  			s << '['; -			for (const Array::value_type & v : a) { -				if (&v != &*a.begin()) { +			for (const auto & element : array) { +				if (&element != &*array.begin()) {  					s << ',';  				} -				(*this)(v); +				(*this)(element);  			}  			s << ']';  		} +  		void  		// NOLINTNEXTLINE(misc-no-recursion) -		operator()(const Object & o) const +		operator()(const Object & object) const  		{  			s << '{'; -			for (const Object::value_type & v : o) { -				if (&v != &*o.begin()) { +			for (const auto & [name, value] : object) { +				if (&name != std::addressof(object.begin()->first)) {  					s << ',';  				} -				(*this)(v.first); +				(*this)(name);  				s << ':'; -				(*this)(v.second); +				(*this)(value);  			}  			s << '}';  		} +  		void  		operator()(const Null &) const  		{  			s << null;  		} +  		void -		operator()(const Boolean & b) const +		operator()(const Boolean & boolean) const  		{ -			s << b; +			s << boolean;  		}  	private: @@ -142,8 +152,8 @@ namespace json {  	};  	void -	serializeValue(const Value & v, std::ostream & s, const std::string & enc) +	serializeValue(const Value & value, std::ostream & strm, const std::string & enc)  	{ -		JsonSerialize(s, enc)(v); +		JsonSerialize(strm, enc)(value);  	}  } diff --git a/libjsonpp/testEncoding.cpp b/libjsonpp/testEncoding.cpp index b92029a..d6bc10f 100644 --- a/libjsonpp/testEncoding.cpp +++ b/libjsonpp/testEncoding.cpp @@ -3,25 +3,27 @@  #include "jsonpp.h"  #include <filesystem> -#include <fstream>  #define XSTR(s) STR(s)  #define STR(s) #s -const std::filesystem::path root(XSTR(ROOT)); -static std::string -writeString(const json::Value & v, const std::string & enc) -{ -	std::stringstream ss; -	json::serializeValue(v, ss, enc); -	return ss.str(); +namespace { +	const std::filesystem::path ROOTDIR(XSTR(ROOT)); + +	std::string +	writeString(const json::Value & value, const std::string & enc) +	{ +		std::stringstream strm; +		json::serializeValue(value, strm, enc); +		return std::move(strm).str(); +	}  }  BOOST_AUTO_TEST_CASE(parse_latin1)  { -	std::stringstream ss("\"A \xD9\xF1\xEE\xE7\xF4\xD0\xE8 string.\""); -	BOOST_REQUIRE_EQUAL(19, ss.str().length()); -	BOOST_REQUIRE_EQUAL("A ÙñîçôÐè string.", std::get<Glib::ustring>(json::parseValue(ss, "latin1"))); +	std::stringstream strm("\"A \xD9\xF1\xEE\xE7\xF4\xD0\xE8 string.\""); +	BOOST_REQUIRE_EQUAL(19, strm.view().length()); +	BOOST_REQUIRE_EQUAL("A ÙñîçôÐè string.", std::get<Glib::ustring>(json::parseValue(strm, "latin1")));  }  BOOST_AUTO_TEST_CASE(write_latin1) @@ -32,8 +34,8 @@ BOOST_AUTO_TEST_CASE(write_latin1)  BOOST_AUTO_TEST_CASE(defaultEncoding)  { -	json::String s("Some string value"); -	std::stringstream ss; -	json::serializeValue(s, ss, ""); -	BOOST_REQUIRE_EQUAL(ss.str(), R"S("Some string value")S"); +	json::String str("Some string value"); +	std::stringstream strm; +	json::serializeValue(str, strm, ""); +	BOOST_REQUIRE_EQUAL(strm.str(), R"S("Some string value")S");  } diff --git a/libjsonpp/testParse.cpp b/libjsonpp/testParse.cpp index 5190599..a6a2c90 100644 --- a/libjsonpp/testParse.cpp +++ b/libjsonpp/testParse.cpp @@ -8,7 +8,10 @@  #define XSTR(s) STR(s)  #define STR(s) #s -const std::filesystem::path root(XSTR(ROOT)); + +namespace { +	const std::filesystem::path ROOTDIR(XSTR(ROOT)); +}  BOOST_AUTO_TEST_CASE(parse_bool_true)  { @@ -236,14 +239,14 @@ BOOST_AUTO_TEST_CASE(parse_string_escapedUnicode)  BOOST_AUTO_TEST_CASE(parse_sample_complexFile1)  { -	std::ifstream inFile((root / "initial" / "sample1.json").string()); +	std::ifstream inFile((ROOTDIR / "initial" / "sample1.json").string());  	json::Value obj = json::parseValue(inFile, "utf-8");  	BOOST_CHECK_EQUAL(obj.index(), 3);  }  BOOST_AUTO_TEST_CASE(parse_sample_complexFile2)  { -	std::ifstream inFile((root / "initial" / "sample2.json").string()); +	std::ifstream inFile((ROOTDIR / "initial" / "sample2.json").string());  	json::Value obj = json::parseValue(inFile, "utf-8");  	BOOST_CHECK_EQUAL(obj.index(), 3);  } @@ -251,7 +254,7 @@ BOOST_AUTO_TEST_CASE(parse_sample_complexFile2)  BOOST_AUTO_TEST_CASE(parse_sample_complexFile2_bulk, *boost::unit_test::disabled())  {  	for (int x = 0; x < 100; x++) { -		std::ifstream inFile((root / "initial" / "sample2.json").string()); +		std::ifstream inFile((ROOTDIR / "initial" / "sample2.json").string());  		std::ignore = json::parseValue(inFile, "utf-8");  	}  } diff --git a/libjsonpp/testSerialise.cpp b/libjsonpp/testSerialise.cpp index 15a579a..8045460 100644 --- a/libjsonpp/testSerialise.cpp +++ b/libjsonpp/testSerialise.cpp @@ -3,20 +3,22 @@  #include "jsonpp.h"  #include <filesystem> -#include <fstream>  #define XSTR(s) STR(s)  #define STR(s) #s -const std::filesystem::path root(XSTR(ROOT)); -using namespace std::literals; +namespace { +	const std::filesystem::path ROOTDIR(XSTR(ROOT)); -static std::string -writeString(const json::Value & v) -{ -	std::stringstream ss; -	json::serializeValue(v, ss, "utf-8"); -	return ss.str(); +	using namespace std::literals; + +	std::string +	writeString(const json::Value & value) +	{ +		std::stringstream strm; +		json::serializeValue(value, strm, "utf-8"); +		return strm.str(); +	}  }  BOOST_AUTO_TEST_CASE(serialise_true) diff --git a/libjsonpp/ustring_wrap.cpp b/libjsonpp/ustring_wrap.cpp deleted file mode 100644 index 039165f..0000000 --- a/libjsonpp/ustring_wrap.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "ustring_wrap.h" diff --git a/libjsonpp/ustring_wrap.h b/libjsonpp/ustring_wrap.h deleted file mode 100644 index 25e9b96..0000000 --- a/libjsonpp/ustring_wrap.h +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -#ifdef __clang__ -#	pragma clang diagnostic push -#	pragma clang diagnostic ignored "-Wdeprecated-copy" -#endif -#include <glibmm/ustring.h> -#ifdef __clang__ -#	pragma clang diagnostic pop -#endif  | 
