Skip to content

Instantly share code, notes, and snippets.

View NikolaiRuhe's full-sized avatar

Nikolai Ruhe NikolaiRuhe

View GitHub Profile
@NikolaiRuhe
NikolaiRuhe / LocatedValueTest.swift
Created May 22, 2022 18:41
Using a result builder to gather source code locations for unit testing.
// example unit test:
func testAscii() async throws {
locate {
"Celcius"
"Farenheit"
"Kelvin"
"yes?"
"no?"
"yes;no;cancel"
func testCSV() throws {
let plainField = Prefix { $0 != .init(ascii: ",") && $0 != .init(ascii: "|") && $0 != .init(ascii: "\n") }
let quotedField = Parse {
"|".utf8
Many {
OneOf {
Parse { "||".utf8 }.map { Substring("").utf8 }
Prefix { $0 != .init(ascii: "|") }
}
func testQuotedField() throws {
let quotedField = Parse {
"|".utf8
Many(into: "") { result, element in
result += String(element)!
} element: {
OneOf {
Parse { "||".utf8 }.map { Substring("|").utf8 }
Prefix(1...) { $0 != .init(ascii: "|") }
}
import Foundation
extension AsyncSequence {
/// Returns another `AsyncSequence` that fetches elements in the background
/// from the base sequence and buffers them for faster access.
public func buffer(limit: Int = 1) -> AsyncBuffer<Self> {
.init(self, limit: limit)
}
}
@NikolaiRuhe
NikolaiRuhe / JSONValue.swift
Created October 15, 2018 20:44
JSON DOM Swift implementation
import Foundation
/// A JSONValue is a generic DOM representation of decoded JSON.
enum JSONValue {
case null
case boolean(Bool)
case number(Double)
case string(String)
case array(JSONArray)
//
// Request.swift
// TLS-Test
//
// Created by Nikolai Ruhe on 12.10.18.
// Copyright © 2018 Nikolai Ruhe. All rights reserved.
//
import Foundation
//
// Copyright (c) 2016, 2018 Nikolai Ruhe. All rights reserved.
//
import Foundation
public extension FileManager {
/// Calculate the allocated size of a directory and all its contents on the volume.
@NikolaiRuhe
NikolaiRuhe / NRLabel.swift
Last active September 23, 2016 05:41
A UILabel subclass that adds padding around the text and handles layout properly.
import UIKit
class NRLabel : UILabel {
var textInsets: UIEdgeInsets = UIEdgeInsetsZero {
didSet { invalidateIntrinsicContentSize() }
}
override func textRectForBounds(bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
var rect = textInsets.apply(bounds)
@NikolaiRuhe
NikolaiRuhe / NRLabel.swift
Last active April 9, 2020 05:01
A UILabel subclass that adds padding around the text and handles layout properly.
import UIKit
class NRLabel : UILabel {
var textInsets = UIEdgeInsets.zero {
didSet { invalidateIntrinsicContentSize() }
}
override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
let insetRect = UIEdgeInsetsInsetRect(bounds, textInsets)
@NikolaiRuhe
NikolaiRuhe / NRLabel.m
Last active November 27, 2018 07:38
A UILabel subclass that adds padding around the text and handles layout properly.
@interface NRLabel : UILabel
@property (nonatomic) UIEdgeInsets textInsets;
@end
@implementation NRLabel
- (void)setTextInsets:(UIEdgeInsets)textInsets
{
_textInsets = textInsets;
[self invalidateIntrinsicContentSize];