Skip to content

Instantly share code, notes, and snippets.

Avatar

Rob Napier rnapier

View GitHub Profile
@rnapier
rnapier / main.m
Last active Apr 5, 2019
GCD accessor
View main.m
//
// main.m
// test
//
// Created by Rob Napier on 4/5/19.
//
#import <Foundation/Foundation.h>
@interface MyClass: NSObject
View scp-10791.md

Item #: SCP-10791

Object Class: Safe

Special Containment Procedures: SCP-10791 must be maintained within the BMP (Basic Multilingual Plane) at all times. Removal from the BMP may cause disaggregation of the component parts, increasing nearby values by 2. While this is not considered a memetic hazard, the accounting department has made it clear that they will not accept non-standard operators wandering the halls since Incident ██/██/███ when [REDACTED].

Description: SCP-10791 is the result of unauthorized experimentation with SCP-889, SCP-1313, and three members of the Unicode Technical Committee. The less that is said about that, the better. What happens several miles north of ███████, Montana, USA....sometimes requires containment.

Since ██/██/2002, SCP-10791 has been contained in the ███████ Mathematical Operators block. Its name and appearance change regularly, though always an unlikely combination of symbols. Critic

View httpsleak.go
package main
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
@rnapier
rnapier / AnyRealmCollectionBox.swift
Created Feb 19, 2019
Boxing up AnyRealmCollection
View AnyRealmCollectionBox.swift
// https://stackoverflow.com/questions/54764362/swift-array-of-tuples-with-generic-tuple-elements/54768030
struct AnyRealmCollectionBox<Element> {
public func index(after i: Int) -> Int { return i + 1 }
public func index(before i: Int) -> Int { return i - 1 }
private let _realm: () -> Realm?
public var realm: Realm? { return _realm() }
private let _isInvalidated: () -> Bool
@rnapier
rnapier / snakecase.swift
Created Feb 14, 2019
Custom key decoding strategy
View snakecase.swift
import Foundation
let json = Data("""
{
"user_name":"Mark",
"user_info":{
"b_a1234":"value_1",
"c_d5678":"value_2"
}
}
@rnapier
rnapier / NSData.bytesView.swift
Last active Jan 30, 2019
Bytes collection for NSData
View NSData.bytesView.swift
import Foundation
// Why bytesView rather than just extending NSData directly?
// Because this way we can keep our extension internal and not conflict
// with someone who imports us and has also extended NSData.
// If you're top-level code, you can just hoist everyting up to NSData directly.
internal extension NSData {
var bytesView: BytesView { return BytesView(self) }
}
View observer.swift
import Foundation
class Singleton: NSObject {
static let sharedInstance = Singleton()
@objc dynamic var aProperty = false
func updateDoesntWork() {
aProperty = !aProperty
}
View covariant.swift
class Fruit {}
class Apple: Fruit {}
class Orange: Fruit {}
class Basket<T> where T: Fruit
{
var items: [T] = []
func add(_ item: T) {
items.append(item)
View MyRandom.java
import java.security.SecureRandom;
// Targeting Android SDK 19
// Goal is a (cryptographic) random long between 2^32 ..< 2^62.
// Ultimately I want to pick ~10M values with predictible and very low liklihood of collision (~0.001% Birthday Attack).
// Approach is to compute a random upper 32 bits between 1..<2^30 and add a lower 32-bits 0..<2^32.
// Need to be careful of lower 32 bits being treated as negative, since that would skew the results out of range.
public class MyRandom {
public static void main(String args[]) {
@rnapier
rnapier / StringEncode.swift
Created Jul 20, 2018
JSONEncoder().stringEncode()
View StringEncode.swift
extension JSONEncoder {
func stringEncode<T>(_ value: T) throws -> String where T : Encodable {
guard let string = String(data: try self.encode(value), encoding: .utf8) else {
throw EncodingError.invalidValue(value, EncodingError.Context(codingPath: [], debugDescription: "Could not UTF-8 encode string"))
}
return string
}
}