Skip to content

Instantly share code, notes, and snippets.

View hollance's full-sized avatar

Matthijs Hollemans hollance

View GitHub Profile
@hollance
hollance / Barfdown.markdown
Last active March 25, 2023 18:16
A markdown-ish parser written in Swift 2.0

Barfdown: a Markdown-ish Parser Written in Swift

Goals for this project:

  • Parse a simplified version of Markdown that is good enough for writing my blog posts.
  • Be reasonably efficient. This means the parser shouldn't copy substrings around if not necessary. This is done by storing all the elements as indexes into the original text.
  • Be small and therefore be easy to understand.
  • No regular expressions. They are the lazy person's solution to parsing. ;-)

This is just a toy project for me to experiment with writing parsers in Swift. Because why not?

@hollance
hollance / Boyer-Moore.swift
Last active December 27, 2018 04:50
Boyer-Moore string search algorithm in Swift 2.0
/*
Boyer-Moore string search
This code is based on the article "Faster String Searches" by Costas Menico
from Dr Dobb's magazine, July 1989. (Yes, 1989!)
http://www.drdobbs.com/database/faster-string-searches/184408171
*/
extension String {
func indexOf(pattern: String) -> String.Index? {
@hollance
hollance / gist:c0e1f84d2932ce39fed8
Created August 22, 2014 17:55
Calculating height for a UILabel
var rect = CGRect(x: 100, y: 10, width: 205, height: 10000)
addressLabel.frame = rect
addressLabel.sizeToFit()
rect.size.height = addressLabel.frame.size.height
addressLabel.frame = rect
@hollance
hollance / TakeWhile.swift
Created August 15, 2014 07:11
Explore Swift Challenge #2
struct TakeWhile<S: SequenceType>: SequenceType {
private let seq: S
private let block: (S.Generator.Element) -> Bool
init(_ seq: S, block: (S.Generator.Element) -> Bool) {
self.seq = seq
self.block = block
}
func generate() -> GeneratorOf<S.Generator.Element> {
var gen = seq.generate()
return GeneratorOf<S.Generator.Element> {
@hollance
hollance / PrimeFactorSequence.swift
Last active August 29, 2015 14:05
Solution to Explore Swift Challenge #1
/*
The code from https://gist.github.com/GarthSnyder/c8efe80675cd00322b51
rewritten as a Swift sequence and generator.
*/
import Foundation
struct PrimeFactorGenerator: GeneratorType {
var n: Int
var divisor = 2
import Foundation
import CoreGraphics
@infix func * (left: CGPoint, right: (a: CGFloat, b: CGFloat)) -> CGPoint {
return CGPoint(x: left.x * right.a, y: left.y * right.b)
}
let pt1 = CGPointMake(100, 50)
let pt2 = pt1 * (2, 3)
@hollance
hollance / Set.swift
Created June 23, 2014 16:05
Very basic set in Swift
class Set<T: Hashable>: Sequence, Printable {
var dictionary = Dictionary<T, Bool>() // private
func addElement(newElement: T) {
dictionary[newElement] = true
}
func removeElement(element: T) {
dictionary[element] = nil
}
extension String {
subscript(i: Int) -> Character {
return Array(self)[i]
}
}
var s = "Hello world"
println(s[0])
@hollance
hollance / MotionStreak.h
Created January 28, 2014 16:34
Motion streak code using SKShapeNode that didn't make it into the book iOS Games by Tutorials (raywenderlich.com).
#import <SpriteKit/SpriteKit.h>
@interface MotionStreak : SKShapeNode
- (void)addPosition:(CGPoint)position;
- (void)updateStreak;
@end
@hollance
hollance / YourMainDataModel.m
Created August 24, 2013 13:39
Saving an NSMutableArray to a plist file using NSKeyedArchiver and NSCoding.
// Gets the path to the app's Documents folder
- (NSString *)documentsDirectory
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return paths[0];
}
// Gets the path to the data file
- (NSString *)dataFilePath
{