Skip to content

Expose getLocationLineAndColumn() as a public API #1282

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

Conversation

KyleFromKitware
Copy link

@KyleFromKitware KyleFromKitware commented Apr 8, 2021

getLocationLineAndColumn() is an internal API used by the error string code, but clients which use Json::Value::getOffsetStart() and Json::Value::getOffsetLimit() may want to use getLocationLineAndColumn() to turn the offset into a line and column. Expose the function as an external API.

};

JSONTEST_FIXTURE_LOCAL(GetLocationLineAndColumnTest, test) {
const std::string example = "line 1\nline 2\r\nline 3\rline 4";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not enough testing. There's only one input here.
There should be an exhaustive loop checking the results of every position.
This input only has one kind of newline.

@@ -392,6 +392,12 @@ bool JSON_API parseFromStream(CharReader::Factory const&, IStream&, Value* root,
*/
JSON_API IStream& operator>>(IStream&, Value&);

/** Get the line and column of a character within a string.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels like a different general function that has nothing to do with JSON.

This isn't part of Reader's API, so I'm not sure it belongs in reader.h.

The out parameters should be a returned struct rather than int references, and the location should be a size_t, as it's encoding an array offset.

The documentation should be more complete about whether the line and column are 1 or 0 based, how it handles newlines, and how it handles unicode multibyte chars. I mean is 八 counted as one character or are all of its utf-8 bytes counted? If you're doing line and column, then you're implicitly talking about display position and it's a can of worms.

But really I have to question its role in the API. It doesn't really fit in.

@KyleFromKitware
Copy link
Author

I created this PR, along with #1281, because I would like to get more detailed line/column information from both the error (right now it only returns a string) and the values in case of success. This PR is not needed very badly, because clients that want to convert a ptrdiff_t to line/column can just copy this algorithm into their own code. I did the refactoring to expose this function as an API because it would seem silly to duplicate the code. We can close this if it's not a good fit.

#1281 is more important to me, because it exposes information that I can't get without parsing the returned error string.

@BillyDonahue
Copy link
Contributor

I created this PR, along with #1281, because I would like to get more detailed line/column information from both the error (right now it only returns a string) and the values in case of success. This PR is not needed very badly, because clients that want to convert a ptrdiff_t to line/column can just copy this algorithm into their own code. I did the refactoring to expose this function as an API because it would seem silly to duplicate the code. We can close this if it's not a good fit.

#1281 is more important to me, because it exposes information that I can't get without parsing the returned error string.

The description field of this PR and #1281 are both empty. Can you write descriptions for them so we can understand the motivation?

@KyleFromKitware
Copy link
Author

The description field of this PR and #1281 are both empty. Can you write descriptions for them so we can understand the motivation?

OK, done.

@cdunn2001
Copy link
Contributor

Let's re-open this after #1281 is done. I don't have any time to look at this myself, and #1281 is independent, right?

@KyleFromKitware
Copy link
Author

Yes, #1281 is independent of this.

@baylesj
Copy link
Contributor

baylesj commented Sep 12, 2024

Closing this due to inactivity. Feel free to reopen if you are able to address feedback.

@baylesj baylesj closed this Sep 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants