MCPcopy Create free account
hub / github.com/avast/retdec / getLineAndColumnFromPosition

Function getLineAndColumnFromPosition

src/utils/string.cpp:871–894  ·  view source on GitHub ↗

* @brief Transform @a position in @c json into line and column location. * * @param json JSON string. * @param position Byte distance from start of JSON string. * * @return Pair (line, column) . */

Source from the content-addressed store, hash-verified

869* @return Pair <tt>(line, column)</tt>.
870*/
871std::pair<std::size_t, std::size_t> getLineAndColumnFromPosition(const std::string &json,
872 std::size_t position) {
873 std::size_t line = 0;
874 std::size_t column = 0;
875
876 if (position >= json.size())
877 return {line, column};
878
879 for (std::size_t p = 0; p < position; ++p) {
880 if (json[p] == '\r' && (p + 1) < json.size() && json[p + 1] == '\n') {
881 ++p;
882 ++line;
883 column = 0;
884 } else if (json[p] == '\n') {
885 ++line;
886 column = 0;
887 } else {
888 ++column;
889 }
890 }
891
892 // column & line start at 1
893 return {line + 1, column + 1};
894}
895
896/**
897* @brief Checks if the string is a number.

Callers 2

readJsonStringMethod · 0.85
TEST_FFunction · 0.85

Calls 1

sizeMethod · 0.45

Tested by 1

TEST_FFunction · 0.68