Skip to content

Instantly share code, notes, and snippets.

View bagpack's full-sized avatar

bagpack bagpack

View GitHub Profile
@bagpack
bagpack / BinarySearch.kt
Created June 4, 2019 07:15
binary search
private fun binarySearch(list: List<Int>, key: Int, min: Int, max: Int): Int {
if (max < min) {
return -1
}
val mid = min + (max - min) / 2
return when {
list[mid] > key -> binarySearch(list, key, min, mid - 1)
list[mid] < key -> binarySearch(list, key, mid + 1, max)
else -> mid
@bagpack
bagpack / ActivityViewController.h
Created March 28, 2018 12:51
UIActivity with WhiteList
#import <UIKit/UIKit.h>
#ifndef ActivityViewController_h
#define ActivityViewController_h
@interface UIActivityViewController (Private)
- (BOOL)_shouldExcludeActivityType:(UIActivity*)activity;
@bagpack
bagpack / Dockerfile
Created March 13, 2018 08:58
php-5.6-apache-mcrypt
FROM php:5.6-apache
ENV APP_ROOT /var/www
WORKDIR $APP_ROOT
RUN apt-get update && apt-get install -y \
libmcrypt-dev
RUN docker-php-ext-install pdo_mysql mysqli mcrypt
RUN a2enmod vhost_alias && a2enmod rewrite && service apache2 restart
@bagpack
bagpack / X509.swift
Created February 16, 2018 06:48
X.509/P12
enum X509Error: Error {
case certificateError(message: String)
case publicKeyError(message: String)
}
class X509 {
// A DER (Distinguished Encoding Rules) representation of an X.509 certificate.
let publicKey: SecKey
@bagpack
bagpack / RichTextParser.swift
Last active February 28, 2018 09:56
<color=#FF00FF>kappa</color> -> NSAttributedString
class RichTextParser {
class func parse(text: String) throws -> NSAttributedString {
let colorPattern = "<color=#([\\w]{6})>(.+?)</color>"
let attributedText = NSMutableAttributedString()
do {
let nsText = (text as NSString)
let regex = try NSRegularExpression(pattern: colorPattern, options: [])
let matches = regex.matches(in: text, options: [], range: NSRange(location: 0, length: nsText.length))
@bagpack
bagpack / Realm+truncateAll.swift
Created October 12, 2017 09:25
truncate all tables in RealmSwift
import RealmSwift
extension Realm {
static func truncateAll(`in` realm: Realm) {
for objectScheme in realm.schema.objectSchema {
if let clazz = Realm.clazz(fromClassName: objectScheme.className) {
try! clazz.deleteAll(in: realm)
}
}
}
extension String {
func colorized(rangeOf: String, color: UIColor, font: UIFont) -> NSAttributedString {
let string = self as NSString
let range = string.range(of: rangeOf)
let attributedText = NSMutableAttributedString(string: self)
attributedText.addAttributes([NSForegroundColorAttributeName: color], range: range)
attributedText.addAttributes([NSFontAttributeName: font], range: range)
return attributedText
}
@bagpack
bagpack / Extensions.swift
Created October 11, 2017 04:16
ex) 9999 -> "9,999"
extension Int {
func decimal() -> String {
struct Static { // swiftlint:disable:this nesting cache formatter
static var formatter: NumberFormatter! = nil
}
if Static.formatter == nil {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.groupingSeparator = ","
@bagpack
bagpack / Dockerfile-amazonlinux-py36
Last active April 28, 2017 10:21
Dockerfile python3.6.0 in amazonlinux
FROM amazonlinux
WORKDIR /work
RUN set -x && \
yum -y install yum-plugin-fastestmirror && \
yum install -y gcc gcc-c++ make git openssl-devel bzip2-devel zlib-devel readline-devel sqlite-devel && \
yum install -y libtiff-devel libjpeg-devel libzip-devel freetype-devel \
lcms2-devel libwebp-devel tcl-devel tk-devel
@bagpack
bagpack / encode_alphanumeric.py
Created October 19, 2016 06:06
Encode alphanumeric in QR code
'''
Encode alphanumeric in QR code
'''
def encode_alphanum(c):
if c >= '0' and c <= '9':
return ord(c) - ord('0')
elif c >= 'A' and c <= 'Z':
return ord(c) - ord('A') + 10
elif c == ' ':