util/StringUtil: move code to StripRight()

This commit is contained in:
Max Kellermann 2014-08-07 14:44:31 +02:00
parent 87bcf739ee
commit f860a2fbd6
2 changed files with 32 additions and 7 deletions

View File

@ -35,17 +35,28 @@ StripLeft(const char *p)
return p;
}
size_t
StripRight(const char *p, size_t length)
{
while (length > 0 && IsWhitespaceNotNull(p[length - 1]))
--length;
return length;
}
void
StripRight(char *p)
{
size_t old_length = strlen(p);
size_t new_length = StripRight(p, old_length);
p[new_length] = 0;
}
char *
Strip(char *p)
{
p = StripLeft(p);
size_t length = strlen(p);
while (length > 0 && IsWhitespaceNotNull(p[length - 1]))
--length;
p[length] = 0;
StripRight(p);
return p;
}

View File

@ -39,6 +39,20 @@ StripLeft(char *p)
return const_cast<char *>(StripLeft((const char *)p));
}
/**
* Determine the string's length as if it was stripped on the right
* side.
*/
gcc_pure
size_t
StripRight(const char *p, size_t length);
/**
* Strip trailing whitespace by null-terminating the string.
*/
void
StripRight(char *p);
/**
* Skip whitespace at the beginning and terminate the string after the
* last non-whitespace character.