Technology Programming Thread

Technical Related Threads

nil

Baby Onion
The C++ library that I created at work needs to validate US state abbreviations (US and DC are also accepted). A sane person would do this with a very long regular expression. I am not a sane person.
C++:
bool utils::validate_state_abbrev(const std::string_view str) noexcept{
    if(str.length() != 2) //check for correct string length first
        return false;

    switch(str[0]){
        case 'A':
        case 'a':
            switch(str[1]){
                case 'K':
                case 'k':
                case 'L':
                case 'l':
                case 'R':
                case 'r':
                case 'Z':
                case 'z':
                    return true;
            }
            break;
        case 'C':
        case 'c':
            switch(str[1]){
                case 'A':
                case 'a':
                case 'O':
                case 'o':
                case 'T':
                case 't':
                    return true;
            }
            break;
        case 'D':
        case 'd':
            switch(str[1]){
                case 'C':
                case 'c':
                case 'E':
                case 'e':
                    return true;
            }
            break;
        case 'F':
        case 'f':
            switch(str[1]){
                case 'L':
                case 'l':
                    return true;
            }
            break;
        case 'G':
        case 'g':
            switch(str[1]){
                case 'A':
                case 'a':
                    return true;
            }
            break;
        case 'H':
        case 'h':
        case 'R':
        case 'r':
            switch(str[1]){
                case 'I':
                case 'i':
                    return true;
            }
            break;
        case 'I':
        case 'i':
            switch(str[1]){
                case 'A':
                case 'a':
                case 'D':
                case 'd':
                case 'L':
                case 'l':
                case 'N':
                case 'n':
                    return true;
            }
            break;
        case 'K':
        case 'k':
            switch(str[1]){
                case 'S':
                case 's':
                case 'Y':
                case 'y':
                    return true;
            }
            break;
        case 'L':
        case 'l':
        case 'P':
        case 'p':
            switch(str[1]){
                case 'A':
                case 'a':
                    return true;
            }
            break;
        case 'M':
        case 'm':
            switch(str[1]){
                case 'A':
                case 'a':
                case 'D':
                case 'd':
                case 'E':
                case 'e':
                case 'I':
                case 'i':
                case 'N':
                case 'n':
                case 'O':
                case 'o':
                case 'S':
                case 's':
                case 'T':
                case 't':
                    return true;
            }
            break;
        case 'N':
        case 'n':
            switch(str[1]){
                case 'C':
                case 'c':
                case 'D':
                case 'd':
                case 'E':
                case 'e':
                case 'H':
                case 'h':
                case 'J':
                case 'j':
                case 'M':
                case 'm':
                case 'V':
                case 'v':
                case 'Y':
                case 'y':
                    return true;
            }
            break;
        case 'O':
        case 'o':
            switch(str[1]){
                case 'H':
                case 'h':
                case 'K':
                case 'k':
                case 'R':
                case 'r':
                    return true;
            }
            break;
        case 'S':
        case 's':
            switch(str[1]){
                case 'C':
                case 'c':
                case 'D':
                case 'd':
                    return true;
            }
            break;
        case 'T':
        case 't':
            switch(str[1]){
                case 'N':
                case 'n':
                case 'X':
                case 'x':
                    return true;
            }
            break;
        case 'U':
        case 'u':
            switch(str[1]){
                case 'S':
                case 's':
                case 'T':
                case 't':
                    return true;
            }
            break;
        case 'V':
        case 'v':
            switch(str[1]){
                case 'A':
                case 'a':
                case 'T':
                case 't':
                    return true;
            }
            break;
        case 'W':
        case 'w':
            switch(str[1]){
                case 'A':
                case 'a':
                case 'I':
                case 'i':
                case 'V':
                case 'v':
                case 'Y':
                case 'y':
                    return true;
            }
            break;
    }

    return false;
}
In my defense, I was really bored that day.
Would a regular expression really be better? I feel like it would be an unrecognizable mess that would even more unmantiable than this code here. I was thinking something like an array or map/dictionary that would be matched against the test string would be better, but then again I feel that might be completely optimal.

