Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-8716] String.split no splitting CR+LF #51228

Closed
swift-ci opened this issue Sep 7, 2018 · 6 comments
Closed

[SR-8716] String.split no splitting CR+LF #51228

swift-ci opened this issue Sep 7, 2018 · 6 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Sep 7, 2018

Previous ID SR-8716
Radar None
Original Reporter Jerrot (JIRA User)
Type Bug
Status Resolved
Resolution Invalid
Environment

Tested with

Xcode 9.4.1 (Swift 4.1)

Xcode 10 beta 5 (Swift 4.2)

Ubuntu 18.04 (Swift 4.2 CONVERGE)

Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug
Assignee None
Priority Medium

md5: 654bd6178aed918ea41e883d75d8e058

Issue Description:

String.split() does not split a String containing CR+LF ("\r\n") correctly when using only one of these characters as separator. Using both characters works though - which I actually not expected to work.

The behaviour can be tested in a playground:

var str = "Hello\r\nplayground" 
str.split(separator: "\r")      // ["Hello\r\nplayground"] ❌
str.split(separator: "\n")      // ["Hello\r\nplayground"] ❌
str.split(separator: "\r\n")    // ["Hello", "playground"] ✅

Expected output was:

str.split(separator: "\r")      // ["Hello", "\nplayground"] ✅
str.split(separator: "\n")      // ["Hello\r", "playground"] ✅
str.split(separator: "\r\n")    // ["Hello", "playground"]   ❓
@swift-ci
Copy link
Collaborator Author

swift-ci commented Sep 7, 2018

Comment by Timo Wälisch (JIRA)

I think there is a broader issue about this. Other methods behave similar and IMHO wrong.

E.g. String.contains behaves fine, but e.g. String.firstIndex(of: ) has the same problems like split().

var str = "Hello\r\nplayground"
str.contains("\r")          // true
str.contains("\n")          // true
str.firstIndex(of: "\r")    // nil ❌
str.firstIndex(of: "\n")    // nil ❌
str.firstIndex(of: "\r\n")  // String.Index

@belkadan
Copy link
Contributor

belkadan commented Sep 8, 2018

This is correct behavior. According to the Unicode standard, "\r\n" is a single Character distinct from both "\r" and "\n".

(I'm oversimplifying here, and @milseman can give you more details, but it's a similar answer to why splitting on "e" gives you ["Pokémon"] instead of ["Pok", "´mon"].)

@belkadan
Copy link
Contributor

belkadan commented Sep 8, 2018

I guess contains is wrong, then.

@milseman
Copy link
Mannequin

milseman mannequin commented Sep 8, 2018

Contains is wrong; it comes from NSString.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Sep 8, 2018

Comment by Timo Wälisch (JIRA)

Oh, thanks for making me smarter then! Since you reopened the issue yourself, I will leave it up to you to close the issue?

@belkadan
Copy link
Contributor

Got it!

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

2 participants