Skip to content

Instantly share code, notes, and snippets.

func findMatching(s: string; first, last: Natural; a, b: static char; d: static int): int {.raises: [ValueError].} =
var i = first.int
var level = 0
while true:
if s[i] == a:
inc level
elif s[i] == b:
dec level
if level == 0:
return i
iterator walkDirRecEx*(dir: string; yieldFilter = {pcFile}; followFilter = {pcDir}; relative = false; checkDir = false; depth = -1): string {.raises: [OSError].} =
## Same as std/os.walkDirRec but with added `depth` parameter. `depth < 0`
## means no depth limit.
if depth != 0:
var stack = @[("", 1)]
var checkDir = checkDir
while stack.len > 0:
let (subDir, subDepth) = stack.pop()
for kind, path in walkDir(dir / subDir, relative = true, checkDir = checkDir):
let rel = subDir / path
import std/asyncdispatch
proc asyncThread*[T](worker: proc (arg: T) {.thread.}; arg: T): Future[void] {.async.} =
## Starts a thread that runs `worker` and completes the Future when the thread exits.
var fut = newFuture[void]("asyncThread")
let ev = newAsyncEvent()
addEvent(ev) do (_: AsyncFD) -> bool:
fut.complete()
true
type
Either*[A, B] = object
case isA: bool
of true:
a: A
of false:
b: B
template isB(e: Either): bool =
not e.isA
@z-------------
z------------- / who_is_my_mummy.sh
Last active December 1, 2021 09:03 — forked from joechrysler/who_is_my_mummy.sh
Find the nearest parent branch of the current git branch
#!/bin/sh
git show-branch -a 2>/dev/null \
| grep '\*' \
| grep -v `git rev-parse --abbrev-ref HEAD` \
| head -n1 \
| perl -pe 's/.*?\[(.*?)\].*/\1/' \
| sed 's/[\^~].*//'
# How it works:
function aliasexpand { # $* command
ALIASRET=$(alias $1 2>/dev/null)
if [[ $? -ne 0 ]]; then
# not an alias
ALIAS=$1
else
ALIASLAYER=$(echo "$ALIASRET" | cut -d '=' -f2 | sed "s/^'//" | sed "s/'$//")
ALIAS=$(aliasexpand $ALIASLAYER)
fi
shift
import std/strutils
import std/tables
import std/sugar
import std/options
import std/sequtils
when defined(js):
import std/dom
type
Response = tuple[text: string, asterisk: bool]
Nim 11m █████████████████████ 100.0%
import macros
template constDecl(n, t, v): untyped =
const n: t = v
macro cenum*(t, body: untyped): untyped =
var
i: BiggestInt = 0
curBaseExpr = newIntLitNode(0)
@z-------------
z------------- / bencodeDecode.js
Last active July 5, 2020 07:09
Seems to work... Not nearly as fast as node-bencode
const MODE_NONE = 0,
MODE_BYTESTRING = 1,
MODE_INT = 2,
MODE_LIST = 3,
MODE_DICT = 4;
function getInt(buf) {
let n = 0;
for (let i = 0; i < buf.length; ++i) {
n += (buf[buf.length - 1 - i] - 48) * 10 ** i;