@@ -104,6 +104,8 @@ extern "C" {
104104 } while (0 )
105105#endif
106106
107+
108+
107109#ifdef _MSC_VER
108110#define SNPRINTF _snprintf_s
109111#pragma warning(push)
@@ -117,6 +119,14 @@ extern "C" {
117119
118120namespace picojson {
119121
122+ // string_view
123+ #if __cplusplus >= 201703L
124+ #include < string_view>
125+ using string_view = std::string_view;
126+ #else
127+ using string_view = const std::string &;
128+ #endif
129+
120130enum {
121131 null_type,
122132 boolean_type,
@@ -137,7 +147,13 @@ struct null {};
137147class value {
138148public:
139149 typedef std::vector<value> array;
140- typedef std::map<std::string, value> object;
150+ typedef std::map<std::string, value
151+ #if __cplusplus >= 201703L
152+ // allow map to use transparent type for comparisons
153+ ,
154+ std::less<>
155+ #endif
156+ > object;
141157 union _storage {
142158 bool boolean_;
143159 double number_;
@@ -161,7 +177,7 @@ class value {
161177 explicit value (int64_t i);
162178#endif
163179 explicit value (double n);
164- explicit value (const std::string & s);
180+ explicit value (string_view s);
165181 explicit value (const array &a);
166182 explicit value (const object &o);
167183#if PICOJSON_USE_RVALUE_REFERENCE
@@ -188,12 +204,12 @@ class value {
188204#endif
189205 bool evaluate_as_boolean () const ;
190206 const value &get (const size_t idx) const ;
191- const value &get (const std::string & key) const ;
207+ const value &get (string_view key) const ;
192208 value &get (const size_t idx);
193- value &get (const std::string & key);
209+ value &get (string_view key);
194210
195211 bool contains (const size_t idx) const ;
196- bool contains (const std::string & key) const ;
212+ bool contains (string_view key) const ;
197213 std::string to_str () const ;
198214 template <typename Iter> void serialize (Iter os, bool prettify = false ) const ;
199215 std::string serialize (bool prettify = false ) const ;
@@ -209,8 +225,7 @@ class value {
209225typedef value::array array;
210226typedef value::object object;
211227
212- inline value::value () : type_(null_type), u_() {
213- }
228+ inline value::value () : type_(null_type), u_() {}
214229
215230inline value::value (int type, bool ) : type_(type), u_() {
216231 switch (type) {
@@ -257,7 +272,7 @@ inline value::value(double n) : type_(number_type), u_() {
257272 u_.number_ = n;
258273}
259274
260- inline value::value (const std::string & s) : type_(string_type), u_() {
275+ inline value::value (string_view s) : type_(string_type), u_() {
261276 u_.string_ = new std::string (s);
262277}
263278
@@ -452,14 +467,14 @@ inline value &value::get(const size_t idx) {
452467 return idx < u_.array_ ->size () ? (*u_.array_ )[idx] : s_null;
453468}
454469
455- inline const value &value::get (const std::string & key) const {
470+ inline const value &value::get (string_view key) const {
456471 static value s_null;
457472 PICOJSON_ASSERT (is<object>());
458473 object::const_iterator i = u_.object_ ->find (key);
459474 return i != u_.object_ ->end () ? i->second : s_null;
460475}
461476
462- inline value &value::get (const std::string & key) {
477+ inline value &value::get (string_view key) {
463478 static value s_null;
464479 PICOJSON_ASSERT (is<object>());
465480 object::iterator i = u_.object_ ->find (key);
@@ -471,7 +486,7 @@ inline bool value::contains(const size_t idx) const {
471486 return idx < u_.array_ ->size ();
472487}
473488
474- inline bool value::contains (const std::string & key) const {
489+ inline bool value::contains (string_view key) const {
475490 PICOJSON_ASSERT (is<object>());
476491 object::const_iterator i = u_.object_ ->find (key);
477492 return i != u_.object_ ->end ();
@@ -522,7 +537,7 @@ inline std::string value::to_str() const {
522537 return std::string ();
523538}
524539
525- template <typename Iter> void copy (const std::string & s, Iter oi) {
540+ template <typename Iter> void copy (string_view s, Iter oi) {
526541 std::copy (s.begin (), s.end (), oi);
527542}
528543
@@ -556,7 +571,7 @@ template <typename Iter> struct serialize_str_char {
556571 }
557572};
558573
559- template <typename Iter> void serialize_str (const std::string & s, Iter oi) {
574+ template <typename Iter> void serialize_str (string_view s, Iter oi) {
560575 *oi++ = ' "' ;
561576 serialize_str_char<Iter> process_char = {oi};
562577 std::for_each (s.begin (), s.end (), process_char);
@@ -703,8 +718,8 @@ template <typename Iter> class input {
703718 }
704719 return true ;
705720 }
706- bool match (const std::string & pattern) {
707- for (std::string::const_iterator pi (pattern.begin ()); pi != pattern.end (); ++pi) {
721+ bool match (string_view pattern) {
722+ for (auto pi (pattern.begin ()); pi != pattern.end (); ++pi) {
708723 if (getc () != *pi) {
709724 ungetc ();
710725 return false ;
@@ -1124,7 +1139,7 @@ template <typename Iter> inline Iter parse(value &out, const Iter &first, const
11241139 return _parse (ctx, first, last, err);
11251140}
11261141
1127- inline std::string parse (value &out, const std::string & s) {
1142+ inline std::string parse (value &out, string_view s) {
11281143 std::string err;
11291144 parse (out, s.begin (), s.end (), &err);
11301145 return err;
@@ -1139,7 +1154,7 @@ inline std::string parse(value &out, std::istream &is) {
11391154template <typename T> struct last_error_t { static std::string s; };
11401155template <typename T> std::string last_error_t <T>::s;
11411156
1142- inline void set_last_error (const std::string & s) {
1157+ inline void set_last_error (string_view s) {
11431158 last_error_t <bool >::s = s;
11441159}
11451160
0 commit comments