WTF Bites


  • Dupa

    @tsaukpaetra said in WTF Bites:

    @kt_ said in WTF Bites:

    @tsaukpaetra said in WTF Bites:

    Status: Made a loop to move to the next delectable button in an array:

    int LastSelectedIndex = SelectedIndex;
    int RotateDirection = Direction == EDirection::Left ? -1 : 1;
    
    for (int newSelectedIndex = SelectedIndex, loops = 0;
    	!SelectButton(newSelectedIndex) || (newSelectedIndex == LastSelectedIndex && loops == 0);
    	newSelectedIndex = newSelectedIndex + RotateDirection + ButtonArray.Num() % ButtonArray.Num())
    {
    	if (newSelectedIndex == LastSelectedIndex)
    		loops++;
    }
    

    I'm not entirely sure I need the LastSelectedIndex variable...

    Edit: And that ButtonArray.Num() (which returns the number of elements, natch) is incorrect here...

    This is so bad it hurts…

    But it works!

    Of course it does.

    Why do you hate variables?


  • Notification Spam Recipient

    @kt_ said in WTF Bites:

    @tsaukpaetra said in WTF Bites:

    @kt_ said in WTF Bites:

    @tsaukpaetra said in WTF Bites:

    Status: Made a loop to move to the next delectable button in an array:

    int LastSelectedIndex = SelectedIndex;
    int RotateDirection = Direction == EDirection::Left ? -1 : 1;
    
    for (int newSelectedIndex = SelectedIndex, loops = 0;
    	!SelectButton(newSelectedIndex) || (newSelectedIndex == LastSelectedIndex && loops == 0);
    	newSelectedIndex = newSelectedIndex + RotateDirection + ButtonArray.Num() % ButtonArray.Num())
    {
    	if (newSelectedIndex == LastSelectedIndex)
    		loops++;
    }
    

    I'm not entirely sure I need the LastSelectedIndex variable...

    Edit: And that ButtonArray.Num() (which returns the number of elements, natch) is incorrect here...

    This is so bad it hurts…

    But it works!

    Of course it does.

    Why do you hate variables?

    I don't? There's four of them right there! In fact, the variable-to-line ratio is nearly 1:2!
    😜


  • Dupa

    @tsaukpaetra said in WTF Bites:

    @kt_ said in WTF Bites:

    @tsaukpaetra said in WTF Bites:

    @kt_ said in WTF Bites:

    @tsaukpaetra said in WTF Bites:

    Status: Made a loop to move to the next delectable button in an array:

    int LastSelectedIndex = SelectedIndex;
    int RotateDirection = Direction == EDirection::Left ? -1 : 1;
    
    for (int newSelectedIndex = SelectedIndex, loops = 0;
    	!SelectButton(newSelectedIndex) || (newSelectedIndex == LastSelectedIndex && loops == 0);
    	newSelectedIndex = newSelectedIndex + RotateDirection + ButtonArray.Num() % ButtonArray.Num())
    {
    	if (newSelectedIndex == LastSelectedIndex)
    		loops++;
    }
    

    I'm not entirely sure I need the LastSelectedIndex variable...

    Edit: And that ButtonArray.Num() (which returns the number of elements, natch) is incorrect here...

    This is so bad it hurts…

    But it works!

    Of course it does.

    Why do you hate variables?

    I don't? There's four of them right there! In fact, the variable-to-line ratio is nearly 1:2!
    😜

    Yeah, yeah. Multiline for declaration should be considered capital crime.


  • Java Dev

    @tsaukpaetra said in WTF Bites:

    Status: Made a loop to move to the next delectable button in an array:

    int LastSelectedIndex = SelectedIndex;
    int RotateDirection = Direction == EDirection::Left ? -1 : 1;
    
    for (int newSelectedIndex = SelectedIndex, loops = 0;
    	!SelectButton(newSelectedIndex) || (newSelectedIndex == LastSelectedIndex && loops == 0);
    	newSelectedIndex = newSelectedIndex + RotateDirection + ButtonArray.Num() % ButtonArray.Num())
    {
    	if (newSelectedIndex == LastSelectedIndex)
    		loops++;
    }
    

    I'm not entirely sure I need the LastSelectedIndex variable...

    Edit: And that ButtonArray.Num() (which returns the number of elements, natch) is incorrect here...

    int n = ButtonArray.Num();
    int prev = SelectedIndex;
    bool left = (Direction == EDirection::Left)
    for( int i = 0 ; i < n ; i++ ) {
        if( SelectButton((left ? prev + n - i : prev + i) % n) ) {
            break;
        }
    }
    

    All helper variables are only there because your variable names are insanely long.



  • @lb_ said in WTF Bites:

    0_1512001034817_5b35bd87-f715-43ee-aeca-f8f387a4fa25-image.png
    :wtf:

    Obviously they implemented it with an embedded chromium browser.


  • Notification Spam Recipient

    @pleegwat said in WTF Bites:

    @tsaukpaetra said in WTF Bites:

    Status: Made a loop to move to the next delectable button in an array:

    int LastSelectedIndex = SelectedIndex;
    int RotateDirection = Direction == EDirection::Left ? -1 : 1;
    
    for (int newSelectedIndex = SelectedIndex, loops = 0;
    	!SelectButton(newSelectedIndex) || (newSelectedIndex == LastSelectedIndex && loops == 0);
    	newSelectedIndex = newSelectedIndex + RotateDirection + ButtonArray.Num() % ButtonArray.Num())
    {
    	if (newSelectedIndex == LastSelectedIndex)
    		loops++;
    }
    

    I'm not entirely sure I need the LastSelectedIndex variable...

    Edit: And that ButtonArray.Num() (which returns the number of elements, natch) is incorrect here...

    int n = ButtonArray.Num();
    int prev = SelectedIndex;
    bool left = (Direction == EDirection::Left)
    for( int i = 0 ; i < n ; i++ ) {
        if( SelectButton((left ? prev + n - i : prev + i) % n) ) {
            break;
        }
    }
    

    All helper variables are only there because your variable names are insanely long.

    You think my variable names are long?

    0_1512064692295_cd7f3467-4ae7-4d6e-9e3b-71064b3d3ede-image.png

    0_1512064723590_917a05ff-9573-4346-af39-55c525d54764-image.png

    And these aren't even the LONGEST ones, I just scanned one of the files I happened to have open.


  • Discourse touched me in a no-no place

    @tsaukpaetra said in WTF Bites:

    You think my variable names are long?

    Yes. I've seen longer, and let's just say it's really quite annoying when coding feels like copying out War and Peace just because a colleague likes to blather in his identifiers.


  • Java Dev

    @dkf said in WTF Bites:

    Yes

    Quite apart from that, it keeps the pattern clear. You can substitute in your extra-long identifiers if you like and break up in more lines.

    The main pitfall in this piece is in the descending case, where (prev-i)%n will NOT do what you want.


  • I survived the hour long Uno hand

    @pleegwat
    Pervy what now? :o


  • Java Dev

    @izzion Pervy stuff? Where? Where? Where?


  • Notification Spam Recipient

    @pleegwat said in WTF Bites:

    @dkf said in WTF Bites:

    Yes

    Quite apart from that, it keeps the pattern clear. You can substitute in your extra-long identifiers if you like and break up in more lines.

    The main pitfall in this piece is in the descending case, where (prev-i)%n will NOT do what you want.

    That's why I did (prev+n-i)%n? I think?


  • Java Dev

    @tsaukpaetra You had idx = idx + dir + n % n. I'm never sure on the precedence there but I think % is before +, so it simplifies to idx = idx + dir.


  • area_can

    How does JavaScript not have a sprintf/String.prototype.format function built in to the standard library? And before you ask, yes, I know about template literals, and no, they don't seem to be very useful for localization.


  • Notification Spam Recipient

    @pleegwat said in WTF Bites:

    @tsaukpaetra You had idx = idx + dir + n % n. I'm never sure on the precedence there but I think % is before +, so it simplifies to idx = idx + dir.

    Right, later I corrected that.


  • Java Dev

    @tsaukpaetra Not in your OP.

    Honestly my main problem with your version is the division of logic. You have half the looping logic in the body, and the search condition in the for construct. IMO when doing a search loop the search condition should be in the body (with a break) and the for should be concerned with looping through the entire set being searched exactly once.



  • @cursorkeys said in WTF Bites:

    Not a totally ridiculous premise, I mean you could if you were armed with a copy of the PE format and you were insane but this article isn't concerned with pesky things like the PE format or reality:

    :rofl:

    To create an "exe" file in Notepad, you must understand and know a programming language such as C++.
    ...
    Step 4
    Type the "exe" file for the program you want to create from the programming language into Notepad. Type the file in C++ programming language.

    They say "C++", but then they tell you to write what's basically compiled machine code directly into the file. I mean, even Assembly would be closer to that than C++, but even that's not quite right.



  • @pleegwat you used break. I like my do ... while version better.


  • Garbage Person

    A colleague was having problems doing a cherrypick. He copied the relevant command from the wiki page I wrote lo these many years ago, edited it, and was getting an inexplicable error:

    % svn merge –c 31734 svn://host/repo/branches/sourcebranch
    svn: E195002: Invalid merge source '–c'; a working copy path can only be used with a repository revision (a number, a date, or head)
    

    It took quite a while to figure out. See if you can as well.

    Idiotic Confluence had changed the hyphen (in a code block!) to an en dash.

  • 🚽 Regular

    @pleegwat said in WTF Bites:

    @tsaukpaetra Not in your OP.

    Honestly my main problem with your version is the division of logic. You have half the looping logic in the body, and the search condition in the for construct. IMO when doing a search loop the search condition should be in the body (with a break) and the for should be concerned with looping through the entire set being searched exactly once.

    This is WTF Bites.</sparta>



  • @Greybeard

    Wait, wait, wait...

    I know that hyphen and eM dash are different characters, but I thought that hyphen and eN dash were the same thing!..?



  • @djls45 said in WTF Bites:

    I know that hyphen and eM dash are different characters, but I thought that hyphen and eN dash were the same thing!..?

    Nope:

    -: U+002D HYPHEN-MINUS
    : U+2012 FIGURE DASH
    : U+2013 EN DASH
    : U+2014 EM DASH

    and for completeness:
    : U+2212 MINUS SIGN


  • Discourse touched me in a no-no place

    @bulb Also, they look different in some fonts.

    Here's a slew of different characters; guess which is which!

    • ‌- ‐ ‑ ‒ – — ― −


  • @dkf

    That's easy

    Ctrl+C
    $ unicode Ctrl+V

    U+200C ZERO WIDTH NON-JOINER
    UTF-8: e2 80 8c UTF-16BE: 200c Decimal: &#8204; Octal: \020014
    ‌
    Category: Cf (Other, Format)
    Bidi: BN (Boundary Neutral)
    
    U+002D HYPHEN-MINUS
    UTF-8: 2d UTF-16BE: 002d Decimal: &#45; Octal: \055
    -
    Category: Pd (Punctuation, Dash)
    Bidi: ES (European Number Separator)
    
    U+2010 HYPHEN
    UTF-8: e2 80 90 UTF-16BE: 2010 Decimal: &#8208; Octal: \020020
    ‐
    Category: Pd (Punctuation, Dash)
    Bidi: ON (Other Neutrals)
    
    U+2011 NON-BREAKING HYPHEN
    UTF-8: e2 80 91 UTF-16BE: 2011 Decimal: &#8209; Octal: \020021
    ‑
    Category: Pd (Punctuation, Dash)
    Bidi: ON (Other Neutrals)
    Decomposition: <noBreak> 2010
    
    U+2012 FIGURE DASH
    UTF-8: e2 80 92 UTF-16BE: 2012 Decimal: &#8210; Octal: \020022
    ‒
    Category: Pd (Punctuation, Dash)
    Bidi: ON (Other Neutrals)
    
    U+2013 EN DASH
    UTF-8: e2 80 93 UTF-16BE: 2013 Decimal: &#8211; Octal: \020023
    –
    Category: Pd (Punctuation, Dash)
    Bidi: ON (Other Neutrals)
    
    U+2014 EM DASH
    UTF-8: e2 80 94 UTF-16BE: 2014 Decimal: &#8212; Octal: \020024
    —
    Category: Pd (Punctuation, Dash)
    Bidi: ON (Other Neutrals)
    
    U+2015 HORIZONTAL BAR
    UTF-8: e2 80 95 UTF-16BE: 2015 Decimal: &#8213; Octal: \020025
    ―
    Category: Pd (Punctuation, Dash)
    Bidi: ON (Other Neutrals)
    
    U+2212 MINUS SIGN
    UTF-8: e2 88 92 UTF-16BE: 2212 Decimal: &#8722; Octal: \021022
    −
    Category: Sm (Symbol, Math)
    Bidi: ES (European Number Separator)
    

    :-D ;-)


  • Discourse touched me in a no-no place

    @bulb The ZWNJ is just to stop ⛔ 👶 from doing markdown stupids.




  • kills Dumbledore

    0_1512121011967_66d2d67e-2107-4864-8960-e90bfe9186fa-image.png

    No, this is known as wish it was two factor.

    Oh well, another Keepass entry I suppose


  • 🚽 Regular


  • Java Dev

    Well, I found an Android WTF. So I was watching some Youtube yesterday on my iPad and I was low on data. But whatever, a couple hours left of the month so if I run out it's no biggie. I did get one automated SMS saying I should buy more data because I was running out that I ignored.

    Jump forward to 23:50-ish when I actually did run out of data. So my phone got the special "no more data" system notification from Android. Which it got just slightly after midnight. So my phone ended up in a state where it would flat out refuse to connect to websites because it was under the impression there was no more data left, despite the fact that I had all of the data available from there being a new month. Also, as that notification cannot be removed at all (except if I buy more data from my provider I guess) I had to resort to rebooting my phone to make it go away. Meanwhile, the iPad had no issues with the whole deal. I guess Apple can score a rare victory over Android for that.



  • @atazhaia Don't you have to manually enter this information? On my android phone, I have to manually set limits via Connections -> Data Usage -> Billing Cycle and Data Warning in the settings menu. There's you also select the date on which a new billing period begins.

    FWIW, I bought my phone independently, so there was no provider-specific stuff pre-installed/setup or so.


  • :belt_onion:

    @greybeard Stupid people who copy and paste... I'm so used to that happening that whenever I type out commands in an email it's muscle memory to type <argument> <space> <Ctrl-Z to reverse the autocorrect>. I could also just DISABLE THE AUTOCORRECT, but... it's muscle memory now... :sadface:


  • Java Dev

    @cvi There are options for setting a limit and warning that are specific to the phone itself. I got warning set up, but I don't have a limit set as previously they'd just drop my speed to Edge speeds once I used up my data.

    Also, I got shared data between my phone and tablet so setting a hard limit on either device wont work well.



  • Found hiding deep into our codebase:

    #ifdef WIN32
    char cmd[512];
    sprintf(cmd, "/c xcopy /y \"%s\" \"%s\"", source, dest);
    
    wchar_t wcmd[512];
    mbstowcs(wcmd, cmd, 512);
    
    SHELLEXECUTEINFO info = {0};
    /* various info.xxx = yyy */
    info.lpFile = L"cmd";
    info.lpParameters = wcmd;
    
    if (ShellExecuteEx(&info)) {
        /* success */
    } else {
        /* error */
    }
    #endif
    

    Count the :wtf: for me, please.

    Bonus question: why does this function fail with a Windows error popup Windows cannot find ´c´. Make sure you typed the name correctly, and then try again.?



  • @remi said in WTF Bites:

    Count the for me, please.

    1. WinAPI (unlike POSIX) has a CopyFile function; there is no excuse for not using it.
    2. xcopy is an external command. There is no reason to run it through cmd /c.
    3. There is a swprintf function to print directly into a wide string (and yes, it can take narrow arguments while writing wide output too), so the extra mbstowcs is silly.

    The rest looks like standard WinAPI call to me though. The only reason that comes to my mind why it would not find just c is that the narrow version of the function is actually called (i.e. UNICODE is not defined), but in that case the info.lpFile = L"cmd" shouldn't compile, so I don't know.


  • Java Dev

    @bulb said in WTF Bites:

    WinAPI (unlike POSIX) has a CopyFile function; there is no excuse for not using it.

    A simple google indicates that copies files; xcopy recursively copies directories. And a file data copy loop is only about 10 lines of code, I don't think it's a particularly glaring omission.



  • @bulb said in WTF Bites:

    The only reason that comes to my mind why it would not find just c is that the narrow version of the function is actually called (i.e. UNICODE is not defined)

    Yep. You're clearly much more Windows-savvy than I am, it took me much longer to find out :sadface:

    but in that case the info.lpFile = L"cmd" shouldn't compile, so I don't know.

    Code does compile, but with a warning. Which, as we all know, means the code is fine and we can commit and move on, right? 🤮

    Bonus-bonus: how to edit the code to make it work (I said "make it work", not "make it good code") -- code-golf style i.e. changing as few chars as possible. I found a 2-chars-added/0-removed solution.



  • @pleegwat That code is actually copying a single file (not obvious from the snippet, but source is a full path to file, not a directory).



  • @remi said in WTF Bites:

    @pleegwat That code is actually copying a single file (not obvious from the snippet, but source is a full path to file, not a directory).

    That actually makes it unfortunately a bit harder to refactor to use CopyFile, since that one requires a full destination path and I only have a directory -- i.e. I have C:\path\to\source\file and C:\path\to\dest, so to call CopyFile I would need to extract file from the first one and add to the second, or change the function signature and all places where it's used, so it's... ugh


  • kills Dumbledore

    @remi said in WTF Bites:

    Count the :wtf: for me, please.

    1. using C++, or C or whatever that shit is
    2. mbstowcs. Did the person who came up with that method just mash their hand over the keyboard?


  • @jaloopa said in WTF Bites:

    @remi said in WTF Bites:

    Count the :wtf: for me, please.

    1. using C++, or C or whatever that shit is

    C, for weird and historical reasons. Be glad it's not FORTRAN, because if it had been so it would likely have been FORTRAN 77. And I don't know how that interfaces with Windows, so I guess the result would have been much uglier (I mean, even uglier than FORTRAN 77 by itself...).

    1. mbstowcs. Did the person who came up with that method just mash their hand over the keyboard?

    That was my first reaction as well. I sure hope I never need to use that function, 'cause there is no way I can remember its name. Even Unix commands seem less cryptic, and that's saying a lot.



  • @remi in addition to what others have mentioned, I don't see any mention of the 512 limit in that sprintf call, so it could potentially create a buffer overflow if source and dest ever happen to not be properly capped.



  • @lb_ Ah, good, I was starting to worry that no-one was seeing that one! phew

    (EDIT: and no, they are not properly capped in any way before calling this function, so long paths will definitely cause a buffer overflow)



  • @remi said in WTF Bites:

    @lb_ Ah, good, I was starting to worry that no-one was seeing that one! phew

    I was about to post that - then for some strange reason I decided to finish reading the thread. I must be broken today...



  • @dcon said in WTF Bites:

    I must be broken today...

    Did you try to turn it off and on again ? 🍹



  • @timebandit said in WTF Bites:

    @dcon said in WTF Bites:

    I must be broken today...

    Did you try to turn it off and on again ? 🍹

    Yes. And I missed the train and had to drive.



  • @dcon Don't worry, that monstruosity is on its way out.

    It turned out that the function is only called in a handful of places (and it's an internal function so I don't have to worry about other modules using it (*)), and always in the same way, so changing the way it's called to add the destination file name is easy.

    So I've replaced it by CopyFile(), and they lived happily ever after...

    (*) and if, despite it being clearly marked as internal, someone managed to use it in another module and their code is now broken, well that'll teach them to use an internal function 😛



  • 0_1512150149143_845086ff-4e52-4e87-9bd2-4ba6b3704c40-image.png

    Translation:

    Incorrect password or new invalid password. New password minimum length: 0. Non-alphanumeric characters required: 1.

    So, my new password could be just @ :rolleyes:


  • I survived the hour long Uno hand

    @timebandit
    Usually I have to turn it on first, and then it turns itself off at the end of its cycle :giggity:



  • @remi said in WTF Bites:

    @jaloopa said in WTF Bites:

    1. mbstowcs. Did the person who came up with that method just mash their hand over the keyboard?

    That was my first reaction as well. I sure hope I never need to use that function, 'cause there is no way I can remember its name. Even Unix commands seem less cryptic, and that's saying a lot.

    That's clearly "multi-byte string to wide-character string".



  • @tsaukpaetra said in WTF Bites:

    Made a loop to move to the next delectable button in an array:

    Do you have a loop for the buttons that don't taste so good?


  • kills Dumbledore

    @djls45 said in WTF Bites:

    clearly

    0_1512156701800_18buy3.jpg


Log in to reply