| 470 | |
| 471 | template <class Char> |
| 472 | inline bool DoIsStringASCII(const Char* characters, size_t length) { |
| 473 | MachineWord all_char_bits = 0; |
| 474 | const Char* end = characters + length; |
| 475 | |
| 476 | // Prologue: align the input. |
| 477 | while (!IsAlignedToMachineWord(characters) && characters != end) { |
| 478 | all_char_bits |= *characters; |
| 479 | ++characters; |
| 480 | } |
| 481 | |
| 482 | // Compare the values of CPU word size. |
| 483 | const Char* word_end = AlignToMachineWord(end); |
| 484 | const size_t loop_increment = sizeof(MachineWord) / sizeof(Char); |
| 485 | while (characters < word_end) { |
| 486 | all_char_bits |= *(reinterpret_cast<const MachineWord*>(characters)); |
| 487 | characters += loop_increment; |
| 488 | } |
| 489 | |
| 490 | // Process the remaining bytes. |
| 491 | while (characters != end) { |
| 492 | all_char_bits |= *characters; |
| 493 | ++characters; |
| 494 | } |
| 495 | |
| 496 | MachineWord non_ascii_bit_mask = |
| 497 | NonASCIIMask<sizeof(MachineWord), Char>::value(); |
| 498 | return !(all_char_bits & non_ascii_bit_mask); |
| 499 | } |
| 500 | |
| 501 | bool IsStringASCII(const StringPiece& str) { |
| 502 | return DoIsStringASCII(str.data(), str.length()); |
no test coverage detected