Skip to content

Instantly share code, notes, and snippets.

@karwa
karwa / replacesubrange-diy.swift
Created October 13, 2020 18:42
A generic implementation of 'replaceSubrange' for contiguous buffers
// 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.
///
@karwa
karwa / codablebridge.swift
Created November 1, 2017 04:57
codable-nscoding bridge
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 }
@karwa
karwa / ConcurrentCollection.swift
Last active July 16, 2020 11:00
Concurrent collection wrapper
import Dispatch
// Import C11 for atomic_flag
// FIXME: SWIFT(canImport)
//#if canImport(Glibc)
// import Glibc.stdatomic
//#elseif canImport(Darwin)
import Darwin.C.stdatomic
//#endif
@karwa
karwa / ASCII.swift
Created May 3, 2020 20:56
ascii prototype
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
}
@karwa
karwa / ASCII.swift
Created May 3, 2020 20:56
ascii prototype
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
}
@karwa
karwa / string-binary.swift
Created February 22, 2020 13:58
String(binary:)
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
@karwa
karwa / collection-split.swift
Last active February 12, 2020 21:09
Split collections by length
// MARK: - Sequence.
extension Sequence {
public func split(maxLength: Int) -> [ArraySlice<Element>] {
return Array(self)._eagerSplit(maxLength: maxLength)
}
}
extension Collection {
@karwa
karwa / generic-base64.swift
Last active December 21, 2019 06:46
Generic base64 encode/decode
// 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)
enum CountPredicate {
case greaterThan(_ n: Int)
case equalTo(_ n: Int)
}
extension Collection {
func count(is predicate: CountPredicate) -> Bool {
switch predicate {
case .equalTo(let val):