Skip to content

Instantly share code, notes, and snippets.

@DmitrySoshnikov
DmitrySoshnikov / regexp-tree-palindrome.js
Created April 17, 2017 20:04
regexp-tree-palindrome
/^$/.test(''); // true
/$^/.test(''); // true
@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-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-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-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 / malloc.cpp
Created May 17, 2017 18:59
Educational memory allocator
/**
* Simple and educational `malloc` implementation.
*
* Dmitry Soshnikov <[email protected]>
*
* 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 / stack-params.c
Last active May 23, 2017 18:22
stack-params.c
// https://godbolt.org/g/2xOK3B
int foo(int x) {
int y = 20;
return x + y;
}
int main() {
foo(10);
return 0;
macro_rules! zoom_and_enhance {
(struct $name:ident { $($fname:ident : $ftype:ty),* }) => {
struct $name {
$($fname : $ftype),*
}
impl $name {
fn field_names() -> &'static [&'static str] {
static NAMES: &'static [&'static str] = &[$(stringify!($fname)),*];
NAMES
macro_rules! map(
{ $($key:expr => $value:expr),+ } => {
{
let mut m = ::std::collections::HashMap::new();
$(
m.insert($key, $value);
)+
m
}
};
use std::fmt::Debug;
use std::any::Any;
// Logger function for any type that implements Debug.
fn log<T: Any + Debug>(value: &T) {
let value_any = value as &Any;
// try to convert our value to a String. If successful, we want to
// output the String's length as well as its value. If not, it's a
// different type: just print it out unadorned.