You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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
@nabijaczleweliripgrep 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.
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.
@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.
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.
@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'
Reacted by Dash, matthieu-m, Vladislav Isenbaev, Dave Goodell and Courtney Hall
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.)
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.
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.
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:
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.
If it contains only ASCII characters, identify it as ASCII.
If it contains ASCII plus 0xA0 (nbsp), supposedly a semi-common
case, identify it as ISO Latin 1.
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.
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.
Activity
nabijaczleweli commentedon Sep 22, 2016
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
BurntSushi commentedon Sep 22, 2016
@nabijaczleweli
ripgrepisn'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, bothgrepandgit grepsupport 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.BurntSushi commentedon Sep 22, 2016
(To be clear, the limit to what I'd be willing to support are text encodings that can be feasibly transcoded to UTF-8.)
alexandernst commentedon Sep 23, 2016
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)?
BurntSushi commentedon Sep 23, 2016
@alexandernst That is precisely one of the problems described in this issue. ;-)
lambda commentedon Sep 24, 2016
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 -rto 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
rgdoes 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.
BurntSushi commentedon Sep 24, 2016
@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
memchrfor 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.rr- commentedon Sep 24, 2016
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 (rgfor text,agfor 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
rgto be able to detect encoding on its own, I'd be plenty happy with being able to manually guide it with--binary,--encoding=sjisetc.BurntSushi commentedon Sep 24, 2016
@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:If you did try this:
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
\win the middle of some bytes somewhere, this would work:BurntSushi commentedon Sep 24, 2016
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.)
rr- commentedon Sep 24, 2016
OK how about this:
--encodingwhich defaults to utf-8--encoding=utf16le testinternally encodestestast\0e\0s\0t\0rgtreats the file as binary and searches for this literal pattern as a sequence of bytesThere are obviously going to be problems around UTF-8 ability to display ö as
LATIN SMALL LETTER O WITH DIAERESISandCOMBINING DIAERESIS+LATIN SMALL LETTER Oas well as inconsistencies in Shift-JIS encoding of characters such asREVERSE SOLIDUSbut maybe it's good enough?BurntSushi commentedon Sep 24, 2016
Well the huge problem with that is things like
\wwon't work. This is why I made the stipulation of a transcoding fromXtoUTF-8and then matching. I mean, an explicit--encodingflag 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.
Nephyrin commentedon Sep 24, 2016
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.
12 remaining items
BurntSushi commentedon Oct 29, 2016
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:
Add support for additional text encodings.
ssokolow commentedon Mar 14, 2017
The Whirlwind tour section of the README still needs to be updated:
Update whirlwind tour with encoding info.
BurntSushi commentedon Mar 14, 2017
@ssokolow Fixed! Thanks.
blfpd commentedon Mar 24, 2017
@BurntSushi You might want to edit your anti-pitch on your blog post.
BurntSushi commentedon Mar 24, 2017
@batisteo Hmm right thanks, I always forget to update the blog.
searcher: bump buffer size
Merge pull request #1 from gcxfd/master
Merge pull request BurntSushi#1 from BurntSushi/master
Merge pull request BurntSushi#1 from anysphere/lukas/parallel-sort-rg