node: implement sprintf, trim, rtrim, ltrim, strtolower, strtoupper, file_put_contents, str_replace

This commit is contained in:
mappu 2020-04-11 13:11:39 +12:00
parent 1188a769ce
commit f6362c4ebf

78
node.go
View File

@ -1006,7 +1006,7 @@ func (this *conversionState) convertNoFreeFloating(n_ node.Node) (string, error)
} else if funcName == `explode` { } else if funcName == `explode` {
if len(callParams) != 2 { if len(callParams) != 2 {
return "", parseErr{n, fmt.Errorf("Call to \\explode() should have 2 arguments, got %d", len(callParams))} return "", parseErr{n, fmt.Errorf("Call to '%s' expected 2 argument(s), got %d", funcName, len(callParams))}
} }
this.importPackages["strings"] = struct{}{} this.importPackages["strings"] = struct{}{}
funcName = `strings.Split` funcName = `strings.Split`
@ -1014,13 +1014,87 @@ func (this *conversionState) convertNoFreeFloating(n_ node.Node) (string, error)
} else if funcName == `implode` { } else if funcName == `implode` {
if len(callParams) != 2 { if len(callParams) != 2 {
return "", parseErr{n, fmt.Errorf("Call to \\implode() should have 2 arguments, got %d", len(callParams))} return "", parseErr{n, fmt.Errorf("Call to '%s' expected 2 argument(s), got %d", funcName, len(callParams))}
} }
this.importPackages["strings"] = struct{}{} this.importPackages["strings"] = struct{}{}
funcName = `strings.Join` funcName = `strings.Join`
callParams[0], callParams[1] = callParams[1], callParams[0] // need to reverse function argument order callParams[0], callParams[1] = callParams[1], callParams[0] // need to reverse function argument order
} else if funcName == `sprintf` {
this.importPackages["fmt"] = struct{}{}
funcName = `fmt.Sprintf`
} else if funcName == `trim` {
if len(callParams) != 1 {
return "", parseErr{n, fmt.Errorf("Call to '%s' expected 2 argument(s), got %d", funcName, len(callParams))}
}
this.importPackages["strings"] = struct{}{}
funcName = `strings.TrimSpace`
} else if funcName == `rtrim` {
if len(callParams) != 1 {
return "", parseErr{n, fmt.Errorf("Call to '%s' expected 1 argument(s), got %d", funcName, len(callParams))}
}
this.importPackages["strings"] = struct{}{}
this.importPackages["unicode"] = struct{}{}
funcName = `strings.TrimRightFunc`
callParams = append(callParams, `unicode.IsSpace`)
} else if funcName == `ltrim` {
if len(callParams) != 1 {
return "", parseErr{n, fmt.Errorf("Call to '%s' expected 1 argument(s), got %d", funcName, len(callParams))}
}
this.importPackages["strings"] = struct{}{}
this.importPackages["unicode"] = struct{}{}
funcName = `strings.TrimLeftFunc`
callParams = append(callParams, `unicode.IsSpace`)
} else if funcName == `strtolower` {
if len(callParams) != 1 {
return "", parseErr{n, fmt.Errorf("Call to '%s' expected 2 argument(s), got %d", funcName, len(callParams))}
}
this.importPackages["strings"] = struct{}{}
funcName = `strings.ToLower`
} else if funcName == `strtoupper` {
if len(callParams) != 1 {
return "", parseErr{n, fmt.Errorf("Call to '%s' expected 2 argument(s), got %d", funcName, len(callParams))}
}
this.importPackages["strings"] = struct{}{}
funcName = `strings.ToUpper`
} else if funcName == `file_put_contents` {
if len(callParams) != 2 {
return "", parseErr{n, fmt.Errorf("Call to '%s' expected 2 argument(s), got %d", funcName, len(callParams))}
}
this.importPackages["io/ioutil"] = struct{}{}
funcName = `ioutil.WriteFile` // (string filename, []byte data, int perm)
callParams[1] = `[]byte(` + callParams[1] + `)`
callParams = append(callParams, `0644`) // default file permissions
} else if funcName == `str_replace` {
// str_replace( mixed $search , mixed $replace , mixed $subject [, int &$count ] )
// n.b. str_replace also supports arrays, that strings.Replace() doesn't do
if !(len(callParams) == 3 || len(callParams) == 4) {
return "", parseErr{n, fmt.Errorf("Call to '%s' expected 3 or 4 argument(s), got %d", funcName, len(callParams))}
}
this.importPackages["strings"] = struct{}{}
funcName = `strings.Replace` // (s, old, new string, n int) string
callParams[0], callParams[1], callParams[2] = callParams[2], callParams[0], callParams[1]
if len(callParams) == 3 {
callParams = append(callParams, `-1`) // replaceAll
// There is a strings.ReplaceAll() added in go1.12 (2018), which is still pretty recent i.e. not in Debian Stable (buster)
}
} }
return funcName + "(" + strings.Join(callParams, ", ") + ")", nil // expr only, no semicolon/newline return funcName + "(" + strings.Join(callParams, ", ") + ")", nil // expr only, no semicolon/newline