Skip to content

Instantly share code, notes, and snippets.

@yhirose
yhirose / compress.go
Last active February 4, 2016 17:00 — forked from iamralch/compress.go
ZIP archives in Golang
import (
"archive/zip"
"io"
"os"
"path/filepath"
"strings"
)
func zipit(source, target string, needBaseDir bool) error {
zipfile, err := os.Create(target)
i := 0
for {
pc, fn, line, ok := runtime.Caller(i)
if ok == false {
break
}
fmt.Printf("[stack-%d] %s[%s:%d]\n", i, runtime.FuncForPC(pc).Name(), fn, line)
i++
}
func columnWidth(s string) int {
runes := []rune(s)
w := 0
for _, r := range runes {
k := width.LookupRune(r).Kind()
if k == width.EastAsianWide || k == width.EastAsianFullwidth {
w = w + 2
} else {
w = w + 1
}
@yhirose
yhirose / utf8.cpp
Created October 23, 2015 13:38
UTF8 character length utility functions
#include <iostream>
#include <string>
//-----------------------------------------------------------------------------
// utf8_char_len
//-----------------------------------------------------------------------------
template <typename T>
size_t utf8_char_len(const char* str, size_t off, T eos)
{
@yhirose
yhirose / replace_all.cpp
Last active November 29, 2015 16:04
replace_all
template <typename T>
std::basic_string<T> replace_all(const std::basic_string<T>& str, const T* from, const T* to)
{
std::basic_string<T> ret;
ret.reserve(str.length());
size_t from_len = 0;
while (from[from_len]) {
from_len++;
}
@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();
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: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;
}
@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 / 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');