Speedup: optimize DNA_elem_array_size to speedup file loading
[blender.git] / source / blender / makesdna / intern / dna_genfile.c
index 2881f3138b273f9e2e3ef958bb842e668c6acdee..ef9c27be1965ec5fcb3e19c66a4b6678494cd92d 100644 (file)
 /* allowed duplicate code from makesdna.c */
 
 /**
- * parses the "[n]" on the end of an array name and returns the number of array elements n.
+ * parses the "[n1][n2]..." on the end of an array name and returns the number of array elements n1*n2*...
  */
 int DNA_elem_array_size(const char *str)
 {
-       int a, mul = 1;
-       const char *cp = NULL;
-
-       for (a = 0; str[a]; a++) {
-               if (str[a] == '[') {
-                       cp = &(str[a + 1]);
-               }
-               else if (str[a] == ']' && cp) {
-                       mul *= atoi(cp);
+       int result = 1;
+       int current = 0;
+       while (true) {
+               char c = *str++;
+               switch (c) {
+                       case '\0':
+                               return result;
+                       case '[':
+                               current = 0;
+                               break;
+                       case ']':
+                               result *= current;
+                               break;
+                       case '0':
+                       case '1':
+                       case '2':
+                       case '3':
+                       case '4':
+                       case '5':
+                       case '6':
+                       case '7':
+                       case '8':
+                       case '9':
+                               current = current * 10 + (c - '0');
+                               break;
+                       default:
+                               break;
                }
        }
-
-       return mul;
 }
 
 /* ************************* END MAKE DNA ********************** */