- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Comparing the result of strpos to FALSE instead of 0 is in case the position is the first character which will return 0.
Admin
AAARGH...
Do a single cast with AS and check for null...
(yeah, I know there are bigger WTF's, but this is a pet peeve of mine!)
Admin
the strrpos()-thing works as intended due to the negative offset: only the first position of $haystack is compared to $needle (in words: 'search backwards for needle, starting 'length'-bytes from the back')
Admin
At least the HTML page injection used a StringBuilder instead of endless and inefficient.NET string concatenation.
Admin
If your application has a "Utils"/"Utility" module, I can guarantee You're Doing It Wrong©®™.
Admin
Wow the first snippet is hard to understand for what it does. And yes
=== 0
would have been an improvement. Funnily enough, today I learn that using-strlen($hay)
as third parameter tostrrpos()
is not equivalent to using0
. That's special,strpos()
doesn't even allow a negative index.It's one thing to know about this quirk, it's another to post code using it on Stackoverflow.
Admin
The "normal" way to do this would/should be more like this:
function startsWith($needle, $haystack) { return strpos($haystack, $needle) === 0; }
Admin
Maybe it's just me, but I don't consider a string can start with "" unless it is "": if you call startsWith("My string", "") I don't expect it to return true because "My string" starts with "M"...
Then again JavaScript gives the same result, so meh.
Admin
Yeah, I think it's just you. To me (and to the language designers), it's completely obvious that EVERY string "starts with" an empty string. Anything else would be a discontinuity in the behavior of the function.
Admin
I see nothing wrong in the first snippet in terms of comparing "!== FALSE". strrpos returns FALSE when the $needle is not found in $haystack, so if you just want to make sure it is found somewhere, this check is ok.
Admin
strlen(), really? You'd have thought by now developers would be aware of multi-byte characters...
Admin
(a) "...and chop off that many characters from the “haystack” using substr, then compare the two strings" -- this creates an unnecessary copy of string in memory. (b) !== false or === 0 both produce identical results. For endsWith function you could either compare the return value with the expected position (which you have already calculated) but checking if the return value is not false is more efficient.
Admin
Imagine embedded html. In COBOL. With every constant string having an explicit length. And a fixed size output buffer 3-4 orders of magnitude bigger than needed, initialized twice, and bigger than L2 cache. Add a half second delay, because mainframe. Then have every single undergraduate at a large public university try to access it for registration all at once, using as many windows as possible so that one might get through.
Admin
I was hoping for an invander zim reference, but I'm not seeing one. Maybe I missed it, but if I didn't, I feel like I got click-baited.
Admin
Pft...4000 lines for a 'God Class'? No no no...what you need is 10000 lines, which will break the compiler as 'who would create a class with more than 9999 lines of code?'
Admin
Using that Integer class is worse than redundancy, since its only function is to compare two int objects. It can't add two ints together.
Admin
While writing startsWith and endsWith this way is hard to understand, it avoids allocating a new string that would happen when using substr. Also, given a good string searching algorithm, there would be no loops involved, and the algorithm would be O(n), where n is length of needle https://en.wikipedia.org/wiki/String_searching_algorithm
Admin
Admin
At least they didn't spell it "Interger" everywhere, then complain that the compiler is broken.
Admin
As has been stated already, in PHP, strpos and strrpos can return zero if the needle is found at the zero position of the haystack string (the return value of strpos/strrpos is the haystack starting point where the needle was found). If you need to do something when the needle is NOT present, you MUST compare to false, and you should use the triple "=" for that comparison. strpos() == false will match any "falsey" value, including zero, but strpos() === false will only match FALSE returned by the fn.
Admin
Not only does that method take a parameter of its own type, 'csr', but it also doesn't even use it!
Admin
The fact that strpos() can return either an integer or a boolean is a WTF all its own.
Still can't login. Can't "like" comments. Can't quote. Can we have Discourse back?
Admin
You missed it.
Admin
csr.getFirstOpenApptHtml(csr)
what I call an "ouroboros method call"
Admin
Both as and is translate to an isinst instruction. The AS keyword is C# is not supported with value types so using AS here would result in compile time error. The code here for the conversion is right. The (int) will be an unbox_any instruction in the il
Admin
It's like for years and years I'd find code along the lines of if a<16 then ="000"+hex(a) else if a<256 then ="00"+hex(a) else if a<1024 then ="0"+hex (a) else =hex(a)
Admin
"Not only does that method take a parameter of its own type, 'csr', but it also doesn't even use it!"
Presumably it used it somewhere in the "// add 300 more lines of stuff to the string builder" part.
Admin
Last time I checked, .Net string concatenation was streamlined and now has the same performance as using a StringBuilder.
Admin
It still needs to create a new string for the concatenated string while StringBuilder does not.
Admin
So that if you're looking to see that if Alice in Wonderland starts with the string "a curious dream", you'll be happy to search through fifty-odd kB of text before finding it and determining that no, the string is not at the start of the text?
Addendum 2016-04-22 06:33: I mean, it's not like PHP hasn't had a substr_compare function for the last decade or so...
Admin
Well, if $haystack can be that large, then do it like this:
function startsWith($needle, $haystack) { return strncmp($needle, $haystack, strlen($needle)) === 0; }
Admin
See comment above.
Admin
Sorry Watson, I don't follow. strncmp will only consider the first X chars or bytes, so the length of haystack is irrelevant, and this will only do X comparisons. strlen is only counting the length of $needle, which I'm assuming to be trivial - so we are never dealing with the whole "50k" string, and nor are any more strings created (depending on php's implementation of strnlen)
Provided strncmp and strlen are both either multibyte or non-multibyte, this will work fine - although you would get problems if one is multibyte and the other isn't.
Admin
Now: write an "endswith" function. Again, see the reply above and note that it has an addendum.
Admin
Wow, that is a quirk; I'm used to writing
foo.lastIndexOf(bar, 0) == 0
as a poor man's startsWith (fortunately JavaScript has a real startsWith now) because that formulation allows you to use expressions forfoo
and/orbar
.Admin
Stack Overflow, because one of us can't be as dumb as all of us.
Admin
Correct. Is starts and ends, at the beginning of the string, and if it's not found at the beginning, it'll waste no CPU searching the rest of the string.