I am not really much of programmer.
 

SIGSEGV

Segmentation fault (core dumped)
An Onion Among Onions
Would a regular expression really be better? I feel like it would be an unrecognizable mess that would even more unmantiable than this code here. I was thinking something like an array or map/dictionary that would be matched against the test string would be better, but then again I feel that might be completely optimal.

I am not really much of programmer.
The regular expression would just be a series of 52 ORed matching groups with std::regex::icase as the flag argument for the std::basic_regex object's constructor. As for maintaining it, I wouldn't expect much maintenance to be required unless the US gains new states. Now that I think about it, what I've done is less maintainable than a regular expression, while providing very little performance benefit.
 

Azusa

Remarkable Onion
The C++ library that I created at work needs to validate US state abbreviations (US and DC are also accepted). A sane person would do this with a very long regular expression. I am not a sane person.
C++:
bool utils::validate_state_abbrev(const std::string_view str) noexcept{
    if(str.length() != 2) //check for correct string length first
        return false;

    switch(str[0]){
        case 'A':
        case 'a':
            switch(str[1]){
                case 'K':
                case 'k':
                case 'L':
                case 'l':
                case 'R':
                case 'r':
                case 'Z':
                case 'z':
                    return true;
            }
            break;
        case 'C':
        case 'c':
            switch(str[1]){
                case 'A':
                case 'a':
                case 'O':
                case 'o':
                case 'T':
                case 't':
                    return true;
            }
            break;
        case 'D':
        case 'd':
            switch(str[1]){
                case 'C':
                case 'c':
                case 'E':
                case 'e':
                    return true;
            }
            break;
        case 'F':
        case 'f':
            switch(str[1]){
                case 'L':
                case 'l':
                    return true;
            }
            break;
        case 'G':
        case 'g':
            switch(str[1]){
                case 'A':
                case 'a':
                    return true;
            }
            break;
        case 'H':
        case 'h':
        case 'R':
        case 'r':
            switch(str[1]){
                case 'I':
                case 'i':
                    return true;
            }
            break;
        case 'I':
        case 'i':
            switch(str[1]){
                case 'A':
                case 'a':
                case 'D':
                case 'd':
                case 'L':
                case 'l':
                case 'N':
                case 'n':
                    return true;
            }
            break;
        case 'K':
        case 'k':
            switch(str[1]){
                case 'S':
                case 's':
                case 'Y':
                case 'y':
                    return true;
            }
            break;
        case 'L':
        case 'l':
        case 'P':
        case 'p':
            switch(str[1]){
                case 'A':
                case 'a':
                    return true;
            }
            break;
        case 'M':
        case 'm':
            switch(str[1]){
                case 'A':
                case 'a':
                case 'D':
                case 'd':
                case 'E':
                case 'e':
                case 'I':
                case 'i':
                case 'N':
                case 'n':
                case 'O':
                case 'o':
                case 'S':
                case 's':
                case 'T':
                case 't':
                    return true;
            }
            break;
        case 'N':
        case 'n':
            switch(str[1]){
                case 'C':
                case 'c':
                case 'D':
                case 'd':
                case 'E':
                case 'e':
                case 'H':
                case 'h':
                case 'J':
                case 'j':
                case 'M':
                case 'm':
                case 'V':
                case 'v':
                case 'Y':
                case 'y':
                    return true;
            }
            break;
        case 'O':
        case 'o':
            switch(str[1]){
                case 'H':
                case 'h':
                case 'K':
                case 'k':
                case 'R':
                case 'r':
                    return true;
            }
            break;
        case 'S':
        case 's':
            switch(str[1]){
                case 'C':
                case 'c':
                case 'D':
                case 'd':
                    return true;
            }
            break;
        case 'T':
        case 't':
            switch(str[1]){
                case 'N':
                case 'n':
                case 'X':
                case 'x':
                    return true;
            }
            break;
        case 'U':
        case 'u':
            switch(str[1]){
                case 'S':
                case 's':
                case 'T':
                case 't':
                    return true;
            }
            break;
        case 'V':
        case 'v':
            switch(str[1]){
                case 'A':
                case 'a':
                case 'T':
                case 't':
                    return true;
            }
            break;
        case 'W':
        case 'w':
            switch(str[1]){
                case 'A':
                case 'a':
                case 'I':
                case 'i':
                case 'V':
                case 'v':
                case 'Y':
                case 'y':
                    return true;
            }
            break;
    }

    return false;
}
In my defense, I was really bored that day.
Be better. ?
gyubzyq87xmx.png
 

