diff options
author | Max Kellermann <max@duempel.org> | 2015-03-04 20:22:05 +0100 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2015-03-04 20:22:05 +0100 |
commit | 250332a8d51c6fc79f156da748b02761143fabe6 (patch) | |
tree | 2c34624d0e12f5e1e1a5694af511c9558d1d1d68 | |
parent | 39825c662239c3a9b5e6e4d534557dc40c076a79 (diff) | |
download | mpd-250332a8d51c6fc79f156da748b02761143fabe6.tar.gz mpd-250332a8d51c6fc79f156da748b02761143fabe6.tar.xz mpd-250332a8d51c6fc79f156da748b02761143fabe6.zip |
util/StringUtil: reorder functions
-rw-r--r-- | src/util/StringUtil.cxx | 90 | ||||
-rw-r--r-- | src/util/StringUtil.hxx | 56 |
2 files changed, 73 insertions, 73 deletions
diff --git a/src/util/StringUtil.cxx b/src/util/StringUtil.cxx index 107354b6e..bb454e904 100644 --- a/src/util/StringUtil.cxx +++ b/src/util/StringUtil.cxx @@ -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) { @@ -79,51 +124,6 @@ Strip(char *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) { assert(haystack != nullptr); diff --git a/src/util/StringUtil.hxx b/src/util/StringUtil.hxx index 4f44ab94a..a88645250 100644 --- a/src/util/StringUtil.hxx +++ b/src/util/StringUtil.hxx @@ -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. * |