Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Bug in NSArray.componentsSeparatedByString()



Hi,

I just came across a strange bug in NSArray.componentsSeparatedByString(). Under certain conditions, the number of components in the array doesn't match the expected number.

I came across the problem while checking the number of columns in a CSV file. To reproduce it, the following conditions must be met:

1. Your last two colums are empty (String ends with two times the separator)
2a. You have a single-character separator AND your string is longer than 255 characters
OR
2b. You have a multi character separator


Here's the output of some sample code with WO 5.3.3. The result should always be 3.

Testing with two single character separators and empty last column
String lenght: 255, number of columns: 3
String lenght: 256, number of columns: 2
Testing with two single character separators and filled last column
String lenght: 255, number of columns: 3
String lenght: 256, number of columns: 3
Testing with two multi character separators and empty last column
String lenght: 255, number of columns: 2
String lenght: 256, number of columns: 2
Testing with two multi character separators and filled last column
String lenght: 255, number of columns: 3
String lenght: 256, number of columns: 3

Here's the reproduction code:

public static void main(String[] args) throws Exception {
System.out.println("Testing with two single character separators and empty last column");
testWithSeparator("|", null);
System.out.println("Testing with two single character separators and filled last column");
testWithSeparator("|", "x");
System.out.println("Testing with two multi character separators and empty last column");
testWithSeparator("||", null);
System.out.println("Testing with two multi character separators and filled last column");
testWithSeparator("||", "x");
}


private static void testWithSeparator(String separator, String contentOfLastColumn) {
if (contentOfLastColumn == null) {
contentOfLastColumn = "";
}
int offset = (2 * separator.length()) + contentOfLastColumn.length();


for (int i = 255 - offset; i <= 256 - offset; i++) {
String s = testString(i) + separator + separator + contentOfLastColumn;
int columns = NSArray.componentsSeparatedByString(s, separator).count();
System.out.println("String lenght: "+s.length() + ", number of columns: " + columns);
}
}


    private static String testString(int len) {
	String s = "";
	for (int i = 0; i < len; i++) {
	    s += "x";
	}
	return s;
    }

Timo

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/webobjects-dev/email@hidden

This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.