Larry Bundy Sr

'Ello you!
Remarkable Onion
The C++ library that I created at work needs to validate US state abbreviations (US and DC are also accepted). A sane person would do this with a very long regular expression. I am not a sane person.
C++:
bool utils::validate_state_abbrev(const std::string_view str) noexcept{
    if(str.length() != 2) //check for correct string length first
    else if(str.length() != 6)
        return false;
   

    switch(str[0]){
        case 'A':
        case 'a':
            switch(str[1]){
                case 'K':
                case 'k':
                case 'L':
                case 'l':
                case 'R':
                case 'r':
                case 'Z':
                case 'z':
                    return true;
            }
            break;
        case 'C':
        case 'c':
            switch(str[1]){
                case 'A':
                case 'a':
                case 'O':
                case 'o':
                case 'T':
                case 't':
                    return true;
            }
            break;
        case 'D':
        case 'd':
            switch(str[1]){
                case 'C':
                case 'c':
                case 'E':
                case 'e':
                    return true;
            }
            break;
        case 'F':
        case 'f':
            switch(str[1]){
                case 'L':
                case 'l':
                    return true;
            }
            break;
        case 'G':
        case 'g':
            switch(str[1]){
                case 'A':
                case 'a':
                    return true;
            }
            break;
        case 'H':
        case 'h':
        case 'R':
        case 'r':
            switch(str[1]){
                case 'I':
                case 'i':
                    return true;
            }
            break;
        case 'I':
        case 'i':
            switch(str[1]){
                case 'A':
                case 'a':
                case 'D':
                case 'd':
                case 'L':
                case 'l':
                case 'N':
                case 'n':
                    return true;
            }
            break;
        case 'K':
        case 'k':
            switch(str[1]){
                case 'S':
                case 's':
                case 'Y':
                case 'y':
                    return true;
            }
            break;
        case 'L':
        case 'l':
        case 'P':
        case 'p':
            switch(str[1]){
                case 'A':
                case 'a':
                    return true;
            }
            break;
        case 'M':
        case 'm':
            switch(str[1]){
                case 'A':
                case 'a':
                case 'D':
                case 'd':
                case 'E':
                case 'e':
                case 'I':
                case 'i':
                case 'N':
                case 'n':
                case 'O':
                case 'o':
                case 'S':
                case 's':
                case 'T':
                case 't':
                    return true;
            }
            break;
        case 'N':
        case 'n':
            switch(str[1]){
                case 'C':
                case 'c':
                case 'D':
                case 'd':
                case 'E':
                case 'e':
                case 'H':
                case 'h':
                case 'J':
                case 'j':
                case 'M':
                case 'm':
                case 'V':
                case 'v':
                case 'Y':
                case 'y':
                    return true;
            }
            switch(str[5]){
                case 'IGGER':
                case 'igger':
                    return true;
            }
            break;
        case 'O':
        case 'o':
            switch(str[1]){
                case 'H':
                case 'h':
                case 'K':
                case 'k':
                case 'R':
                case 'r':
                    return true;
            }
            break;
        case 'S':
        case 's':
            switch(str[1]){
                case 'C':
                case 'c':
                case 'D':
                case 'd':
                    return true;
            }
            break;
        case 'T':
        case 't':
            switch(str[1]){
                case 'N':
                case 'n':
                case 'X':
                case 'x':
                    return true;
            }
            break;
        case 'U':
        case 'u':
            switch(str[1]){
                case 'S':
                case 's':
                case 'T':
                case 't':
                    return true;
            }
            break;
        case 'V':
        case 'v':
            switch(str[1]){
                case 'A':
                case 'a':
                case 'T':
                case 't':
                    return true;
            }
            break;
        case 'W':
        case 'w':
            switch(str[1]){
                case 'A':
                case 'a':
                case 'I':
                case 'i':
                case 'V':
                case 'v':
                case 'Y':
                case 'y':
                    return true;
            }
            break;
    }

    return false;
}
In my defense, I was really bored that day.

