mirror of
https://github.com/google/go-jsonnet.git
synced 2025-09-28 17:01:02 +02:00
Add std.encodeUTF8 and std.decodeUTF8
This commit is contained in:
parent
7af3b8496c
commit
c1743b9a2a
34
builtins.go
34
builtins.go
@ -575,6 +575,38 @@ func builtinMd5(i *interpreter, trace TraceElement, x value) (value, error) {
|
||||
return makeValueString(hex.EncodeToString(hash[:])), nil
|
||||
}
|
||||
|
||||
func builtinEncodeUTF8(i *interpreter, trace TraceElement, x value) (value, error) {
|
||||
str, err := i.getString(x, trace)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s := str.getString()
|
||||
elems := make([]*cachedThunk, 0, len(s)) // it will be longer if characters fall outside of ASCII
|
||||
for _, c := range []byte(s) {
|
||||
elems = append(elems, readyThunk(makeValueNumber(float64(c))))
|
||||
}
|
||||
return makeValueArray(elems), nil
|
||||
}
|
||||
|
||||
func builtinDecodeUTF8(i *interpreter, trace TraceElement, x value) (value, error) {
|
||||
arr, err := i.getArray(x, trace)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bs := make([]byte, len(arr.elements)) // it will be longer if characters fall outside of ASCII
|
||||
for pos := range arr.elements {
|
||||
v, err := i.evaluateInt(arr.elements[pos], trace)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if v < 0 || v > 255 {
|
||||
return nil, i.Error(fmt.Sprintf("Bytes must be integers in range [0, 255], got %d", v), trace)
|
||||
}
|
||||
bs[pos] = byte(v)
|
||||
}
|
||||
return makeValueString(string(bs)), nil
|
||||
}
|
||||
|
||||
// Maximum allowed unicode codepoint
|
||||
// https://en.wikipedia.org/wiki/Unicode#Architecture_and_terminology
|
||||
const codepointMax = 0x10FFFF
|
||||
@ -1016,6 +1048,8 @@ var funcBuiltins = buildBuiltinMap([]builtin{
|
||||
&unaryBuiltin{name: "md5", function: builtinMd5, parameters: ast.Identifiers{"x"}},
|
||||
&ternaryBuiltin{name: "strReplace", function: builtinStrReplace, parameters: ast.Identifiers{"str", "from", "to"}},
|
||||
&unaryBuiltin{name: "parseJson", function: builtinParseJSON, parameters: ast.Identifiers{"str"}},
|
||||
&unaryBuiltin{name: "encodeUTF8", function: builtinEncodeUTF8, parameters: ast.Identifiers{"str"}},
|
||||
&unaryBuiltin{name: "decodeUTF8", function: builtinDecodeUTF8, parameters: ast.Identifiers{"arr"}},
|
||||
&unaryBuiltin{name: "native", function: builtinNative, parameters: ast.Identifiers{"x"}},
|
||||
|
||||
// internal
|
||||
|
7
testdata/decodeUTF8.golden
vendored
Normal file
7
testdata/decodeUTF8.golden
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
[
|
||||
"",
|
||||
"A",
|
||||
"AAA",
|
||||
"§",
|
||||
"zażółć geślą jaźń"
|
||||
]
|
7
testdata/decodeUTF8.jsonnet
vendored
Normal file
7
testdata/decodeUTF8.jsonnet
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
[
|
||||
std.decodeUTF8([]),
|
||||
std.decodeUTF8([65]),
|
||||
std.decodeUTF8([65,65,65]),
|
||||
std.decodeUTF8([194,167]),
|
||||
std.decodeUTF8([122,97,197,188,195,179,197,130,196,135,32,103,101,197,155,108,196,133,32,106,97,197,186,197,132]),
|
||||
]
|
42
testdata/encodeUTF8.golden
vendored
Normal file
42
testdata/encodeUTF8.golden
vendored
Normal file
@ -0,0 +1,42 @@
|
||||
[
|
||||
[ ],
|
||||
[
|
||||
65
|
||||
],
|
||||
[
|
||||
65,
|
||||
65,
|
||||
65
|
||||
],
|
||||
[
|
||||
194,
|
||||
167
|
||||
],
|
||||
[
|
||||
122,
|
||||
97,
|
||||
197,
|
||||
188,
|
||||
195,
|
||||
179,
|
||||
197,
|
||||
130,
|
||||
196,
|
||||
135,
|
||||
32,
|
||||
103,
|
||||
101,
|
||||
197,
|
||||
155,
|
||||
108,
|
||||
196,
|
||||
133,
|
||||
32,
|
||||
106,
|
||||
97,
|
||||
197,
|
||||
186,
|
||||
197,
|
||||
132
|
||||
]
|
||||
]
|
7
testdata/encodeUTF8.jsonnet
vendored
Normal file
7
testdata/encodeUTF8.jsonnet
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
[
|
||||
std.encodeUTF8(''),
|
||||
std.encodeUTF8('A'),
|
||||
std.encodeUTF8('AAA'),
|
||||
std.encodeUTF8('§'),
|
||||
std.encodeUTF8('zażółć geślą jaźń'),
|
||||
]
|
Loading…
x
Reference in New Issue
Block a user