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

{{substr "BatMan" -3}} generating error #7993

Closed
jeremielp opened this issue Nov 27, 2020 · 7 comments · Fixed by #7998
Closed

{{substr "BatMan" -3}} generating error #7993

jeremielp opened this issue Nov 27, 2020 · 7 comments · Fixed by #7998
Labels

Comments

@jeremielp
Copy link

jeremielp commented Nov 27, 2020

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

$ hugo version
Hugo Static Site Generator v0.75.0-FEF924BA windows/amd64 BuildDate: 2020-09-14T09:19:16Z

Does this issue reproduce with the latest release?

Not tested.

Looking at this page of the documentation:
https://gohugo.io/functions/substr/
It says: "To extract characters from the end of the string, use a negative start number."

I was expecting this to happen, like the substr PHP function:

{{substr "BatMan" -3}} → "Man"
{{substr "BatMan" -2}} → "an"

Yet, I got errors

{{substr "BatMan" -3}}
execute of template failed: template: partials/test.html:1:3: executing "partials/test.html" at <substr "BatMan" -3>: error calling substr: runtime error: slice bounds out of range **[-2:]**

{{substr "BatMan" -2}}
execute of template failed: template: partials/test.html:1:3: executing "partials/test.html" at <substr "BatMan" -3>: error calling substr: runtime error: slice bounds out of range **[-1:]**

This one is working and returning the full string
{{substr "BatMan" -1}} → "BatMan"

@jmooring
Copy link
Member

{{ substr "BatMan" -1 3 }} → "Man"
{{ substr "BatMan" -2 3 }} → "tMa"
{{ substr "BatMan" -3 3 }} → "atM"

With a negative start number you're saying, "Start this many characters from the end, and go backwards this many characters."

@jeremielp
Copy link
Author

jeremielp commented Nov 27, 2020

This is not coherent with any other programming languages.

How one can know that -1 means the end of the string and -2 means 1 char from the end. Because if I follow your logic, then -1 should mean start 1 character from the end.

At least an example should be added to the doc

@jmooring
Copy link
Member

I agree. I expected {{ substr "BatMan" -2 }} to yield an.

@jmooring
Copy link
Member

jmooring commented Nov 28, 2020

OK, so the documentation now shows the current behavior, but I'm not sure if the current behavior is correct for this example:

{{ substr "BatMan" -3 3 }} → "atM"

Shouldn't this be "Man" instead? There's a test for this in strings_test.go, but I think the test is wrong:

{"abcdef", -3, 3, "bcd"},

Shouldn't this be "def" instead?

We're extracting backward instead of forward when the start number is negative, but I think we should always be extracting forward (left to right).

EDIT: I think Python goes right to left with a negative start number. Perhaps we're modeling that behavior.

moorereason added a commit to moorereason/hugo that referenced this issue Nov 28, 2020
Fix miscalculations when start is negative.  Results should now match
PHP substr.

Fixes gohugoio#7993
@bep bep closed this as completed in #7998 Nov 28, 2020
bep pushed a commit that referenced this issue Nov 28, 2020
Fix miscalculations when start is negative.  Results should now match
PHP substr.

Fixes #7993
@jmooring
Copy link
Member

@moorereason,

Thank you for addressing this. There is one remaining problem.

Statement Expected Return Actual Return
substr "abcdef" 0 0 empty string abcdef
substr "abcdef" 1 0 empty string bcdef
substr "abcdef" -1 0 empty string f

If length is zero, you should always get an empty string. This matches PHP substr behavior.

moorereason added a commit to moorereason/hugo that referenced this issue Nov 28, 2020
When length parameter is zero, always get an empty string.

Updates gohugoio#7993
moorereason added a commit to moorereason/hugo that referenced this issue Nov 28, 2020
When length parameter is zero, always get an empty string.

Updates gohugoio#7993
moorereason added a commit to moorereason/hugo that referenced this issue Nov 28, 2020
When length parameter is zero, always return an empty string.

Updates gohugoio#7993
@moorereason
Copy link
Contributor

Thanks, @jmooring. I've submitted a new PR to fix that case.

bep pushed a commit that referenced this issue Dec 1, 2020
When length parameter is zero, always return an empty string.

Updates #7993
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 26, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants