• Tom (unregistered)

    Comparing the result of strpos to FALSE instead of 0 is in case the position is the first character which will return 0.

  • TheCPUWizard (unregistered)

    AAARGH...

      if (obj is Integer)
       {
                Integer integer = (Integer)obj;
        }
    

    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!)

  • PHP4Moneyz (unregistered)

    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')

  • Crawford (unregistered)

    At least the HTML page injection used a StringBuilder instead of endless and inefficient.NET string concatenation.

  • Milkman Dan (unregistered)

    If your application has a "Utils"/"Utility" module, I can guarantee You're Doing It Wrong©®™.

  • (nodebb)

    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 to strrpos() is not equivalent to using 0. 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.

  • OliC (unregistered)

    The "normal" way to do this would/should be more like this:

    function startsWith($needle, $haystack) { return strpos($haystack, $needle) === 0; }

  • Jester (unregistered) in reply to gleemonk

    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.

  • MV (unregistered) in reply to Jester

    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.

  • Balu (unregistered)

    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.

  • Ashley Sheridan (google)

    strlen(), really? You'd have thought by now developers would be aware of multi-byte characters...

  • Salman (unregistered)

    (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.

  • Simon (unregistered)

    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.

  • Church (unregistered)

    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.

  • Been there, done that... (unregistered)

    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?'

  • Russell Judge (google)

    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.

  • Li Huan Jeow (google)

    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

  • I'm not a robot (unregistered) in reply to Li Huan Jeow
    While writing startsWith and endsWith this way is hard to understand, it avoids allocating a new string that would happen when using substr.
    Ease of understanding is usually more important, but in any case PHP has substr_compare which is easier to understand than the presented version and (hopefully; I can't be bothered to actually try it) just as efficient.
  • D-Coder (unregistered) in reply to I'm not a robot
         if (obj is Integer)
         {
            Integer integer = (Integer)obj;
            return _value.CompareTo(integer._value);
         }
         else
         {
            throw new ArgumentException("Object is not of type Integer");
         }
    

    At least they didn't spell it "Interger" everywhere, then complain that the compiler is broken.

  • Red Five (unregistered)

    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.

  • lordofduct (unregistered)

    Not only does that method take a parameter of its own type, 'csr', but it also doesn't even use it!

  • TenshiNo (unregistered) in reply to Red Five

    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?

  • Angela Anuszewski (google) in reply to Church

    You missed it.

  • molleafauss (github)

    csr.getFirstOpenApptHtml(csr)

    what I call an "ouroboros method call"

  • foobar (unregistered) in reply to TheCPUWizard

    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

  • jgh (unregistered)

    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)

  • (nodebb) in reply to lordofduct

    "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.

  • Drak (unregistered) in reply to Crawford

    Last time I checked, .Net string concatenation was streamlined and now has the same performance as using a StringBuilder.

  • Balu (unregistered) in reply to Drak

    It still needs to create a new string for the concatenated string while StringBuilder does not.

  • (nodebb) in reply to OliC

    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...

  • OliC (unregistered) in reply to Watson

    Well, if $haystack can be that large, then do it like this:

    function startsWith($needle, $haystack) { return strncmp($needle, $haystack, strlen($needle)) === 0; }

  • (nodebb) in reply to OliC

    See comment above.

  • Oli C (unregistered) in reply to Watson

    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.

  • (nodebb) in reply to Oli C

    Now: write an "endswith" function. Again, see the reply above and note that it has an addendum.

  • (nodebb) in reply to gleemonk

    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 for foo and/or bar.

  • justanotherloser (unregistered) in reply to gleemonk

    Stack Overflow, because one of us can't be as dumb as all of us.

  • Anonymous (unregistered) in reply to PHP4Moneyz

    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')

    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.

Leave a comment on “Mysterious Mysteries of Strange Mystery”

Log In or post as a guest

Replying to comment #:

« Return to Article