Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose getLocationLineAndColumn() as a public API #1412

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions include/json/reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,14 @@ bool JSON_API parseFromStream(CharReader::Factory const&, IStream&, Value* root,
*/
JSON_API IStream& operator>>(IStream&, Value&);

/** Line and column within a document (1-based). */
struct DocumentLocation {
int line;
int column;
};

/** Return the location of a character within a string. */
DocumentLocation JSON_API locateInDocument(char const* beginDoc, size_t offset);
} // namespace Json

#pragma pack(pop)
Expand Down
47 changes: 27 additions & 20 deletions src/lib_json/json_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ bool Reader::decodeDouble(Token& token, Value& decoded) {
value = -std::numeric_limits<double>::infinity();
else if (!std::isinf(value))
return addError(
"'" + String(token.start_, token.end_) + "' is not a number.", token);
"'" + String(token.start_, token.end_) + "' is not a number.", token);
}
decoded = value;
return true;
Expand Down Expand Up @@ -767,24 +767,9 @@ Reader::Char Reader::getNextChar() {

void Reader::getLocationLineAndColumn(Location location, int& line,
int& column) const {
Location current = begin_;
Location lastLineStart = current;
line = 0;
while (current < location && current != end_) {
Char c = *current++;
if (c == '\r') {
if (*current == '\n')
++current;
lastLineStart = current;
++line;
} else if (c == '\n') {
lastLineStart = current;
++line;
}
}
// column & line start at 1
column = int(location - lastLineStart) + 1;
++line;
auto loc = locateInDocument(document_.data(), location - document_.data());
line = loc.line;
column = loc.column;
}

String Reader::getLocationLineAndColumn(Location location) const {
Expand Down Expand Up @@ -1660,7 +1645,7 @@ bool OurReader::decodeDouble(Token& token, Value& decoded) {
value = -std::numeric_limits<double>::infinity();
else if (!std::isinf(value))
return addError(
"'" + String(token.start_, token.end_) + "' is not a number.", token);
"'" + String(token.start_, token.end_) + "' is not a number.", token);
}
decoded = value;
return true;
Expand Down Expand Up @@ -1991,6 +1976,28 @@ bool parseFromStream(CharReader::Factory const& fact, IStream& sin, Value* root,
return reader->parse(begin, end, root, errs);
}

DocumentLocation locateInDocument(char const* beginDoc, size_t offset) {
int line = 1;
int col = 1;
const char* last = beginDoc + offset;
for (; beginDoc != last; ++beginDoc) {
switch (*beginDoc) {
case '\r':
if (beginDoc + 1 != last && beginDoc[1] == '\n')
continue; // consume CRLF as a single token.
[[fallthrough]]; // CR without a following LF is same as LF
case '\n':
col = 1;
++line;
break;
default:
++col;
break;
}
}
return {line, col};
}

IStream& operator>>(IStream& sin, Value& root) {
CharReaderBuilder b;
String errs;
Expand Down
30 changes: 30 additions & 0 deletions src/test_lib_json/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3903,6 +3903,36 @@ JSONTEST_FIXTURE_LOCAL(FuzzTest, fuzzDoesntCrash) {
example.size()));
}

struct LocateInDocumentTest : JsonTest::TestCase {
void testLocateInDocument(const char* doc, size_t location, int row,
int column) {
Json::DocumentLocation actualLocation =
Json::locateInDocument(doc, location);
JSONTEST_ASSERT_EQUAL(row, actualLocation.line);
JSONTEST_ASSERT_EQUAL(column, actualLocation.column);
}
};

JSONTEST_FIXTURE_LOCAL(LocateInDocumentTest, locateInDocTest) {
const std::string example1 = "line 1\nline 2\r\nline 3\rline 4";
const char* example2 = "\nline 2\r\r\n\nline 5\0 \n\rline 7\r\n";
struct TestSpec {
const char* doc;
size_t offset;
int line;
int column;
};
const TestSpec specs[] = {
{example1.data(), example1.find("line 1"), 1, 1},
{example1.data(), example1.find("e 4"), 4, 4},
// string terminates at \0, can't use find()
{example2, 21, 7, 1},
};
for (const auto& spec : specs) {
testLocateInDocument(spec.doc, spec.offset, spec.line, spec.column);
}
}

int main(int argc, const char* argv[]) {
JsonTest::Runner runner;

Expand Down