Skip to content

Instantly share code, notes, and snippets.

@cmkilger
cmkilger / SQLite.h
Last active September 28, 2017 07:51
Objective-C SQLite wrapper
#import <Foundation/Foundation.h>
@interface SQLiteDatabase: NSObject
- (nullable instancetype)initWithPath:(nonnull NSString *)path;
- (void)executeQuery:(nonnull NSString *)query;
- (void)executeQuery:(nonnull NSString *)query callback:(void(^ _Nullable)(NSDictionary<NSString *, id> * _Nonnull, BOOL * _Nonnull))callback;
@end
@cmkilger
cmkilger / Clusterer.swift
Created October 18, 2017 23:01
OPTICS algorithm in Swift
import Foundation
public protocol Distanceable {
associatedtype Distance: Comparable
func distance(_ other: Self) -> Distance
}
public class Clusterer {
private class Point<ValueType: Distanceable> {
let value: ValueType
@cmkilger
cmkilger / UTF16Parsing.md
Created August 24, 2018 17:39
Code samples to get a substring using a UTF-16 range

Substrings from UTF-16 ranges

All these code examples produce the following output.

5
😀

Swift

@cmkilger
cmkilger / convert2xcframework
Created February 16, 2021 18:44
Converts iOS/Simulator universal frameworks into xcframeworks
#!/bin/sh
# Assumes that the framework contains i382, x86_64, armv7, and arm64
for path in "$@"
do
framework=$(basename "$path")
# Make sure it's a framework'
if [ "${framework##*.}" != "framework" ]; then
@cmkilger
cmkilger / preferred_localizations.js
Created April 13, 2022 20:32
Pick a preferred language from a set of languages based on a list of preferences
// Finds the best locale from available locales based on the user's preferences. Falls back on the default locale if none is found.
function preferredLocale(preferences, availableLocales, defaultLocale) {
// Parses the BCP 47 locale into a ISO-639-3 language, ISO-15924 script, and M.49 region.
function parseLocale(locale) {
// Map grandfathered language identifiers to ISO-639
const grandfatheredMap = {
'art-lojban': 'jbo', 'i-ami': 'ami', 'i-bnn': 'bnn', 'i-hak': 'hak', 'i-klingon': 'tlh', 'i-lux': 'lb', 'i-navajo': 'nv',
'i-pwn': 'pwn', 'i-tao': 'tao', 'i-tay': 'tay', 'i-tsu': 'tsu', 'no-bok': 'nb', 'no-nyn': 'nn', 'sgn-BE-FR': 'sfb',
'sgn-BE-NL': 'vgt', 'sgn-CH-DE': 'sgg', 'zh-guoyu': 'zh', 'zh-hakka': 'hak', 'zh-min-nan': 'nan', 'zh-xiang': 'hsn'
}