Reviewers: dcarney,

Message:
Committed patchset #1 manually as r17931.

Description:
Lexer-shell: skip utf16 magic bytes when reading files.

[email protected]
BUG=

Committed: https://code.google.com/p/v8/source/detail?r=17931

Please review this at https://codereview.chromium.org/78233003/

SVN Base: https://v8.googlecode.com/svn/branches/experimental/parser

Affected files (+19, -6 lines):
  M src/lexer/even-more-experimental-scanner.cc


Index: src/lexer/even-more-experimental-scanner.cc
diff --git a/src/lexer/even-more-experimental-scanner.cc b/src/lexer/even-more-experimental-scanner.cc index ac26d1d16bb1c6cabe012865138c19871372355b..96c753f7d372b46c7072c3bc005f90cb6cbd50df 100644
--- a/src/lexer/even-more-experimental-scanner.cc
+++ b/src/lexer/even-more-experimental-scanner.cc
@@ -69,19 +69,32 @@ const byte* ReadFile(const char* name, Isolate* isolate,
   int file_size = ftell(file);
   rewind(file);

-  *size = file_size * repeat;
-
-  byte* chars = new byte[*size];
+  byte* file_contents = new byte[file_size];
   for (int i = 0; i < file_size;) {
-    int read = static_cast<int>(fread(&chars[i], 1, file_size - i, file));
+    int read =
+        static_cast<int>(fread(&file_contents[i], 1, file_size - i, file));
     i += read;
   }
   fclose(file);

-  for (int i = file_size; i < *size; i++) {
-    chars[i] = chars[i - file_size];
+  // If the file contains the UTF16 little endian magic bytes, skip them.
+ // FIXME: what if we see big endian magic bytes? Do we do the right thing for
+  // big endian anyway?
+  byte* start = file_contents;
+  if (*start == 0xff && *(start + 1) == 0xfe) {
+    start += 2;
+    file_size -= 2;
   }

+  *size = file_size * repeat;
+  byte* chars = new byte[*size];
+
+  for (int i = 0; i < *size; i++) {
+    chars[i] = start[i % file_size];
+  }
+
+  delete file_contents;
+
   return chars;
 }



--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to