Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Let's "switch" to Something Else

Let's "switch" to Something Else

Scheduled Pinned Locked Moved The Weird and The Wonderful
c++
34 Posts 17 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Tom Delany

    C++ code. This is either brilliant, or just bloody awful. I'm coming down on the side of "bloody awful": recv in the curr structure below is declared as:

    BYTE recv\[64\];
    
    DWORD   verb;
    ...
    verb = (upper\[curr->recv\[0\]\] << 24) + (upper\[curr->recv\[1\]\] << 16) + (upper\[curr->recv\[2\]\] << 8) + 0x20;
    
    switch (verb)
    {
    case 'CMD ':
       ...
       break;
    
    case 'MON ':
       ...
       break;
    
    case 'END ':
       ...
       break;
    
    case 'EXT ':
       ...
       break;
    
    default:
        ...
        break;
    }
    

    WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated. There are 10 kinds of people in the world: People who know binary and people who don't.

    P Offline
    P Offline
    PIEBALDconsult
    wrote on last edited by
    #5

    I did similar in C about twenty years ago, but I could only fit two characters into a character literal. If you'll pardon my rusty C:

    char* s ;
    // read a line from a file into s
    short* i = s ;
    switch ( *i ) ...

    This enabled me to easily parse some reports. I wish I still had the code. :sigh:

    H 1 Reply Last reply
    0
    • T Tom Delany

      C++ code. This is either brilliant, or just bloody awful. I'm coming down on the side of "bloody awful": recv in the curr structure below is declared as:

      BYTE recv\[64\];
      
      DWORD   verb;
      ...
      verb = (upper\[curr->recv\[0\]\] << 24) + (upper\[curr->recv\[1\]\] << 16) + (upper\[curr->recv\[2\]\] << 8) + 0x20;
      
      switch (verb)
      {
      case 'CMD ':
         ...
         break;
      
      case 'MON ':
         ...
         break;
      
      case 'END ':
         ...
         break;
      
      case 'EXT ':
         ...
         break;
      
      default:
          ...
          break;
      }
      

      WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated. There are 10 kinds of people in the world: People who know binary and people who don't.

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #6

      It's ugly, yes. But it also has the potential to be fast. By loading the three characters and one space into a machine word, it is a single machine instruction to compare each value, and if the compiler spots that - and it damn well should - then it can use a machine-word look up table and index into a machine-word jump table. And you can't get much faster than that! If this is in time-critical code, then it could be justified. Provided it was well commented. If it's in user input handling, then the author needs to meet Mr Firing Squad for being too "clever". :laugh:

      The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      T 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        It's ugly, yes. But it also has the potential to be fast. By loading the three characters and one space into a machine word, it is a single machine instruction to compare each value, and if the compiler spots that - and it damn well should - then it can use a machine-word look up table and index into a machine-word jump table. And you can't get much faster than that! If this is in time-critical code, then it could be justified. Provided it was well commented. If it's in user input handling, then the author needs to meet Mr Firing Squad for being too "clever". :laugh:

        The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

        T Offline
        T Offline
        Tom Delany
        wrote on last edited by
        #7

        It's in a thread where it's listening for something from a TCPIP connection (sort of a command channel). It's low-volume, and as such, I don't think the "cleverness" is warranted. And no, it's not commented at all. It's not that I don't understand what he was doing. I just think he was being too clever for his own good. And in the (unlikely?) event that the code is ever ported to a big-endian system, it would be completely broken.

        WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated. There are 10 kinds of people in the world: People who know binary and people who don't.

        OriginalGriffO B N J 4 Replies Last reply
        0
        • T Tom Delany

          It's in a thread where it's listening for something from a TCPIP connection (sort of a command channel). It's low-volume, and as such, I don't think the "cleverness" is warranted. And no, it's not commented at all. It's not that I don't understand what he was doing. I just think he was being too clever for his own good. And in the (unlikely?) event that the code is ever ported to a big-endian system, it would be completely broken.

          WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated. There are 10 kinds of people in the world: People who know binary and people who don't.

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #8

          Perhaps not a meeting with Mr Firing Squad than, but a meeting with Mr Big Stick seems warranted. :laugh:

          The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          1 Reply Last reply
          0
          • T Tom Delany

            It's in a thread where it's listening for something from a TCPIP connection (sort of a command channel). It's low-volume, and as such, I don't think the "cleverness" is warranted. And no, it's not commented at all. It's not that I don't understand what he was doing. I just think he was being too clever for his own good. And in the (unlikely?) event that the code is ever ported to a big-endian system, it would be completely broken.

            WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated. There are 10 kinds of people in the world: People who know binary and people who don't.

            B Offline
            B Offline
            BobJanova
            wrote on last edited by
            #9

            I don't think it would, as it's doing the + and shift manually. If it was just switch(recv[0]) then it would be endian vulnerable. In the absence of language or framework features that allow you to switch on a string, I have no problem with this.

            J 1 Reply Last reply
            0
            • T Tom Delany

              C++ code. This is either brilliant, or just bloody awful. I'm coming down on the side of "bloody awful": recv in the curr structure below is declared as:

              BYTE recv\[64\];
              
              DWORD   verb;
              ...
              verb = (upper\[curr->recv\[0\]\] << 24) + (upper\[curr->recv\[1\]\] << 16) + (upper\[curr->recv\[2\]\] << 8) + 0x20;
              
              switch (verb)
              {
              case 'CMD ':
                 ...
                 break;
              
              case 'MON ':
                 ...
                 break;
              
              case 'END ':
                 ...
                 break;
              
              case 'EXT ':
                 ...
                 break;
              
              default:
                  ...
                  break;
              }
              

              WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated. There are 10 kinds of people in the world: People who know binary and people who don't.

              G Offline
              G Offline
              Gary Wheeler
              wrote on last edited by
              #10

              That's how you handle tags in ICC profiles, for example. It's a perfectly reasonable thing to do.

              Tom Delany wrote:

              WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated.

              Still one of my favorite sigs on CP.

              Software Zen: delete this;

              J 1 Reply Last reply
              0
              • B BobJanova

                I don't think it would, as it's doing the + and shift manually. If it was just switch(recv[0]) then it would be endian vulnerable. In the absence of language or framework features that allow you to switch on a string, I have no problem with this.

                J Offline
                J Offline
                jibalt
                wrote on last edited by
                #11

                You've missed the point ... all multibyte char literals are endian-sensitive. To make it endian-insensitive, it should be

                #define VB(a,b,c) (((((a)<<8)+(b))<<8)+(c))

                verb = VB(upper[curr->recv[0]], upper[curr->recv[1]], upper[curr->recv[2]]);

                switch (verb)
                {
                case VB('C','M','D'):
                ...

                B R J 3 Replies Last reply
                0
                • G Gary Wheeler

                  That's how you handle tags in ICC profiles, for example. It's a perfectly reasonable thing to do.

                  Tom Delany wrote:

                  WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated.

                  Still one of my favorite sigs on CP.

                  Software Zen: delete this;

                  J Offline
                  J Offline
                  jibalt
                  wrote on last edited by
                  #12

                  No endian-sensitive code (which includes all multibyte chars) is "perfectly sensible".

                  G 1 Reply Last reply
                  0
                  • J jibalt

                    No endian-sensitive code (which includes all multibyte chars) is "perfectly sensible".

                    G Offline
                    G Offline
                    Gary Wheeler
                    wrote on last edited by
                    #13

                    'Sensibility' of a particular approach always depends upon the problem being solved. Making code endian-insensitive is necessary only if you plan on porting it between different endian environments.

                    Software Zen: delete this;

                    1 Reply Last reply
                    0
                    • J jibalt

                      You've missed the point ... all multibyte char literals are endian-sensitive. To make it endian-insensitive, it should be

                      #define VB(a,b,c) (((((a)<<8)+(b))<<8)+(c))

                      verb = VB(upper[curr->recv[0]], upper[curr->recv[1]], upper[curr->recv[2]]);

                      switch (verb)
                      {
                      case VB('C','M','D'):
                      ...

                      B Offline
                      B Offline
                      BobJanova
                      wrote on last edited by
                      #14

                      Oh really? So in that case doing switch(*recv) should be safe.

                      J 1 Reply Last reply
                      0
                      • B BobJanova

                        Oh really? So in that case doing switch(*recv) should be safe.

                        J Offline
                        J Offline
                        jibalt
                        wrote on last edited by
                        #15

                        I have no idea which of the things I wrote are the subject of your "oh really", but all of them are correct. As for the rest, it's a non sequitur. Both *recv and multi-char literals are endian-sensitive, so you appear to have committed a fallacy of affirmation of the consequent ... a rather basic failure of logic. But if you want to go around switching on 4-char literals thinking that it's portable, be my guest ... just don't do it in any code that might affect my life.

                        B 1 Reply Last reply
                        0
                        • T Tom Delany

                          It's in a thread where it's listening for something from a TCPIP connection (sort of a command channel). It's low-volume, and as such, I don't think the "cleverness" is warranted. And no, it's not commented at all. It's not that I don't understand what he was doing. I just think he was being too clever for his own good. And in the (unlikely?) event that the code is ever ported to a big-endian system, it would be completely broken.

                          WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated. There are 10 kinds of people in the world: People who know binary and people who don't.

                          N Offline
                          N Offline
                          NAANsoft
                          wrote on last edited by
                          #16

                          A wee bit of commmenting would be nice, but other than that I cannot see a problem here. As for porting to big-endian system: First of all, that will never happen. Secondly, there will be more places to fix if it did happen. Thirdly, the code is platform-restricted anyway (to Windows), so there is not a problem...:cool:

                          R 1 Reply Last reply
                          0
                          • T Tom Delany

                            C++ code. This is either brilliant, or just bloody awful. I'm coming down on the side of "bloody awful": recv in the curr structure below is declared as:

                            BYTE recv\[64\];
                            
                            DWORD   verb;
                            ...
                            verb = (upper\[curr->recv\[0\]\] << 24) + (upper\[curr->recv\[1\]\] << 16) + (upper\[curr->recv\[2\]\] << 8) + 0x20;
                            
                            switch (verb)
                            {
                            case 'CMD ':
                               ...
                               break;
                            
                            case 'MON ':
                               ...
                               break;
                            
                            case 'END ':
                               ...
                               break;
                            
                            case 'EXT ':
                               ...
                               break;
                            
                            default:
                                ...
                                break;
                            }
                            

                            WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated. There are 10 kinds of people in the world: People who know binary and people who don't.

                            C Offline
                            C Offline
                            ClockMeister
                            wrote on last edited by
                            #17

                            ?? what's wrong with it?

                            1 Reply Last reply
                            0
                            • J jibalt

                              You've missed the point ... all multibyte char literals are endian-sensitive. To make it endian-insensitive, it should be

                              #define VB(a,b,c) (((((a)<<8)+(b))<<8)+(c))

                              verb = VB(upper[curr->recv[0]], upper[curr->recv[1]], upper[curr->recv[2]]);

                              switch (verb)
                              {
                              case VB('C','M','D'):
                              ...

                              R Offline
                              R Offline
                              Rob Grainger
                              wrote on last edited by
                              #18

                              You're not proposing he should use VB are you?

                              1 Reply Last reply
                              0
                              • N NAANsoft

                                A wee bit of commmenting would be nice, but other than that I cannot see a problem here. As for porting to big-endian system: First of all, that will never happen. Secondly, there will be more places to fix if it did happen. Thirdly, the code is platform-restricted anyway (to Windows), so there is not a problem...:cool:

                                R Offline
                                R Offline
                                Rob Grainger
                                wrote on last edited by
                                #19

                                Why is that code platform-restricted to Windows? I've seen DWORDs on many platforms, so there's really nothing here to suggest that.

                                N 1 Reply Last reply
                                0
                                • R Rob Grainger

                                  Why is that code platform-restricted to Windows? I've seen DWORDs on many platforms, so there's really nothing here to suggest that.

                                  N Offline
                                  N Offline
                                  NAANsoft
                                  wrote on last edited by
                                  #20

                                  Ah, but it was Windows that started it. Nyah, nyah... ;P As for the two other points, they are still valid...

                                  1 Reply Last reply
                                  0
                                  • T Tom Delany

                                    C++ code. This is either brilliant, or just bloody awful. I'm coming down on the side of "bloody awful": recv in the curr structure below is declared as:

                                    BYTE recv\[64\];
                                    
                                    DWORD   verb;
                                    ...
                                    verb = (upper\[curr->recv\[0\]\] << 24) + (upper\[curr->recv\[1\]\] << 16) + (upper\[curr->recv\[2\]\] << 8) + 0x20;
                                    
                                    switch (verb)
                                    {
                                    case 'CMD ':
                                       ...
                                       break;
                                    
                                    case 'MON ':
                                       ...
                                       break;
                                    
                                    case 'END ':
                                       ...
                                       break;
                                    
                                    case 'EXT ':
                                       ...
                                       break;
                                    
                                    default:
                                        ...
                                        break;
                                    }
                                    

                                    WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated. There are 10 kinds of people in the world: People who know binary and people who don't.

                                    R Offline
                                    R Offline
                                    RafagaX
                                    wrote on last edited by
                                    #21

                                    I suppose he didn't liked If - else- ifs...

                                    CEO at: - Rafaga Systems - Para Facturas - Modern Components for the moment...

                                    1 Reply Last reply
                                    0
                                    • T Tom Delany

                                      C++ code. This is either brilliant, or just bloody awful. I'm coming down on the side of "bloody awful": recv in the curr structure below is declared as:

                                      BYTE recv\[64\];
                                      
                                      DWORD   verb;
                                      ...
                                      verb = (upper\[curr->recv\[0\]\] << 24) + (upper\[curr->recv\[1\]\] << 16) + (upper\[curr->recv\[2\]\] << 8) + 0x20;
                                      
                                      switch (verb)
                                      {
                                      case 'CMD ':
                                         ...
                                         break;
                                      
                                      case 'MON ':
                                         ...
                                         break;
                                      
                                      case 'END ':
                                         ...
                                         break;
                                      
                                      case 'EXT ':
                                         ...
                                         break;
                                      
                                      default:
                                          ...
                                          break;
                                      }
                                      

                                      WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated. There are 10 kinds of people in the world: People who know binary and people who don't.

                                      M Offline
                                      M Offline
                                      Member_5893260
                                      wrote on last edited by
                                      #22

                                      It's sort of both... it'd be properly brilliant if the "verb =" line were commented...

                                      1 Reply Last reply
                                      0
                                      • T Tom Delany

                                        It's in a thread where it's listening for something from a TCPIP connection (sort of a command channel). It's low-volume, and as such, I don't think the "cleverness" is warranted. And no, it's not commented at all. It's not that I don't understand what he was doing. I just think he was being too clever for his own good. And in the (unlikely?) event that the code is ever ported to a big-endian system, it would be completely broken.

                                        WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated. There are 10 kinds of people in the world: People who know binary and people who don't.

                                        J Offline
                                        J Offline
                                        jschell
                                        wrote on last edited by
                                        #23

                                        Tom Delany wrote:

                                        event that the code is ever ported to a big-endian system, it would be completely broken.

                                        No it wouldn't. The code POSTED would work regardless of that. As mentioned it is a TCP stream so it sequential. The switch statement is using ascii. The only way endianess would matter would be if the stream started to use a different character set. And if it did that it would fail if 1. The character system was not using a 8 bit lower representation of that matched ascii (UTF8 does where UTF16 does not.) 2. AND if the protocol changed. And certainly if item 2 is true then all sorts of problems could result. Such as the rest of the code, following the switch, failing as well.

                                        1 Reply Last reply
                                        0
                                        • J jibalt

                                          You've missed the point ... all multibyte char literals are endian-sensitive. To make it endian-insensitive, it should be

                                          #define VB(a,b,c) (((((a)<<8)+(b))<<8)+(c))

                                          verb = VB(upper[curr->recv[0]], upper[curr->recv[1]], upper[curr->recv[2]]);

                                          switch (verb)
                                          {
                                          case VB('C','M','D'):
                                          ...

                                          J Offline
                                          J Offline
                                          jschell
                                          wrote on last edited by
                                          #24

                                          jibalt wrote:

                                          You've missed the point ... all multibyte char literals are endian-sensitive.
                                           
                                          To make it endian-insensitive, it should be

                                          That however ignores the point that the code was written to support a specific protocol over TCP. If the character set of the protocol changed then that would be just one thing that would likely break.

                                          J 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups