Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: src/lexer/even-more-experimental-scanner.cc

Issue 78233003: Lexer-shell: skip utf16 magic bytes when reading files. (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 the V8 project authors. All rights reserved. 1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 const byte* ReadFile(const char* name, Isolate* isolate, 62 const byte* ReadFile(const char* name, Isolate* isolate,
63 int* size, int repeat) { 63 int* size, int repeat) {
64 FILE* file = fopen(name, "rb"); 64 FILE* file = fopen(name, "rb");
65 *size = 0; 65 *size = 0;
66 if (file == NULL) return NULL; 66 if (file == NULL) return NULL;
67 67
68 fseek(file, 0, SEEK_END); 68 fseek(file, 0, SEEK_END);
69 int file_size = ftell(file); 69 int file_size = ftell(file);
70 rewind(file); 70 rewind(file);
71 71
72 *size = file_size * repeat; 72 byte* file_contents = new byte[file_size];
73
74 byte* chars = new byte[*size];
75 for (int i = 0; i < file_size;) { 73 for (int i = 0; i < file_size;) {
76 int read = static_cast<int>(fread(&chars[i], 1, file_size - i, file)); 74 int read =
75 static_cast<int>(fread(&file_contents[i], 1, file_size - i, file));
77 i += read; 76 i += read;
78 } 77 }
79 fclose(file); 78 fclose(file);
80 79
81 for (int i = file_size; i < *size; i++) { 80 // If the file contains the UTF16 little endian magic bytes, skip them.
82 chars[i] = chars[i - file_size]; 81 // FIXME: what if we see big endian magic bytes? Do we do the right thing for
82 // big endian anyway?
83 byte* start = file_contents;
84 if (*start == 0xff && *(start + 1) == 0xfe) {
85 start += 2;
86 file_size -= 2;
83 } 87 }
84 88
89 *size = file_size * repeat;
90 byte* chars = new byte[*size];
91
92 for (int i = 0; i < *size; i++) {
93 chars[i] = start[i % file_size];
94 }
95
96 delete file_contents;
97
85 return chars; 98 return chars;
86 } 99 }
87 100
88 101
89 102
90 } 103 }
91 } 104 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698