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-3597] CharacterSet isEmpty method failing #313

Open
swift-ci opened this issue Jan 10, 2017 · 2 comments
Open

[SR-3597] CharacterSet isEmpty method failing #313

swift-ci opened this issue Jan 10, 2017 · 2 comments
Labels
bug Something isn't working

Comments

@swift-ci
Copy link

swift-ci commented Jan 10, 2017

Previous ID SR-3597
Radar None
Original Reporter Nteissler (JIRA User)
Type Bug

Attachment: Download

Additional Detail from JIRA
Votes 0
Component/s
Labels Bug, CharacterSet
Assignee None
Priority Medium

md5: 27b6a52ddd0420df6cf1819fe7ce0c67

Issue Description:

I was trying to user CharacterSet to check if a user input string contains any non decimal digit characters. I use CharacterSet.decimalDigits and take the intersection of that with the user input. If this intersection is empty, it presumably means the user hasn't entered valid input. Yet the intersection is not empty.

let digits = CharacterSet.decimalDigits  
let letters = CharacterSet(charactersIn: "abcd") // never prints

let intersection = digits.intersection(letters)  
for c in "abcd".characters {  
  if intersection.contains(UnicodeScalar(String(c))!) {  
    print("contains \(c)") // never prints  
  }  
}

for i in 0...9 {  
  if intersection.contains(UnicodeScalar(String(i))!) {  
    print("contains \(i)")  
  }  
}

print("intersection is empty: (intersection.isEmpty)") // prints false

I even tried looping over all unicode scalars to test for membership, and that doesn't print anything.

for i in 0x0000...0xFFFF {  
  guard let c = UnicodeScalar(i) else {  
    continue  
  }  
  if intersection.contains(c) {  
    print("contains \(c)")  
  }  
}

Why is the set non empty?

Note: using let digits = CharacterSet(charactersIn: "1234567890") works as expected. I know that the decimalDigits contains more than just 0-9, but the intersection should still be empty.

@xwu
Copy link

xwu commented Jan 12, 2017

let digits = CharacterSet.decimalDigits  
print(digits.intersection(digits.inverted).isEmpty) // false  
print(digits.intersection(digits.inverted) == digits.inverted.intersection(digits)) // false  

Oops...

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis
Copy link

@parkera I know this is an old bug that might very well be fixed, but still, could we relocate it to one of the Foundation repositories for triage?

@parkera parkera transferred this issue from apple/swift Nov 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants