Skip to content

Commit 34ccc8c

Browse files
committed
ini: don't use std::distance on input iterator
1 parent f377ad9 commit 34ccc8c

File tree

4 files changed

+7
-10
lines changed

4 files changed

+7
-10
lines changed

include/chen/data/ini.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace chen
3131
class ini
3232
{
3333
public:
34-
typedef chen::input_iterator<const char, const char> iterator;
34+
typedef chen::position_iterator<const char, const char> iterator; // it's a input iterator
3535

3636
typedef std::unordered_map<std::string, std::string> property_type; // k/v pair
3737
typedef std::pair<std::string, property_type> section_type; // each section

src/data/ini.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ void chen::ini::exception(const iterator &beg, iterator &cur, iterator &end)
187187
}
188188
else
189189
{
190-
auto pos = chen::num::str(std::distance(beg, cur));
190+
auto pos = chen::num::str(cur.distance());
191191
auto tok = std::isprint(*cur) ? std::string(1, *cur) : chen::str::format("\\x%02x", static_cast<int>(*cur));
192192

193193
throw chen::ini::error(chen::str::format("ini: unexpected token '%s' at position %s", tok.c_str(), pos.c_str()));
@@ -202,10 +202,7 @@ bool chen::ini::advance(const iterator &beg, iterator &cur, iterator &end)
202202
++cur;
203203

204204
// check if end
205-
if (cur == end)
206-
return false;
207-
208-
return true;
205+
return cur != end;
209206
}
210207

211208
// decode
@@ -314,7 +311,7 @@ void chen::ini::decode(chen::ini::property_type &out, const iterator &beg, itera
314311

315312
if (out.find(key) != out.end())
316313
{
317-
auto pos = chen::num::str(std::distance(beg, cur) - key.size());
314+
auto pos = chen::num::str(cur.distance() - key.size());
318315
throw chen::ini::error(chen::str::format("ini: duplicate key '%s' found at position %s", key.c_str(), pos.c_str()));
319316
}
320317

@@ -449,7 +446,7 @@ void chen::ini::decode(std::string &out, const iterator &beg, iterator &cur, ite
449446
catch (...)
450447
{
451448
// e.g: \xD83D\xDE00, it's a emoji character
452-
auto pos = chen::num::str(std::distance(beg, cur) - 4);
449+
auto pos = chen::num::str(cur.distance() - 4);
453450
throw chen::ini::error(chen::str::format("ini: invalid unicode char '\\u%s' at position %s", unicode, pos.c_str()));
454451
}
455452
}

test/src/data/ini.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ TEST(DataIniTest, General)
1818
// fail
1919
for (int i = 1; i <= 5; ++i)
2020
{
21-
EXPECT_THROW(chen::ini::parse(chen::fs::read(conf::data + chen::str::format("/ini/fail%d.ini", i))), chen::ini::error);
21+
EXPECT_THROW(chen::ini::parse(conf::data + chen::str::format("/ini/fail%d.ini", i), true), chen::ini::error);
2222
}
2323

2424
// pass

test/src/data/json.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ TEST(DataJsonTest, Validate)
125125
if (i == 18) // I don't think too deep is an error
126126
continue;
127127

128-
EXPECT_THROW(chen::json::validate(chen::fs::read(conf::data + chen::str::format("/json/fail%d.json", i))), chen::json::error);
128+
EXPECT_THROW(chen::json::validate(conf::data + chen::str::format("/json/fail%d.json", i), true), chen::json::error);
129129
}
130130

131131
// pass

0 commit comments

Comments
 (0)