How to check whether a character is printable (from Cpp perspective)?

That sort of information is available, but it is more detailed because Character’s answer to that is not as clear‐cut. What you will need is something like the following, but it will require some tuning to determine exactly what you want it to do in your particular use case.

extension Unicode.Scalar {
  var isPrintable: Bool {
    switch properties.generalCategory {
    case .uppercaseLetter, .lowercaseLetter, .titlecaseLetter,
      /* ... */
      .mathSymbol, .currencySymbol, .modifierSymbol, .otherSymbol:
        return true
    case .control, .format:
      return false
    }
  }
}

extension Character {
  var isPrintable: Bool {
    return unicodeScalars.contains(where: { $0.isPrintable })
  }
}

The list of general categories is here, and some additional properties are listed here.

4 Likes