OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
308 | 308 |
309 void MemoryMappedExternalResource::Init(const char* filename) { | 309 void MemoryMappedExternalResource::Init(const char* filename) { |
310 file_ = OS::MemoryMappedFile::open(filename); | 310 file_ = OS::MemoryMappedFile::open(filename); |
311 if (file_ != NULL) { | 311 if (file_ != NULL) { |
312 filename_ = StrDup(filename); | 312 filename_ = StrDup(filename); |
313 data_ = reinterpret_cast<char*>(file_->memory()); | 313 data_ = reinterpret_cast<char*>(file_->memory()); |
314 length_ = file_->size(); | 314 length_ = file_->size(); |
315 } | 315 } |
316 } | 316 } |
317 | 317 |
318 | |
319 bool MemoryMappedExternalResource::EnsureIsAscii(bool abort_if_failed) const { | |
320 bool is_ascii = true; | |
321 | |
322 int line_no = 1; | |
323 const char* start_of_line = data_; | |
324 const char* end = data_ + length_; | |
325 for (const char* p = data_; p < end; p++) { | |
326 char c = *p; | |
327 if ((c & 0x80) != 0) { | |
328 // Non-ASCII detected: | |
329 is_ascii = false; | |
330 | |
331 // Report the error and abort if appropriate: | |
332 if (abort_if_failed) { | |
333 int char_no = static_cast<int>(p - start_of_line) - 1; | |
334 | |
335 ASSERT(filename_ != NULL); | |
336 PrintF("\n\n\n" | |
337 "Abort: Non-Ascii character 0x%.2x in file %s line %d char %d", | |
338 c, filename_, line_no, char_no); | |
339 | |
340 // Allow for some context up to kNumberOfLeadingContextChars chars | |
341 // before the offending non-ASCII char to help the user see where | |
342 // the offending char is. | |
343 const int kNumberOfLeadingContextChars = 10; | |
344 const char* err_context = p - kNumberOfLeadingContextChars; | |
345 if (err_context < data_) { | |
346 err_context = data_; | |
347 } | |
348 // Compute the length of the error context and print it. | |
349 int err_context_length = static_cast<int>(p - err_context); | |
350 if (err_context_length != 0) { | |
351 PrintF(" after \"%.*s\"", err_context_length, err_context); | |
352 } | |
353 PrintF(".\n\n\n"); | |
354 OS::Abort(); | |
355 } | |
356 | |
357 break; // Non-ASCII detected. No need to continue scanning. | |
358 } | |
359 if (c == '\n') { | |
360 start_of_line = p; | |
361 line_no++; | |
362 } | |
363 } | |
364 | |
365 return is_ascii; | |
366 } | |
367 | |
368 | |
369 } } // namespace v8::internal | 318 } } // namespace v8::internal |
OLD | NEW |