Skip to content

Instantly share code, notes, and snippets.

@bmitchelmore
bmitchelmore / DataHex.swift
Created March 12, 2024 03:58
Efficient Data to hex string
extension Data {
private static let hexDigits = Array("0123456789abcdef".utf8)
var hex: String {
var chars = [Unicode.UTF8.CodeUnit]()
chars.reserveCapacity(2 * count)
for byte in self {
chars.append(Self.hexDigits[Int(byte >> 4)]) // byte / 16
chars.append(Self.hexDigits[Int(byte & 15)]) // byte % 16
}
return String(decoding: chars, as: UTF8.self)

Keybase proof

I hereby claim:

  • I am bmitchelmore on github.
  • I am bmitchelmore (https://keybase.io/bmitchelmore) on keybase.
  • I have a public key ASCfQyU4tXEf60pJOZb0z0FMuc1T9zOFMnLhS4q8vGqcvwo

To claim this, I am signing this object:

@bmitchelmore
bmitchelmore / OrderedSet.swift
Last active August 23, 2019 04:41
Constant time OrderedSet
struct OrderedSet<T: Hashable> {
private class Link {
var previous: Link?
var next: Link?
var value: T
init(value: T) {
self.value = value
}
}
@bmitchelmore
bmitchelmore / gist:5196446
Created March 19, 2013 14:15
Implementing maxlength for UITextField. This will prevent any character sequence from being inserted that would make the textfield's text exceed the maxlength. If you paste in multicharacter sequences that will make the field exceed its max length, the entire sequence will be rejected.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSInteger result = textField.text.length;
result -= range.length;
result += string.length;
return result <= MAX_LENGTH;
}
@bmitchelmore
bmitchelmore / inplace-radix.js
Created February 28, 2012 04:52
In Place Radix Sort in Javascript
var nums = [35, 25, 53, 3, 102, 203, 230, 1005];
// Figure out the number of binary digits we're dealing with
var k = Math.max.apply(null, nums.map(function(i) {
return Math.ceil(Math.log(i)/Math.log(2));
}));
for (var d = 0; d < k; ++d) {
for (var i = 0, p = 0, b = 1 << d, n = nums.length; i < n; ++i) {
var o = nums[i];