Skip to content

Instantly share code, notes, and snippets.

View phausler's full-sized avatar

Philippe Hausler phausler

View GitHub Profile
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>
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)
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]"
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)
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)
//
// main.swift
// Stacks
//
// Created by Philippe Hausler on 6/14/17.
// Copyright © 2017 Philippe Hausler. All rights reserved.
//
import Foundation
import Darwin
@phausler
phausler / main.swift
Created May 19, 2017 02:05
Hashability via Codability
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
@phausler
phausler / defer.h
Created March 20, 2017 15:43
Defer for c
__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
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
internal class NSAutoreleasePool {
fileprivate static var _current = NSThreadSpecific<NSAutoreleasePool>()
internal static var current: NSAutoreleasePool {
return _current.get() {
return NSAutoreleasePool()
}
}
var depth: Int = 0