Skip to content

When does canBeConverted(to: .ascii) return true for non-ASCII strings? #4378

@markovsdima

Description

@markovsdima

Question

I'm analyzing a Swift String extension String.swift and found a guard condition that appears to be unreachable:

extension String {
    func asciified() -> String? {
        guard !isASCII else {
            return self
        }
        guard !canBeConverted(to: .ascii) else {
            return nil  // ← This line seems unreachable
        }
        // ... CFStringTransform logic
    }
}

My Testing

I tested extensively with various non-ASCII strings, and ALL returned canBeConverted(to: .ascii) = false:

  • Accented characters: "café", "naïve", "résumé" → false
  • Cyrillic: "Привет", "Москва" → false
  • Emojis: "🚀", "😀😃😄" → false
  • Japanese: "こんにちは" → false
  • Special Unicode: "—", "…", "«»" → false
  • Fullwidth characters: "A", "123" → false
  • Combining characters: "e\u{0301}" (é) → false
  • Control characters: BOM, Zero Width Space, etc. → false

Questions

  1. Are there any real-world cases where a non-ASCII string returns canBeConverted(to: .ascii) = true?
  2. Is this guard condition effectively "dead code"?
  3. What was the original intention of this check?

Context

I discovered this while integrating Matrix into my own messaging app. These lines puzzled me and took over an hour to analyze, which made me realize this might be confusing other developers too.

The function works perfectly without this guard - all test strings are successfully transliterated by CFStringTransform. I'm trying to understand if this condition serves a purpose I'm missing or if it can be safely removed.
Any insights would be greatly appreciated!

If this is indeed dead code, I'd be happy to submit a PR to remove it.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions