Skip to content

Commit

Permalink
SwiftFormat pass
Browse files Browse the repository at this point in the history
  • Loading branch information
orchetect committed Sep 23, 2024
1 parent 82b92bc commit db9f6c3
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 31 deletions.
26 changes: 17 additions & 9 deletions Sources/SwiftASCII/ASCIICharacter.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
//
// ASCIICharacter.swift
// SwiftASCII • https://github.com/orchetect/SwiftASCII
// © 2022 Steffan Andrews • Licensed under MIT License
// © 2021-2024 Steffan Andrews • Licensed under MIT License
//

import Foundation

/// ASCII Character:
/// A type containing a `Character` instance that is guaranteed to conform to ASCII encoding.
/// Offers a validating `exactly: Character` failable initializer and a `_ lossy: Character` conversion initializer.
/// Offers a validating `exactly: Character` failable initializer and a `_ lossy: Character`
/// conversion initializer.
public struct ASCIICharacter: Hashable {
/// The ASCII character returned as a `Character`
public let characterValue: Character
Expand All @@ -32,11 +33,13 @@ public struct ASCIICharacter: Hashable {
asciiValue = getASCIIValue
}

/// Returns a new `ASCIICharacter` instance from the source character, converting a non-ASCII character to its closest ASCII equivalent if necessary.
/// Returns a new `ASCIICharacter` instance from the source character, converting a non-ASCII
/// character to its closest ASCII equivalent if necessary.
@inlinable
public init(_ lossy: Character) {
guard let getASCIIValue = lossy.asciiValue else {
// if ASCII encoding fails, fall back to a default character instead of throwing an exception
// if ASCII encoding fails, fall back to a default character instead of throwing an
// exception

var translated = String(lossy).asciiStringLossy
if translated.stringValue.isEmpty { translated = "?" }
Expand All @@ -51,7 +54,8 @@ public struct ASCIICharacter: Hashable {
asciiValue = getASCIIValue
}

/// Returns a new `ASCIICharacter` instance if the source string contains a single character and the character is a valid ASCII character.
/// Returns a new `ASCIICharacter` instance if the source string contains a single character and
/// the character is a valid ASCII character.
@_disfavoredOverload
@inlinable
public init?<S: StringProtocol>(exactly source: S) {
Expand All @@ -67,7 +71,8 @@ public struct ASCIICharacter: Hashable {
asciiValue = getASCIIValue
}

/// Returns a new `ASCIICharacter` instance if the source string contains a single character, converting a non-ASCII character to its closest ASCII equivalent if necessary.
/// Returns a new `ASCIICharacter` instance if the source string contains a single character,
/// converting a non-ASCII character to its closest ASCII equivalent if necessary.
@inlinable
public init<S: StringProtocol>(_ lossy: S) {
let char: Character = lossy.first ?? "?"
Expand All @@ -76,7 +81,8 @@ public struct ASCIICharacter: Hashable {
}

/// Returns a new `ASCIICharacter` instance if the source data is a single ASCII character.
/// Returns `nil` if the source data is not a single byte or if it contains a non-ASCII character byte.
/// Returns `nil` if the source data is not a single byte or if it contains a non-ASCII
/// character byte.
@inlinable
public init?(exactly source: Data) {
guard source.count == 1 else { return nil }
Expand Down Expand Up @@ -113,7 +119,8 @@ extension ASCIICharacter: ExpressibleByExtendedGraphemeClusterLiteral {

extension ASCIICharacter: CustomStringConvertible {
public var description: String {
// If not a printable character, return an empty string and don't allow any non-printable ASCII control characters through
// If not a printable character, return an empty string and don't allow any non-printable
// ASCII control characters through

(32 ... 126).contains(asciiValue)
? String(characterValue)
Expand Down Expand Up @@ -234,7 +241,8 @@ extension Sequence where Element == ASCIICharacter {
ASCIIString(self)
}

/// Returns a new string by concatenating the elements of the sequence, adding the given separator between each element.
/// Returns a new string by concatenating the elements of the sequence, adding the given
/// separator between each element.
public func joined(separator: ASCIIString) -> ASCIIString {
let joinedStr = map { "\($0.characterValue)" }
.joined(separator: separator.stringValue)
Expand Down
16 changes: 10 additions & 6 deletions Sources/SwiftASCII/ASCIIString.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
//
// ASCIIString.swift
// SwiftASCII • https://github.com/orchetect/SwiftASCII
// © 2022 Steffan Andrews • Licensed under MIT License
// © 2021-2024 Steffan Andrews • Licensed under MIT License
//

import Foundation

/// ASCII String:
/// A type containing a `String` instance that is guaranteed to conform to ASCII encoding.
/// Offers a validating `exactly: String` failable initializer and a `_ lossy: String` conversion initializer.
/// Offers a validating `exactly: String` failable initializer and a `_ lossy: String` conversion
/// initializer.
public struct ASCIIString: Hashable {
/// The ASCII string returned as a `String`
public let stringValue: String
Expand Down Expand Up @@ -40,13 +41,15 @@ public struct ASCIIString: Hashable {
rawData = source
}

/// Returns a new `ASCIIString` instance from the source string, removing or converting any non-ASCII characters if necessary.
/// Returns a new `ASCIIString` instance from the source string, removing or converting any
/// non-ASCII characters if necessary.
@inlinable
public init<S: StringProtocol>(_ lossy: S) {
guard lossy.allSatisfy({ $0.isASCII }),
let asciiData = lossy.data(using: .ascii)
else {
// if ASCII encoding fails, fall back to a default string instead of throwing an exception
// if ASCII encoding fails, fall back to a default string instead of throwing an
// exception

stringValue = lossy.asciiStringLossy.stringValue
rawData = stringValue.data(using: .ascii) ?? Data([])
Expand Down Expand Up @@ -182,7 +185,8 @@ extension Sequence where Element == ASCIIString {
)
}

/// Returns a new string by concatenating the elements of the sequence, adding the given separator between each element.
/// Returns a new string by concatenating the elements of the sequence, adding the given
/// separator between each element.
@_disfavoredOverload
public func joined(separator: ASCIIString) -> ASCIIString {
let joinedStr = map { $0.stringValue }
Expand All @@ -203,7 +207,7 @@ extension Sequence where Element == ASCIIString {
extension ASCIIString {
/// Internal use only.
/// Used when string and data are already known to be valid ASCII.
internal init(guaranteedASCII string: String, rawData: Data) {
init(guaranteedASCII string: String, rawData: Data) {
stringValue = string
self.rawData = rawData
}
Expand Down
12 changes: 6 additions & 6 deletions Sources/SwiftASCII/CharacterSet.swift
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
//
// CharacterSet.swift
// SwiftASCII • https://github.com/orchetect/SwiftASCII
// © 2022 Steffan Andrews • Licensed under MIT License
// © 2021-2024 Steffan Andrews • Licensed under MIT License
//

import Foundation

extension CharacterSet {
/// Includes all ASCII characters, including printable and non-printable (0...127)
internal static let ascii = CharacterSet(
static let ascii = CharacterSet(
charactersIn: UnicodeScalar(0) ... UnicodeScalar(127)
)

/// Includes all printable ASCII characters (32...126)
internal static let asciiPrintable = CharacterSet(
static let asciiPrintable = CharacterSet(
charactersIn: UnicodeScalar(32) ... UnicodeScalar(126)
)

/// Includes all ASCII characters, including printable and non-printable (0...31)
internal static let asciiNonPrintable = CharacterSet(
static let asciiNonPrintable = CharacterSet(
charactersIn: UnicodeScalar(0) ... UnicodeScalar(31)
)

/// Includes all extended ASCII characters (128...255)
internal static let asciiExtended = CharacterSet(
static let asciiExtended = CharacterSet(
charactersIn: UnicodeScalar(128) ... UnicodeScalar(255)
)

/// Includes all ASCII characters and extended characters (0...255)
internal static let asciiFull = CharacterSet(
static let asciiFull = CharacterSet(
charactersIn: UnicodeScalar(0) ... UnicodeScalar(255)
)
}
11 changes: 7 additions & 4 deletions Sources/SwiftASCII/String.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// String.swift
// SwiftASCII • https://github.com/orchetect/SwiftASCII
// © 2022 Steffan Andrews • Licensed under MIT License
// © 2021-2024 Steffan Andrews • Licensed under MIT License
//

import Foundation
Expand Down Expand Up @@ -36,11 +36,14 @@ extension StringProtocol {

/// Converts a `String` to `ASCIIString` lossily.
///
/// Performs a lossy conversion, transforming characters to printable ASCII substitutions where necessary.
/// Performs a lossy conversion, transforming characters to printable ASCII substitutions where
/// necessary.
///
/// Note that some characters may be transformed to representations that occupy more than one ASCII character. For example: char 189 (½) will be converted to "1/2"
/// Note that some characters may be transformed to representations that occupy more than one
/// ASCII character. For example: char 189 (½) will be converted to "1/2"
///
/// Where a suitable character substitution can't reasonably be performed, a question-mark "?" will be substituted.
/// Where a suitable character substitution can't reasonably be performed, a question-mark "?"
/// will be substituted.
@available(OSX 10.11, iOS 9.0, *)
public var asciiStringLossy: ASCIIString {
let transformed = applyingTransform(
Expand Down
4 changes: 2 additions & 2 deletions Tests/SwiftASCIITests/ASCIICharacter Tests.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//
// ASCIICharacter Tests.swift
// SwiftASCII • https://github.com/orchetect/SwiftASCII
// © 2022 Steffan Andrews • Licensed under MIT License
// © 2021-2024 Steffan Andrews • Licensed under MIT License
//

import XCTest
import SwiftASCII
import XCTest

class ASCIICharacterTests: XCTestCase {
override func setUp() { super.setUp() }
Expand Down
4 changes: 2 additions & 2 deletions Tests/SwiftASCIITests/ASCIIString Tests.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//
// ASCIIString Tests.swift
// SwiftASCII • https://github.com/orchetect/SwiftASCII
// © 2022 Steffan Andrews • Licensed under MIT License
// © 2021-2024 Steffan Andrews • Licensed under MIT License
//

import XCTest
import SwiftASCII
import XCTest

class ASCIIStringTests: XCTestCase {
override func setUp() { super.setUp() }
Expand Down
4 changes: 2 additions & 2 deletions Tests/SwiftASCIITests/String Tests.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//
// String Tests.swift
// SwiftASCII • https://github.com/orchetect/SwiftASCII
// © 2022 Steffan Andrews • Licensed under MIT License
// © 2021-2024 Steffan Andrews • Licensed under MIT License
//

import XCTest
import SwiftASCII
import XCTest

class StringTests: XCTestCase {
override func setUp() { super.setUp() }
Expand Down

0 comments on commit db9f6c3

Please sign in to comment.