[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [53024] trunk/blender/source/blender: avoid using strlen() for comparisons in for loops.
Campbell Barton
ideasman42 at gmail.com
Sat Dec 15 08:57:29 CET 2012
Revision: 53024
http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=53024
Author: campbellbarton
Date: 2012-12-15 07:57:16 +0000 (Sat, 15 Dec 2012)
Log Message:
-----------
avoid using strlen() for comparisons in for loops. for expanding whitespace in the text editor and ui paste.
Modified Paths:
--------------
trunk/blender/source/blender/datatoc/datatoc.c
trunk/blender/source/blender/editors/interface/interface_handlers.c
trunk/blender/source/blender/editors/space_text/text_ops.c
trunk/blender/source/blender/makesdna/intern/makesdna.c
Modified: trunk/blender/source/blender/datatoc/datatoc.c
===================================================================
--- trunk/blender/source/blender/datatoc/datatoc.c 2012-12-15 06:12:40 UTC (rev 53023)
+++ trunk/blender/source/blender/datatoc/datatoc.c 2012-12-15 07:57:16 UTC (rev 53024)
@@ -51,6 +51,7 @@
FILE *fpin, *fpout;
long size;
int i;
+ int argv_len;
if (argc < 2) {
printf("Usage: datatoc <data_file_from> <data_file_to>\n");
@@ -75,7 +76,8 @@
printf("Making C file <%s>\n", argv[2]);
#endif
- for (i = 0; i < (int)strlen(argv[1]); i++)
+ argv_len = (int)strlen(argv[1]);
+ for (i = 0; i < argv_len; i++)
if (argv[1][i] == '.') argv[1][i] = '_';
fpout = fopen(argv[2], "w");
Modified: trunk/blender/source/blender/editors/interface/interface_handlers.c
===================================================================
--- trunk/blender/source/blender/editors/interface/interface_handlers.c 2012-12-15 06:12:40 UTC (rev 53023)
+++ trunk/blender/source/blender/editors/interface/interface_handlers.c 2012-12-15 07:57:16 UTC (rev 53024)
@@ -1674,10 +1674,11 @@
{
char buf[UI_MAX_DRAW_STR] = {0};
char *str, *p, *pbuf;
- int len, x, i, changed = 0;
+ int x, changed = 0;
+ int str_len, buf_len;
str = data->str;
- len = strlen(str);
+ str_len = strlen(str);
/* paste */
if (paste) {
@@ -1687,28 +1688,28 @@
if (p && p[0]) {
unsigned int y;
- i = 0;
- while (*p && *p != '\r' && *p != '\n' && i < UI_MAX_DRAW_STR - 1) {
- buf[i++] = *p;
+ buf_len = 0;
+ while (*p && *p != '\r' && *p != '\n' && buf_len < UI_MAX_DRAW_STR - 1) {
+ buf[buf_len++] = *p;
p++;
}
- buf[i] = 0;
+ buf[buf_len] = 0;
/* paste over the current selection */
if ((but->selend - but->selsta) > 0) {
ui_textedit_delete_selection(but, data);
- len = strlen(str);
+ str_len = strlen(str);
}
- for (y = 0; y < strlen(buf); y++) {
+ for (y = 0; y < buf_len; y++) {
/* add contents of buffer */
- if (len + 1 < data->maxlen) {
+ if (str_len + 1 < data->maxlen) {
for (x = data->maxlen; x > but->pos; x--)
str[x] = str[x - 1];
str[but->pos] = buf[y];
but->pos++;
- len++;
- str[len] = '\0';
+ str_len++;
+ str[str_len] = '\0';
}
}
Modified: trunk/blender/source/blender/editors/space_text/text_ops.c
===================================================================
--- trunk/blender/source/blender/editors/space_text/text_ops.c 2012-12-15 06:12:40 UTC (rev 53023)
+++ trunk/blender/source/blender/editors/space_text/text_ops.c 2012-12-15 07:57:16 UTC (rev 53024)
@@ -1131,21 +1131,20 @@
TextLine *tmp;
FlattenString fs;
size_t a, j;
- char *text_check_line, *new_line;
+ char *new_line;
int extra, number; //unknown for now
int type = RNA_enum_get(op->ptr, "type");
-
- tmp = text->lines.first;
-
+
/* first convert to all space, this make it a lot easier to convert to tabs
* because there is no mixtures of ' ' && '\t' */
- while (tmp) {
- text_check_line = tmp->line;
+ for (tmp = text->lines.first; tmp; tmp = tmp->next) {
+ const char *text_check_line = tmp->line;
+ const int text_check_line_len = tmp->len;
number = flatten_string(st, &fs, text_check_line) + 1;
flatten_string_free(&fs);
new_line = MEM_callocN(number, "Converted_Line");
j = 0;
- for (a = 0; a < strlen(text_check_line); a++) { //foreach char in line
+ for (a = 0; a < text_check_line_len; a++) { //foreach char in line
if (text_check_line[a] == '\t') { //checking for tabs
//get the number of spaces this tabs is showing
//i don't like doing it this way but will look into it later
@@ -1175,20 +1174,19 @@
tmp->line = new_line;
tmp->len = strlen(new_line);
tmp->format = NULL;
- tmp = tmp->next;
}
if (type == TO_TABS) { // Converting to tabs
//start over from the beginning
- tmp = text->lines.first;
- while (tmp) {
- text_check_line = tmp->line;
+ for (tmp = text->lines.first; tmp; tmp = tmp->next) {
+ const char *text_check_line = tmp->line;
+ const int text_check_line_len = tmp->len;
extra = 0;
- for (a = 0; a < strlen(text_check_line); a++) {
+ for (a = 0; a < text_check_line_len; a++) {
number = 0;
for (j = 0; j < (size_t)st->tabnumber; j++) {
- if ((a + j) <= strlen(text_check_line)) { //check to make sure we are not pass the end of the line
+ if ((a + j) <= text_check_line_len) { //check to make sure we are not pass the end of the line
if (text_check_line[a + j] != ' ') {
number = 1;
}
@@ -1201,12 +1199,12 @@
}
if (extra > 0) { //got tabs make malloc and do what you have to do
- new_line = MEM_callocN(strlen(text_check_line) - (((st->tabnumber * extra) - extra) - 1), "Converted_Line");
+ new_line = MEM_callocN(text_check_line_len - (((st->tabnumber * extra) - extra) - 1), "Converted_Line");
extra = 0; //reuse vars
- for (a = 0; a < strlen(text_check_line); a++) {
+ for (a = 0; a < text_check_line_len; a++) {
number = 0;
for (j = 0; j < (size_t)st->tabnumber; j++) {
- if ((a + j) <= strlen(text_check_line)) { //check to make sure we are not pass the end of the line
+ if ((a + j) <= text_check_line_len) { //check to make sure we are not pass the end of the line
if (text_check_line[a + j] != ' ') {
number = 1;
}
@@ -1233,7 +1231,6 @@
tmp->len = strlen(new_line);
tmp->format = NULL;
}
- tmp = tmp->next;
}
}
Modified: trunk/blender/source/blender/makesdna/intern/makesdna.c
===================================================================
--- trunk/blender/source/blender/makesdna/intern/makesdna.c 2012-12-15 06:12:40 UTC (rev 53023)
+++ trunk/blender/source/blender/makesdna/intern/makesdna.c 2012-12-15 07:57:16 UTC (rev 53024)
@@ -984,7 +984,7 @@
/* little test first... */
/* Mind the breaking condition here! */
if (debugSDNA) printf("\tStart of header scan:\n");
- for (i = 0; strlen(includefiles[i]); i++) {
+ for (i = 0; *(includefiles[i]) != '\0'; i++) {
sprintf(str, "%s%s", baseDirectory, includefiles[i]);
if (debugSDNA) printf("\t|-- Converting %s\n", str);
if (convert_include(str)) {
@@ -1100,7 +1100,7 @@
else {
/* add all include files defined in the global array */
- for (i = 0; strlen(includefiles[i]); i++) {
+ for (i = 0; *(includefiles[i]) != '\0'; i++) {
fprintf(fp, "#include \"%s%s\"\n", baseDirectory, includefiles[i]);
}
More information about the Bf-blender-cvs
mailing list