-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
function gets error if START
equals to string's length
#8113
Comments
That error is expected because counting starts at 0, not 1. So your start position is actually out of bounds. That behaviour changed in 0.80.0 and is now the same way substr works in other languages. That it worked before was a bug ;). Is there a specific case where you would want an empty string to be returned? I would expect something like I am not sure if that is the updated version yet: https://gohugo.io/functions/substr/ - there was a PR for the documentation to fix up the explanations for the "new" substr handling some days ago. |
@zerovip, It's late here (power outage at work has me up at 3am), and I'm too tired to think clearly about this issue. So, in the meantime... Can you explain your use case? How would you work around this issue? |
Thank you for your reply. Sorry but I don't think it is related to where counting starts. The only difference is the valid bounds shrunk.
Sure I can explain my case:
to get
to get, for example I am not familiar with Hugo, so I will be very happy and grateful if there is a better way to do so. Thank you in advance! Here is my codes: {{ $curr_page := . }}
{{ $curr_lang := $curr_page.Language.Lang }}
{{ $curr_trans_list := slice }}
{{ range $curr_page.Translations }}
{{ $curr_trans_list = $curr_trans_list | append .Language.Lang }}
{{ end }}
{{ range $.Site.Home.AllTranslations }}
{{ if (eq .Language.Lang $curr_lang) }}
<li class='left_non-footer_option_active'>{{ .Language.LanguageName }} ⦿ </li>
{{ else }}
{{ if (in $curr_trans_list .Language.Lang) }}
<li class='left_non-footer_option_non-active'>
<a href="{{ .Permalink }}{{ substr $curr_page.RelPermalink 4 }}">{{ .Language.LanguageName }} ○ </a>
</li>
{{ else }}
<li class='left_non-footer_option_non-active'>{{ .Language.LanguageName }} ⨂ </li>
{{ end }}
{{ end }}
{{ end }} Oh, how would I work around… emmmm, I might write a In my opinion, there is no harm to get an empty string rather than get an error. Besides, I really don't have any good argument for that. So what do other people think? It is not in a hurry, so you can go to bed first. I am in China and it is six pm in the evening. We can listen to more others' opinions. |
PHP doesn't complain either, and we refer to the PHP implementation in our documentation. Try this: I encountered this breaking change a couple of days ago. I had some code that grabbed everything after the first character of a string, and I didn't provide a special case to check if the string was only a single character. This worked in 0.79, though it is arguably lazy coding:
Changes required for 0.80:
|
Yes! You are right!!! Thank you very much. I tried PHP and Javascript, they all don't raise errors! Thank you very much! That must be a strong argument! |
Interesting. I assumed from the PHP docs that substr returning I think we can revert to the previous behavior by getting rid of the error and just returning an empty string. I'll work on it. |
@moorereason, Thank you. |
Thank you. Do we go further? I mean cancel the bound and always return empty string when |
Most other substr implementations don't error out in out-of-bounds cases but simply return an empty string (or a value that's printed as an empty string). We'll follow their lead and not exit template execution. Allow the user decide what to do with the empty result. Fixes gohugoio#8113
PR #8115 submitted. PTAL |
I know it's splitting hairs, but per PHP documentation out of bounds https://www.php.net/manual/en/function.substr.php#refsect1-function.substr-errors |
Just as @moorereason explained, The PR LGTM. @moorereason , thank you for your work! And, @davidsneighbour , @jmooring , thank you for your participating in the discussion! |
If it's about that casting anything into a string will print an empty string if it's not already a stringable variable. A lot of these tickets here in the issue tracker are about "please don't throw an error if I do this" and half of the other tickets are "hugo is not throwing an error if I do this". Looks like Golang has an issue with typing :) As long as it's documented it's good. |
Most other substr implementations don't error out in out-of-bounds cases but simply return an empty string (or a value that's printed as an empty string). We'll follow their lead and not exit template execution. Allow the user decide what to do with the empty result. Fixes #8113
Most other substr implementations don't error out in out-of-bounds cases but simply return an empty string (or a value that's printed as an empty string). We'll follow their lead and not exit template execution. Allow the user decide what to do with the empty result. Fixes gohugoio#8113
Most other substr implementations don't error out in out-of-bounds cases but simply return an empty string (or a value that's printed as an empty string). We'll follow their lead and not exit template execution. Allow the user decide what to do with the empty result. Fixes gohugoio#8113
Most other substr implementations don't error out in out-of-bounds cases but simply return an empty string (or a value that's printed as an empty string). We'll follow their lead and not exit template execution. Allow the user decide what to do with the empty result. Fixes gohugoio#8113
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.
Issue
Hi, all.
After upgrading to version v0.80.0, there is an error:
I expected that
{{ substr "foo" 3 }}
gives me empty string, and this always works until v0.80.0.I believe commit 64789fb caused this problem. It was
before, and now:
Can removing "-1" fix this?
Thank you in advance, and happy new year!
The text was updated successfully, but these errors were encountered: