-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
std_strpad.go
54 lines (44 loc) · 1.49 KB
/
std_strpad.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Code generated by __generator__/interpreter.go at once
package builtin
import (
"math"
"strings"
"github.com/ysugimoto/falco/interpreter/context"
"github.com/ysugimoto/falco/interpreter/function/errors"
"github.com/ysugimoto/falco/interpreter/value"
)
const Std_strpad_Name = "std.strpad"
var Std_strpad_ArgumentTypes = []value.Type{value.StringType, value.IntegerType, value.StringType}
func Std_strpad_Validate(args []value.Value) error {
if len(args) != 3 {
return errors.ArgumentNotEnough(Std_strpad_Name, 3, args)
}
for i := range args {
if args[i].Type() != Std_strpad_ArgumentTypes[i] {
return errors.TypeMismatch(Std_strpad_Name, i+1, Std_strpad_ArgumentTypes[i], args[i].Type())
}
}
return nil
}
// Fastly built-in function implementation of std.strpad
// Arguments may be:
// - STRING, INTEGER, STRING
// Reference: https://developer.fastly.com/reference/vcl/functions/strings/std-strpad/
func Std_strpad(ctx *context.Context, args ...value.Value) (value.Value, error) {
// Argument validations
if err := Std_strpad_Validate(args); err != nil {
return value.Null, err
}
s := value.Unwrap[*value.String](args[0]).Value
width := value.Unwrap[*value.Integer](args[1]).Value
pad := value.Unwrap[*value.String](args[2]).Value
w := int(math.Abs(float64(width)))
if len(s) >= w {
return &value.String{Value: s}, nil
}
p := strings.Repeat(pad, w-len(s))
if width < 0 {
return &value.String{Value: s + p[0:w-len(s)]}, nil
}
return &value.String{Value: p[0:w-len(s)] + s}, nil
}