hcl2: add strlen function and update docs. (#14463)

This commit is contained in:
James Rasell 2022-09-06 18:42:40 +02:00 committed by GitHub
parent 962b1f78e8
commit 813c5daa96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 47 additions and 21 deletions

3
.changelog/14463.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:improvement
jobspec: Added `strlen` HCL2 function to determine the length of a string
```

View File

@ -81,6 +81,7 @@ func Functions(basedir string, allowFS bool) map[string]function.Function {
"slice": stdlib.SliceFunc,
"sort": stdlib.SortFunc,
"split": stdlib.SplitFunc,
"strlen": stdlib.StrlenFunc,
"strrev": stdlib.ReverseFunc,
"substr": stdlib.SubstrFunc,
"timeadd": stdlib.TimeAddFunc,

View File

@ -1,15 +1,15 @@
---
layout: docs
page_title: length - Functions - Configuration Language
description: The length function determines the length of a collection or string.
description: The length function determines the length of a collection.
---
# `length` Function
`length` determines the length of a given list, map, or string.
`length` determines the length of a given list, map, or tuple.
If given a list or map, the result is the number of elements in that collection.
If given a string, the result is the number of characters in the string.
If given a list, map, or tuple, the result is the number of elements in that
collection.
## Examples
@ -20,21 +20,4 @@ If given a string, the result is the number of characters in the string.
2
> length({"a" = "b"})
1
> length("hello")
5
```
When given a string, the result is the number of characters, rather than the
number of bytes or Unicode sequences that form them:
```shell-session
> length("👾🕹️")
2
```
A "character" is a _grapheme cluster_, as defined by
[Unicode Standard Annex #29](http://unicode.org/reports/tr29/). Note that
remote APIs may have a different definition of "character" for the purpose of
length limits on string arguments; a Nomad provider is responsible for
translating Nomad's string representation into that used by its respective
remote system and applying any additional validation rules to it.

View File

@ -0,0 +1,35 @@
---
layout: docs
page_title: strlen - Functions - Configuration Language
description: The strlen function determines the length of a string.
---
# `strlen` Function
`strlen` determines the length of a given string.
```hcl
strlen(string)
```
## Examples
```shell-session
> length("hello")
5
```
When given a string, the result is the number of characters, rather than the
number of bytes or Unicode sequences that form them:
```shell-session
> length("👾🕹️")
2
```
A "character" is a _grapheme cluster_, as defined by
[Unicode Standard Annex #29](http://unicode.org/reports/tr29/). Note that
remote APIs may have a different definition of "character" for the purpose of
length limits on string arguments; a Nomad provider is responsible for
translating Nomad's string representation into that used by its respective
remote system and applying any additional validation rules to it.

View File

@ -1322,6 +1322,10 @@
"title": "split",
"path": "job-specification/hcl2/functions/string/split"
},
{
"title": "strlen",
"path": "job-specification/hcl2/functions/string/strlen"
},
{
"title": "strrev",
"path": "job-specification/hcl2/functions/string/strrev"