mirror of
https://github.com/wader/fq.git
synced 2024-12-24 05:41:38 +03:00
interp: find buffer should always use ByteRuneReader
Fixes grep and bgrep with buffer
This commit is contained in:
parent
d6d94842b5
commit
16d1f45de1
@ -841,7 +841,7 @@ func (i *Interp) find(c interface{}, a []interface{}) gojq.Iter {
|
||||
}
|
||||
|
||||
var re string
|
||||
var flags string
|
||||
var byteRunes bool
|
||||
|
||||
switch a0 := a[0].(type) {
|
||||
case string:
|
||||
@ -855,10 +855,11 @@ func (i *Interp) find(c interface{}, a []interface{}) gojq.Iter {
|
||||
for _, b := range reBuf {
|
||||
reRs = append(reRs, rune(b))
|
||||
}
|
||||
flags = "b"
|
||||
byteRunes = true
|
||||
re = string(reRs)
|
||||
}
|
||||
|
||||
var flags string
|
||||
if len(a) > 1 {
|
||||
flags, ok = a[1].(string)
|
||||
if !ok {
|
||||
@ -866,6 +867,10 @@ func (i *Interp) find(c interface{}, a []interface{}) gojq.Iter {
|
||||
}
|
||||
}
|
||||
|
||||
if strings.Contains(flags, "b") {
|
||||
byteRunes = true
|
||||
}
|
||||
|
||||
// TODO: err to string
|
||||
// TODO: extract to regexpextra? "all" FindReaderSubmatchIndex that can iter?
|
||||
sre, err := gojqextra.CompileRegexp(re, "gimb", flags)
|
||||
@ -885,7 +890,7 @@ func (i *Interp) find(c interface{}, a []interface{}) gojq.Iter {
|
||||
// raw bytes regexp matching is a bit tricky, what we do is to read each byte as a codepoint (ByteRuneReader)
|
||||
// and then we can use UTF-8 encoded codepoint to match a raw byte. So for example \u00ff (encoded as 0xc3 0xbf)
|
||||
// will match the byte \0xff
|
||||
if strings.Contains(flags, "b") {
|
||||
if byteRunes {
|
||||
// byte mode, read each byte as a rune
|
||||
rr = ioextra.ByteRuneReader{RS: bb}
|
||||
} else {
|
||||
|
8
pkg/interp/testdata/grep.fqtest
vendored
8
pkg/interp/testdata/grep.fqtest
vendored
@ -6,6 +6,10 @@ mp3> grep(44100, "ID", "^ID3$", "^ID.?$", "Info", "magic", "\u00ff", [0x49, 0x44
|
||||
0xe0| 50 | P |.frames[1].header.sample_rate: 44100
|
||||
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
||||
0x1b0| 52 | R |.frames[2].header.sample_rate: 44100
|
||||
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
||||
0x1c0| 11 4b 36 4a 08 83 58 c9| .K6J..X.|.frames[2].data: 114b364a088358c920d4295298c8c8f9...
|
||||
0x1d0|20 d4 29 52 98 c8 c8 f9 13 80 40 24 bc 91 23 42| .)R......@$..#B|
|
||||
* |until 0x279.7 (178) | |
|
||||
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
||||
0x0|49 44 33 |ID3 |.headers[0].magic: "ID3" (Correct)
|
||||
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
||||
@ -35,6 +39,10 @@ mp3> fgrep(44100, "ID", "^ID3$", "^ID.?$", "Info", "magic", "\u00ff", [0x49, 0x4
|
||||
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
||||
0x0|49 44 33 |ID3 |.headers[0].magic: "ID3" (Correct)
|
||||
mp3> bgrep(44100, "ID", "^ID3$", "^ID.?$", "Info", "magic", "\u00ff", [0x49, 0x44])
|
||||
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
||||
0x1c0| 11 4b 36 4a 08 83 58 c9| .K6J..X.|.frames[2].data: 114b364a088358c920d4295298c8c8f9...
|
||||
0x1d0|20 d4 29 52 98 c8 c8 f9 13 80 40 24 bc 91 23 42| .)R......@$..#B|
|
||||
* |until 0x279.7 (178) | |
|
||||
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
||||
0x0|49 44 33 |ID3 |.headers[0].magic: "ID3" (Correct)
|
||||
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
||||
|
Loading…
Reference in New Issue
Block a user