diff options
author | Campbell Barton <ideasman42@gmail.com> | 2013-03-10 09:11:18 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2013-03-10 09:11:18 +0400 |
commit | 2022567116319a1756098715adb475eb49d8c6a9 (patch) | |
tree | 434f0f1c5fef5bbeac56a9808a1b1d0eaea16e0e /source/blender | |
parent | 14bbde04412c94c71104659b22652a59c39cdacb (diff) |
patch [#34103] path_util_split_name_num.patch
from Lawrence D'Oliveiro (ldo)
Simplify implementation of BLI_split_name_num
- With some changes of my own to avoid second call to strlen()
Diffstat (limited to 'source/blender')
-rw-r--r-- | source/blender/blenlib/intern/path_util.c | 47 |
1 files changed, 23 insertions, 24 deletions
diff --git a/source/blender/blenlib/intern/path_util.c b/source/blender/blenlib/intern/path_util.c index b7196fc6db5..a6f50c5082a 100644 --- a/source/blender/blenlib/intern/path_util.c +++ b/source/blender/blenlib/intern/path_util.c @@ -162,42 +162,41 @@ void BLI_stringenc(char *string, const char *head, const char *tail, unsigned sh * Returns the length of *left. * * Foo.001 -> "Foo", 1 - * Returns the length of "Foo" + * Returning the length of "Foo" * * \param left Where to return copy of part preceding delim * \param nr Where to return value of numeric suffix * \param name String to split * \param delim Delimiter character + * \return Length of \a left */ int BLI_split_name_num(char *left, int *nr, const char *name, const char delim) { - int a; + const int name_len = strlen(name); *nr = 0; - a = strlen(name); - memcpy(left, name, (a + 1) * sizeof(char)); - - if (a > 1 && name[a - 1] == delim) return a; - - while (a--) { - if (name[a] == delim) { - left[a] = 0; - *nr = atol(name + a + 1); - /* casting down to an int, can overflow for large numbers */ - if (*nr < 0) - *nr = 0; - return a; - } - /* non-numeric suffix--give up */ - if (isdigit(name[a]) == 0) break; - - left[a] = 0; + memcpy(left, name, (name_len + 1) * sizeof(char)); + + /* name doesn't end with a delimiter "foo." */ + if ((name_len > 1 && name[name_len - 1] == delim) == 0) { + int a = name_len; + while (a--) { + if (name[a] == delim) { + left[a] = '\0'; /* truncate left part here */ + *nr = atol(name + a + 1); + /* casting down to an int, can overflow for large numbers */ + if (*nr < 0) + *nr = 0; + return a; + } + else if (isdigit(name[a]) == 0) { + /* non-numeric suffix - give up */ + break; + } + } } - for (a = 0; name[a]; a++) - left[a] = name[a]; - - return a; + return name_len; } /** |