\r\n is one Character

"\r\n" is a single Character. Is this the correct behaviour?

1 Like

Yes, a Character corresponds to a Unicode grapheme cluster, and "\r\n" is considered a single grapheme cluster.

-Joe

···

On Jan 27, 2016, at 7:52 PM, Darren Mo via swift-users <swift-users@swift.org> wrote:

"\r\n" is a single Character. Is this the correct behaviour?

1 Like

​For a new line, Unix uses LF, Classic Mac uses ​CR, Windows uses CRLF.
They both means the CR in Unicode.

let str = "\r\n"

str.characters.count // 1

str.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) // 2

str.unicodeScalars.first!.value // 13

zhaoxin

···

On Thu, Jan 28, 2016 at 11:52 AM, Darren Mo via swift-users < swift-users@swift.org> wrote:

"\r\n" is a single Character. Is this the correct behaviour?

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Thanks, Joe. Found it at UAX #29: Unicode Text Segmentation.

···

On Jan 28, 2016, at 1:56 PM, Joe Groff <jgroff@apple.com> wrote:

On Jan 27, 2016, at 7:52 PM, Darren Mo via swift-users <swift-users@swift.org> wrote:

"\r\n" is a single Character. Is this the correct behaviour?

Yes, a Character corresponds to a Unicode grapheme cluster, and "\r\n" is considered a single grapheme cluster.

-Joe

1 Like