MCPcopy Create free account
hub / github.com/Genivia/ugrep / is_binary

Function is_binary

src/ugrep.cpp:702–714  ·  view source on GitHub ↗

return true if s[0..n-1] contains a \0 (NUL) or a non-displayable invalid UTF-8 encoding

Source from the content-addressed store, hash-verified

700
701// return true if s[0..n-1] contains a \0 (NUL) or a non-displayable invalid UTF-8 encoding
702inline 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
717inline bool check_binary(const char *s, size_t n)

Callers 2

check_binaryFunction · 0.70
init_is_binaryMethod · 0.70

Calls 1

isutf8Function · 0.85

Tested by

no test coverage detected