util/StringUtil: reorder functions

This commit is contained in:
Max Kellermann 2015-03-04 20:22:05 +01:00
parent 39825c6622
commit 250332a8d5
2 changed files with 73 additions and 73 deletions

View File

@ -26,6 +26,51 @@
#include <assert.h>
#include <string.h>
bool
StringStartsWith(const char *haystack, const char *needle)
{
const size_t length = strlen(needle);
return memcmp(haystack, needle, length) == 0;
}
bool
StringEndsWith(const char *haystack, const char *needle)
{
const size_t haystack_length = strlen(haystack);
const size_t needle_length = strlen(needle);
return haystack_length >= needle_length &&
memcmp(haystack + haystack_length - needle_length,
needle, needle_length) == 0;
}
const char *
FindStringSuffix(const char *p, const char *suffix)
{
const size_t p_length = strlen(p);
const size_t suffix_length = strlen(suffix);
if (p_length < suffix_length)
return nullptr;
const char *q = p + p_length - suffix_length;
return memcmp(q, suffix, suffix_length) == 0
? q
: nullptr;
}
char *
CopyString(char *gcc_restrict dest, const char *gcc_restrict src, size_t size)
{
size_t length = strlen(src);
if (length >= size)
length = size - 1;
char *p = std::copy_n(src, length, dest);
*p = '\0';
return p;
}
const char *
StripLeft(const char *p)
{
@ -78,51 +123,6 @@ Strip(char *p)
return p;
}
bool
StringStartsWith(const char *haystack, const char *needle)
{
const size_t length = strlen(needle);
return memcmp(haystack, needle, length) == 0;
}
bool
StringEndsWith(const char *haystack, const char *needle)
{
const size_t haystack_length = strlen(haystack);
const size_t needle_length = strlen(needle);
return haystack_length >= needle_length &&
memcmp(haystack + haystack_length - needle_length,
needle, needle_length) == 0;
}
const char *
FindStringSuffix(const char *p, const char *suffix)
{
const size_t p_length = strlen(p);
const size_t suffix_length = strlen(suffix);
if (p_length < suffix_length)
return nullptr;
const char *q = p + p_length - suffix_length;
return memcmp(q, suffix, suffix_length) == 0
? q
: nullptr;
}
char *
CopyString(char *gcc_restrict dest, const char *gcc_restrict src, size_t size)
{
size_t length = strlen(src);
if (length >= size)
length = size - 1;
char *p = std::copy_n(src, length, dest);
*p = '\0';
return p;
}
bool
string_array_contains(const char *const* haystack, const char *needle)
{

View File

@ -24,6 +24,34 @@
#include <stddef.h>
gcc_pure
bool
StringStartsWith(const char *haystack, const char *needle);
gcc_pure
bool
StringEndsWith(const char *haystack, const char *needle);
/**
* Check if the given string ends with the specified suffix. If yes,
* returns the position of the suffix, and nullptr otherwise.
*/
gcc_pure
const char *
FindStringSuffix(const char *p, const char *suffix);
/**
* Copy a string. If the buffer is too small, then the string is
* truncated. This is a safer version of strncpy().
*
* @param size the size of the destination buffer (including the null
* terminator)
* @return a pointer to the null terminator
*/
gcc_nonnull_all
char *
CopyString(char *dest, const char *src, size_t size);
/**
* Returns a pointer to the first non-whitespace character in the
* string, or to the end of the string.
@ -82,34 +110,6 @@ StripRight(char *p);
char *
Strip(char *p);
gcc_pure
bool
StringStartsWith(const char *haystack, const char *needle);
gcc_pure
bool
StringEndsWith(const char *haystack, const char *needle);
/**
* Check if the given string ends with the specified suffix. If yes,
* returns the position of the suffix, and nullptr otherwise.
*/
gcc_pure
const char *
FindStringSuffix(const char *p, const char *suffix);
/**
* Copy a string. If the buffer is too small, then the string is
* truncated. This is a safer version of strncpy().
*
* @param size the size of the destination buffer (including the null
* terminator)
* @return a pointer to the null terminator
*/
gcc_nonnull_all
char *
CopyString(char *dest, const char *src, size_t size);
/**
* Checks whether a string array contains the specified string.
*