Giter Site home page Giter Site logo

Comments (3)

dkegel-fastly avatar dkegel-fastly commented on August 26, 2024

Here's a demo of what I'm trying to do. Not quite happy yet...

package main
  
import (
        "fmt"
        "regexp"
)

func main() {
        input := "aaa aaa"

        // Porting FindStringIndex() to fsm
        r := regexp.MustCompile(`a{2,3}`)
        match := r.FindStringIndex(input)
        fmt.Printf("regexp: r.FindStringIndex returns %d, %d\n", match[0], match[1])
        start, end := Match(input)
        fmt.Printf("fsm: Match returns %d, %d\n", start, end)

        // Porting ReplaceAll to fsm
        out := r.ReplaceAllString(input, "b")
        fmt.Printf("regexp: ReplaceAllString returns %s\n", out)
        out = myReplaceAll(input, "b")
        fmt.Printf("fsm: myReplaceAll returns %s\n", out)
}

// myReplaceAll finds all sequences in value that Match, and replaces them with repl.
// Any matches that might result from the replacement are ignored.
func myReplaceAll(value, repl string) string {
        remaining := value
        out := ""
        for remaining != "" {
                start, end := Match(remaining)
                if start == -1 {
                        out = out + remaining
                        remaining = ""
                } else {
                        println("match", remaining[start:end], "start", start, "end", end)
                        out = out + remaining[0:start] + repl
                        remaining = remaining[end:]
                }
        }
        return out
}

Save that as main.go, then create match.go with something like

re -k str -l go -r pcre 'a{2,3}' | sed 's/fsm_fsm/main/' > match.go

and run with

go run main.go match.go

Currently, it outputs

regexp: r.FindStringIndex returns 0, 3
fsm: Match returns 0, 2
regexp: ReplaceAllString returns b b
match aa start 0 end 2
match aa start 2 end 4
fsm: myReplaceAll returns ba ba

Examining the generated code, fsm expands the quantifier a{2,3} to return immediately after the 2nd a, without even trying to consume a 3rd one.
(I filed #358 for that earlier, but may not have provided enough context.)

This is a fine optimization for detecting a match, so evidently my hack to return the end offset of the match is breaking an assumption libfsm made, and/or I'm misusing libfsm in some other way.

from libfsm.

dhobsd avatar dhobsd commented on August 26, 2024

from libfsm.

dkegel-fastly avatar dkegel-fastly commented on August 26, 2024

Thanks. I updated the problem description to address your last suggestion about induced matches.
I'm not worried about the inefficiency of using string for the moment.

I am very interested in suggestions for coaxing libfsm into actually finishing the match of a{2,3} instead
of calling it a day after aa.

from libfsm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.