From f6362c4ebfc4482b0538f24630e358544ddadc08 Mon Sep 17 00:00:00 2001 From: mappu Date: Sat, 11 Apr 2020 13:11:39 +1200 Subject: [PATCH] node: implement sprintf, trim, rtrim, ltrim, strtolower, strtoupper, file_put_contents, str_replace --- node.go | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 76 insertions(+), 2 deletions(-) diff --git a/node.go b/node.go index 7acb79a..b47915a 100644 --- a/node.go +++ b/node.go @@ -1006,7 +1006,7 @@ func (this *conversionState) convertNoFreeFloating(n_ node.Node) (string, error) } else if funcName == `explode` { 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{}{} funcName = `strings.Split` @@ -1014,13 +1014,87 @@ func (this *conversionState) convertNoFreeFloating(n_ node.Node) (string, error) } else if funcName == `implode` { 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{}{} funcName = `strings.Join` 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