mirror of
https://github.com/johnkerl/miller.git
synced 2026-08-03 21:13:09 +00:00
Accept some now-passing regex cases
This commit is contained in:
parent
5c738cc6e2
commit
696f85d96a
21 changed files with 16 additions and 39 deletions
|
|
@ -1,3 +1,5 @@
|
|||
// xxx update comment
|
||||
|
||||
package lib
|
||||
|
||||
// TODO:
|
||||
|
|
@ -6,23 +8,6 @@ package lib
|
|||
// o UTs for that
|
||||
// * flesh out RegexCaptureBinaryFunctionCallsiteNode to do that
|
||||
|
||||
// xxx update comment
|
||||
|
||||
// The key is the Go library's regex.FindAllStringIndex. It gives us start
|
||||
// (inclusive) and end (exclusive) indices for matches.
|
||||
//
|
||||
// Example: for pattern "foo" and input "abc foo def foo ghi" we'll have
|
||||
// matrix [[4 7] [12 15]] which indicates matches from positions 4-6 and
|
||||
// 12-14. We simply need to concatenate
|
||||
// * 0-3 "abc " not matching
|
||||
// * 4-6 "foo" matching
|
||||
// * 7-11 " def " not matching
|
||||
// * 12-14 "foo" matching
|
||||
// * 15-18 " ghi" not matching
|
||||
//
|
||||
// Example: with pattern "f.*o" and input "abc foo def foo ghi" we'll have
|
||||
// matrix [[4 15]] so "foo def foo" will be a matched substring.
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
|
|
@ -88,6 +73,7 @@ func CompileMillerRegexOrDie(regexString string) *regexp.Regexp {
|
|||
// xxx MakeEmptyCaptures function for CST state
|
||||
|
||||
// xxx ReplacementHasCaptures function
|
||||
// xxx ReplacementMatrixExtractor ...
|
||||
|
||||
// RegexSubWithoutCaptures implements the sub DSL function when the replacement
|
||||
// string has none of "\1".."\9".
|
||||
|
|
@ -97,12 +83,12 @@ func RegexSubWithoutCaptures(
|
|||
replacement string,
|
||||
) string {
|
||||
regex := CompileMillerRegexOrDie(sregex)
|
||||
// to do
|
||||
return RegexSubWithoutCapturesCompiled(input, regex, replacement)
|
||||
}
|
||||
|
||||
// RegexSubWithoutCapturesCompiled is the same as RegexSubWithoutCaptures but
|
||||
// with compiled regex instead of regex-as-string.
|
||||
// with compiled regex instead of regex-as-string. This is a simple use of
|
||||
// the Go regexp library.
|
||||
func RegexSubWithoutCapturesCompiled(
|
||||
input string,
|
||||
regex *regexp.Regexp,
|
||||
|
|
@ -140,8 +126,9 @@ func RegexSubCompiledWithCaptures(
|
|||
return regexSubGsubWithCapturesAux(input, regex, replacement, true)
|
||||
}
|
||||
|
||||
// RegexGsubWithoutCaptures implements the gsub DSL function when the replacement
|
||||
// string has none of "\1".."\9".
|
||||
// RegexGsubWithoutCaptures implements the gsub DSL function when the
|
||||
// replacement string has none of "\1".."\9". This is a simple use of the Go
|
||||
// regexp library.
|
||||
func RegexGsubWithoutCaptures(
|
||||
input string,
|
||||
sregex string,
|
||||
|
|
@ -179,7 +166,13 @@ func RegexGsubWithCaptures(
|
|||
// and the =~ callsite doesn't know if captures will be used or not. So,
|
||||
// RegexMatches always returns the captures array. It is stored within the CST
|
||||
// state.
|
||||
func RegexMatches(input string, sregex string) (matches bool, capturesOneUp []string) {
|
||||
func RegexMatches(
|
||||
input string,
|
||||
sregex string,
|
||||
) (
|
||||
matches bool,
|
||||
capturesOneUp []string,
|
||||
) {
|
||||
regex := CompileMillerRegexOrDie(sregex)
|
||||
return regexMatchesAux(input, regex)
|
||||
}
|
||||
|
|
@ -187,18 +180,6 @@ func RegexMatches(input string, sregex string) (matches bool, capturesOneUp []st
|
|||
// ----------------------------------------------------------------
|
||||
// Package-internal/implementation functions
|
||||
|
||||
// xxx:
|
||||
// $ go run foo.go "ab_cde ab_cde" "(ab)_(cde)"
|
||||
// MATRIX [][]int{[]int{0, 6, 0, 2, 3, 6}, []int{8, 14, 8, 10, 11, 14}}
|
||||
// 0 6 "ab_cde"
|
||||
// n:6
|
||||
// 2 0 2 "ab"
|
||||
// 4 3 6 "cde"
|
||||
// 8 14 "ab_cde"
|
||||
// n:6
|
||||
// 2 8 10 "ab"
|
||||
// 4 11 14 "cde"
|
||||
|
||||
// regexSubGsubWithCapturesAux is the implementation for sub/gsub when the
|
||||
// replacement string uses captures in the form "\1".."\9".
|
||||
func regexSubGsubWithCapturesAux(
|
||||
|
|
@ -265,7 +246,6 @@ func regexSubGsubWithCapturesAux(
|
|||
// "..." -> "..."
|
||||
// "fg_hij" -> "hij:fg" --- and here
|
||||
// "..." -> "..."
|
||||
//
|
||||
interpolateCaptures(
|
||||
replacement,
|
||||
// TODO: move to caller where it can be precomputed and stored
|
||||
|
|
@ -361,10 +341,7 @@ func regexMatchesAux(
|
|||
// * 6-9 is for the second capture "cde"
|
||||
|
||||
di := 1
|
||||
for si := 2; si < n; si += 2 {
|
||||
if di > 9 {
|
||||
break
|
||||
}
|
||||
for si := 2; si < n && di <= 9; si += 2 {
|
||||
start := row[si]
|
||||
end := row[si+1]
|
||||
captures[di] = input[start:end]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue