Skip to content

Instantly share code, notes, and snippets.

@karwa
karwa / docc-bootstring.swift
Last active Jun 14, 2022
proof of concept bootstring encoding for docc
View docc-bootstring.swift
// Copyright The swift-url Contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
@karwa
karwa / AutoFormPOC.swift
Created Jan 29, 2022
POC form generation using private reflection APIs
View AutoFormPOC.swift
import SwiftUI
struct ContentView: View {
@State var immutableForm = false
@State var data = Person(
name: "Johnny Appleseed", address: "The spaceship", dateOfBirth: Date(),
aSwitch: true, anImmutableString: "Hello, world!", anImmutableSwitch: false
)
var body: some View {
View benchmark-uniqueid.swift
// Use the following Package.swift:
// -----------------------------------------------------------
// swift-tools-version:5.5
// The swift-tools-version declares the minimum version of Swift required to build this package.
//
//import PackageDescription
//
//let package = Package(
// name: "uniqueid-test",
// dependencies: [
@karwa
karwa / replacesubrange-diy.swift
Created Oct 13, 2020
A generic implementation of 'replaceSubrange' for contiguous buffers
View replacesubrange-diy.swift
// 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.
///
View array_inplace.swift
extension Collection {
public subscript(inplace_slice bounds: Range<Index>) -> Slice<Self> {
get { fatalError() }
_modify {
var slice = Slice(base: self, bounds: bounds)
yield &slice
}
}
}
@karwa
karwa / ASCII.swift
Created May 3, 2020
ascii prototype
View ASCII.swift
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
ascii prototype
View ASCII.swift
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
}
View string-binary.swift
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 Feb 12, 2020
Split collections by length
View collection-split.swift
// MARK: - Sequence.
extension Sequence {
public func split(maxLength: Int) -> [ArraySlice<Element>] {
return Array(self)._eagerSplit(maxLength: maxLength)
}
}
extension Collection {