Phil Thompson <p...@riverbankcomputing.com> wrote:

> Done - something similar.

Thank you... and sorry for arriving a bit late. In case you find it
useful, here is a better patch that performs universal newline
translation at the basic level (i.e., not only when parsing a Python
string).

 As experience shows, pylupdate4 does not handle .py files correctly
unless the end of line style is Unix (\n is assumed to be the only line
terminator except when parsing .ui files). This is true even out of the
context of a backslash at the end of a line. This patch fixes this too
(tested with the 3 kinds of EOL style).

Regards

-- 
Florent
--- a/pylupdate/fetchtr.cpp
+++ b/pylupdate/fetchtr.cpp
@@ -91,40 +91,79 @@
 // the string to read from and current position in the string (otherwise)
 static QString yyInStr;
 static int yyInPos;
-static int buf;
+// - 'rawbuf' is used to hold bytes before universal newline translation.
+// - 'buf' is its higher-level counterpart, where every end-of-line appears as
+//   a single '\n' character, regardless of the end-of-line style used in input
+//   files.
+static int buf, rawbuf;
 
 static int (*getChar)();
 static int (*peekChar)();
 
 static bool yyParsingUtf8;
 
-static int getCharFromFile()
+static int getCharFromFile_ll()
 {
     int c;
 
-    if ( buf < 0 )
+    if ( rawbuf < 0 )           // Empty raw buffer?
         c = getc( yyInFile );
     else {
+        c = rawbuf;
+        rawbuf = -1;            // Declare the raw buffer empty.
+    }
+
+    // Universal newline translation, similar to what Python does
+    if ( c == '\r' ) {
+        c = getc( yyInFile ); // Last byte of a \r\n sequence?
+        if ( c != '\n')
+            {
+                rawbuf = c; // No, put it in 'rawbuf' for later processing.
+                // Logical character that will be seen by higher-level functions
+                c = '\n';
+            }
+        // In all cases, c == '\n' here.
+    }
+
+    return c;
+}
+
+static int getCharFromFile()
+{
+    int c;
+
+    if ( buf < 0 ) {            // Empty buffer?
+        c = getCharFromFile_ll();
+    } else {
         c = buf;
-        buf = -1;
+        buf = -1;               // Declare the buffer empty.
     }
-    if ( c == '\n' )
-        yyCurLineNo++;
+
+    if ( c == '\n' )            // This is after universal newline translation
+        yyCurLineNo++;          // (i.e., a "logical" newline character).
+
     return c;
 }
 
 static int peekCharFromFile()
 {
-    int c = getc( yyInFile );
-    buf = c;
-    return c;
+    if ( buf >= 0 && buf != EOF )
+        qWarning("peekCharFromFile() called at least twice in a row "
+                 "without any intervening call to getCharFromFile(); "
+                 "bytes will be lost!");
+
+    // Read a character, possibly performing universal newline translation,
+    // and put it in 'buf' so that the next call to getCharFromFile() finds it
+    // already available.
+    buf = getCharFromFile();
+    return buf;
 }
 
 static void startTokenizer( const char *fileName, int (*getCharFunc)(),
                             int (*peekCharFunc)(), QTextCodec *codecForTr, QTextCodec *codecForSource )
 {
     yyInPos = 0;
-    buf = -1;
+    buf = rawbuf = -1;
     getChar = getCharFunc;
     peekChar = peekCharFunc;
 
@@ -340,6 +379,11 @@
     #endif
                                 if ( yyStringLen < sizeof(yyString) - 1 )
                                     yyString[yyStringLen++] = (char) n;
+                            } else if ( yyCh == '\n' ) {
+                                /* Backslash at the end of a line in a string;
+                                   gobble the newline char and get the next
+                                   character for further processing */
+                                yyCh = getChar();
                             } else {
                                 const char *p = strchr( tab, yyCh );
                                 if ( yyStringLen < sizeof(yyString) - 1 )
_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Reply via email to