• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Bug in NSArray.componentsSeparatedByString()
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Bug in NSArray.componentsSeparatedByString()


  • Subject: Bug in NSArray.componentsSeparatedByString()
  • From: Timo Hoepfner <email@hidden>
  • Date: Thu, 31 May 2007 12:27:32 +0200

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:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Bug in NSArray.componentsSeparatedByString()
      • From: Lachlan Deck <email@hidden>
  • Prev by Date: checkbox selected
  • Next by Date: Re: checkbox selected
  • Previous by thread: Re: checkbox selected
  • Next by thread: Re: Bug in NSArray.componentsSeparatedByString()
  • Index(es):
    • Date
    • Thread