2020-04-07 10:51:31 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2020-04-16 06:38:05 +00:00
|
|
|
"fmt"
|
2020-04-07 10:51:31 +00:00
|
|
|
"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-16 06:38:05 +00:00
|
|
|
|
|
|
|
// PROBLEM 1: PHP has some lax parsing for unknown escape sequences
|
2020-04-12 02:39:38 +00:00
|
|
|
// e.g. regex("foo\.") will be silently parsed as regex("foo.")
|
|
|
|
|
2020-04-16 06:38:05 +00:00
|
|
|
// PROBLEM 2: PHP supports multiline string literals, strconv.Unquote does not
|
|
|
|
// Apply fixup
|
|
|
|
s2 := strings.Replace(strings.Replace(s, "\r", `\r`, -1), "\n", `\n`, -1)
|
|
|
|
|
|
|
|
ret, err := strconv.Unquote(s2)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Parsing string literal `%s`: %v", s, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
2020-04-07 10:51:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|