Skip to content

Commit 400b744

Browse files
committed
Merge pull request #172 from cdunn2001/master
Fix bug in ValueIteratorBase::operator- Fixes #169.
2 parents 9c91b99 + bd55164 commit 400b744

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

Diff for: include/json/value.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,7 @@ class JSON_API ValueIteratorBase {
946946
bool operator!=(const SelfType& other) const { return !isEqual(other); }
947947

948948
difference_type operator-(const SelfType& other) const {
949-
return computeDistance(other);
949+
return other.computeDistance(*this);
950950
}
951951

952952
/// Return either the index or the member name of the referenced value as a

Diff for: src/lib_json/json_valueiterator.inl

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ ValueIteratorBase::difference_type
7777
ValueIteratorBase::computeDistance(const SelfType& other) const {
7878
#ifndef JSON_VALUE_USE_INTERNAL_MAP
7979
#ifdef JSON_USE_CPPTL_SMALLMAP
80-
return current_ - other.current_;
80+
return other.current_ - current_;
8181
#else
8282
// Iterator for null value are initialized using the default
8383
// constructor, which initialize current_ to the default

Diff for: src/test_lib_json/main.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -1861,6 +1861,23 @@ JSONTEST_FIXTURE(CharReaderFailIfExtraTest, commentAfterBool) {
18611861
JSONTEST_ASSERT_EQUAL(true, root.asBool());
18621862
delete reader;
18631863
}
1864+
1865+
struct IteratorTest : JsonTest::TestCase {};
1866+
1867+
JSONTEST_FIXTURE(IteratorTest, distance) {
1868+
Json::Value json;
1869+
json["k1"] = "a";
1870+
json["k2"] = "b";
1871+
int dist;
1872+
std::string str;
1873+
for (Json::ValueIterator it = json.begin(); it != json.end(); ++it) {
1874+
dist = it - json.begin();
1875+
str = it->asString().c_str();
1876+
}
1877+
JSONTEST_ASSERT_EQUAL(1, dist);
1878+
JSONTEST_ASSERT_STRING_EQUAL("b", str);
1879+
}
1880+
18641881
int main(int argc, const char* argv[]) {
18651882
JsonTest::Runner runner;
18661883
JSONTEST_REGISTER_FIXTURE(runner, ValueTest, checkNormalizeFloatingPointStr);
@@ -1905,6 +1922,8 @@ int main(int argc, const char* argv[]) {
19051922
JSONTEST_REGISTER_FIXTURE(runner, CharReaderFailIfExtraTest, commentAfterArray);
19061923
JSONTEST_REGISTER_FIXTURE(runner, CharReaderFailIfExtraTest, commentAfterBool);
19071924

1925+
JSONTEST_REGISTER_FIXTURE(runner, IteratorTest, distance);
1926+
19081927
JSONTEST_REGISTER_FIXTURE(runner, WriterTest, dropNullPlaceholders);
19091928
JSONTEST_REGISTER_FIXTURE(runner, StreamWriterTest, dropNullPlaceholders);
19101929

0 commit comments

Comments
 (0)