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

add slice and shorten_json_string #11598

Merged
merged 5 commits into from
Mar 22, 2023
Merged

add slice and shorten_json_string #11598

merged 5 commits into from
Mar 22, 2023

Conversation

maloel
Copy link
Collaborator

@maloel maloel commented Mar 22, 2023

This is a utility function I used in one PoC that thought would be useful to have in rsutils, especially now that we're making a bigger use of json.

It also adds what I consider a very useful utility slice, similar to a string_view. For example, I wanted to refer to the json string inside flexible_msg but it's not null-teminated. The easy way is by using a slice:
LOG_DEBUG( "publishing metadata: " << shorten_json_string( slice( md.custom_data< char const >(), md._data.size() )));

Anyway I didn't want to lose this so decided to PR.

@maloel maloel requested a review from OhadMeir March 22, 2023 06:55
// find an opening
slice::const_iterator it = outside.begin() + 1; // opening {, separating comma, etc.
bool in_quote = false;
while( it < outside.end() && ( in_quote || *it != '[' && *it != '{' ) )
Copy link
Contributor

Choose a reason for hiding this comment

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

It is better to move *it != '[' && *it != '{' to a separate function is_opening, the meaning will be clearer.
For the very least surround with parenthesis, don't count on operator && precedence over ||.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't like parens that aren't needed generally... I'll try to add is_opening

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed

const_iterator _begin, _end;

public:
slice( const_iterator begin, const_iterator end )
Copy link
Contributor

Choose a reason for hiding this comment

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

It is better to leave a blank line between functions.
Fix if you'll have some other changes to this file.

return ellipsis( slice(), str ); // impossible: "{ ... }" is the minimum

// Try to find an inside block that can be taken out
ellipsis final;
Copy link
Contributor

Choose a reason for hiding this comment

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

final can serve as a C++ keyword in other context, better to use other name. result?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed

Copy link
Contributor

@OhadMeir OhadMeir left a comment

Choose a reason for hiding this comment

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

LGTM

@maloel maloel merged commit b305fb0 into IntelRealSense:dds Mar 22, 2023
@maloel maloel deleted the utils branch March 22, 2023 10:31
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.

2 participants