Skip to content

Instantly share code, notes, and snippets.

View nitely's full-sized avatar
🟢
online

Esteban C Borsani nitely

🟢
online
View GitHub Profile
@nitely
nitely / scanner.py
Created March 30, 2017 05:39
Python re.Scanner (almost) without using internals
import sre_compile
import re
class Scanner:
def __init__(self, rules, flags=0):
self.scanner = sre_compile.compile(
'(%s)' % '|'.join('(%s)' % pattern for pattern in rules),
flags)
@nitely
nitely / jasmine_test.js
Last active April 11, 2017 20:15
Jasmine testing a function with constructor, instance properties and static properties
/*
var myProto = function(myArg) {
this.myArg = myArg;
};
myProto.prototype.myMethod = function() {};
myProto.myStaticMethod = function() {};
@nitely
nitely / rpn.py
Last active December 18, 2020 23:03
Python shunting-yard algorithm
# LICENSE: MIT
import collections
RIGHT, LEFT = range(2)
Op = collections.namedtuple('Op', [
'precedence',
'associativity'])
@nitely
nitely / re3.py
Last active June 13, 2020 21:50
Python NFA regex engine based on Thompson's paper
# -*- coding: utf-8 -*-
"""
COPYRIGHT: esteban castro borsani @ nitely
LICENSE: MIT
"""
import collections
@nitely
nitely / test.py
Last active September 7, 2017 07:34
Multi-stage table (for storing unicode chars)
# missing imports here: GRAPHEME_BREAK_INDEXES, GRAPHEME_BREAK_TYPES, BLOCK_SIZE
def get_grapheme_type(ch):
block_offset = GRAPHEME_BREAK_INDEXES[ch // block_size] # floor
return GRAPHEME_BREAK_TYPES[block_offset][ch % block_size]
def test():
with open('./GraphemeBreakProperty.txt', 'r', encoding='utf-8') as fh:
data = list(parse(fh))
for cp, tcp in data:
@nitely
nitely / unicodenames.md
Last active September 16, 2017 06:23
How Python's unicodedata (unicodenames) works

Fredrik Lundh's unicodenames

From: "Fredrik Lundh" [email protected] Date: Sun, 16 Jul 2000 20:40:46 +0200

The unicodenames database consists of two parts: a name database which maps character codes to names, and a code database, mapping names to codes.

  • The Name Database (getname)
@nitely
nitely / runbench.nim
Created November 16, 2017 08:33
nim runes bench
import streams
import unicode
import nimbench
iterator runes*(s: Stream): Rune =
## reads runes from a stream. Raises `EIO` if
## an error occurred.
var buff = newString(4)
var rune: Rune
var n = 0
@nitely
nitely / runes_stream2.nim
Last active January 7, 2018 04:19
nim runes for unbuffered stream
iterator runes*(s: Stream, buffSize=64000): Rune =
## reads runes from a stream. Resuming is not supported.
## It must be ran until completion, otherwise it
## may've consumed more than what was yielded. Make it a
## closure and pass it around when that behaviour is needed. Raises
## `EIO` if an error occurred.
var
buff = newString(buffSize)
rune: Rune
n = 0
@nitely
nitely / __init__.py
Created November 27, 2017 20:58
Python Markdown parser
# -*- coding: utf-8 -*-
# Copyright (c) 2017 by Esteban Castro Borsani.
# Released under MIT license
from .elements import (
Header,
Quote,
HRule,
UListItem,
@nitely
nitely / aoc2017_day1_1.nim
Last active December 8, 2017 04:06
Nim AoC2017
#[
--- Day 1: Inverse Captcha ---
The night before Christmas, one of Santa's Elves calls you in a panic. "The printer's broken! We can't print the Naughty or Nice List!" By the time you make it to sub-basement 17, there are only a few minutes until midnight. "We have a big problem," she says; "there must be almost fifty bugs in this system, but nothing else can print The List. Stand in this square, quick! There's no time to explain; if you can convince them to pay you in stars, you'll be able to--" She pulls a lever and the world goes blurry.
When your eyes can focus again, everything seems a lot more pixelated than before. She must have sent you inside the computer! You check the system clock: 25 milliseconds until midnight. With that much time, you should be able to collect all fifty stars by December 25th.
Collect stars by solving puzzles. Two puzzles will be made available on each day millisecond in the advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one s