Skip to content

Instantly share code, notes, and snippets.

View RockfordWei's full-sized avatar

Rockford Wei RockfordWei

View GitHub Profile
@RockfordWei
RockfordWei / demo.swift
Created August 22, 2017 14:55
How to reuse Perfect Swift MySQL connection threaded safely.
import MySQL
import PerfectThread
#if os(Linux)
import Glibc
#else
import Darwin
#endif
let mysql = MySQL()
let lock = Threading.Lock()
@RockfordWei
RockfordWei / object-description.swift
Created August 31, 2017 19:24
Swift 4 Struct Description
import Foundation
public struct Person {
public var name = ""
public var age = 0
}
let rocky = Person(name: "rocky", age: 24)
print(rocky)
@RockfordWei
RockfordWei / main.swift
Last active September 2, 2017 16:33
Swift 4.0 JSON Usage
import Foundation
public struct Person: Codable, Equatable {
public var name = ""
public var age = 0
public static func == (lhs: Person, rhs: Person) -> Bool {
return lhs.name == rhs.name && lhs.age == rhs.age
}
}
@RockfordWei
RockfordWei / scanhex.swift
Created September 11, 2017 20:55
hex scan
import Foundation
public extension String {
public func scanHex() -> UInt32? {
var value = UInt32(0)
#if os(Linux)
if Scanner(string: self).scanHexInt(&value) {
return value
} else {
return nil
@RockfordWei
RockfordWei / Package.swift
Created November 7, 2017 14:23
How to generate a static library
// swift-tools-version:4.0
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "Extest",
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
@RockfordWei
RockfordWei / main.c
Created November 16, 2017 17:07
camel game
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define max_water 3
#define max_food 3
int done = 0;
int traveled = 0, thirst = 0, camel_tiredness = 0;
@RockfordWei
RockfordWei / shell.swift
Created December 7, 2017 16:49
run bash command
import Foundation
#if os(OSX)
func bash(command: String) -> String? {
let task = Process()
task.launchPath = "/bin/bash"
task.arguments = ["-c", command]
let o = Pipe()
task.standardOutput = o
task.launch()
task.waitUntilExit()
@RockfordWei
RockfordWei / aes128.swift
Created January 4, 2018 14:55
CommonCrypto Vs PerfectCrypto
import PerfectCrypto
func AES128Encryption(_ string: String, key: String) -> String? {
guard let hashData = key.digest(.sha384) else { return nil }
let hashKeyData:[UInt8] = hashData[0..<32].map {$0}
let ivData:[UInt8] = hashData[32..<48].map {$0}
let data:[UInt8] = string.utf8.map { $0 }
guard let x = data.encrypt(.aes_128_cbc, key: hashKeyData, iv: ivData),
let y = x.encode(.base64) else {
return nil
@RockfordWei
RockfordWei / gitflow-breakdown.md
Created April 16, 2018 13:48 — forked from JamesMGreene/gitflow-breakdown.md
A comparison of using `git flow` commands versus raw `git` commands.

Initialize

gitflow git
git flow init git init
  git commit --allow-empty -m "Initial commit"
  git checkout -b develop master

Connect to the remote repository

@RockfordWei
RockfordWei / EncodedScaler.py
Last active July 13, 2018 18:53
Non-linear scale
import numpy as np
import pandas as pd
import random
import math
class EncodedScaler:
milestones = [0,0,0]
unit = 0
count= 0
size = 0