Changeset View
Changeset View
Standalone View
Standalone View
source/blender/blenlib/intern/string.c
| Show First 20 Lines • Show All 771 Lines • ▼ Show 20 Lines | if (end_p > p) { | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| return totstrip; | return totstrip; | ||||
| } | } | ||||
| /** | /** | ||||
| * Strip trailing whitespace from \a src | |||||
| * - " foo " -> " foo" | |||||
| * | |||||
| * \return The length of \a dst | |||||
| */ | |||||
| size_t BLI_str_rstrip_whitespace(char *__restrict dst, const char *__restrict src, const size_t maxlen) | |||||
| { | |||||
| char *end_p; | |||||
campbellbarton: For a zero length string, this isnt going to work righ, since it will become negative.
Could… | |||||
| size_t len; | |||||
| BLI_assert(src != dst); | |||||
| /* early return on empty string */ | |||||
| if (UNLIKELY(*src == '\0')) { | |||||
| *dst = '\0'; | |||||
| return 0; | |||||
| } | |||||
| end_p = (char *)src + (strlen(src) - 1); /* position at last character */ | |||||
| while (isspace(*end_p)) { | |||||
| end_p--; | |||||
| } | |||||
| end_p++; | |||||
| len = MIN2((size_t)(end_p - src), maxlen); | |||||
| memcpy(dst, src, len); | |||||
| dst[len] = '\0'; | |||||
| return len; | |||||
| } | |||||
| /** | |||||
| * Strip leading whitespace from \a src | |||||
| * - " foo " -> "foo " | |||||
| * | |||||
| * \return The length of \a dst | |||||
| */ | |||||
| size_t BLI_str_lstrip_whitespace(char *__restrict dst, const char *__restrict src, const size_t maxlen) | |||||
| { | |||||
| size_t len; | |||||
Done Inline ActionsBetter not add function calls in this macro, it may call multiple times. just do... len = strlen(src); CLAMP_MAX(len, maxlen); ... may as well do the same for other areas, even when not using function calls. campbellbarton: Better not add function calls in this macro, it may call multiple times.
just do...
len =… | |||||
| BLI_assert(src != dst); | |||||
| while (isspace(*src)) { | |||||
| src++; | |||||
| } | |||||
| len = strlen(src); | |||||
| CLAMP_MAX(len, maxlen); | |||||
| memcpy(dst, src, len); | |||||
| dst[len] = '\0'; | |||||
| return len; | |||||
| } | |||||
| /** | |||||
| * Return index of a string in a string array. | * Return index of a string in a string array. | ||||
| * | * | ||||
| * \param str The string to find. | * \param str The string to find. | ||||
| * \param str_array Array of strings. | * \param str_array Array of strings. | ||||
| * \param str_array_len The length of the array, or -1 for a NULL-terminated array. | * \param str_array_len The length of the array, or -1 for a NULL-terminated array. | ||||
| * \return The index of str in str_array or -1. | * \return The index of str in str_array or -1. | ||||
| */ | */ | ||||
| int BLI_str_index_in_array_n(const char *__restrict str, const char **__restrict str_array, const int str_array_len) | int BLI_str_index_in_array_n(const char *__restrict str, const char **__restrict str_array, const int str_array_len) | ||||
| ▲ Show 20 Lines • Show All 178 Lines • Show Last 20 Lines | |||||
For a zero length string, this isnt going to work righ, since it will become negative.
Could just do some check...
if (UNLIKELY(*src == '\0')) { *dst = '\0'; return 0; }... or edit the loop to support the case of an empty string.