- Proposal: SE-NNNN
- Authors: Karl Wagner
- Review Manager: TBD
- Status: Awaiting review
During the review process, add the following fields as needed:
- Decision Notes: Rationale, Additional Commentary
// This file contains a generic implementation of 'replaceSubrange' for contiguous buffers, | |
// including those only accessible indirectly such as `ManagedBuffer` subclasses. | |
// | |
// It includes optimisations for in-place replacement as well as consuming the original storage when additional | |
// capacity is required. The implementation is adapted from the standard library's source code, where it forms | |
// the basis of Array's implementation of replaceSubrange. | |
/// An object which contains a unique, mutable buffer. | |
/// This protocol is a required level of indirection so that `replaceElements` can allocate, fill, and return objects which provide their buffers indirectly. | |
/// |
import Foundation | |
/// This isn't safe to use before Swift gets ABI stability, because generic classes | |
/// could change their names. Also, be sure to register bridges with the Obj-C runtime | |
/// if using to decode during iOS state restoration. | |
/// | |
class CodableBridge<Wrapped: Codable>: NSObject, NSSecureCoding { | |
let value: Wrapped | |
init(_ value: Wrapped) { self.value = value } |
import Dispatch | |
// Import C11 for atomic_flag | |
// FIXME: SWIFT(canImport) | |
//#if canImport(Glibc) | |
// import Glibc.stdatomic | |
//#elseif canImport(Darwin) | |
import Darwin.C.stdatomic | |
//#endif |
public struct ASCII: Equatable, Comparable { | |
public var codePoint: UInt8 | |
@inlinable public init(_ v: UInt8) { self.codePoint = v } | |
} | |
extension ASCII { | |
// Homogenous comparisons. | |
@inlinable public static func < (lhs: ASCII, rhs: ASCII) -> Bool { | |
lhs.codePoint < rhs.codePoint | |
} |
public struct ASCII: Equatable, Comparable { | |
public var codePoint: UInt8 | |
@inlinable public init(_ v: UInt8) { self.codePoint = v } | |
} | |
extension ASCII { | |
// Homogenous comparisons. | |
@inlinable public static func < (lhs: ASCII, rhs: ASCII) -> Bool { | |
lhs.codePoint < rhs.codePoint | |
} |
extension String { | |
/// Creates a string of ones and zeros representing the given value in binary. | |
/// | |
/// Unlike `String.init<T: BinaryInteger>(_:radix:uppercase:)`, negative values | |
/// are represented as their true, two's-complement bitpattern. | |
/// | |
@inlinable | |
public init<T: BinaryInteger>(binary value: T) { | |
let bitWidth = value.bitWidth |
// MARK: - Sequence. | |
extension Sequence { | |
public func split(maxLength: Int) -> [ArraySlice<Element>] { | |
return Array(self)._eagerSplit(maxLength: maxLength) | |
} | |
} | |
extension Collection { | |
// This is a version of swift-corelibs-foundation's base64 encoder/decoder, | |
// adapted to encode/decode any Collection of UInt8s. | |
// | |
// This means you can directly encode/decode: | |
// Array<UInt8>, String.UTF8View, Unsafe{Mutable}BufferPointer<UInt8>, Unsafe{Raw}BufferPointer, | |
// SwiftNIO's ByteBuffer, Data, NSData and DispatchData (of course), and more. | |
// | |
// e.g. | |
// let encoded = "Hello, this is an encoded String! 🏆".utf8.base64EncodedString() | |
// print(encoded) |
During the review process, add the following fields as needed:
enum CountPredicate { | |
case greaterThan(_ n: Int) | |
case equalTo(_ n: Int) | |
} | |
extension Collection { | |
func count(is predicate: CountPredicate) -> Bool { | |
switch predicate { | |
case .equalTo(let val): |