-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
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
Comments
With a negative start number you're saying, "Start this many characters from the end, and go backwards this many characters." |
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 |
I agree. I expected |
OK, so the documentation now shows the current behavior, but I'm not sure if the current behavior is correct for this example:
Shouldn't this be "Man" instead? There's a test for this in strings_test.go, but I think the test is wrong:
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. |
Fix miscalculations when start is negative. Results should now match PHP substr. Fixes gohugoio#7993
Fix miscalculations when start is negative. Results should now match PHP substr. Fixes #7993
Thank you for addressing this. There is one remaining problem.
If length is zero, you should always get an empty string. This matches PHP |
When length parameter is zero, always get an empty string. Updates gohugoio#7993
When length parameter is zero, always get an empty string. Updates gohugoio#7993
When length parameter is zero, always return an empty string. Updates gohugoio#7993
Thanks, @jmooring. I've submitted a new PR to fix that case. |
When length parameter is zero, always return an empty string. Updates #7993
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. |
What version of Hugo are you using (
hugo version
)?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"
The text was updated successfully, but these errors were encountered: