Extend BLI_str_partition_ex: add possibility to define a right limit to the string.

Now you can define `end` pointer as right limit of the string (allows to easily search
in substring, especially useful when searching from right).
This commit is contained in:
Bastien Montagne 2015-06-27 10:22:29 +02:00
parent e78b03f9e9
commit 4d043c99dc
5 changed files with 46 additions and 17 deletions

@ -92,7 +92,9 @@ bool BLI_strn_endswith(const char *__restrict str, const char *__restrict end, s
size_t BLI_str_partition(const char *str, const char delim[], char **sep, char **suf) ATTR_NONNULL();
size_t BLI_str_rpartition(const char *str, const char delim[], char **sep, char **suf) ATTR_NONNULL();
size_t BLI_str_partition_ex(const char *str, const char delim[], char **sep, char **suf, const bool from_right) ATTR_NONNULL();
size_t BLI_str_partition_ex(
const char *str, const char *end, const char delim[], char **sep, char **suf, const bool from_right)
ATTR_NONNULL(1, 3, 4, 5);
#ifdef __cplusplus
}

@ -69,7 +69,9 @@ int BLI_str_utf8_char_width_safe(const char *p) ATTR_NONNULL();
size_t BLI_str_partition_utf8(const char *str, const unsigned int delim[], char **sep, char **suf) ATTR_NONNULL();
size_t BLI_str_rpartition_utf8(const char *str, const unsigned int delim[], char **sep, char **suf) ATTR_NONNULL();
size_t BLI_str_partition_ex_utf8(const char *str, const unsigned int delim[], char **sep, char **suf, const bool from_right) ATTR_NONNULL();
size_t BLI_str_partition_ex_utf8(
const char *str, const char *end, const unsigned int delim[], char **sep, char **suf, const bool from_right)
ATTR_NONNULL(1, 3, 4, 5);
#define BLI_UTF8_MAX 6 /* mem */
#define BLI_UTF8_WIDTH_MAX 2 /* columns */

@ -856,7 +856,7 @@ bool BLI_str_endswith(const char *__restrict str, const char *end)
*/
size_t BLI_str_partition(const char *str, const char delim[], char **sep, char **suf)
{
return BLI_str_partition_ex(str, delim, sep, suf, false);
return BLI_str_partition_ex(str, NULL, delim, sep, suf, false);
}
/**
@ -870,31 +870,53 @@ size_t BLI_str_partition(const char *str, const char delim[], char **sep, char *
*/
size_t BLI_str_rpartition(const char *str, const char delim[], char **sep, char **suf)
{
return BLI_str_partition_ex(str, delim, sep, suf, true);
return BLI_str_partition_ex(str, NULL, delim, sep, suf, true);
}
/**
* Find the first char matching one of the chars in \a delim, either from left or right.
*
* \param str The string to search within.
* \param end If non-NULL, the right delimiter of the string.
* \param delim The set of delimiters to search for, as unicode values.
* \param sep Return value, set to the first delimiter found (or NULL if none found).
* \param suf Return value, set to next char after the first delimiter found (or NULL if none found).
* \param from_right If %true, search from the right of \a str, else, search from its left.
* \return The length of the prefix (i.e. *sep - str).
*/
size_t BLI_str_partition_ex(const char *str, const char delim[], char **sep, char **suf, const bool from_right)
size_t BLI_str_partition_ex(
const char *str, const char *end, const char delim[], char **sep, char **suf, const bool from_right)
{
const char *d;
char *(*func)(const char *str, int c) = from_right ? strrchr : strchr;
BLI_assert(end == NULL || end > str);
*sep = *suf = NULL;
for (d = delim; *d != '\0'; ++d) {
char *tmp = func(str, *d);
const char *tmp;
if (end) {
if (from_right) {
for (tmp = end - 1; (tmp >= str) && (*tmp != *d); tmp--);
if (tmp < str) {
tmp = NULL;
}
}
else {
tmp = func(str, *d);
if (tmp >= end) {
tmp = NULL;
}
}
}
else {
tmp = func(str, *d);
}
if (tmp && (from_right ? (*sep < tmp) : (!*sep || *sep > tmp))) {
*sep = tmp;
*sep = (char *)tmp;
}
}
@ -903,7 +925,7 @@ size_t BLI_str_partition_ex(const char *str, const char delim[], char **sep, cha
return (size_t)(*sep - str);
}
return strlen(str);
return end ? (size_t)(end - str) : strlen(str);
}
/**

@ -736,26 +736,29 @@ char *BLI_str_prev_char_utf8(const char *p)
size_t BLI_str_partition_utf8(const char *str, const unsigned int delim[], char **sep, char **suf)
{
return BLI_str_partition_ex_utf8(str, delim, sep, suf, false);
return BLI_str_partition_ex_utf8(str, NULL, delim, sep, suf, false);
}
size_t BLI_str_rpartition_utf8(const char *str, const unsigned int delim[], char **sep, char **suf)
{
return BLI_str_partition_ex_utf8(str, delim, sep, suf, true);
return BLI_str_partition_ex_utf8(str, NULL, delim, sep, suf, true);
}
size_t BLI_str_partition_ex_utf8(const char *str, const unsigned int delim[], char **sep, char **suf,
const bool from_right)
size_t BLI_str_partition_ex_utf8(
const char *str, const char *end, const unsigned int delim[], char **sep, char **suf, const bool from_right)
{
const unsigned int *d;
const size_t str_len = strlen(str);
const size_t str_len = end ? (size_t)(end - str) : strlen(str);
size_t index;
/* Note that here, we assume end points to a valid utf8 char! */
BLI_assert(end == NULL || (end >= str && (BLI_str_utf8_as_unicode(end) != BLI_UTF8_ERR)));
*suf = (char *)(str + str_len);
for (*sep = (char *)(from_right ? BLI_str_find_prev_char_utf8(str, str + str_len) : str), index = 0;
*sep != NULL && **sep != '\0';
*sep = (char *)(from_right ? (char *)BLI_str_find_prev_char_utf8(str, *sep) : str + index))
*sep >= str && (!end || *sep < end) && **sep != '\0';
*sep = (char *)(from_right ? BLI_str_find_prev_char_utf8(str, *sep) : str + index))
{
const unsigned int c = BLI_str_utf8_as_unicode_and_size(*sep, &index);

@ -251,7 +251,7 @@ static bool node_select_grouped_name(SpaceNode *snode, bNode *node_act, const bo
size_t pref_len_act, pref_len_curr;
char *sep, *suf_act, *suf_curr;
pref_len_act = BLI_str_partition_ex_utf8(node_act->name, delims, &sep, &suf_act, from_right);
pref_len_act = BLI_str_partition_ex_utf8(node_act->name, NULL, delims, &sep, &suf_act, from_right);
/* Note: in case we are searching for suffix, and found none, use whole name as suffix. */
if (from_right && !(sep && suf_act)) {
@ -263,7 +263,7 @@ static bool node_select_grouped_name(SpaceNode *snode, bNode *node_act, const bo
if (node->flag & SELECT) {
continue;
}
pref_len_curr = BLI_str_partition_ex_utf8(node->name, delims, &sep, &suf_curr, from_right);
pref_len_curr = BLI_str_partition_ex_utf8(node->name, NULL, delims, &sep, &suf_curr, from_right);
/* Same as with active node name! */
if (from_right && !(sep && suf_curr)) {