Monday, March 12, 2007

Protecting Projects from Poisonous People

There was a talk at Google on Jan. 25, 2007 entitled "How to Protect Your Open Source Project from Poisonous People". A good deal of us have met one of the presenters, Brian Fitzpatrick. I'm sure some of you saw this on Google's site or on Slashdot. In case you skipped over it, didn't know of its existence, or want an easy place to be able to find it again, I'm posting the video here.

I suggest that everyone watch this. Since we are all running and working on open source projects this could be very helpful to all of us.

(Also, I think it is a good example of how we should capture and archive our ETL presentations)

Monday, February 19, 2007

Java issue: making sure a string is an int

So the more I work with numbers, the more I confront strings that need to be converted to numbers.

I'm tired of not knowing the best way to check if a String is a valid integer. I know that there are libraries out there (I think apache has one) that does this efficiently but I don't want another library. Of course, I googled the issue and brainstormed some.

From what I can think of there are 4 ways to do this:
1) try/catch with the Integer.parse(String s)
2) iterating through the string and checking Character.isDigit(character), with allowing the first one to be a '-'
3) using string.match("[+-]?[0-9]+");
4) a precompiled pattern using "[+-]?[0-9]+"

So I wrote a program that goes through a randomly generated array of a million integers and tries each way on the whole array. I thought you all would be interested in the results.

What I normally got (plus or minus some inconsequential amount of milliseconds):

1) 228
2) 190
3) 51136
4) 7722

By the way, I know that other people have done similar things. There is a guy at IBM who did it ( http://www-03.ibm.com/developerworks/blogs/page/BillHiggins?entry=be_careful_about_using_java) but only with positive numbers and that wasn't good enough for me. I needed to see how the '-' impacted the findings. It didn't change the results too much. Coincidentally, we both also came up with the same 4 ways.

I'm attaching the code I used. If any of you can think of a different way to check then please let me know.

(If you noticed anything I did wrong then let me know as well).

Thanks,
Peter

PS: The Code

import java.util.Calendar;
import java.util.Random;
import java.util.regex.Pattern;


public class IsInteger {

public final static Pattern integerPattern = Pattern.compile("[-+]?[0-9]+");


public static boolean isIntegerExceptionCatching(String string) {
try {
Integer.parseInt(string);
} catch (NumberFormatException nfe) {
return false;
}
return true;
}

public static boolean isIntegerStringMatch(String string) {
return string.matches("[+-]?[0-9]+");
}

public static boolean isIntegerPrecompiledPattern(String string) {
return integerPattern.matcher(string).matches();
}

public static boolean isIntegerCharacterMatching(String string) {
int start = 0;
if (string.charAt(0) == '-') {
start = 1;
}
for ( int i = start; i < string.length(); i++) {;
char c = string.charAt(i);
if (!(Character.isDigit(c) || c == '-')) return false;
}
return true;
}


public static void main(String[] args) {

int amount = 1000000;
String[] values = new String[amount];
//just to be sure, I tried with a couple of different seeds
//Random generator = new Random( 49302037 );
//Random generator = new Random( 289087293 );
Random generator = new Random( 194387934 );
for (int i = 0; i < amount; i++) {
values[i] = Integer.toString(generator.nextInt());
}

long start = Calendar.getInstance().getTimeInMillis();
for (int i = 0; i < amount; i++) {
isIntegerExceptionCatching(values[i]);
}
long end = Calendar.getInstance().getTimeInMillis();
System.out.println("Exception Catching: " + (end - start));

start = Calendar.getInstance().getTimeInMillis();
for (int i = 0; i < amount; i++) {
isIntegerPrecompiledPattern(values[i]);
}
end = Calendar.getInstance().getTimeInMillis();
System.out.println("Precompiled Pattern: " + (end - start));

start = Calendar.getInstance().getTimeInMillis();
for (int i = 0; i < amount; i++) {
isIntegerStringMatch(values[i]);
}
end = Calendar.getInstance().getTimeInMillis();
System.out.println("String matching: " + (end - start));

start = Calendar.getInstance().getTimeInMillis();
for (int i = 0; i < amount; i++) {
isIntegerCharacterMatching(values[i]);
}
end = Calendar.getInstance().getTimeInMillis();
System.out.println("Character Check: " + (end - start));

}

}

Sunday, January 28, 2007

Xcode: Poor Subversion Support

Ayayayay... Let me quote from Getting Control with Subversion and Xcode:

Support for Subversion is built into Xcode (version 1.5 and later). However, you must create your Subversion repository and import your project into Subversion on the command line before managing it in Xcode.

I think such poor support is inconsistent with Xcode's mission. The gap between Eclipse and the other participants is widening...