• 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
Re: Dealing with Mac/Unix/Windows line breaks
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Dealing with Mac/Unix/Windows line breaks


  • Subject: Re: Dealing with Mac/Unix/Windows line breaks
  • From: Dustin Voss <email@hidden>
  • Date: Mon, 4 Aug 2003 22:19:56 -0700

I must correct the code below.

On Monday, August 4, 2003, at 03:55 PM, Dustin Voss wrote:

On Sunday, August 3, 2003, at 04:59 PM, Francisco Tolmasky wrote:

In a document that accepts copies from different sources, how should you deal with line breaks. For example, let's say you have a text view with macintosh line breaks. Someone hits copy from a document with unix line breaks, and then pastes to you text view. Do you now have a mixed format string in the text view? Do you have to intercept the string and convert the line breaks? Is there an easy way to do so, like [string stringWithLineBreaks: Mac]?

Unfortunately, there's no easy way I've found to convert line breaks. I don't know if NSString automatically converts all line breaks to '\n' or what.

I suppose this code would work:

NormalizeLineBreaks (NSMutableString *str)
{
NSRange entireString = NSMakeRange (0, [str length]);
// One of the following is redundant, but I don't care which. Hopefully, it'll be a no-op.
[str replaceOccurrencesOfString:@"\u000D\u000A" withString:@"\n" options:NSLiteralSearch range:entireString];
[str replaceOccurrencesOfString:@"\u000D" withString:@"\n" options:NSLiteralSearch range:entireString];
[str replaceOccurrencesOfString:@"\u000A" withString:@"\n" options:NSLiteralSearch range:entireString];
[str replaceOccurrencesOfString:@"\u2028" withString:@"\n" options:NSLiteralSearch range:entireString];
[str replaceOccurrencesOfString:@"\u2029" withString:@"\n" options:NSLiteralSearch range:entireString];
}

Ideally, I could have written that function to replace everything with '\u2028' instead of '\n'. '\u2028' is Unicode's Line Separator character, designed to eliminate any confusion on the issue. But since NSCharacterSet doesn't consider '\u2028' to be a newline character (see "+ whitespaceAndNewlineCharacterSet"), I expect the rest of Cocoa doesn't handle it too well, either.

I've since learned that GCC 3.1 does not support the '\u????' escape sequence. That makes the above code uglier. All the @"\u????" expressions have to be replaced with something like this:

[NSString stringWithFormat:@"%C", 0x????]
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Re: Dealing with Mac/Unix/Windows line breaks (From: Dustin Voss <email@hidden>)

  • Prev by Date: Re: BOM and UTF-8 (was Re: Question about line breaks and file types)
  • Next by Date: Java in Panther
  • Previous by thread: Re: Dealing with Mac/Unix/Windows line breaks
  • Next by thread: Re: Dealing with Mac/Unix/Windows line breaks
  • Index(es):
    • Date
    • Thread