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;
}
use gcc and do case '0' ... '9':, you worthless faggot
Name:
Anonymous2007-09-14 18:43 ID:exfP63F/
>>3
I don't use GCC. I would have used if if I felt like doing that. The compiler has some chance of optimizing the switches which is why they were used.
>>13
are you trolling you fucking piece of shit?
First of all, using a single `char' instead of `int' causes most compilers to produce more assembly.
He has no idea about base2, (all that case '0': ... case '9': shit is by far WORSE than a ch >= '0' && ch <= '9')
He uses another pointer (ip_ptr) where he could just use ip_addr.
And all that inline assembly makes it so unportable unmaintanable and shitty .. seriously i'm going to puke.
int valid_ipv4(const char *);
/* dont pass NULL faggot. */
int valid_ipv4(const char *ip) {
>>14
So I suppose 6, 7, 8, 9 can't appear in IP addresses. You exponentially phail.
Name:
Anonymous2007-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.
Name:
Anonymous2007-09-15 17:03 ID:qPpm2sL3
Why didn't you write it entirely in assembler?
Name:
Anonymous2007-09-15 17:16 ID:PlK+8pzu
ENTERPRISE TURNKEY INTERNET PROTOCOL ADDRESS VERIFICATION SYSTEM
int valid_ipv4( const char * ip )
{
uint8_t dc = 3;
uint16_t f = 0xFFFF;
nloop:
if ( *ip == '.' )
{
if ( !dc )
{
return 0;
}
dc--;
if ( f != 0xFFFF )
{
if ( f > 0xFF )
{
return 0;
}
f = 0xFFFF;
}
goto nloop_cont;
}
if ( *ip >= '0'
&& *ip <= '9' )
{
if ( f == 0xFFFF )
{
f = 0;
}
3. Assignment inside of an if statement
This being bad is arguable.
4. Using "!" instead of the far more readable "!= NULL"
This is bullshite. Why not then clarify: a != NULL != NULL? Or a != NULL != NULL != NULL, which is even more readable.
Name:
Anonymous2007-09-15 20:15 ID:d2asGXBU
>>19
The proper, agreed-upon way to write this is !(a != NULL) == 0.
Name:
Anonymous2007-09-15 20:19 ID:ZrRYnvOP
This being bad is arguable.
No it's not. It's begging for a bug.
Why not then clarify: a != NULL != NULL? Or a != NULL != NULL != NULL, which is even more readable.
That has to be the stupidest thing I've read today on 4chan. I regret to inform you that he has a point, your ridiculous example to the contrary.
I think it's pretty obvious which of you two has written more C.
Name:
Anonymous2007-09-15 20:40 ID:fQFnkf8W
>>19
Funny how you nitpick on that while you didn't notice on the quite big mistake I made in that statement.
I meant:
4. Using "!" instead of the far more readable "== NULL"
Of course this only applies to checks on pointers.
Good answers and all, but what about flexibility? Extensibility? Portability? Well-defined APIs?
package org.fourchan.dis.prog.faggotry;
/**
* IPv4Checker.java
*
* @author Anonymous
*/
public class IPv4Checker implements IIPChecker {
private static final String DELIMITER_REGEXP = "\\.";
private static final int NUM_BLOCKS = 4;
/**
* The string to be checked
*/
private String s;
/**
* Helper class to check a number block for validity.
*/
private class BlockChecker {
private String block;
public BlockChecker(String b) {
this.block = b;
}
private boolean isValidBlock() {
try {
int n = Integer.parseInt(this.block);
return (0 <= n && n <= 255);
} catch (NumberFormatException e) {
return false;
}
}
}
/**
* Constructor
* @param s String to be checked for IPv4 validity.
*/
public IPv4Checker(String s) {
this.s = s;
}
/**
* @return <code>true</code> if and only if the String with which this
* IPv4Checker was instantiated is a valid IPv4 IP address.
*/
public boolean isValidIP() {
String[] blocks = this.s.split(DELIMITER_REGEXP);
if (blocks.length != NUM_BLOCKS)
return false;
for (int i = 0; i < NUM_BLOCKS; i++) {
if (!new BlockChecker(blocks[i]).isValidBlock())
return false;
}
return true;
}
public static void main(String[] args) {
/*
* Perform unit tests
*/
System.out.println(new IPv4Checker("192.168.0.1").isValidIP());
System.out.println(new IPv4Checker("255.255.255.255").isValidIP());
}
}
You'll notice I haven't written IIPChecker. I'll leave that as an exercise to the reader.