Skip to content

Instantly share code, notes, and snippets.

View luikore's full-sized avatar

luikore luikore

View GitHub Profile
@luikore
luikore / readme.md
Last active August 29, 2015 14:01
repeatable reads in mysql and pg

Mysql doesn't auto lock or version lock records when select in repeatable read transactions. While PG can forbid concurrent updates.

The test table DDL in PG:

create table articles (
  "id" serial,
  "n" integer
);
@luikore
luikore / compare.c
Created July 13, 2014 10:33
pool alloc is about 2.5 times faster and smaller
/*
benchmark with gtime -f "%M"
pool alloc:
time: 0.028571
22331392
malloc:
@luikore
luikore / 0_reuse_code.js
Last active August 29, 2015 14:07
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@luikore
luikore / profile.sh
Created October 31, 2014 07:04
shrink pdf size
# need to install ghostscript first
pdf-shrink() {
if [[ -e $1 ]]; then
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile=$2 $1
else
echo 'usage: pdf-shrink in.pdf out.pdf'
fi
}
@luikore
luikore / china_id_verify.rb
Last active August 29, 2015 14:12
Verify Chinese ID card number
def cn_id_verify id, gender=nil
return false if id !~ /\A\d{17}[\dX]\z/i
r = 0
x = 1
id.chars.take(17).reverse_each {|a|
x = (x * 2) % 11
r += a.to_i * x
}
last = (12 - r % 11)
last = 'X' if last == 10
@luikore
luikore / test.md
Last active August 29, 2015 14:12
markdown on v2ex

gist is...

the way to markdown

  • and so you can edit/delete post
@luikore
luikore / gen_regexp_fullwidth.rb
Last active August 29, 2015 14:15
Regexp to Match Fullwidth Characters
# generate a regexp for match full-width characters
# data from
#
# A ; Ambiguous 不确定
# F ; Fullwidth 全宽
# H ; Halfwidth 半宽
# N ; Neutral 中性
# Na ; Narrow 窄
# W ; Wide 宽
#
require 'state_machine'
class StateMachine::Machine
def par_event events, test_field, trans
i = 1
# binary 11....1
full = (1 << events.size) - 1
events.each do |e|
j = i
event e do
transition(hash.merge(:if => (lambda{ |obj|
@luikore
luikore / 99 bottles of beer
Created June 24, 2009 15:23
99 bottles of beer
# 99 bottles of beer
class Fixnum
def bottle
"#{self} bottles of beer".
sub(/^-1/,'99').
sub(/^0/,'no more').
sub(/^(1\ .+)s/, '\1')
end
def wall
"#{self.bottle} on the wall"
@luikore
luikore / state_lexer.rb
Created June 30, 2009 16:21
state lexer
# state lexer
require 'strscan'
class StateLexer
attr_reader :state
class ScanError < StandardError; end
def initialize opts = {:states => []}