php2go/quote.go

27 lines
532 B
Go
Raw Normal View History

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
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")
}