Skip to content

Commit 73cca1d

Browse files
committed
much more string_view usage
1 parent 164db27 commit 73cca1d

File tree

1 file changed

+51
-43
lines changed

1 file changed

+51
-43
lines changed

lib/include/cpp-json/json.h

Lines changed: 51 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ inline const array &as_array(const value &v);
6666
inline const std::string &as_string(const value &v);
6767

6868
// does the given object have a given key?
69-
inline bool has_key(const value &v, const std::string &key) noexcept;
70-
inline bool has_key(const object &o, const std::string &key) noexcept;
69+
inline bool has_key(const value &v, std::string_view key) noexcept;
70+
inline bool has_key(const object &o, std::string_view key) noexcept;
7171

7272
// create a value from some JSON
7373
inline value parse(std::string_view s);
@@ -445,8 +445,8 @@ class object {
445445
const_iterator cend() const noexcept { return values_.end(); }
446446

447447
public:
448-
iterator find(const std::string &s) noexcept;
449-
const_iterator find(const std::string &s) const noexcept;
448+
iterator find(std::string_view s) noexcept;
449+
const_iterator find(std::string_view s) const noexcept;
450450

451451
public:
452452
size_type size() const noexcept {
@@ -462,11 +462,11 @@ class object {
462462
}
463463

464464
public:
465-
value operator[](const std::string &key) const;
466-
value &operator[](const std::string &key);
465+
value operator[](std::string_view key) const;
466+
value &operator[](std::string_view key);
467467

468-
value at(const std::string &key) const;
469-
value &at(const std::string &key);
468+
value at(std::string_view key) const;
469+
value &at(std::string_view key);
470470

471471
public:
472472
template <class T>
@@ -486,7 +486,7 @@ class object {
486486

487487
// NOTE(eteran): The values are stored in insertion order above,
488488
// but we use this map to have a fast lookup of key -> index
489-
std::map<std::string, size_t> index_map_;
489+
std::map<std::string, size_t, std::less<>> index_map_;
490490
};
491491

492492
inline object::iterator begin(object &obj) noexcept {
@@ -743,16 +743,16 @@ class value {
743743
Type type() const noexcept { return type_; }
744744

745745
public:
746-
value operator[](const std::string &key) const;
746+
value operator[](std::string_view key) const;
747747
value operator[](std::size_t n) const;
748-
value &operator[](const std::string &key);
748+
value &operator[](std::string_view key);
749749
value &operator[](std::size_t n);
750750

751751
public:
752752
inline value at(std::size_t n) const;
753753
inline value &at(std::size_t n);
754-
inline value at(const std::string &key) const;
755-
inline value &at(const std::string &key);
754+
inline value at(std::string_view key) const;
755+
inline value &at(std::string_view key);
756756

757757
public:
758758
value operator[](const ptr &ptr) const;
@@ -921,8 +921,29 @@ class parser {
921921
}
922922

923923
public:
924-
int line() const noexcept { return line_; }
925-
int column() const noexcept { return column_; }
924+
struct location_type {
925+
size_t line;
926+
size_t column;
927+
};
928+
929+
location_type location() {
930+
size_t line = 1;
931+
size_t col = 1;
932+
933+
if (cur_ < end_) {
934+
935+
for (auto i = begin_; i < cur_; ++i) {
936+
if (*i == '\n') {
937+
++line;
938+
col = 1;
939+
} else {
940+
++col;
941+
}
942+
}
943+
}
944+
945+
return location_type{line, col};
946+
}
926947

927948
private:
928949
static constexpr char ArrayBegin = '[';
@@ -1073,18 +1094,8 @@ class parser {
10731094
}
10741095

10751096
private:
1076-
void update_pos() {
1077-
if (*cur_ == '\n') {
1078-
column_ = 0;
1079-
++line_;
1080-
} else {
1081-
++column_;
1082-
}
1083-
}
1084-
10851097
void consume_whitespace() {
10861098
while (!at_end() && std::isspace(*cur_)) {
1087-
update_pos();
10881099
++cur_;
10891100
}
10901101
}
@@ -1109,7 +1120,6 @@ class parser {
11091120
return '\0';
11101121
}
11111122

1112-
update_pos();
11131123
return *cur_++;
11141124
}
11151125

@@ -1128,9 +1138,6 @@ class parser {
11281138
std::string_view::const_iterator begin_;
11291139
std::string_view::const_iterator cur_;
11301140
std::string_view::const_iterator end_;
1131-
1132-
int line_ = 1;
1133-
int column_ = 0;
11341141
};
11351142

11361143
inline std::string to_string(const value &v) {
@@ -1221,14 +1228,14 @@ std::string &as_string(value &v) {
12211228
return v.as_string();
12221229
}
12231230

1224-
inline bool has_key(const value &v, const std::string &key) noexcept {
1231+
inline bool has_key(const value &v, std::string_view key) noexcept {
12251232
if (is_object(v)) {
12261233
return has_key(as_object(v), key);
12271234
}
12281235
return false;
12291236
}
12301237

1231-
inline bool has_key(const object &o, const std::string &key) noexcept {
1238+
inline bool has_key(const object &o, std::string_view key) noexcept {
12321239
return o.find(key) != o.end();
12331240
}
12341241

@@ -1238,8 +1245,9 @@ inline value parse(std::string_view s) {
12381245
try {
12391246
return p.parse();
12401247
} catch (exception &e) {
1241-
e.line = p.line();
1242-
e.column = p.column();
1248+
auto loc = p.location();
1249+
e.line = loc.line;
1250+
e.column = loc.column;
12431251
throw;
12441252
}
12451253
}
@@ -1675,15 +1683,15 @@ inline object::object(std::initializer_list<object_entry> list) {
16751683
}
16761684
}
16771685

1678-
inline value object::operator[](const std::string &key) const {
1686+
inline value object::operator[](std::string_view key) const {
16791687
return at(key);
16801688
}
16811689

1682-
inline value &object::operator[](const std::string &key) {
1690+
inline value &object::operator[](std::string_view key) {
16831691
return at(key);
16841692
}
16851693

1686-
inline object::iterator object::find(const std::string &s) noexcept {
1694+
inline object::iterator object::find(std::string_view s) noexcept {
16871695

16881696
auto it = index_map_.find(s);
16891697
if (it != index_map_.end()) {
@@ -1693,7 +1701,7 @@ inline object::iterator object::find(const std::string &s) noexcept {
16931701
return values_.end();
16941702
}
16951703

1696-
inline object::const_iterator object::find(const std::string &s) const noexcept {
1704+
inline object::const_iterator object::find(std::string_view s) const noexcept {
16971705
auto it = index_map_.find(s);
16981706
if (it != index_map_.end()) {
16991707
return values_.begin() + it->second;
@@ -1707,7 +1715,7 @@ inline object::const_iterator object::find(const std::string &s) const noexcept
17071715
* @param key
17081716
* @return
17091717
*/
1710-
inline value object::at(const std::string &key) const {
1718+
inline value object::at(std::string_view key) const {
17111719

17121720
auto it = index_map_.find(key);
17131721
if (it != index_map_.end()) {
@@ -1722,7 +1730,7 @@ inline value object::at(const std::string &key) const {
17221730
* @param key
17231731
* @return
17241732
*/
1725-
inline value &object::at(const std::string &key) {
1733+
inline value &object::at(std::string_view key) {
17261734

17271735
auto it = index_map_.find(key);
17281736
if (it != index_map_.end()) {
@@ -1858,7 +1866,7 @@ inline value &value::at(std::size_t n) {
18581866
* @param key
18591867
* @return
18601868
*/
1861-
inline value value::at(const std::string &key) const {
1869+
inline value value::at(std::string_view key) const {
18621870
return as_object().at(key);
18631871
}
18641872

@@ -1867,7 +1875,7 @@ inline value value::at(const std::string &key) const {
18671875
* @param key
18681876
* @return
18691877
*/
1870-
inline value &value::at(const std::string &key) {
1878+
inline value &value::at(std::string_view key) {
18711879
return as_object().at(key);
18721880
}
18731881

@@ -1876,7 +1884,7 @@ inline value &value::at(const std::string &key) {
18761884
* @param key
18771885
* @return
18781886
*/
1879-
inline value value::operator[](const std::string &key) const {
1887+
inline value value::operator[](std::string_view key) const {
18801888
return as_object()[key];
18811889
}
18821890

@@ -1894,7 +1902,7 @@ inline value value::operator[](std::size_t n) const {
18941902
* @param key
18951903
* @return
18961904
*/
1897-
inline value &value::operator[](const std::string &key) {
1905+
inline value &value::operator[](std::string_view key) {
18981906
return as_object()[key];
18991907
}
19001908

0 commit comments

Comments
 (0)