Skip to content

Instantly share code, notes, and snippets.

View PetarKirov's full-sized avatar

Petar Kirov PetarKirov

View GitHub Profile
@PetarKirov
PetarKirov / ts.d
Last active January 28, 2021 08:29
Relative timestamp filter program in D
void main()
{
import core.time : MonoTime;
import std.stdio : stdin, writef;
import std.string : wrap;
const start = MonoTime.currTime;
foreach (line; stdin.byLineCopy)
{
const now = MonoTime.currTime;
const diff = now - start;
@PetarKirov
PetarKirov / atomic.d
Created January 25, 2021 10:50
Atomic(T) type in D
unittest
{
shared Atomic!int ai;
assert(ai.get == 0); // atomicLoad / std::atomic<T>::load
ai = 5; // atomicStore via opAssign / std::atomic<T>::operator=
assert(ai.get == 5); // atomicLoad
assert(ai.exchange(3) == 5); // atomicExchange / std::atomic<T>::exchange
assert(ai.get == 3); // atomicLoad
assert(ai.cas(3, 42)); // cas / std::atomic<T>::compare_exchange_strong
assert(ai.get == 42); // atomicLoad
@PetarKirov
PetarKirov / test.d
Created January 4, 2021 15:32
dlang - validate input matches enum member name
void main()
{
static immutable pairs = [__traits(allMembers, Pair)];
pragma (msg, pairs); // ["EURUSD", "GBPUSD", "USDCHF", "XAUUSD"]
import std.stdio : writeln;
"EURUSD".parseEnum!Pair.writeln;
"GBPUSD".parseEnum!Pair.writeln;
"USDCHF".parseEnum!Pair.writeln;
@PetarKirov
PetarKirov / soa.d
Last active December 28, 2020 08:59
Structure of Arrays Type Constructor
void main()
{
import std.stdio : writeln;
static struct Vec3
{
float x, y, z;
}
auto soa = SoA!Vec3(5);
@PetarKirov
PetarKirov / 01_REAME.md
Last active December 15, 2020 07:48
Chromium / Google Chrome: Discard / freeze all tabs without closing (working in incognito mode as well)

Discard all tabs in Google Chrome / Chromium

Why?

There are multiple extensions that claim to do this, but I didn't find one that was working in incognito mode.

Steps:

  1. Go to chrome://discards/
  2. Open the DevTools and click on the Console tab (or press [Esc])
@PetarKirov
PetarKirov / example.d
Created December 2, 2020 17:15
D statically allocated class instnace
void main()
{
throw globalInstance!Error("This is an Error()", null);
}
T globalInstance(T, bool tls = true, Args...)(auto ref Args args)
if (is(T == class))
{
// There really should be a trait for this:
enum classAlignment = 2 * (void*).sizeof; // until then, here's a reasonable guess :)
@PetarKirov
PetarKirov / shell.nix
Created November 24, 2020 13:34
Nix: use specific version of Nodejs and set LD_LIBRARY_PATH
{ pkgs ? import <nixpkgs> { } }:
with pkgs;
mkShell {
buildInputs = [
fish
git
nodejs-12_x
(yarn.override { nodejs = nodejs-12_x; })
gnumake
python3
@PetarKirov
PetarKirov / git_status.d
Created November 18, 2020 23:41
Git status parser
import std.exception : enforce;
import std.format : format;
import std.file : exists, getcwd, isDir, isFile;
import std.path : absolutePath, buildNormalizedPath, dirName, relativePath;
import std.stdio : writeln, writefln;
import std.range;
void main(string[] args)
{
enforce(args.length == 2, "Usage:\n\tabs_to_rel_git_path <path>");
@PetarKirov
PetarKirov / check_if_invariants_are_enabled.d
Last active November 18, 2020 06:31
D: Check if invariants are enabled using CTFE
bool invariantsEnabled() @trusted nothrow pure
{
static struct HasInvariant
{
@safe pure:
invariant { throw new Exception("From invariant"); }
void triggerInvaraint() { }
}
try
HasInvariant().triggerInvaraint();
@PetarKirov
PetarKirov / app.d
Last active November 16, 2020 07:00
LDC 1.24.0. / CT string compare / druntime symbols
extern(C) void _start() {
static if ("a" == "a") {}
}