30 lines
658 B
Go
30 lines
658 B
Go
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
|
|
// PHP has some lax parsing for unknown escape sequences
|
|
// e.g. regex("foo\.") will be silently parsed as regex("foo.")
|
|
|
|
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")
|
|
}
|