Bug 564369 - streamline TokenStream::getChar(), part 7. r=cdleary.
authorNicholas Nethercote <nnethercote@mozilla.com>
Wed, 12 May 2010 21:42:16 -0700
changeset 42701 91a4406e7a8c304d9dc51503ce809a327130002f
parent 42700 65f84aca73d15fb3effa162e0d33491ced4b54ff
child 42702 ad37e6e5bb42bd7d6f3ada46c9369c056db0b1bc
push id1
push userroot
push dateTue, 26 Apr 2011 22:38:44 +0000
treeherdermozilla-beta@bfdb6e623a36 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerscdleary
bugs564369
milestone1.9.3a5pre
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
Bug 564369 - streamline TokenStream::getChar(), part 7. r=cdleary.
js/src/jsscan.cpp
--- a/js/src/jsscan.cpp
+++ b/js/src/jsscan.cpp
@@ -265,52 +265,47 @@ js_fgets(char *buf, int size, FILE *file
 
 /*
  * Nb: This does *not* append a terminating '\0'.  Returns the number of chars
  * read from the file.
  */
 int
 TokenStream::fillUserbuf()
 {
-    // We must be careful with ASCII newlines:
-    //
-    // - \n ends a line
-    // - \r not followed by \n ends a line
-    // - \r\n ends a line at the \n
-    //
-    // For the last case, we avoid splitting a \r\n pair in order to keep
-    // things simpler for getChar().  To do this we keep one element in buf in
-    // reserve;  that way, if the nth char we get is \r, we can peek ahead one
-    // more and get \n as the (n+1)th char if it follows.
+    // We avoid splitting a \r\n pair, because this makes things much easier
+    // for getChar().  To do this, we only try to fill userbuf up with
+    // LINE_LIMIT-1 chars.  Once we've reached that number, if the last one is
+    // \r then we check if the following one is \n;  if so we get it too,
+    // knowing that we have space for it.
 
     jschar *buf = userbuf.base;
     int n = LINE_LIMIT - 1;     // reserve space for \n following a \r
     JS_ASSERT(n > 0);
     int i;
-    for (i = 0; i < n; i++) {
+    i = 0;
+    while (true) {
         int c = fast_getc(file);
         if (c == EOF)
             break;
         buf[i] = (jschar) (unsigned char) c;
-        if (c == '\n') {
-            i++;
-            break;
-        }
-        if (c == '\r') { 
-            i++;
-            // Look for a following \n.  We know we have space in buf for it.
-            c = fast_getc(file);
-            if (c == EOF)
-                break;
-            buf[i] = (jschar) (unsigned char) c;
-            if (c == '\n') {
-                i++;
-                break;
+        i++;
+
+        if (i == n) {
+            if (buf[i - 1] == '\r') {
+                // Look for a following \n.  We know we have space in buf for it.
+                c = fast_getc(file);
+                if (c == EOF)
+                    break;
+                if (c == '\n') {
+                    buf[i] = (jschar) (unsigned char) c;
+                    i++;
+                    break;
+                }
+                ungetc(c, file);    // \r wasn't followed by \n, unget
             }
-            ungetc(c, file);    // \r wasn't followed by \n, unget
             break;
         }
     }
     return i;
 }
 
 /*
  * This gets the next char, normalizing all EOL sequences to '\n' as it goes.