-
Notifications
You must be signed in to change notification settings - Fork 273
Enhance split_string to support splitting on white space [TG-2922] #2071
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
Changes from 5 commits
e87edbf
252c24c
07009d4
145f474
a385d9b
fc8ba88
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,7 +75,6 @@ set_target_properties( | |
mmcc | ||
pointer-analysis | ||
solvers | ||
string_utils | ||
test-bigint | ||
testing-utils | ||
unit | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,11 +7,16 @@ Author: Daniel Poetzl | |
\*******************************************************************/ | ||
|
||
#include "string_utils.h" | ||
#include "invariant.h" | ||
|
||
#include <cassert> | ||
#include <cctype> | ||
#include <algorithm> | ||
|
||
/// Remove all whitespace characters from either end of a string. Whitespace | ||
/// in the middle of the string is left unchanged | ||
/// \param s: the string to strip | ||
/// \return The stripped string | ||
std::string strip_string(const std::string &s) | ||
{ | ||
auto pred=[](char c){ return std::isspace(c); }; | ||
|
@@ -30,15 +35,29 @@ std::string strip_string(const std::string &s) | |
return s.substr(i, (j-i+1)); | ||
} | ||
|
||
/// Given a string s, split into a sequence of substrings when separated by | ||
/// specified delimiter. | ||
/// \param s: The string to split up | ||
/// \param delim: The character to use as the delimiter | ||
/// \param [out] result: The sub strings. Must be empty. | ||
/// \param strip: If true, strip_string will be used on each element, removing | ||
/// whitespace from the beginning and end of each element | ||
/// \param remove_empty: If true, all empty-string elements will be removed. | ||
/// This is applied after strip so whitespace only elements will be removed if | ||
/// both are set to true | ||
void split_string( | ||
const std::string &s, | ||
char delim, | ||
std::vector<std::string> &result, | ||
bool strip, | ||
bool remove_empty) | ||
{ | ||
assert(result.empty()); | ||
assert(!std::isspace(delim)); | ||
PRECONDITION(result.empty()); | ||
if(strip && std::isspace(delim)) | ||
{ | ||
throw std::invalid_argument( | ||
"delim can't be a space character if using strip"); | ||
} | ||
|
||
if(s.empty()) | ||
{ | ||
|
@@ -47,7 +66,7 @@ void split_string( | |
} | ||
|
||
std::string::size_type n=s.length(); | ||
assert(n>0); | ||
INVARIANT(n > 0, "Empty string case should already be handled"); | ||
|
||
std::string::size_type start=0; | ||
std::string::size_type i; | ||
|
@@ -87,7 +106,11 @@ void split_string( | |
std::string &right, | ||
bool strip) | ||
{ | ||
assert(!std::isspace(delim)); | ||
if(strip && std::isspace(delim)) | ||
{ | ||
throw std::invalid_argument( | ||
"delim can't be a space character if using strip"); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason that this condition/throw block can't just be an INVARIANT ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the unit test There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm - ok, though that smells a little bit of the mechanics of unit testing driving the architecture of the code, but I don't have a hugely strong opinion. I just felt that an INVARIANT, or perhaps actually PRECONDITION now I think more, was more inline with other functions in this file that do things like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am with @chrisr-diffblue: I think this should be a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't agree - I think of all the code we write, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think @thk123 does make a good point about the 'library like' use-case - perhaps the answer would be to have 'CATCHABLE_PRECONDITION' along with @tautschnig suggestion to enable error path testing? - i.e. in the library use-case Thomas is talking about, it could be caught by the caller to avoid duplicating the error handling. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are two different "users" to be distinguished: the developer using the library and the end-user feeding input to what the developer has built. The end-user should not be subjected to internal limitations, but for a developer it's reasonable to assume that they have read the documentation. It is of course perfectly reasonable to use exceptions for either class of "users," but that's not what the code base does right now. If you think this should change, I'd go via a new issue and/or have some further discussion. Because your proposed code is of course very nice and clean I'd keep it so that a future patch to introduce the exceptions is very low effort. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In all this I do think that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry I'm not clear on what you propose I do with this code for this PR? Do you mean keep the code in this PR, or change this PR to a precondition (and remove the associated unit tests) and stash this version away for a later day? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is what I'd do if it were my PR: if things can be made to work via |
||
|
||
std::vector<std::string> result; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
# Unit test binaries | ||
miniBDD | ||
sharing_node | ||
string_utils | ||
unit_tests |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cheeky of me, but as you are touching this function, do you fancy adding some Doxygen? ;-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will pick this up as part of the follow on tidy up PR as this is blocking another PR #2046