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
package main
import (
"fmt"
"regexp"
)
func main() {
for _, p := range []string{"foo", "^foo"} {
prefix, _ := regexp.MustCompile(p).LiteralPrefix()
fmt.Printf("prefix = '%v'\n", prefix)
}
}
What did you expect to see?
The documentation for LiteralPrefix says:
LiteralPrefix returns a literal string that must begin any match of the regular expression re. It returns the boolean true if the literal string comprises the entire regular expression.
So I expected to see "foo" in both cases, since every any match of the regular expression ^foo begins with the literal "foo".
I opened a separate issue because I wanted to restrict the discussion in #30425 only to the change in behavior between Go versions and what to do about that.
This issue questions the documented semantics of LiteralPrefix in general, compared to its actual behavior. Your new comment says that the 1.2 behavior is the correct one, which sounds logical. However, this means:
"^foo" : prefix="" complete=false
While the documentation of LiteralPrefix says:
LiteralPrefix returns a literal string that must begin any match of the regular expression re. It returns the boolean true if the literal string comprises the entire regular expression.
Any match of "^foo" must begin with foo, so on the face of it, the returned prefix="" seems surprising. Would you disagree?
We could update the comment on LiteralPrefix to make the distinction for anchored regexes, which would bring it in line with the actual behavior. Or we could make LiteralPrefix return foo for "^foo", but complete=false.
If you prefer to discuss everything in #30425, no problem, I can close this issue.
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
What did you expect to see?
The documentation for
LiteralPrefix
says:So I expected to see "foo" in both cases, since every any match of the regular expression
^foo
begins with the literal "foo".What did you see instead?
In #21463 (comment), @rsc says:
Also, see discussion in #30425
The text was updated successfully, but these errors were encountered: