Code Monkey home page Code Monkey logo

Comments (9)

BurntSushi avatar BurntSushi commented on June 3, 2024 1

You already can:

$ printf 'abc\xFF\xFE\xAAxyz' | rg -ao '(?-u:\xFF\xFE\xAA)' | xxd
00000000: fffe aa0a                                ....

from ripgrep.

RecRanger avatar RecRanger commented on June 3, 2024

Does this support non-unicode sequences?

from ripgrep.

BurntSushi avatar BurntSushi commented on June 3, 2024

Huh? My example demonstrates definitively that it does...

from ripgrep.

BurntSushi avatar BurntSushi commented on June 3, 2024

\xFF\xFE is not valid UTF-8.

from ripgrep.

RecRanger avatar RecRanger commented on June 3, 2024

Awesome, thanks! Unfortunately the average Joe doesn't know how to identify a valid unicode string from hex chars. Thanks for your help :)

from ripgrep.

RecRanger avatar RecRanger commented on June 3, 2024

Can you explain why you use -a here? The documentation isn't very clear on what the flag actually changes:

  -a, --text                      Search binary files as if they were text.

from ripgrep.

BurntSushi avatar BurntSushi commented on June 3, 2024

Because you're looking at the succinct docs. Use --help (as -h says at the top) to see the expanded docs:

    -a, --text
        This flag instructs ripgrep to search binary files as if they were
        text. When this flag is present, ripgrep's binary file detection is
        disabled. This means that when a binary file is searched, its contents
        may be printed if there is a match. This may cause escape codes to be
        printed that alter the behavior of your terminal.

        When binary file detection is enabled, it is imperfect. In general, it
        uses a simple heuristic. If a NUL byte is seen during search, then the
        file is considered binary and searching stops (unless this flag is
        present). Alternatively, if the --binary flag is used, then ripgrep
        will only quit when it sees a NUL byte after it sees a match (or
        searches the entire file).

        This flag overrides the --binary flag.

        This flag can be disabled with --no-text.

The -a/--text flag is pretty standard among greps. It disables binary detection. By default, greps will treat \x00 (NUL) as indicating that a file is binary data and maybe should be skipped or at least not printed to a terminal.

from ripgrep.

RecRanger avatar RecRanger commented on June 3, 2024

By default, greps will treat \x00 (NUL) as indicating that a file is binary data and maybe should be skipped or at least not printed to a terminal.

I still can't wrap my head around how this differs from the --binary flag, which also means "files should not be skipped, even if it's binary". Funny that --text and --binary have nearly identical meanings, depsite being opposites generally.

from ripgrep.

BurntSushi avatar BurntSushi commented on June 3, 2024
  • The default for recursive search is to completely skip binary files. That is, whenever a NUL byte is seen, ripgrep immediately stops the search and skips the rest of the file.
  • When --binary is given, it means, "ripgrep should not omit matches in files that are binary data, but otherwise should still be careful about printing binary data to a terminal." So when a NUL byte is found, if a match hasn't been found yet, ripgrep will continue searching. If a match is found, then a message is printed and the rest of the file is skipped: binary file matches (found "\0" byte around offset 0).
  • When --text is given, it means, "ripgrep should treat all files as text files and not concern itself with binary data." In this case, there is no NUL byte detection or guarding against printing binary data to your terminal.

I probably would have chosen a different model for exposing this, but it wasn't worth abdicating the -a/--text flag which is widely supported.

from ripgrep.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.