Skip to content

Instantly share code, notes, and snippets.

View mwaterfall's full-sized avatar

Michael Waterfall mwaterfall

View GitHub Profile
@mwaterfall
mwaterfall / StringExtensionHTML.swift
Last active April 17, 2025 13:04
Decoding HTML Entities in Swift
// Very slightly adapted from http://stackoverflow.com/a/30141700/106244
// 99.99% Credit to Martin R!
// Mapping from XML/HTML character entity reference to character
// From http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references
private let characterEntities : [String: Character] = [
// XML predefined entities:
""" : "\"",
"&" : "&",
@mwaterfall
mwaterfall / validate.py
Last active October 29, 2024 03:14
Recursively validates all python files with pyflakes that were modified since the last validation, and provides basic stats. Ignores hidden directories.
#
# Recursively validates all python files with pyflakes that were modified
# since the last validation, and provides basic stats. Ignores hidden
# directories.
#
# NOTE:
# You should set your favourite version control system to ignore
# the validate.db file that is used to track when which files
# have changed since last validation.
#
@mwaterfall
mwaterfall / gist:953664
Created May 3, 2011 16:28
NSDate from Internet Date & Time String
//
// NSDate+InternetDateTime.h
// MWFeedParser
//
// Created by Michael Waterfall on 07/10/2010.
// Copyright 2010 Michael Waterfall. All rights reserved.
//
#import <Foundation/Foundation.h>
@mwaterfall
mwaterfall / gist:953657
Created May 3, 2011 16:24
Runtime iOS Version Checking
/*
* System Versioning Preprocessor Macros
*/
#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
public protocol Changeable {}
extension Changeable {
/// Calls the `changes` closure passing the receiver value as an `inout` parameter. The final value of the argument
/// after the `changes` closure returns is then returned from this method. Benefits to using this function come
/// when used against value types.
///
/// Example:
///
/// extension UIEdgeInsets: Changeable {}
enum Material: String {
case wood, metal, glass, other
}
extension Material: Codable {}
extension Material: UnknownCaseRepresentable {
static let unknownCase: Material = .other
}
protocol UnknownCaseRepresentable: RawRepresentable, CaseIterable {
static var unknownCase: Self { get }
}
extension UnknownCaseRepresentable where Self.RawValue: Equatable {
init(rawValue: RawValue) {
let value = Self.allCases.first(where: { $0.rawValue == rawValue })
self = value ?? Self.unknownCase
}
}
extension Material {
init(from decoder: Decoder) throws {
self = try Material(from: decoder, default: .other)
}
}
extension RawRepresentable where RawValue: Decodable {
init(from decoder: Decoder, default: Self) throws {
let container = try decoder.singleValueContainer()
let rawValue = try container.decode(RawValue.self)
self = Self(rawValue: rawValue) ?? `default`
}
}
extension Material {
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let rawMaterial = try container.decode(String.self)
self = Material(rawValue: rawMaterial) ?? .other
}
}