]> granicus.if.org Git - handbrake/commitdiff
decsrtsub: rework character substitution logic.
authorRodeo <tdskywalker@gmail.com>
Wed, 8 May 2013 21:51:00 +0000 (21:51 +0000)
committerRodeo <tdskywalker@gmail.com>
Wed, 8 May 2013 21:51:00 +0000 (21:51 +0000)
Don't pointlessly convert a line break to a space if it's the last character.

Also, only skip a CR character if it's followed by an LF character. Otherwise, replace it with an LF character or a space, depending on the line number.

git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@5446 b64f7644-9d1e-0410-96f1-a4d463321fa5

libhb/decsrtsub.c

index 9bc9cedcdc81c09ddb15034f9577af2e903e83d0..4a827f460789d0f39c1aa4853cdf220559b0151b 100644 (file)
@@ -321,7 +321,7 @@ static hb_buffer_t *srt_read( hb_work_private_t *pv )
              */
             pv->last_entry_number = entry_number;
             resync = 0;
-            if( *pv->current_entry.text )
+            if (*pv->current_entry.text != '\0')
             {
                 long length;
                 char *p, *q;
@@ -342,30 +342,35 @@ static hb_buffer_t *srt_read( hb_work_private_t *pv )
 
                 length = strlen( pv->current_entry.text );
 
-                for( q = p = pv->current_entry.text; *p; p++)
+                for (q = p = pv->current_entry.text; *p != '\0'; p++)
                 {
-                    if( *p == '\n' )
+                    if (*p == '\n' || *p == '\r')
                     {
-                        if ( line == 1 )
+                        if (*(p + 1) == '\n' || *(p + 1) == '\0')
                         {
-                            *q = *p;
+                            // followed by line break or last character, skip it
+                            length--;
+                            continue;
+                        }
+                        else if (line == 1)
+                        {
+                            // replace '\r' with '\n'
+                            *q   = '\n';
                             line = 2;
                         }
                         else
                         {
+                            // all subtitles on one line
+                            // replace line breaks with spaces
                             *q = ' ';
                         }
                         q++;
                     }
-                    else if( *p != '\r' )
+                    else
                     {
                         *q = *p;
                         q++;
                     }
-                    else
-                    {
-                        length--;
-                    }
                 }
                 *q = '\0';
 
@@ -392,7 +397,7 @@ static hb_buffer_t *srt_read( hb_work_private_t *pv )
     }
 
     hb_buffer_t *buffer = NULL;
-    if( *pv->current_entry.text )
+    if (*pv->current_entry.text != '\0')
     {
         long length;
         char *p, *q;
@@ -411,30 +416,35 @@ static hb_buffer_t *srt_read( hb_work_private_t *pv )
 
         length = strlen( pv->current_entry.text );
 
-        for( q = p = pv->current_entry.text; *p; p++)
+        for (q = p = pv->current_entry.text; *p != '\0'; p++)
         {
-            if( *p == '\n' )
+            if (*p == '\n' || *p == '\r')
             {
-                if ( line == 1 )
+                if (*(p + 1) == '\n' || *(p + 1) == '\0')
                 {
-                    *q = *p;
+                    // followed by line break or last character, skip it
+                    length--;
+                    continue;
+                }
+                else if (line == 1)
+                {
+                    // replace '\r' with '\n'
+                    *q   = '\n';
                     line = 2;
                 }
                 else
                 {
+                    // all subtitles on one line
+                    // replace line breaks with spaces
                     *q = ' ';
                 }
                 q++;
             }
-            else if( *p != '\r' )
+            else
             {
                 *q = *p;
                 q++;
             }
-            else
-            {
-                length--;
-            }
         }
         *q = '\0';