[vlc-commits] [Git][videolan/vlc][master] 2 commits: input/subtitles: remove useless strcpy_get_ext

Jean-Baptiste Kempf (@jbk) gitlab at videolan.org
Sat Jul 10 07:16:57 UTC 2021



Jean-Baptiste Kempf pushed to branch master at VideoLAN / VLC


Commits:
2392e310 by Marvin Scholz at 2021-07-10T07:06:15+00:00
input/subtitles: remove useless strcpy_get_ext

The result stored in tmp_fname_ext is never used by the
surrounding code and this was the only use of this function.

- - - - -
485005a0 by Marvin Scholz at 2021-07-10T07:06:15+00:00
input/subtitles: rewrite string trimming

Rewrites the string trimming used by the subtitle comparison
to operate in-place, reducing the amount of copies and moves
the stripping algorithm into one function, making it easier to
tweak the way it behaves in the future.

- - - - -


1 changed file:

- src/input/subtitles.c


Changes:

=====================================
src/input/subtitles.c
=====================================
@@ -45,61 +45,68 @@
  */
 static const char *const sub_exts[] = { SLAVE_SPU_EXTENSIONS, "" };
 
-static void strcpy_trim( char *d, const char *s )
+/**
+ * Remove file extension in-place
+ */
+static void filename_strip_ext_inplace(char *str)
 {
-    unsigned char c;
-
-    /* skip leading whitespace */
-    while( ((c = *s) != '\0') && !isalnum(c) )
-    {
-        s++;
-    }
-    for(;;)
-    {
-        /* copy word */
-        while( ((c = *s) != '\0') && isalnum(c) )
-        {
-            *d = tolower(c);
-            s++; d++;
-        }
-        if( *s == 0 ) break;
-        /* trim excess whitespace */
-        while( ((c = *s) != '\0') && !isalnum(c) )
-        {
-            s++;
-        }
-        if( *s == 0 ) break;
-        *d++ = ' ';
-    }
-    *d = 0;
+    char *tmp = strrchr(str, '.');
+    if (tmp)
+        *tmp = '\0';
 }
 
-static void strcpy_strip_ext( char *d, const char *s )
+/**
+ * Trim special characters from a filename
+ * 
+ * Trims whitespaces and other non-alphanumeric
+ * characters from filenames.
+ * 
+ * \warning This function operates on the passed string
+ * without copying. It might return a pointer different to
+ * the passed one, in case it trims characters at the beginning.
+ * Therefore it is essential that the return value is used where
+ * the trimmed version of the string is needed and the returned
+ * pointer must not be free()d but rather the original pointer!
+ */
+VLC_USED static char *filename_trim_inplace(char *str)
 {
+    char *ret = str;
     unsigned char c;
 
-    const char *tmp = strrchr(s, '.');
-    if( !tmp )
-    {
-        strcpy(d, s);
-        return;
-    }
-    else
-        strlcpy(d, s, tmp - s + 1 );
-    while( (c = *d) != '\0' )
+    // Trim leading non-alnum
+    while( (c = *str) != '\0' && !isalnum(c) )
+        str++;
+    ret = str;
+
+    // Trim inline nonalnum groups
+    char *writehead = str;
+    bool consecutive = false;
+    while( (c = *str) != '\0' )
     {
-        *d = tolower(c);
-        d++;
+        if ( !isalnum(c) )
+        {
+            if ( consecutive )
+            {
+                str++;
+                continue;
+            }
+
+            c = ' ';
+            consecutive = true;
+        } else {
+            consecutive = false;
+        }
+
+        *writehead++ = tolower(c);
+        str++;
     }
-}
+    *writehead = '\0';
 
-static void strcpy_get_ext( char *d, const char *s )
-{
-    const char *tmp = strrchr(s, '.');
-    if( !tmp )
-        strcpy(d, "");
-    else
-        strcpy( d, tmp + 1 );
+    // Remove trailing space, if any
+    if ( consecutive )
+        *(writehead - 1) = '\0';
+
+    return ret;
 }
 
 static int whiteonly( const char *s )
