HUGO

Substr trim barfs on 1char

What version of Hugo are you using (hugo version)?

$ hugo version
Hugo Static Site Generator v0.81.0-DEV/extended openbsd/amd64 BuildDate: unknown

Does this issue reproduce with the latest release?

{{ $path := "/"}}
{{ $path := ( substr $path 0 -1 ) }}
{{ $path :=  printf "/js/%s/%s" $path "boot.js" }}

produces:

js//boot.js

not a problem for unix paths but it’s not expected behaviour.

if I change $path to "XX/" the output is as expected js/XX/boot.js

The substr function does exactly what it should do: it removes one trailing character, setting $path to an empty string.

You are getting /js//boot.js (with double slashes) due to the printf format string; the first %s is an empty string.

Perhaps you can use path.Join instead:

{{ path.Join "/js" $path "boot.js" }}

it does for strings longer than one character. substr with negative index seems to barf when the string is one character long. At least in the version I’m working.

Which version?

The “out-of-bounds” issue was addressed about 8 months ago in 8a26ab0, available in v0.81.0 and later. You are currently using v0.81.0-DEV (built prior to release of v0.81.0).

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.