Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

goto prevails...

Name: Anonymous 2007-09-14 18:15 ID:exfP63F/

...teh haterz phail.

int valid_ip(const char *ip_addr) {
    int rem;
    size_t dots;
    char ch, *ip_ptr;

    if (!(ip_ptr = (char *) ip_addr)) {
        return 0;
    }

    dots = 0, rem = 3;    __asm {xor ebx, ebx};
    for (; ch = *ip_ptr; ++ip_ptr) {
        __asm push ecx __asm mov ecx, ebx __asm jecxz ECX_WEAK0 __asm dec ecx
        __asm jecxz ECX_WEAK1 __asm dec ecx __asm jecxz ECX_WEAK2 __asm jmp STRONG
        ECX_WEAK0: goto WEAK0; ECX_WEAK1: goto WEAK1; ECX_WEAK2: goto WEAK2;

        STRONG:
        __asm pop ecx
        switch (ch) {
        case '0': case '1':    case '2': case '3':    case '4': case '5': case '6':    case '7': case '8':    case '9': --rem; goto DIGIT_CHECK;
        case '.': ++dots; goto DOT_CHECK;
        default: return 0;
        }

        WEAK0:
        __asm pop ecx
        switch (ch) {
        case '0': case '1':    --rem; __asm {mov ebx, 3}; goto DIGIT_CHECK;
        case '2': --rem; __asm {inc ebx}; goto DIGIT_CHECK;
        case '3': case '4': case '5': case '6': case '7': case '8': case '9': rem -= 2; __asm {mov ebx, 3}; goto DIGIT_CHECK;
        case '.': ++dots; goto DOT_CHECK;
        default: return 0;
        }

        WEAK1:
        __asm pop ecx
        switch (ch) {
        case '0': case '1':    case '2': case '3':    case '4': --rem; __asm {mov ebx, 3}; goto DIGIT_CHECK;
        case '5': --rem; __asm {inc ebx}; goto DIGIT_CHECK;
        case '6': case '7': case '8': case '9': rem = 0; __asm {mov ebx, 3}; goto DIGIT_CHECK;
        case '.': ++dots; goto DOT_CHECK;
        default: return 0;
        }

        WEAK2:
        __asm pop ecx
        switch (ch) {
        case '0': case '1':    case '2': case '3':    case '4': case '5': --rem; __asm {mov ebx, 3}; goto DIGIT_CHECK;
        case '.': ++dots; goto DOT_CHECK;
        default: return 0;
        }

        DIGIT_CHECK: if (rem < 0) {return 0;} else {continue;}
        DOT_CHECK: if (rem == 3 || dots > 3) {return 0;} else {rem = 3; __asm {xor ebx, ebx}; continue;}
    }

    return (rem != 3 && dots == 3) ? 1 : 0;
}

Name: Anonymous 2007-09-15 16:50 ID:fQFnkf8W

In addition to the senseless use of x86 assembly and goto's, and the shitty indentation I would like to point you to the following:

1. Not using constant initializers for rem and dots
2. Using size_t intstead of an (unsigned) int for dots
3. Assignment inside of an if statement
4. Using "!" instead of the far more readable "!= NULL"
5. Casting a const char* to a char*
6. The use of the sometimes slow "jecxz" instead of the more natural "jz", making it worse than what most compilers would generate
7. The use of goto's that could have been omitted if the jumps just went straight their targets
8. Repeated code for the '.' and default cases and the use of gotos because the switches should be the other way around

>(all that case '0': ... case '9': shit is by far WORSE than a ch >= '0' && ch <= '9')
Any compiler worth using will output the same code for both.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List