@@ -104,6 +104,8 @@ extern "C" {
104
104
} while (0 )
105
105
#endif
106
106
107
+
108
+
107
109
#ifdef _MSC_VER
108
110
#define SNPRINTF _snprintf_s
109
111
#pragma warning(push)
@@ -117,6 +119,14 @@ extern "C" {
117
119
118
120
namespace picojson {
119
121
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
+
120
130
enum {
121
131
null_type,
122
132
boolean_type,
@@ -137,7 +147,13 @@ struct null {};
137
147
class value {
138
148
public:
139
149
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;
141
157
union _storage {
142
158
bool boolean_;
143
159
double number_;
@@ -161,7 +177,7 @@ class value {
161
177
explicit value (int64_t i);
162
178
#endif
163
179
explicit value (double n);
164
- explicit value (const std::string & s);
180
+ explicit value (string_view s);
165
181
explicit value (const array &a);
166
182
explicit value (const object &o);
167
183
#if PICOJSON_USE_RVALUE_REFERENCE
@@ -188,12 +204,12 @@ class value {
188
204
#endif
189
205
bool evaluate_as_boolean () const ;
190
206
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 ;
192
208
value &get (const size_t idx);
193
- value &get (const std::string & key);
209
+ value &get (string_view key);
194
210
195
211
bool contains (const size_t idx) const ;
196
- bool contains (const std::string & key) const ;
212
+ bool contains (string_view key) const ;
197
213
std::string to_str () const ;
198
214
template <typename Iter> void serialize (Iter os, bool prettify = false ) const ;
199
215
std::string serialize (bool prettify = false ) const ;
@@ -209,8 +225,7 @@ class value {
209
225
typedef value::array array;
210
226
typedef value::object object;
211
227
212
- inline value::value () : type_(null_type), u_() {
213
- }
228
+ inline value::value () : type_(null_type), u_() {}
214
229
215
230
inline value::value (int type, bool ) : type_(type), u_() {
216
231
switch (type) {
@@ -257,7 +272,7 @@ inline value::value(double n) : type_(number_type), u_() {
257
272
u_.number_ = n;
258
273
}
259
274
260
- inline value::value (const std::string & s) : type_(string_type), u_() {
275
+ inline value::value (string_view s) : type_(string_type), u_() {
261
276
u_.string_ = new std::string (s);
262
277
}
263
278
@@ -452,14 +467,14 @@ inline value &value::get(const size_t idx) {
452
467
return idx < u_.array_ ->size () ? (*u_.array_ )[idx] : s_null;
453
468
}
454
469
455
- inline const value &value::get (const std::string & key) const {
470
+ inline const value &value::get (string_view key) const {
456
471
static value s_null;
457
472
PICOJSON_ASSERT (is<object>());
458
473
object::const_iterator i = u_.object_ ->find (key);
459
474
return i != u_.object_ ->end () ? i->second : s_null;
460
475
}
461
476
462
- inline value &value::get (const std::string & key) {
477
+ inline value &value::get (string_view key) {
463
478
static value s_null;
464
479
PICOJSON_ASSERT (is<object>());
465
480
object::iterator i = u_.object_ ->find (key);
@@ -471,7 +486,7 @@ inline bool value::contains(const size_t idx) const {
471
486
return idx < u_.array_ ->size ();
472
487
}
473
488
474
- inline bool value::contains (const std::string & key) const {
489
+ inline bool value::contains (string_view key) const {
475
490
PICOJSON_ASSERT (is<object>());
476
491
object::const_iterator i = u_.object_ ->find (key);
477
492
return i != u_.object_ ->end ();
@@ -522,7 +537,7 @@ inline std::string value::to_str() const {
522
537
return std::string ();
523
538
}
524
539
525
- template <typename Iter> void copy (const std::string & s, Iter oi) {
540
+ template <typename Iter> void copy (string_view s, Iter oi) {
526
541
std::copy (s.begin (), s.end (), oi);
527
542
}
528
543
@@ -556,7 +571,7 @@ template <typename Iter> struct serialize_str_char {
556
571
}
557
572
};
558
573
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) {
560
575
*oi++ = ' "' ;
561
576
serialize_str_char<Iter> process_char = {oi};
562
577
std::for_each (s.begin (), s.end (), process_char);
@@ -703,8 +718,8 @@ template <typename Iter> class input {
703
718
}
704
719
return true ;
705
720
}
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 ) {
708
723
if (getc () != *pi ) {
709
724
ungetc ();
710
725
return false ;
@@ -1124,7 +1139,7 @@ template <typename Iter> inline Iter parse(value &out, const Iter &first, const
1124
1139
return _parse (ctx, first, last, err);
1125
1140
}
1126
1141
1127
- inline std::string parse (value &out, const std::string & s) {
1142
+ inline std::string parse (value &out, string_view s) {
1128
1143
std::string err;
1129
1144
parse (out, s.begin (), s.end (), &err);
1130
1145
return err;
@@ -1139,7 +1154,7 @@ inline std::string parse(value &out, std::istream &is) {
1139
1154
template <typename T> struct last_error_t { static std::string s; };
1140
1155
template <typename T> std::string last_error_t <T>::s;
1141
1156
1142
- inline void set_last_error (const std::string & s) {
1157
+ inline void set_last_error (string_view s) {
1143
1158
last_error_t <bool >::s = s;
1144
1159
}
1145
1160
0 commit comments