-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
std_strtof.go
88 lines (76 loc) · 2.59 KB
/
std_strtof.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Code generated by __generator__/interpreter.go at once
package builtin
import (
"regexp"
"strconv"
"strings"
"github.com/ysugimoto/falco/interpreter/context"
"github.com/ysugimoto/falco/interpreter/function/errors"
"github.com/ysugimoto/falco/interpreter/value"
)
const Std_strtof_Name = "std.strtof"
var Std_strtof_ArgumentTypes = []value.Type{value.StringType, value.IntegerType}
func Std_strtof_Validate(args []value.Value) error {
if len(args) != 2 {
return errors.ArgumentNotEnough(Std_strtof_Name, 2, args)
}
for i := range args {
if args[i].Type() != Std_strtof_ArgumentTypes[i] {
return errors.TypeMismatch(Std_strtof_Name, i+1, Std_strtof_ArgumentTypes[i], args[i].Type())
}
}
return nil
}
func Std_strtof_Decimal(s string) (value.Value, error) {
f, err := strconv.ParseFloat(s, 64)
if err != nil {
return value.Null, errors.New(Std_strtof_Name, "Failed to parse string to decimal float: %s", s)
}
return &value.Float{Value: f}, nil
}
var hexMantissaSuffix = regexp.MustCompile(`p[+-][0-9]+$`)
func Std_strtof_Hex(s string) (value.Value, error) {
// To convert hex float string correctly in Golang, mantissa suffix is needed
if !hexMantissaSuffix.MatchString(s) {
s += "p0"
}
f, err := strconv.ParseFloat(s, 64)
if err != nil {
return value.Null, errors.New(Std_strtof_Name, "Failed to parse string to hex float: %s", s)
}
return &value.Float{Value: f}, nil
}
// Fastly built-in function implementation of std.strtof
// Arguments may be:
// - STRING, INTEGER
// Reference: https://developer.fastly.com/reference/vcl/functions/strings/std-strtof/
func Std_strtof(ctx *context.Context, args ...value.Value) (value.Value, error) {
// Argument validations
if err := Std_strtof_Validate(args); err != nil {
return value.Null, err
}
s := value.Unwrap[*value.String](args[0]).Value
base := value.Unwrap[*value.Integer](args[1]).Value
switch base {
case 0:
if strings.HasPrefix(s, "0x") {
return Std_strtof_Hex(s)
}
return Std_strtof_Decimal(s)
case 10:
if strings.HasPrefix(s, "0x") {
ctx.FastlyError = &value.String{Value: "EPARSENUM"}
return value.Null, errors.New(Std_strtof_Name, "string must not have 0x prefix of when base number is 10")
}
return Std_strtof_Decimal(s)
case 16:
if !strings.HasPrefix(s, "0x") {
ctx.FastlyError = &value.String{Value: "EPARSENUM"}
return value.Null, errors.New(Std_strtof_Name, "string must have 0x prefix of when base number is 16")
}
return Std_strtof_Hex(s)
default:
ctx.FastlyError = &value.String{Value: "EPARSENUM"}
return value.Null, errors.New(Std_strtof_Name, "Base number accepts only 0, 10 and 16")
}
}