Skip to content

add support for other text encodings #1

Closed

Description

@BurntSushi

Right now, ripgrep only supports reading UTF-8 encoded text (even if some of it is invalid). In my estimation, this is probably good enough for the vast majority of use cases.

However, it may be useful to search other encodings. I don't think I'd be willing to, say, modify the regex engine itself to support other encodings, but if it were easy to do transcoding on the fly, then I think it wouldn't add too much complexity. The encoding_rs project in particular appears to support this type of text decoding.

Some thoughts/concerns:

  • Transcoding would require using the incremental searcher as opposed to the memory map searcher. (Which is fine.)
  • Transcoding requires picking a source encoding, and doing this seems non-trivial. You might imagine the CLI growing a new flag that specifies a text encoding, but what happens if you want to search directories containing files with many different types of text encodings? Do we need a more elaborate system? I'm inclined towards not, since I think the juice probably isn't worth the squeeze.

Activity

  1. added
    enhancement
    question
    on Sep 9, 2016
  2. nabijaczleweli commented on Sep 22, 2016

    @nabijaczleweli

    TBH I'd just ignore (and/or die violently on) anything that doesn't map to UTF-8, which is the de facto standard in the Rust world.
    Trying to support non-utf8 encodings seems more trouble than it's worth, IMO

  3. BurntSushi commented on Sep 22, 2016

    @BurntSushi
    OwnerAuthor

    @nabijaczleweli ripgrep isn't just for Rust people though. There are plenty of non-English speaking countries where UTF-8 isn't quite everywhere. There's enough folks that care about it that there are at least a couple search tools that do implement searching on files that aren't UTF-8. The Platinum Searcher and highway come to mind. Of course, both grep and git grep support other encodings as well. I'm also told that Windows has lots of UTF-16 data too.

    Anyway, it's totally possible that this may never happen. But it's also a somewhat feasible thing to support without sacrificing performance for the UTF-8 case (at the cost of some code complexity). It is honestly somewhat low on my list of things I'd like to do with ripgrep.

  4. BurntSushi commented on Sep 22, 2016

    @BurntSushi
    OwnerAuthor

    (To be clear, the limit to what I'd be willing to support are text encodings that can be feasibly transcoded to UTF-8.)

  5. alexandernst commented on Sep 23, 2016

    @alexandernst

    How do you plan to detect the encoding of the text that is being ripgrep-ed in order to correctly decode it (or transcode it to utf8)?

  6. BurntSushi commented on Sep 23, 2016

    @BurntSushi
    OwnerAuthor

    @alexandernst That is precisely one of the problems described in this issue. ;-)

  7. lambda commented on Sep 24, 2016

    @lambda
    Contributor

    Just yesterday, I was given a dump of log files from a Windows server, and took a while to figure out why I couldn't find the string that someone else reported as being in one of the files, when I was using grep -r to look for it. Took me a bit to figure out that while most of the files were in some ASCII compatible encoding, a few of them were in UTF-16.

    For a tool that does recursive grepping, a lot of times what you have is some big directory tree, and you don't really know what's in it, and so you're looking for some needle in that big haystack. Not knowing what's in it can include not knowing the encoding.

    Right now, what rg does is skip UTF-16 files as binary, and not report anything about them, so you wouldn't notice if you missed files due to this issue.

    I think you do want something that applies a heuristic to determine the encoding, though I'm not sure you want that by default.

    I think the way I would go about supporting this is by reporting files that could be text but are not valid UTF-8; so, those files which it's searching as invalid UTF-8, and those which it's detecting as binary but which could be UTF-16, UTF-16BE, or UTF-16LE, or possibly other encodings. If you don't at least report something, you won't know that you're missing something due to the encoding issue, but I'm not sure you'd want to do any kind of automatic encoding detection by default.

    Then you could add a flag that will do heuristic encoding detection, based on BOM for UTF-16, or even/odd nulls hack for UTF-16 BE or LE, or maybe even character frequency based detection for other encodings.

  8. BurntSushi commented on Sep 24, 2016

    @BurntSushi
    OwnerAuthor

    @lambda I agree we need to do something, but we are definitely limited. Things like, "report if a file isn't valid UTF-8" are probably off the table, because that would be way too expensive. We're probably limited to scanning the first few hundred/thousand bytes or so. Right now, it's a simple memchr for the NUL byte to determine if it's a "binary" file or not, to give you an idea of the scale. Reporting the existence of every binary file seems a bit... extreme. Hmm. I'll noodle on it.

  9. rr- commented on Sep 24, 2016

    @rr-

    To me the inability to rg --binary "\xDE\xAD\xBE\xEF" is an absolute deal breaker. I work with huge binary files a lot and I often want to search for raw byte strings. I don't want to use two tools at once (rg for text, ag for binary).

    Also grepping for anything in Japanese companies, be it source code or text documents, almost always means you deal with Shift-JIS.

    TBH I wouldn't expect rg to be able to detect encoding on its own, I'd be plenty happy with being able to manually guide it with --binary, --encoding=sjis etc.

  10. BurntSushi commented on Sep 24, 2016

    @BurntSushi
    OwnerAuthor

    @rr- You can do that, it's just not documented on ripgrep (it's buried inside the regex engine documentation, which obviously is not going to be discoverable to end users, I need to work on that). Try this instead:

    rg '(?-u)\xDE\xAD\xBE\xEF'
    

    If you did try this:

    rg '\xDE\xAD\xBE\xEF'
    

    Then each hex escape is treated as a Unicode codepoint with its corresponding UTF-8 encoding. But in the former case, we disable Unicode support and get raw byte support. Unicode can actually be selectively re-enabled, so if you wanted a Unicode aware \w in the middle of some bytes somewhere, this would work:

    rg '(?-u)\xDE\xAD(?u:\w+)\xBE\xEF'
    
  11. BurntSushi commented on Sep 24, 2016

    @BurntSushi
    OwnerAuthor

    But yes, I think everyone understands that only supporting UTF-8 (errm, ASCII compatible text encodings, technically) might be good enough for the vast majority of users, but is definitely going to be a deal breaker for many as well. This issue really doesn't need more motivation, it needs feasible ideas that can be implemented, and those need to take into account UX, performance and actual correctness. (Thanks @lambda for getting that started.)

  12. rr- commented on Sep 24, 2016

    @rr-

    OK how about this:

    1. Expose --encoding which defaults to utf-8
    2. Using --encoding=utf16le test internally encodes test as t\0e\0s\0t\0
    3. rg treats the file as binary and searches for this literal pattern as a sequence of bytes

    There are obviously going to be problems around UTF-8 ability to display ö as LATIN SMALL LETTER O WITH DIAERESIS and COMBINING DIAERESIS + LATIN SMALL LETTER O as well as inconsistencies in Shift-JIS encoding of characters such as REVERSE SOLIDUS but maybe it's good enough?

  13. BurntSushi commented on Sep 24, 2016

    @BurntSushi
    OwnerAuthor

    Well the huge problem with that is things like \w won't work. This is why I made the stipulation of a transcoding from X to UTF-8 and then matching. I mean, an explicit --encoding flag is basically an answer to this problem: it's exactly what I suggested way above. But it has downsides, like, what if your directory tree has some UTF-16 and some UTF-8?

    Certainly, we can throw our hands in the air, require specific instruction of encoding by the end user and call it a day. But it doesn't really help getting at the unknown-unknowns that @lambda mentioned. Making unknown-unknowns discoverable is an important feature IMO. But perhaps it is orthogonal to text encoding support.

  14. Nephyrin commented on Sep 24, 2016

    @Nephyrin

    There's some similar discussion of UTF-16 for Ag @ ggreer/the_silver_searcher#914

    Using byte-order-marks to detect UTF-other encodings may work. At some point, if there's no meta-data to indicate encoding, a tool can only be so-magical.

  15. 12 remaining items

  16. BurntSushi commented on Oct 29, 2016

    @BurntSushi
    OwnerAuthor

    I don't know what the UI should look like, but we need one. I would
    probably start by looking at iconv.

    On Oct 28, 2016 9:56 PM, "Eric Kidd" notifications@github.com wrote:

    Fair enough. :-)

    Yes, I agree it would be good to implement the transliteration machinery
    before the detection code! If there's no auto-detection, what should the UI
    for selecting an input encoding look like?

    As for automatic detection, looking at the source to the encoding
    detectors for the first time in a couple of years, the logic to detect the
    major encodings is surprisingly straightforward:

    1. If it has a BOM, use the BOM to identify it as UTF-8, UTF-16 or
      UTF-32. This is the only code path that will ever identify a file as UTF-16
      or UTF-32, interestingly enough.
    2. If it contains only ASCII characters, identify it as ASCII.
    3. If it contains ASCII plus 0xA0 (nbsp), supposedly a semi-common
      case, identify it as ISO Latin 1.
    4. If none of the above match, run a UTF-8 prober
      https://cgit.freedesktop.org/uchardet/uchardet/tree/src/nsUTF8Prober.cpp.
      When working on substudy, I found this this was usually overkill, because
      UTF-8 has a very distinctive and strict structure, and you can usually
      validate it simply by asking: "Does this contain at least one UTF-8
      character and does it parse as valid UTF-8?" If true, it's generally
      reasonable to assume UTF-8.

    This will generally produce decent results for ASCII and the various UTF
    encodings. Given your "pure Rust" requirement, it's probably wisest not to
    try for anything more right away. All the necessary language models, etc.,
    are available in uchardet, but there's a fair amount of fiddly code to
    handle all the weird encodings out there.

    —
    You are receiving this because you were mentioned.
    Reply to this email directly, view it on GitHub
    #1 (comment),
    or mute the thread
    https://github.com/notifications/unsubscribe-auth/AAb34n7FpwVKii93xQLcD-Me9EfNTg-Nks5q4qeygaJpZM4J5OAx
    .

  17. added a commit that references this issue on Mar 12, 2017
    8bbe58d
  18. ssokolow commented on Mar 14, 2017

    @ssokolow

    The Whirlwind tour section of the README still needs to be updated:

    One last thing before we get started: ripgrep assumes UTF-8 everywhere. It can still search files that are invalid UTF-8 (like, say, latin-1), but it will simply not work on UTF-16 encoded files or other more exotic encodings. Support for other encodings may happen.

  19. added a commit that references this issue on Mar 14, 2017
    46f9482
  20. BurntSushi commented on Mar 14, 2017

    @BurntSushi
    OwnerAuthor

    @ssokolow Fixed! Thanks.

  21. blfpd commented on Mar 24, 2017

    @blfpd

    @BurntSushi You might want to edit your anti-pitch on your blog post.

    If you need to search files with text encodings other than UTF-8 (like UTF-16), then ripgrep won’t work.

  22. BurntSushi commented on Mar 24, 2017

    @BurntSushi
    OwnerAuthor

    @batisteo Hmm right thanks, I always forget to update the blog.

  23. added a commit that references this issue on Mar 23, 2021
  24. added a commit that references this issue on Mar 13, 2023
  25. added a commit that references this issue on Nov 28, 2023
  26. added a commit that references this issue on Aug 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementquestion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions