[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [55148] trunk/blender/source/blender/ blenlib/intern/path_util.c: patch [#34103] path_util_split_name_num.patch

Campbell Barton ideasman42 at gmail.com
Sun Mar 10 06:11:19 CET 2013


Revision: 55148
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=55148
Author:   campbellbarton
Date:     2013-03-10 05:11:18 +0000 (Sun, 10 Mar 2013)
Log Message:
-----------
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()

Modified Paths:
--------------
    trunk/blender/source/blender/blenlib/intern/path_util.c

Modified: trunk/blender/source/blender/blenlib/intern/path_util.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/path_util.c	2013-03-10 04:43:15 UTC (rev 55147)
+++ trunk/blender/source/blender/blenlib/intern/path_util.c	2013-03-10 05:11:18 UTC (rev 55148)
@@ -162,42 +162,41 @@
  * 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));
+	memcpy(left, name, (name_len + 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;
+	/* 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;
+			}
 		}
-		/* non-numeric suffix--give up */
-		if (isdigit(name[a]) == 0) break;
-		
-		left[a] = 0;
 	}
 
-	for (a = 0; name[a]; a++)
-		left[a] = name[a];
-
-	return a;
+	return name_len;
 }
 
 /**




More information about the Bf-blender-cvs mailing list