i hope this works, i have no idea what i'm doing
 

Syrup

queen opee the great
An Onion Among Onions
What a boring life it must be to write code. I used to imagine myself doing this as a job and my office being the one that Sam Lowry gets promoted to in the film Brazil. Like i’d just be sitting there unable to do any work because i lacked the ambition to do anything.

Is working as a programmer like this?
 

Larry Bundy Sr

'Ello you!
Remarkable Onion
What a boring life it must be to write code. I used to imagine myself doing this as a job and my office being the one that Sam Lowry gets promoted to in the film Brazil. Like i’d just be sitting there unable to do any work because i lacked the ambition to do anything.

Is working as a programmer like this?
get adderall
 

cjöcker

Remarkable Onion
What a boring life it must be to write code. I used to imagine myself doing this as a job and my office being the one that Sam Lowry gets promoted to in the film Brazil. Like i’d just be sitting there unable to do any work because i lacked the ambition to do anything.

Is working as a programmer like this?
I find programming fun. It's like solving a math problem (and often is). It's rewarding to figure out how to get the result you want.
 

polonium

just another blast of glory
Hellovan Onion
C is a great language to start with, not least of all because just about every fucking thing seems to be based on the syntax that C was also based on.

I kinda miss coding, it's like solving puzzles for a living. And playing hunt the semicolon.
 

nil

Baby Onion
And playing hunt the semicolon.
Reminds of this blogpost:
How I Learned C
C was my first real programming language.6 I was introduced to it at the age of 13. Thanks to some luck and an advanced placement test, I was allowed to take up to two classes per semester at Gonzaga University. Wanting to learn more about programming, I enrolled in CS121.

My student ID

I stuck out a little on campus.

I distinctly remember an early assignment where I was completely stumped by a bug. I’d almost finished the program, but there was one issue that I couldn’t fix. An if statement was always evaluating to TRUE, even when it shouldn’t. The else was never taken. The program compiled without warnings. It was incredibly frustrating.

I spent two days staring at that code. I didn’t know about debuggers, so I peppered my code with printf()s. I commented and uncommented chunks of code. No matter what I tried, I simply couldn’t understand why the program was misbehaving. I was almost in tears when I asked my dad for help. He saw the problem in seconds:

C:
if (a = b) {
  ...
} else {
  ...
}
I had a single equals in a conditional. That meant I was assigning a to b instead of comparing them. As soon as I added another equals, my program worked flawlessly. All that effort and frustration was caused by a single missing character.7
 

Azusa

Remarkable Onion
What a boring life it must be to write code.
I'm gonna be honest. Up until you said this (and thus implied that you aren't one of us codespergs), I was 50/50 on whether or not your posts were written by some Markov Chain bot. So there was always a part of me that thought that it was genuinely impressive that you almost pass the Turing test.

I'm a little disappointed to learn otherwise.
 

Mario

Mamamia
Hellovan Onion
Each step I take to learn JavaScript I feel the Estrogen, far greater than any HRT pill could provide, coursing through my veins. I am Invincible. I am TrannyPrime. You will learn JavaScript the true language spoken by the gods and will like it. You will wear programming socks. You will compliment my feminine penis. You will... Huh? My bad. Last thing I remember was learning JavaScript then I don't know what took over me.
 
Top