Skip to content

Instantly share code, notes, and snippets.

@yhirose
yhirose / lz4_decompress.rb
Created April 7, 2014 02:38
Decompress LZ4 data
def lz4_decompress(path)
def read_value(data, pos, size)
case size
when 1; type = 'C'
when 2; type = 'S'
when 4; type = 'L'
end
[pos + size, data[pos, size].unpack(type)[0]]
end
@yhirose
yhirose / gist:e6208ef2a571c89c1956
Last active August 29, 2015 14:16
filter_and_map
template <typename T, typename Pred, typename Conv>
auto filter_and_map(const vector<T>& cont, Pred pred, Conv conv) -> vector<typename std::remove_const<decltype(conv(T()))>::type> {
vector<typename std::remove_const<decltype(conv(T()))>::type> r;
for (const auto& el: cont) {
if (pred(el)) r.push_back(conv(el));
}
return r;
}
bool read_file(const char* path, vector<char>& buff)
{
ifstream ifs(path, ios::in | ios::binary);
if (ifs.fail()) {
return false;
}
buff.resize(static_cast<unsigned int>(ifs.seekg(0, ios::end).tellg()));
if (!buff.empty()) {
ifs.seekg(0, ios::beg).read(&buff[0], static_cast<streamsize>(buff.size()));
@yhirose
yhirose / gist:cb48d799db81047a4b10
Created June 17, 2015 14:04
regex_replace_with_lambda
template<typename T, typename F>
std::basic_string<T> regex_replace_with_lambda(
const std::basic_string<T>& str,
const std::basic_regex<T>& re,
F f)
{
typedef std::basic_string<T> StrT;
auto ret = StrT();
@yhirose
yhirose / memoize.php
Created February 2, 2012 03:32
PHP Lambda based memoizer
<?php
// Lambda based memoizer
function memoize($fn) {
$cache = array();
return function ($key) use($fn, &$cache) {
if (!isset($cache[$key])) {
$cache[$key] = $fn($key);
}
return $cache[$key];
@yhirose
yhirose / NumConv.vim
Created February 2, 2012 22:06
Conver number to number at cursor in VIM
" Conver number to number at cursor.
function! NumConv(base, fmt)
let b = a:base
let fmt = a:fmt
let w = expand('<cword>')
let d = str2nr(w, b)
let h = printf(fmt, d)
execute "normal ciw" . h
endfunction
#------------------------------------------------------------------------------
# play_sound
#------------------------------------------------------------------------------
def play_sound(url_string)
url = NSURL.URLWithString(url_string)
s = NSSound.alloc.initWithContentsOfURL(url, byReference: false)
s.delegate = self
s.play
end
//-----------------------------------------------------------------------------
// alert
//-----------------------------------------------------------------------------
[[NSAlert alertWithMessageText:@"Caption"
defaultButton:@"OK"
alternateButton:nil
otherButton:nil
informativeTextWithFormat:@"Message"] runModal];
@yhirose
yhirose / gist:3195927
Created July 29, 2012 03:32
PHP tiny mustache implementation
<?php
# Simple Mustache template engine
function render($tmpl, $params) {
return preg_replace_callback(
'/(?:\{\{(\w+)\}\}|\{\{\{(\w+)\}\}\})/',
function($m) use($params) {
if (isset($params[$m[1]])) {
return htmlspecialchars($params[$m[1]]);
} else if (isset($params[$m[2]])) {
@yhirose
yhirose / pinyin-bookmarklet.txt
Created October 5, 2012 00:46
Pinyin book marklet for WOL
javascript:%0Aif(window.hasOwnProperty('pinyin'))%0Awindow.pinyin();else%0A$('script').attr('src','https://raw.github.com/yhirose/wol-bookmarklets/master/pinyin.js').appendTo('body');