diff --git a/.changelog/14463.txt b/.changelog/14463.txt new file mode 100644 index 000000000000..800462972d44 --- /dev/null +++ b/.changelog/14463.txt @@ -0,0 +1,3 @@ +```release-note:improvement +jobspec: Added `strlen` HCL2 function to determine the length of a string +``` diff --git a/jobspec2/functions.go b/jobspec2/functions.go index 9e489902d22a..4e9130a3a9cc 100644 --- a/jobspec2/functions.go +++ b/jobspec2/functions.go @@ -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, diff --git a/website/content/docs/job-specification/hcl2/functions/collection/length.mdx b/website/content/docs/job-specification/hcl2/functions/collection/length.mdx index 70aa9ff1b8f1..3c56ff2b3625 100644 --- a/website/content/docs/job-specification/hcl2/functions/collection/length.mdx +++ b/website/content/docs/job-specification/hcl2/functions/collection/length.mdx @@ -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. diff --git a/website/content/docs/job-specification/hcl2/functions/string/strlen.mdx b/website/content/docs/job-specification/hcl2/functions/string/strlen.mdx new file mode 100644 index 000000000000..c08c871d46c8 --- /dev/null +++ b/website/content/docs/job-specification/hcl2/functions/string/strlen.mdx @@ -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. diff --git a/website/data/docs-nav-data.json b/website/data/docs-nav-data.json index b9628c4d4c8e..cc4fb70377e7 100644 --- a/website/data/docs-nav-data.json +++ b/website/data/docs-nav-data.json @@ -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"