return true if s[0..n-1] contains a \0 (NUL) or a non-displayable invalid UTF-8 encoding
| 700 | |
| 701 | // return true if s[0..n-1] contains a \0 (NUL) or a non-displayable invalid UTF-8 encoding |
| 702 | inline bool is_binary(const char *s, size_t n) |
| 703 | { |
| 704 | // not --null-data or --encoding=null-data that permit NUL in the input and non-UTF-8 like GNU grep |
| 705 | if (flag_encoding_type == reflex::Input::file_encoding::null_data) |
| 706 | return false; |
| 707 | |
| 708 | // not -a and -U or -W: file is binary if it has a \0 (NUL) or an invalid UTF-8 encoding when --encoding is unset |
| 709 | if (!flag_text && (!flag_binary || flag_with_hex) && flag_encoding_type == reflex::Input::file_encoding::plain) |
| 710 | return !reflex::isutf8(s, s + n); |
| 711 | |
| 712 | // otherwise, file is binary if it contains a \0 (NUL) which is what GNU grep checks |
| 713 | return memchr(s, '\0', n) != NULL; |
| 714 | } |
| 715 | |
| 716 | // return true if -X or if -W and match s[0..n-1] is binary |
| 717 | inline bool check_binary(const char *s, size_t n) |
no test coverage detected