provide Glib::ustring() variant of strip_whitespace_edges

git-svn-id: svn://localhost/ardour2/branches/2.0-ongoing@6541 d708f5d6-7413-0410-9779-e7cbd77b26cf
This commit is contained in:
Paul Davis 2010-01-22 16:57:30 +00:00
parent db70bc225d
commit 1c26bd74d7
2 changed files with 49 additions and 34 deletions

View file

@ -22,11 +22,16 @@
#include <string>
namespace Glib {
class ustring;
}
namespace PBD {
// returns the empty string if the entire string is whitespace
// so check length after calling.
extern void strip_whitespace_edges (std::string& str);
extern void strip_whitespace_edges (Glib::ustring& str);
} // namespace PBD

View file

@ -18,6 +18,7 @@
*/
#include <pbd/whitespace.h>
#include <glibmm/ustring.h>
using namespace std;
@ -26,55 +27,64 @@ namespace PBD {
void
strip_whitespace_edges (string& str)
{
string::size_type i;
string::size_type len;
string::size_type s = 0;
string::size_type i;
string::size_type len;
string::size_type s = 0;
len = str.length();
len = str.length();
if (len == 1) {
return;
}
if (len == 1) {
return;
}
/* strip front */
/* strip front */
for (i = 0; i < len; ++i) {
if (isgraph (str[i])) {
break;
}
}
for (i = 0; i < len; ++i) {
if (isgraph (str[i])) {
break;
}
}
if (i == len) {
/* it's all whitespace, not much we can do */
if (i == len) {
/* it's all whitespace, not much we can do */
str = "";
return;
}
return;
}
/* strip back */
/* strip back */
if (len > 1) {
if (len > 1) {
s = i;
i = len - 1;
s = i;
i = len - 1;
if (s == i) {
return;
}
if (s == i) {
return;
}
do {
if (isgraph (str[i]) || i == 0) {
break;
}
do {
if (isgraph (str[i]) || i == 0) {
break;
}
--i;
--i;
} while (true);
} while (true);
str = str.substr (s, (i - s) + 1);
str = str.substr (s, (i - s) + 1);
} else {
str = str.substr (s);
}
} else {
str = str.substr (s);
}
}
void
strip_whitespace_edges (Glib::ustring& str)
{
string copy (str.raw());
strip_whitespace_edges (copy);
str = copy;
}
} // namespace PBD