Skip to content

Instantly share code, notes, and snippets.

🏠
Working from home

Tony Han tony612

🏠
Working from home
Block or report user

Report or block tony612

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View mmap_test.c
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
int main(void) {
size_t pagesize = getpagesize();
printf("System page size: %zu bytes\n", pagesize);
View binary_parse_slow.exs
defmodule BinaryParseSlow do
def parse(bin) do
parse(bin, 0)
end
def parse(bin, acc) do
case do_parse(bin) do
{:nofin, x, rest} ->
parse(rest, acc + x)
{:fin, x} ->
View binary_parse_fast.exs
# For binary 0000 0001 0100 0000 1000 1000
# The result is 0b1 + 0b100_0000 + 0b1000 = 73
defmodule BinaryParseFast do
def parse(bin) do
parse(bin, 0)
end
# A byte beginning with 0 means the end, but still need to add the last 7 bits
def parse(<<0::1, x::7, _::bits>>, acc), do: acc + x
# A byte beginning with 1 means there is more data needed to be processed.
View generate_all_float.ex
for a1 <- 0..1,
a2 <- 0..1,
a3 <- 0..1,
a4 <- 0..1,
a5 <- 0..1,
a6 <- 0..1,
a7 <- 0..1,
a8 <- 0..1,
a9 <- 0..1,
@tony612
tony612 / binary_parse_fast.exs
Last active Jul 25, 2019
Erlang binary parsing comparing
View binary_parse_fast.exs
# For 0000 0001 0100 0000 1000 1000
# The result is 0b1 + 0b100_0000 + 0b1000 = 73
defmodule BinaryParseFast do
def parse(bin) do
parse(bin, 0)
end
# def parse(<<0::1, x::7, _::bits>>, acc), do: acc + x
# def parse(<<1::1, x::7, rest::bits>>, acc) do
# parse(rest, acc + x)
@tony612
tony612 / bench.exs
Last active Jul 22, 2019
Bench pattern match with functions
View bench.exs
defmodule Foo do
Enum.each(1..100, fn i ->
def field(unquote(:"foo#{i}")) do
"field #{unquote(i)}"
end
def unquote(:"field_foo#{i}")() do
"field #{unquote(i)}"
end
end)
@tony612
tony612 / test.js
Last active Jul 3, 2019
Test node protobuf float
View test.js
var protobuf = require('protobufjs');
protobuf.load("test.proto", function (err, root) {
if (err)
throw err;
var Message = root.lookupType("test.Message");
var payload = { foo1: 123.0, foo2: 123.5 };
var errMsg = Message.verify(payload);
View sentry_fingerprinter.ex
defmodule Sentry.Fingerprinter do
def custom_fingerprints(metadata, _msg) do
file = Keyword.get(metadata, :file)
line = Keyword.get(metadata, :line)
if file && line do
%{
application: Keyword.get(metadata, :application),
module: Keyword.get(metadata, :module),
function: Keyword.get(metadata, :function),
View linux_config.sh
export PS1="\\u@\\h:\\w:\$(git branch 2>/dev/null | grep '^*' | colrm 1 2)\$ "
#set-option -g mouse on
setw -g mode-keys vi
bind-key -Tcopy-mode-vi 'v' send -X begin-selection
bind-key -Tcopy-mode-vi 'y' send -X copy-selection
# set -g terminal-overrides 'xterm*:smcup@:rmcup@'
## Debian apt
@tony612
tony612 / test_binary_parse.exs
Last active Feb 19, 2019
Elixir binary parsing test
View test_binary_parse.exs
defmodule Foo do
def foo1(bin, arg \\ 0) do
case do_foo1(bin) do
{:end, bin} = r ->
{r, arg}
{a, rest} ->
foo1(rest, arg + 1)
end
end
You can’t perform that action at this time.