Skip to content

Instantly share code, notes, and snippets.

View NikolaiRuhe's full-sized avatar

Nikolai Ruhe NikolaiRuhe

View GitHub Profile
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 / 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];
@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
@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
Created July 15, 2016 06:50
A UILabel subclass that adds padding around the text and handles layout properly.
import UIKit
class NRLabel : UILabel {
var textInsets: UIEdgeInsets = UIEdgeInsetsZero
override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
let insetRect = UIEdgeInsetsInsetRect(bounds, textInsets)
let textRect = super.textRect(forBounds: insetRect, limitedToNumberOfLines: numberOfLines)
let invertedInsets = UIEdgeInsets(top: -textInsets.top,
extension CBUUID {
func representativeString() -> String {
let data = self.data
let buffer = UnsafeBufferPointer<UInt8>(start: UnsafePointer(data.bytes), count: data.length)
let hex: (UInt8) -> String = { ($0 <= 9 ? "0" : "") + String($0, radix: 16, uppercase: true) }
let dashInserter: (Int) -> String = { [3, 5, 7, 9].contains($0) ? "-" : "" }
return buffer.map(hex)
.enumerate()
@NikolaiRuhe
NikolaiRuhe / gist:5546537
Last active December 17, 2015 03:49
Simple demo code illustrating the advantages of enumerateSubstringsInRange over componentsSeparatedByString.
//
// Created by Nikolai Ruhe on 2013-05-09.
// Copyright (c) 2013 Savoy Software. All rights reserved.
//
#import <Foundation/Foundation.h>
static NSString *replaceLongWords(NSString *originalString, NSString *replaceWord, NSUInteger maxChar)
{
NSMutableString *result = [NSMutableString stringWithCapacity:[originalString length]];