Skip to content

Instantly share code, notes, and snippets.

View krzyzanowskim's full-sized avatar

Marcin Krzyzanowski krzyzanowskim

View GitHub Profile
@krzyzanowskim
krzyzanowskim / NumberOfBits.swift
Last active August 29, 2015 14:14
Calculate number of bits for Big Integer
// Playground - noun: a place where people can play
import Foundation
func numberOfBits(bytes:[Byte]) -> Int {
if (bytes.count == 0) {
return 0;
}
var toRemove = 0;
@krzyzanowskim
krzyzanowskim / EncryptAESWithPHP
Created February 1, 2015 22:59
How to encrypt data with padding usign AES cipher, complatible with CryptoSwift defaults.
// Marcin Krzyżanwski
// Code for https://github.com/krzyzanowskim/CryptoSwift/issues/20
// PHP
function encrypt($plaintext, $key, $iv) {
$ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $plaintext, MCRYPT_MODE_CBC, $iv);
return base64_encode($ciphertext);
}
function decrypt($ciphertext_base64, $key, $iv) {
@krzyzanowskim
krzyzanowskim / switf_neste_function_self
Created February 25, 2015 14:26
Swift nested function and variable reference
class Foo {
var some:String = "string"
func One() {
func Nested(weakSelf:Foo) {
if (weakSelf.some == "integer") { // no error
}
}
@krzyzanowskim
krzyzanowskim / swift_crash
Last active August 29, 2015 14:16
Swift crash. Initializer on protocol.
// Version 6.3 (6D532l)
protocol Proto {
init()
}
struct S1: Proto {
init() {}
}
@krzyzanowskim
krzyzanowskim / integerWithBytes
Last active May 30, 2018 03:36
integerWithBytes Swift way
// Playground - noun: a place where people can play
import Foundation
typealias Byte = UInt8
protocol GenericIntegerType: IntegerType {
init(_ v: Int)
init(_ v: UInt)
init(_ v: Int8)
@krzyzanowskim
krzyzanowskim / swift_array_dance
Created March 10, 2015 17:13
Swift NSArray wiredeness
import Foundation
let arr1 = []
arr1 is NSArray // true
arr1 is NSMutableArray // false
arr1.append(1) // 'NSArray' does not have a member named 'append'
var arr2 = []
arr2 is NSArray // true
arr2 is NSMutableArray // false
extension Array {
/** split in chunks with given chunk size */
func chunks(chunksize:Int) -> [Array<T>] {
var words:[[T]] = [[T]]()
words.reserveCapacity(self.count / chunksize)
for var idx = chunksize; idx <= self.count; idx = idx + chunksize {
let word = Array(self[idx - chunksize..<idx]) // this is slow
words.append(word) // slower than this
@krzyzanowskim
krzyzanowskim / swift_+_vs_extend
Created March 16, 2015 20:36
+ vs extend() performance
import Foundation
func perf(text: String, closure: () -> ()) {
let measurementStart = NSDate();
closure()
let measurementStop = NSDate();
let executionTime = measurementStop.timeIntervalSinceDate(measurementStart)
@krzyzanowskim
krzyzanowskim / swift_4_dim_array
Created March 16, 2015 22:14
4 dimensional array
let array = [[[[UInt8]]]](count: 1, repeatedValue:
[[[UInt8]]](count: 2, repeatedValue:
[[UInt8]](count: 2, repeatedValue:
[UInt8](count: 2, repeatedValue: 0))))
import Foundation
public class Foo {
private var key:NSData
private var goo:NSData?
public init(key: NSData) {
self.key = key
self.goo = nil
}