-
Notifications
You must be signed in to change notification settings - Fork 273
Result of split_string() can be empty #368
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ Author: Daniel Poetzl | |
|
||
#include <cassert> | ||
#include <cctype> | ||
#include <algorithm> | ||
|
||
#include "string_utils.h" | ||
|
||
|
@@ -25,22 +26,18 @@ Author: Daniel Poetzl | |
|
||
std::string strip_string(const std::string &s) | ||
{ | ||
std::string::size_type n=s.length(); | ||
auto pred=[](char c){ return std::isspace(c); }; | ||
|
||
// find first non-space char | ||
unsigned i; | ||
for(i=0; i<n; i++) | ||
{ | ||
if(!std::isspace(s[i])) | ||
break; | ||
} | ||
if(i==n) | ||
std::string::const_iterator left | ||
=std::find_if_not(s.begin(), s.end(), pred); | ||
if(left==s.end()) | ||
return ""; | ||
|
||
std::string::const_reverse_iterator r_it; | ||
for(r_it=s.rbegin(); std::isspace(*r_it); r_it++); | ||
std::string::size_type i=std::distance(s.begin(), left); | ||
|
||
unsigned j=std::distance(r_it, s.rend())-1; | ||
std::string::const_reverse_iterator right | ||
=std::find_if_not(s.rbegin(), s.rend(), pred); | ||
std::string::size_type j=std::distance(right, s.rend())-1; | ||
|
||
return s.substr(i, (j-i+1)); | ||
} | ||
|
@@ -76,12 +73,12 @@ void split_string( | |
std::string::size_type n=s.length(); | ||
assert(n>0); | ||
|
||
unsigned start=0; | ||
unsigned i; | ||
std::string::size_type start=0; | ||
std::string::size_type i; | ||
|
||
for (i=0; i<n; i++) | ||
for(i=0; i<n; i++) | ||
{ | ||
if (s[i]==delim) | ||
if(s[i]==delim) | ||
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. While you are at it: get rid of |
||
{ | ||
std::string new_s=s.substr(start, i-start); | ||
|
||
|
@@ -90,6 +87,7 @@ void split_string( | |
|
||
if(!remove_empty || !new_s.empty()) | ||
result.push_back(new_s); | ||
|
||
start=i+1; | ||
} | ||
} | ||
|
@@ -102,7 +100,8 @@ void split_string( | |
if(!remove_empty || !new_s.empty()) | ||
result.push_back(new_s); | ||
|
||
assert(!result.empty()); | ||
if(result.empty()) | ||
result.push_back(""); | ||
} | ||
|
||
/*******************************************************************\ | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Nit picking (applies here and for the other case of std::find_if_not): place the "=" at the end of the previous line, or maybe even use
auto left=...
and put it all on a single line.