Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hcl2: add strlen function and update docs. #14463

Merged
merged 2 commits into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/14463.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
jobspec: Added `strlen` HCL2 function to determine the length of a string
```
1 change: 1 addition & 0 deletions jobspec2/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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.
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 4 additions & 0 deletions website/data/docs-nav-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down