Skip to content

Instantly share code, notes, and snippets.

View ik5's full-sized avatar
🎯
Focusing

ik5 ik5

🎯
Focusing
View GitHub Profile
@ik5
ik5 / to_uint64.go
Created January 27, 2017 13:09
convert a list of items in a string to a uint64 list
package main
import (
"fmt"
"strconv"
"strings"
)
func toUin64(data, sep string) []uint64 {
fields := strings.Split(data, sep)
@ik5
ik5 / requeue.go
Created January 28, 2017 19:49
how to re-queue a scheduled item in amqp
package main
import (
"fmt"
"net/url"
"github.com/streadway/amqp"
)
// InitByConfigServices loads configuration from a conf file
@ik5
ik5 / main.go
Created February 18, 2017 10:33
go 1.8 plugins
package main
import "C"
import (
"fmt"
"plugin"
)
func main() {
p, err := plugin.Open("./plugins.so")
@ik5
ik5 / scryp.go
Created March 2, 2017 20:35
example on using scrypt
package main
import (
"crypto/rand"
"fmt"
"golang.org/x/crypto/scrypt"
)
func main() {
@ik5
ik5 / pause_resume.go
Created July 30, 2017 16:59
Example on pause and resume threads in golang
package main
import (
"fmt"
"runtime"
"time"
)
// Thread stages
const (
@ik5
ik5 / sanitize.rb
Last active November 23, 2017 08:29
Example of how to sanitize file names (base, without path) to avoid any malicious actions
# help to avoid path traversal, and execution of anything on a machine
# due to file name
def escape_file_name(name)
# regex is a s follows:
# if it's the begining of the string, or there is no escape char
# for the following chars,
# add an escape for that char
name.gsub(/(^|[^\\])([\s\!\'\"#$&\^\*\`\/\(\)\[\]\?\{\}\|\~])/) do |match|
"\\#{match[1]}"
end
@ik5
ik5 / func.sh
Last active March 3, 2018 08:23
Bash Example for short var names methodology
#!/bin/sh
func() {
for p in $(echo /tmp/*); do
f=$(basename -a "$path")
echo $f
done
}
@ik5
ik5 / documented_code.c
Last active March 14, 2018 08:38
example of self documenting code
/* ... */
const char * get_message(void * payload) {
payload_t *data = payload;
if (!data->len > MIN_PAYLOAD_LEN) {
return null;
}
return sprintf("%s%s", data->name, data->content);
}
@ik5
ik5 / regexp_vs_start_with.rb
Created March 18, 2018 10:02
Benchmark that calculate the difference between regexp and start_with?
require 'benchmark'
require 'securerandom'
N = 1_000_000
REGEX = /^(abc|Abc|ABC)/
def gen_randstr(len = 24)
SecureRandom.base64(len)
end
require 'benchmark'
N = 1_000_000
REGEX = /^abc/
STR = 'abc are letters'.freeze
Benchmark.bm(13) do |x|
x.report('start_with?') do
N.times do
STR.start_with?('abc')