• 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 15:55:25 -0700

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.
_______________________________________________
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.
  • Follow-Ups:
    • Re: Dealing with Mac/Unix/Windows line breaks
      • From: Tom Sutcliffe <email@hidden>
    • Re: Dealing with Mac/Unix/Windows line breaks
      • From: Dustin Voss <email@hidden>
References: 
 >Dealing with Mac/Unix/Windows line breaks (From: Francisco Tolmasky <email@hidden>)

  • Prev by Date: NSAutoreleaseNoPool console message
  • Next by Date: Re: Question about line breaks and file types
  • Previous by thread: Dealing with Mac/Unix/Windows line breaks
  • Next by thread: Re: Dealing with Mac/Unix/Windows line breaks
  • Index(es):
    • Date
    • Thread