sregx icon indicating copy to clipboard operation
sregx copied to clipboard

Not working as expected?

Open timnewsham opened this issue 2 months ago • 1 comments

The example from the README.md does not seem to be doing what I expect:

$ grep string README.md 
an input string and produces an output string. The following commands are
* `p`: prints the input string, and then returns the input string.
* `d`: returns the empty string.
* `c/<s>/`: returns the string `<s>`.
* `s/<p>/<s>/`: returns a string where substrings matching the regular
* `x/<p>/<cmd>`: returns a string where all substrings matching the regular
  to the particular substring.
* `y/<p>/<cmd>`: returns a string where each part of the string that is not
  unmatched string.
Print all lines that contain "string":
x/.*\n/ g/string/p
Delete all occurrences of "string" and print the result:
x/string/d | p
occurring in double or single quoted strings:
The syntax library supports parsing and compiling a string into a structural
  small language that has some string manipulation functions like `toupper`. A
$ sregx 'x/.*\n/ g/string/p' README.md 
[nothing]

Am I holding this wrong or is there a bug here?

timnewsham avatar Oct 31 '25 22:10 timnewsham

Output gets written to a buffer which is copied to the output before the command is executed and not after the command is executed:

diff --git a/cmd/sregx/main.go b/cmd/sregx/main.go
index 1a370c1..2c224d3 100644
--- a/cmd/sregx/main.go
+++ b/cmd/sregx/main.go
@@ -137,9 +137,8 @@ func main() {
                outputf = f
        }
 
-       io.Copy(outputf, output)
-
        out := cmds.Evaluate(data)
+       io.Copy(outputf, output)
        if !hasP(cmds) {
                _, err := outputf.Write(out)
                must(err)

timnewsham avatar Oct 31 '25 23:10 timnewsham