-
-
Notifications
You must be signed in to change notification settings - Fork 661
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
[js] HxOverrides.substr #4365
Comments
The specification says that result is unspecified for negative length, so we can remove that check I think (https://github.com/HaxeFoundation/haxe/blob/development/std/String.hx#L141). I"m all for removing the override for cc @aduros, @ncannasse |
We test the negative length with pos 0 case at the moment: https://github.com/HaxeFoundation/haxe/blob/development/tests/unit/src/unitstd/String.unit.hx#L123 I"m not sure if we should un-fix this for JS. |
Can anyone benchmark how having/removing this affects performance before it is getting removed? |
@markknol What would be a suitable benchmark? I"m sure in isolation one might get results to show it is faster after removal, but for it to be representative, you"d have to benchmark a parser or something, in which case I doubt it will matter. |
I was more thinking of a runtime performance check (https://jsperf.com/ or something), one with normal substr the other with the hxoverride version. |
Well, if it works on all platforms, that should be added to the specs maybe? |
What"s the actual use case for that The documentation says that negative len is unspecified, so I think it"s fine to "unfix" that (for all targets) even if there was a test. Thoughts? |
What |
Well, that logic is a bit twisted, but I think this code actually only executes when pos == 0 and len < 0: haxe/std/js/_std/HxOverrides.hx Line 76 in 0bdafec
|
I see what you mean now. I"m still reluctant to un-fix this, but I"m also reluctant to specify it. That HxOverrides.substr definitely needs a cleanup either way. |
If we have the possibility to specify something in a crossplatform way I don"t see why we shouldn"t do it. It will reduce the amount of bad surprises when moving code from one platform to another, especially when it"s something that is potentially widely used such as substr. |
Yes, but I"m not sure if supporting this strange behaviour (pos == 0 && len < 0) is worth adding a layer over native implementation. As for "widely used", this particular case is:
|
I used it at least once to cut off the file extension of some dot path string, for which A problem with specifying is that you also have crazy cases like |
Okay, I reverted the change and will try to cleanup current implementation later. I think we can still provide "lighter" substr implementation when |
I cleaned up our substr implementation a bit and removed negative pos handling when |
Just for the record: if you look at the original commits (first post, 2006, remember?), you see where the negative length code comes from. It was first implemented as such and the |
I think I"m using it sometimes, usually in the following way |
I agree it is intuitive what |
À la PHP:
|
can we make it simple and consistent like this? A negative number so for a string
|
I would be fine with that as long as we don"t have to provide additional checks on many targets. |
@kevinresol First of all, it"s important to note that As for |
Ah I didn"t notice there were |
Oh seems that ECMAScript does specify that negative values are clamped at zero for |
Does somebody want to open a pull request that adds the required test cases for |
Pulling this to Haxe 4 as messing with the String API seems to be our common hobby at the moment. |
And back to 4.1 because we found different hobbies. |
ES doesn"t support negative
Hovewer that case with the negative |
It seems that
String.substr
with negative index seems to work for IE > 8 nowadays. So it might make sense to remove the old override with thejs-es5
flag as it is done withArray.indexOf
and the like.On a sidenote the override function has support for negative length as well for some reason:
https://github.com/HaxeFoundation/haxe/blob/development/std/js/_std/HxOverrides.hx#L75
Should it?
But then that only works with
pos = 0
due to the first condition:https://github.com/HaxeFoundation/haxe/blob/development/std/js/_std/HxOverrides.hx#L70
This is some pretty old code from 2006 actually:
2373abc
1c90918
a2c9d5e
Might be worth a revision...;-)
The text was updated successfully, but these errors were encountered: