changeset 32979:4905f5a87357

Replace some awkward and unnecessary usages of strlen(). 1. A string length of zero or greater than zero can be determined by checking the first byte. 2. Leave it to strdup() to determine the length and to do allocation for a string that is to be copied. 3. If neither the string length nor the index variable is altered in a loop, string[index] (!= 0) is the condition to go with.
author ib
date Thu, 10 Mar 2011 14:20:36 +0000
parents b68d209b6f28
children 2bfd000bb789
files gui/mplayer/gui_common.c gui/skin/cut.c gui/skin/skin.c
diffstat 3 files changed, 10 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/gui/mplayer/gui_common.c	Thu Mar 10 13:22:11 2011 +0000
+++ b/gui/mplayer/gui_common.c	Thu Mar 10 14:20:36 2011 +0000
@@ -91,7 +91,7 @@
     }
 
     if (c) {
-        for (i = 0; i < (int)strlen(tmp); i++) {
+        for (i = 0; tmp[i]; i++) {
             int t = 0;
 
             if (c == 1)
--- a/gui/skin/cut.c	Thu Mar 10 13:22:11 2011 +0000
+++ b/gui/skin/cut.c	Thu Mar 10 14:20:36 2011 +0000
@@ -26,7 +26,7 @@
     int n;
     unsigned int i, c;
 
-    for (c = 0, n = 0, i = 0; i < strlen(in); i++) {
+    for (c = 0, n = 0, i = 0; in[i]; i++) {
         if (in[i] == sep)
             n++;
         if (n >= num && in[i] != sep && c + 1 < maxout)
--- a/gui/skin/skin.c	Thu Mar 10 13:22:11 2011 +0000
+++ b/gui/skin/skin.c	Thu Mar 10 14:20:36 2011 +0000
@@ -179,7 +179,7 @@
 
     mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[skin]  %send (%s)\n", space, name);
 
-    if (strlen(window_name)) {
+    if (window_name[0]) {
         window_name[0] = 0;
         currSection    = NULL;
         currSubItem    = NULL;
@@ -711,14 +711,13 @@
     item->y      = y;
     item->width  = -1;
     item->height = -1;
+    item->label  = strdup(tmp);
 
-    if ((item->label = malloc(strlen(tmp) + 1)) == NULL) {
+    if (!item->label) {
         ERRORMESSAGE(MSGTR_SKIN_FONT_NotEnoughtMemory);
         return 1;
     }
 
-    strcpy(item->label, tmp);
-
     return 0;
 }
 
@@ -766,14 +765,13 @@
     item->y      = y;
     item->width  = sx;
     item->height = -1;
+    item->label  = strdup(tmp);
 
-    if ((item->label = malloc(strlen(tmp) + 1)) == NULL) {
+    if (!item->label) {
         ERRORMESSAGE(MSGTR_SKIN_FONT_NotEnoughtMemory);
         return 1;
     }
 
-    strcpy(item->label, tmp);
-
     return 0;
 }
 
@@ -829,10 +827,10 @@
 {
     int i;
 
-    if (strlen(in) == 0)
+    if (!*in)
         return NULL;
 
-    for (i = 0; i < (int)strlen(in); i++)
+    for (i = 0; in[i]; i++)
         if (in[i] == what)
             in[i] = whereof;
 
@@ -843,7 +841,7 @@
 {
     int c = 0, id = 0, i;
 
-    if (strlen(in) == 0)
+    if (!*in)
         return NULL;
 
     while (c != (int)strlen(in)) {