Skip to content

Instantly share code, notes, and snippets.

View natecook1000's full-sized avatar

Nate Cook natecook1000

View GitHub Profile
@natecook1000
natecook1000 / operatorCharacters.swift
Last active September 27, 2023 20:42
Allowed characters for Swift operators
View operatorCharacters.swift
import Foundation
extension UnicodeScalar : ForwardIndexType {
public func successor() -> UnicodeScalar {
return UnicodeScalar(value + 1)
}
}
var operatorHeads: [UnicodeScalar] = Array("=-+!*%<>&|^~?".unicodeScalars)
operatorHeads += Array("\u{00A1}" ... "\u{00A7}")
View isNullOrEmpty.swift
protocol StringType {
var isEmpty: Bool { get }
}
extension String : StringType { }
extension Optional where Wrapped: StringType {
var isNullOrEmpty: Bool {
return self?.isEmpty ?? true
}
View InterpolationDefault.swift
extension String.StringInterpolation {
mutating func appendInterpolation<T>(_ value: T?, default: @autoclosure () -> String) {
self.appendLiteral(value.map(String.init(describing:)) ?? `default`())
}
}
let num = Int("21")
print("The number is: \(num, default: "<invalid>")")
// The number is: 21
@natecook1000
natecook1000 / homogeneous.swift
Last active April 15, 2023 07:56
Swift-only homogeneousnessedness
View homogeneous.swift
extension String {
var isHomogeneous: Bool {
if let firstChar = characters.first {
for char in dropFirst(characters) where char != firstChar {
return false
}
}
return true
}
}
View integerWithBytes.swift
// integerWithBytes.swift
// as seen in http://natecook.com/blog/2015/03/a-sanatorium-for-swift-generics/
//
// (c) 2015 Nate Cook, licensed under the MIT license
protocol BitshiftOperationsType {
func <<(lhs: Self, rhs: Self) -> Self
func >>(lhs: Self, rhs: Self) -> Self
func <<=(inout lhs: Self, rhs: Self)
func >>=(inout lhs: Self, rhs: Self)
@natecook1000
natecook1000 / NSCalendar+Swift.swift
Created March 18, 2015 03:08
Swift-friendly NSCalendar methods
View NSCalendar+Swift.swift
// NSCalendar+Swift.swift
// A set of Swift-idiomatic methods for NSCalendar
//
// (c) 2015 Nate Cook, licensed under the MIT license
extension NSCalendar {
/// Returns the hour, minute, second, and nanoseconds of a given date.
func getTimeFromDate(date: NSDate) -> (hour: Int, minute: Int, second: Int, nanosecond: Int) {
var (hour, minute, second, nanosecond) = (0, 0, 0, 0)
getHour(&hour, minute: &minute, second: &second, nanosecond: &nanosecond, fromDate: date)
@natecook1000
natecook1000 / CalculatorView.swift
Last active June 6, 2022 01:00
An IBInspectable Calculator Construction Set
View CalculatorView.swift
// CalculatorView.swift
// as seen in http://nshipster.com/ibinspectable-ibdesignable/
//
// (c) 2015 Nate Cook, licensed under the MIT license
/// The alignment for drawing an String inside a bounding rectangle.
enum NCStringAlignment {
case LeftTop
case CenterTop
case RightTop
View OptionSet+Collection.swift
/// This is a useful index that can store a comparable element or the end of
/// a collection. Similar to https://github.com/apple/swift/pull/15193
enum IndexWithEnd<T : Comparable> : Comparable {
case element(T)
case end
static func < (lhs: IndexWithEnd, rhs: IndexWithEnd) -> Bool {
switch (lhs, rhs) {
case (.element(let l), .element(let r)):
return l < r
View Transposition.swift
struct Transposition<Base: Collection>: RandomAccessCollection
where Base.Element: RandomAccessCollection
{
typealias Index = Int
typealias Indices = Range<Int>
var base: Base
struct Transposed: Collection {
typealias Index = Base.Index
View Swift Unicode Properties.md

The following table lists the Unicode properties as described here, along with the corresponding Unicode.Scalar.Properties property and type, if implemented.

Unicode Property Unicode.Scalar.Properties Type
General
Name name String?
Name_Alias nameAlias String?
Block
Age age `Unicode.Versi