@@ -216,10 +223,8 @@ int subtitles_Detect( input_thread_t *p_this, char *psz_path, const char *psz_na
     int i_fuzzy = var_GetInteger( p_this, "sub-autodetect-fuzzy" );
     if ( i_fuzzy == 0 )
         return VLC_EGENERIC;
-    int i_fname_len;
     input_item_slave_t **pp_slaves = *ppp_slaves;
     int i_slaves = *p_slaves;
-    char *f_fname_noext = NULL, *f_fname_trim = NULL;
     char **subdirs; /* list of subdirectories to look in */
 
     if( !psz_name_org )
@@ -237,31 +242,18 @@ int subtitles_Detect( input_thread_t *p_this, char *psz_path, const char *psz_na
         return VLC_ENOMEM;
     }
 
-    const char *f_fname = strrchr( psz_fname, DIR_SEP_CHAR );
-    if( !f_fname )
+    char *f_fname_trim = strrchr( psz_fname, DIR_SEP_CHAR );
+    if( !f_fname_trim )
     {
         free( f_dir );
         free( psz_fname );
         return VLC_EGENERIC;
     }
-    f_fname++; /* Skip the '/' */
-    f_dir[f_fname - psz_fname] = 0; /* keep dir separator in f_dir */
+    f_fname_trim++; /* Skip the '/' */
+    f_dir[f_fname_trim - psz_fname] = 0; /* keep dir separator in f_dir */
 
-    i_fname_len = strlen( f_fname );
-
-    f_fname_noext = malloc(i_fname_len + 1);
-    f_fname_trim = malloc(i_fname_len + 1 );
-    if( !f_fname_noext || !f_fname_trim )
-    {
-        free( f_dir );
-        free( f_fname_noext );
-        free( f_fname_trim );
-        free( psz_fname );
-        return VLC_ENOMEM;
-    }
-
-    strcpy_strip_ext( f_fname_noext, f_fname );
-    strcpy_trim( f_fname_trim, f_fname_noext );
+    filename_strip_ext_inplace(f_fname_trim);
+    f_fname_trim = filename_trim_inplace(f_fname_trim);
 
     subdirs = paths_to_list( f_dir, psz_path );
     for( int j = -1; (j == -1) || ( j >= 0 && subdirs != NULL && subdirs[j] != NULL ); j++ )
@@ -283,16 +275,16 @@ int subtitles_Detect( input_thread_t *p_this, char *psz_path, const char *psz_na
             if( psz_name[0] == '.' || !subtitles_Filter( psz_name ) )
                 continue;
 
-            char tmp_fname_noext[strlen( psz_name ) + 1];
-            char tmp_fname_trim[strlen( psz_name ) + 1];
-            char tmp_fname_ext[strlen( psz_name ) + 1];
+            char *tmp_fname = strdup(psz_name);
+            if (!tmp_fname)
+                break;
+
             const char *tmp;
             int i_prio = 0;
 
             /* retrieve various parts of the filename */
-            strcpy_strip_ext( tmp_fname_noext, psz_name );
-            strcpy_get_ext( tmp_fname_ext, psz_name );
-            strcpy_trim( tmp_fname_trim, tmp_fname_noext );
+            filename_strip_ext_inplace(tmp_fname);
+            char *tmp_fname_trim = filename_trim_inplace(tmp_fname);
 
             if( !strcmp( tmp_fname_trim, f_fname_trim ) )
             {
@@ -320,6 +312,9 @@ int subtitles_Detect( input_thread_t *p_this, char *psz_path, const char *psz_na
                 /* doesn't contain the movie name, prefer files in f_dir over subdirs */
                 i_prio = SLAVE_PRIORITY_MATCH_NONE;
             }
+            free(tmp_fname);
+            tmp_fname_trim = NULL;
+
             if( i_prio >= i_fuzzy )
             {
                 struct stat st;
@@ -367,8 +362,6 @@ int subtitles_Detect( input_thread_t *p_this, char *psz_path, const char *psz_na
         free( subdirs );
     }
     free( f_dir );
-    free( f_fname_trim );
-    free( f_fname_noext );
     free( psz_fname );
 
     for( int i = 0; i < i_slaves; i++ )



View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/0635abe236b1306bb4aaa83ebdbbc0d953ab5e5e...485005a0b6aa4719044a1c0db7e598b9acd5d0e6

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/0635abe236b1306bb4aaa83ebdbbc0d953ab5e5e...485005a0b6aa4719044a1c0db7e598b9acd5d0e6
You're receiving this email because of your account on code.videolan.org.




More information about the vlc-commits mailing list