Skip to content

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 1 commit into from
Jan 9, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions src/util/string_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Author: Daniel Poetzl

#include <cassert>
#include <cctype>
#include <algorithm>

#include "string_utils.h"

Expand All @@ -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);
Copy link
Collaborator

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.

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));
}
Expand Down Expand Up @@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

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

While you are at it: get rid of unsigned and use std::string::size_type in all those places.

{
std::string new_s=s.substr(start, i-start);

Expand All @@ -90,6 +87,7 @@ void split_string(

if(!remove_empty || !new_s.empty())
result.push_back(new_s);

start=i+1;
}
}
Expand All @@ -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("");
}

/*******************************************************************\
Expand Down