Skip to content

Commit

Permalink
runes() can take optional boolean to request the array to be of integ…
Browse files Browse the repository at this point in the history
…ers instead of strings (#249)
  • Loading branch information
ldemailly authored Sep 27, 2024
1 parent c147fc0 commit f2d997c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
17 changes: 13 additions & 4 deletions extensions/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,22 +326,31 @@ func createJSONAndEvalFunctions(c *Config) {
func createStrFunctions() { //nolint:funlen // we do have quite a few, yes.
strFn := object.Extension{
MinArgs: 1,
MaxArgs: 1,
ArgTypes: []object.Type{object.STRING},
MaxArgs: 2,
ArgTypes: []object.Type{object.STRING, object.BOOLEAN},
}
strFn.Name = "runes" // like explode.gr explodeRunes but go side and not recursive.
strFn.Name = "runes" // like explode.gr explodeRunes but go side and not recursive. option to get as int array.
strFn.Help = "optionally as array of integers"
strFn.Callback = func(_ any, _ string, args []object.Object) object.Object {
inp := args[0].(object.String).Value
asInt := len(args) == 2 && args[1].(object.Boolean).Value
gorunes := []rune(inp)
l := len(gorunes)
object.MustBeOk(l)
runes := make([]object.Object, l)
for i, r := range gorunes {
runes[i] = object.String{Value: string(r)}
if asInt {
runes[i] = object.Integer{Value: int64(r)}
} else {
runes[i] = object.String{Value: string(r)}
}
}
return object.NewArray(runes)
}
MustCreate(strFn)
strFn.MaxArgs = 1
strFn.Help = ""
strFn.ArgTypes = []object.Type{object.STRING}
strFn.Name = "rune_len"
strFn.Callback = func(_ any, _ string, args []object.Object) object.Object {
return object.Integer{Value: int64(utf8.RuneCountInString(args[0].(object.String).Value))}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
fortio.org/safecast v1.0.0
fortio.org/sets v1.2.0
fortio.org/struct2env v0.4.1
fortio.org/terminal v0.17.1
fortio.org/terminal v0.19.0
fortio.org/testscript v0.3.2 // only for tests
fortio.org/version v1.0.4
github.com/rivo/uniseg v0.4.7
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ fortio.org/struct2env v0.4.1 h1:rJludAMO5eBvpWplWEQNqoVDFZr4RWMQX7RUapgZyc0=
fortio.org/struct2env v0.4.1/go.mod h1:lENUe70UwA1zDUCX+8AsO663QCFqYaprk5lnPhjD410=
fortio.org/term v0.23.0-fortio-6 h1:pKrUX0tKOxyEhkhLV50oJYucTVx94rzFrXc24lIuLvk=
fortio.org/term v0.23.0-fortio-6/go.mod h1:7buBfn81wEJUGWiVjFNiUE/vxWs5FdM9c7PyZpZRS30=
fortio.org/terminal v0.17.1 h1:s8Nv4l1eRXZ4cYhKwUCwmtA1sziHV2i02TY0FpZG434=
fortio.org/terminal v0.17.1/go.mod h1:iQz9usYEwGYvGPQ//qAEJkWcDLhUyhpVzFBRYGL53MA=
fortio.org/terminal v0.19.0 h1:17DmldS0SaBohVFFUVNgxEuj/erOxa6dZqc6rHNisI8=
fortio.org/terminal v0.19.0/go.mod h1:wZsEDVhPvnI3I8KNyy+cDbUsIMeh86NeP96Z6ku/hKs=
fortio.org/testscript v0.3.2 h1:ks5V+Y6H6nmeGqnVlZuLdiFwpqXemDkEnyGgCZa/ZNA=
fortio.org/testscript v0.3.2/go.mod h1:Z2kUvEDHYETV8FLxsdA6zwSZ8sZUiTNJh2Dw5c4a3Pg=
fortio.org/version v1.0.4 h1:FWUMpJ+hVTNc4RhvvOJzb0xesrlRmG/a+D6bjbQ4+5U=
Expand Down

0 comments on commit f2d997c

Please sign in to comment.