Skip to content

Instantly share code, notes, and snippets.

View huandu's full-sized avatar

Huan Du huandu

View GitHub Profile
@huandu
huandu / nginx-cors.conf
Last active July 11, 2020 12:28 — forked from sbuzonas/nginx-cors.conf
Nginx CORS maps
map $http_origin $allow_origin {
default "$http_origin";
}
map $request_method $cors_method {
default "allowed";
"OPTIONS" "preflight";
}
map $cors_method $cors_max_age {
@huandu
huandu / list_properties.swift
Created November 22, 2014 18:00
List properties of a swift class.
import Foundation
struct Point {
var x = 0.0
var y = 0.0
}
class PropertyHub {
var simpleProp = "Foo"
var structProp = Point()
@huandu
huandu / OUTPUT.txt
Last active July 18, 2019 12:01
Show currency in different locales
$ go run main.go
CNY cn-ZH 123,456.79元
CNY en-US ¥123,456.79
CNY es-MX ¥123,456.79
CNY en-AU ¥123,456.79
CNY ja-JP ¥123,456.79
CNY es-PE ¥123,456.79
USD cn-ZH $123,456.79
USD en-US $123,456.79
USD es-MX $123,456.79
@huandu
huandu / macro_strrchr.cpp
Created July 12, 2014 15:13
A macro implementation of `strrchr()`
#include <iostream>
using namespace std;
#define _STRRCHR_IMPL_COMMON(str, ch, offset) (str)[sizeof((str)) - 1 - (offset)] == (ch)? (str) + sizeof((str)) - (offset): sizeof((str)) <= (offset) + 1? (str)
#define _STRRCHR_IMPL_31(str, ch) (_STRRCHR_IMPL_COMMON(str, ch, 31): (str))
#define _STRRCHR_IMPL_30(str, ch) (_STRRCHR_IMPL_COMMON(str, ch, 30): _STRRCHR_IMPL_31(str, ch))
#define _STRRCHR_IMPL_29(str, ch) (_STRRCHR_IMPL_COMMON(str, ch, 29): _STRRCHR_IMPL_30(str, ch))
#define _STRRCHR_IMPL_28(str, ch) (_STRRCHR_IMPL_COMMON(str, ch, 28): _STRRCHR_IMPL_29(str, ch))
#define _STRRCHR_IMPL_27(str, ch) (_STRRCHR_IMPL_COMMON(str, ch, 27): _STRRCHR_IMPL_28(str, ch))
@huandu
huandu / parse_csv_demo.sh
Created December 3, 2014 05:28
Demo to parse CSV file with awk.
# parse csv files to an awk array.
awk -F, '{
parse_csv(r);
# replace following line with your code.
print r[1], r[2], r[3], r[4];
} function parse_csv(r, _quote, _i, _n) {
_i = 1;
_quote = 0; # in a quoted string or not.
@huandu
huandu / queue.go
Last active September 29, 2017 07:02
第二种方案:一个简单的并发可控、任务可随意拼接的任务队列实现
// 一个简单的并发可控、任务可随意拼接的任务队列实现。
// 仅作概念演示用,细节不要纠结。
//
// 基本结构:
// Task:当前任务共享的上下文,任务通过上下文交换数据,一个任务可分为很多的工作(Work)
// Dispatcher:任务队列管理器,负责创建 Task 并使用合适的 Worker 来处理数据
// Worker:任务的抽象接口
// XXXWorker:各个具体的任务处理逻辑
// WorkerBench:一个 Worker 池,确保当前正在运行的 Worker 数量不超过限制
package main
@huandu
huandu / README.md
Created May 31, 2013 08:25
Quick thoughts about my new personal project, "the sisters".

System admin work is boring. I want to find a way to save myself (and other system admins if possible).

I was inspired by github's hubot. It's expressive simple and really make system admin's work funny. I like the idea learning by watching. I want to use the hubot in my project.

But sadly, hubot just misses one important thing - it's a centralized bot without any network level support. In my scenario, I have some computers in cloud, some in real racks and some in company's intranet. I don't have a server that can communicate to all other servers. I need a "bridge" to connect all servers so that I can use the bot to control everything.

What's more, I want the "bridge" is able to work as a back door application. I don't want to be bothered by authentication details. I need to use one unified credential to operate all servers.

I call this bridge application as "the sisters". This name comes from a Japanese novel [Toaru Majutsu no Index](http://ja.wikipedia.org/wiki/%E3%81%A8%E3

@huandu
huandu / README.md
Last active December 11, 2015 15:58
Sample concept source files for project "shana".

"shana" is a project to introduce html-like rendering experience to opengl-based cross platform mobile apps. It will be built on top of cocos2d-x 2.x.

"shana" will define a new mark-up UI language looks like html. It will use stylesheet and CSS selectors to ease styling work. To make styling work even more flexible, "shana" will support expressions in all node attributes and style rules. Writing "center" in attribute "x", or "parent.width/2+10" in attribute "width" will work exactly as expected.

"shana" will include a compiler to generate binary shana file from xml sources and a C++ lib to render shana file and manipulate nodes. It may include an http server to compile source files on the fly. And it may also include several apps to load and preview shana file on mobile devices.

"shana" project scope is quite big. Currently, I'm doing very early design work. If you are interested in this project, please feel free to let me know. Your suggestion and/or participant is highly appreciated.

@huandu
huandu / flip_cards.html
Created December 19, 2012 03:43
A flip card demo for Chrome and Safari.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Test Flip Cards Performance</title>
<link rel="stylesheet" href="cards.css" />
<style type="text/css">
body { background: #000; padding-top: 200px; font-family: Helvetica; }
#scene { background: url(body.jpg) no-repeat; margin: auto; text-align: center; width: 640px; height: 350px; padding: 50px; }
@huandu
huandu / git-checkout-all-remotes
Created October 6, 2012 03:54
git checkout all remote branches
#! /bin/bash
REMOTE_NAME=$1
if [ "$REMOTE_NAME" == "" ]; then
cat <<EOF
Usage: $0 remote
remote Remote name, which can be 'origin' in common case.
EOF