This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This hasnt been robustly tested for all alignments but is a simple malloc heap implementation that | |
// is roughly based upon the K&R reference examples; it has a few caveats - namely it is not suitable | |
// for systems with more than one core and provides no thread local affinities nor does it have any | |
// locking to prevent concurrent access. However it is roughly suitable for implementing enough to | |
// heap allocate reference types in Swift. | |
struct SingleCoreAllocator { | |
static let shared = SingleCoreAllocator() | |
struct State { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
public struct Gate<T: Sendable>: Sendable { | |
struct Pass: Sendable { | |
var value: T | |
var continuaton: UnsafeContinuation<Void, Never> | |
} | |
struct Pending: Sendable { | |
var continuation: UnsafeContinuation<T, Never> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension StringProtocol { | |
public func data2(using encoding: String.Encoding, allowLossyConversion: Bool = true) -> Data? { | |
if let d = withBackingBuffer(ascii: { (asciiBuffer) -> Data? in | |
let byteCount = asciiBuffer.count | |
guard byteCount > 0 else { return Data() } | |
switch encoding { | |
case .ascii: fallthrough | |
case .utf8: fallthrough | |
case .nonLossyASCII: return Data(bytes: asciiBuffer.baseAddress!, count: byteCount) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var inStr:String | |
var outStr:String | |
// bad IP-literal (there are no square brackets around the address) | |
inStr = "2606:2800:220:1:248:1893:25c8:1946" | |
outStr = inStr.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)! | |
// outStr = "2606%3A2800%3A220%3A1%3A248%3A1893%3A25c8%3A1946" | |
// valid IP-literal (there are square brackets around the address) | |
inStr = "[2606:2800:220:1:248:1893:25c8:1946]" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fileprivate var boolEncoding: (Int8, Int8) = (0x42, 0x00) | |
fileprivate var int8Encoding: (Int8, Int8) = (0x63, 0x00) | |
fileprivate var uint8Encoding: (Int8, Int8) = (0x43, 0x00) | |
fileprivate var int16Encoding: (Int8, Int8) = (0x73, 0x00) | |
fileprivate var uint16Encoding: (Int8, Int8) = (0x53, 0x00) | |
fileprivate var int32Encoding: (Int8, Int8) = (0x69, 0x00) | |
fileprivate var uint32Encoding: (Int8, Int8) = (0x49, 0x00) | |
fileprivate var intEncoding: (Int8, Int8) = (0x6c, 0x00) | |
fileprivate var uintEncoding: (Int8, Int8) = (0x4c, 0x00) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typealias T1<T> = (T, T) | |
typealias T2<T> = T1<T1<T>> | |
typealias T3<T> = T2<T2<T>> | |
typealias T4<T> = T3<T3<T>> | |
typealias T5<T> = T4<T4<T>> | |
typealias T6<T> = T5<T5<T>> | |
print(MemoryLayout<T6<Int>>.size) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.swift | |
// Stacks | |
// | |
// Created by Philippe Hausler on 6/14/17. | |
// Copyright © 2017 Philippe Hausler. All rights reserved. | |
// | |
import Foundation | |
import Darwin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
struct HashingSingleValueEncodingContainer : SingleValueEncodingContainer { | |
var owner: HashingEncoder | |
mutating func combineHash<T>(of element: T?) where T: Hashable { | |
if let elt = element { | |
owner.combine(elt, hash: elt.hashValue) { (other: Any) -> Bool in | |
if let otherValue = other as? T { | |
return elt == otherValue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__attribute__((used)) | |
static inline void __deferred(void (^*b)(void)) { if (b) (*b)(); } | |
#define ___PASTE__(x, y) x ## y | |
#define __PASTE__(x, y) ___PASTE__(x, y) | |
#define defer(block) void (^__PASTE__(__cleanup, __COUNTER__))(void) __attribute__((cleanup(__deferred), unused)) = block |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Decimal { | |
var doubleValue: Double { | |
var d = 0.0 | |
var i = _length | |
if _length == 0 && _isNegative != 0 { return Double.nan } | |
while i > 0 { | |
var m: UInt16 | |
switch i - 1 { | |
case 0: | |
m = _mantissa.0 |
NewerOlder