2020-04-07 10:51:31 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func phpUnquote(s string) (string, error) {
|
|
|
|
if len(s) == 0 {
|
|
|
|
return "", errors.New("expected quotes around string")
|
|
|
|
}
|
|
|
|
|
|
|
|
if s[0] == '"' {
|
|
|
|
// Quote system is similar enough to Go's that strconv will probably just work
|
2020-04-12 02:39:38 +00:00
|
|
|
// PHP has some lax parsing for unknown escape sequences
|
|
|
|
// e.g. regex("foo\.") will be silently parsed as regex("foo.")
|
|
|
|
|
2020-04-07 10:51:31 +00:00
|
|
|
return strconv.Unquote(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s[0] == '\'' {
|
|
|
|
// Single-quotes escape fewer things
|
|
|
|
unSQuo := strings.NewReplacer(`\'`, `'`, `\\`, `\`)
|
|
|
|
return unSQuo.Replace(s[1 : len(s)-1]), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", errors.New("expected single/double quotes")
|
|
|
|
}
|