Skip to content

Instantly share code, notes, and snippets.

@DmitrySoshnikov
DmitrySoshnikov / malloc.cpp
Created May 17, 2017 18:59
Educational memory allocator
/**
* Simple and educational `malloc` implementation.
*
* Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*
* Maintains explicit linked list of allocated memory blocks. Each block
* has a header, containing meta-information, such as whether a block is
* free, its size, and a reference to the next block.
*
* Homework assignments:
@DmitrySoshnikov
DmitrySoshnikov / regexp-x-flag-re.js
Created April 24, 2017 23:35
regexp-x-flag-re.js
// Using `x` flag with `re` shorthand
// Meta-chars like \d can be escaped using single slash.
// Plugin should specify `useRe` option.
re`/
# A regular expression for date.
(?<year>\d{4})- # year part of a date
(?<month>\d{2})- # month part of a date
@DmitrySoshnikov
DmitrySoshnikov / regexp-x-flag.js
Created April 24, 2017 23:30
regexp-x-flag.js
// Using `x` flag with `RegExp`:
new RegExp(`
# A regular expression for date.
(?<year>\\d{4})- # year part of a date
(?<month>\\d{2})- # month part of a date
(?<day>\\d{2}) # day part of a date
@DmitrySoshnikov
DmitrySoshnikov / regexp-named-groups.js
Last active April 25, 2017 05:49
regexp-named-groups.js
// Using named capturing groups:
const re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
const result = re.exec('2017-04-24');
// NOTE: need runtime support to access `groups`,
// see `useRuntime` option:
console.log(result.groups.year); // 2017
@DmitrySoshnikov
DmitrySoshnikov / regexp-s-flag.js
Created April 24, 2017 23:12
regexp-s-flag.js
// Using `s` flag to match all chars
// including \n:
const allCharsRe = /.+/s;
console.log(allCharsRe.test('\n')); // true
@DmitrySoshnikov
DmitrySoshnikov / regexp-tree-palindrome.js
Created April 17, 2017 20:04
regexp-tree-palindrome
/^$/.test(''); // true
/$^/.test(''); // true
/
# A regular expression for date.
(?<year>\d{4})- # year part of a date
(?<month>\d{2})- # month part of a date
(?<day>\d{2}) # day part of a date
/x
// Is translated into:
@DmitrySoshnikov
DmitrySoshnikov / regexp-tree-compat-transpiler.js
Created April 17, 2017 19:57
regexp-tree-compat-transpiler
const regexpTree = require('regexp-tree');
// Using new syntax.
const originalRe = '/(?<all>.)\\k<all>/s';
// For legacy engines.
const compatTranspiledRe = regexpTree
.compatTranspile(originalRe)
.toRegExp();
@DmitrySoshnikov
DmitrySoshnikov / regexp-tree-optimizer.js
Created April 17, 2017 19:54
regexp-tree-optimizer
const regexpTree = require('regexp-tree');
const originalRe = /[a-zA-Z_0-9][A-Z_\da-z]*\e{1,}/;
const optimizedRe = regexpTree
.optimize(originalRe)
.toRegExp();
console.log(optimizedRe); // /\w+e+/
@DmitrySoshnikov
DmitrySoshnikov / regexp-tree-generator.js
Last active March 30, 2017 05:39
regexp-tree-generator
const regexpTree = require('regexp-tree');
const re = regexpTree.generate({
type: 'RegExp',
body: {
type: 'Char',
value: 'a',
kind: 'simple',
},
flags: 'gi',