Skip to content

Instantly share code, notes, and snippets.

View qi7chen's full-sized avatar
🎯
Focusing

Johnnie qi7chen

🎯
Focusing
  • Ubisoft
  • China
View GitHub Profile
@qi7chen
qi7chen / prod_con.lua
Created November 29, 2014 09:56
lua coroutine producer/consumer
local coroutine = coroutine
local function receive(p)
local s, value = coroutine.resume(p)
return value
end
local function send(x)
coroutine.yield(x)
end
-- map(table, function)
-- e.g: map({1,2,3}, function(a) return a*2 end) -> {2,4,6}
function map(tbl, func)
local newtbl = {}
for i,v in pairs(tbl) do
newtbl[i] = func(v)
end
return newtbl
end
@qi7chen
qi7chen / zmq_curve.cpp
Created November 23, 2014 04:46
zmq curve server and client
#include <assert.h>
#include <stdint.h>
#include <string>
#include <iostream>
#include <thread>
#include <chrono>
#include <zmq.h>
#include <zmq_utils.h>
@qi7chen
qi7chen / stripJsonComments.js
Created November 14, 2014 03:37
strip json comments
function stripJsonComments(str) {
var currentChar;
var nextChar;
var insideString = false;
var insideComment = false;
var ret = '';
for (var i = 0; i < str.length; i++) {
currentChar = str[i];
nextChar = str[i + 1];
#include <algorithm> // std::min
#ifdef _WIN32
#include <windows.h>
#include <dbghelp.h>
#pragma comment(lib, "dbghelp")
#define TRACE_MAX_STACK_FRAMES 1024
#define TRACE_MAX_FUNCTION_NAME_LENGTH 1024
/*
* folly::merge() is an implementation of std::merge with one additonal
* guarantee: if the input ranges overlap, the order that values *from the two
* different ranges* appear in the output is well defined (std::merge only
* guarantees relative ordering is maintained within a single input range).
* This semantic is very useful when the output container removes duplicates
* (such as std::map) to guarantee that elements from b override elements from
* a.
*
#include <stdint.h>
#include <stdio.h>
int32_t GetFileLength(const char* filename)
{
assert(filename);
FILE* fp = fopen(filename, "r");
if (fp)
{
int32_t length = 0;
@qi7chen
qi7chen / bin2hex.cpp
Created November 12, 2014 04:44
binary to hex
std::string bin2hex(const void* data, size_t len)
{
std::string result;
result.reserve(len * 2);
static const char hex[] = "0123456789abcdef";
for (size_t i = 0; i < len; i++)
{
char ch = ((const char*)data)[i];
result += hex[ch >> 4];
result += hex[ch & 0x0f];
@qi7chen
qi7chen / tickcount.c
Created November 12, 2014 04:42
get tick counter
#include <stdint.h>
#include <assert.h>
#include <time.h>
#define CHECK assert
#if defined(_MSC_VER)
#include <windows.h>
inline uint64_t getPerformanceFreqency()
@qi7chen
qi7chen / lua_socket.c
Created October 25, 2014 12:48
BSD socket in lua
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <Windows.h>
#include <malloc.h>
#include <assert.h>
#include <lua.hpp>
#define SOCKET_HANDLE "socket*."
#define check_socket(L) (*(SOCKET*)luaL_checkudata(L, 1, SOCKET_HANDLE))