Skip to content

Instantly share code, notes, and snippets.

@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++;
}
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
}
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++
}
@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)
@yhirose
yhirose / debug.h
Created July 20, 2016 23:11
Debug Print
#define debug(...) { fprintf(stderr, __VA_ARGS__); fprintf(stderr, "\n"); }
// bash> tail -f debug.log
// bash> [command] 2> debug.log
Expr ← Sum
Sum ← Product (('+' / '-') Product)*
Product ← Value (('*' / '/') Value)*
Value ← [0-9]+ / '(' Expr ')'
@yhirose
yhirose / yhirose-karabiner-elements-complex-modifications.json
Created January 5, 2018 19:51
My Karabiner-Elements Complex-Modifications
{
"title": "For yhirose's HHKB",
"rules": [
{
"description": "Change Shift+Esc to ~",
"manipulators": [
{
"type": "basic",
"from": {
"key_code": "escape",
[alias]
co = checkout
st = status
br = branch
ci = commit
l = log --date=short --pretty='format:%C(yellow)%h %C(cyan)%ad %C(green)%<(16,trunc)%an%Creset%C(yellow)%d%Creset %s'
lg = log --date=short --pretty='format:%C(yellow)%h %C(cyan)%ad %C(green)%<(16,trunc)%an%Creset%C(yellow)%d%Creset %s' --graph
@yhirose
yhirose / ArrayInt.cpp
Last active May 23, 2019 14:48
ArrayInt Translator examples with ANTLR 4 and cpp-peglib
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <sstream>
#include "peglib.h"
using namespace peg;
using namespace std;
bool translate(const string& text, string& result) {