Skip to content

Instantly share code, notes, and snippets.

@JakobOvrum
JakobOvrum / htmlentities.d
Last active August 29, 2015 14:18
std.regex and HTML entities
import std.regex;
immutable string[string] namedEntities;
// Construct AA at startup
shared static this()
{
namedEntities = [
"gt": ">",
"lt": "<",
@JakobOvrum
JakobOvrum / uda.d
Created April 1, 2015 10:22
Helper templates for User-Defined Attributes
private template isAttribute(Attribute)
{
enum isAttribute(alias other) = is(typeof(other) == Attribute);
enum isAttribute(Other) = is(Other == Attribute);
}
private template isAttribute(alias Attribute)
{
enum isAttribute(alias other) = is(typeof(other) == Attribute!Args, Args...);
enum isAttribute(Other) = is(Other == Attribute!Args, Args...);
@JakobOvrum
JakobOvrum / urltitle.d
Last active August 29, 2015 14:17
Print HTML title from untrusted URL
import core.time, std.algorithm, std.range,
std.string, std.net.curl, std.regex;
import std.stdio : stderr, stdout;
immutable usage = `%s <URL to HTML page>`;
enum StatusCode
{
success = 0,
@JakobOvrum
JakobOvrum / final_.d
Last active August 29, 2015 14:13
Library implementation of the `final` storage class
/**
Type constructor for final variables.
A final variable is "head-const"; it cannot be directly
mutated or rebound, but references reached through the variable
are typed with their original mutability.
*/
struct Final(T)
if(!is(T == const) && !is(T == immutable))
{
@JakobOvrum
JakobOvrum / sortedcontainer.d
Created November 8, 2014 04:27
WIP, possibly abandoned, higher-order sorted container
module std.container.sortedcontainer;
import std.stdio;
import std.array : empty, front, popFront;
import std.range : ElementType, SortedRange, assumeSorted, hasAssignableElements, isInputRange, isRandomAccessRange;
// SortedRange should really support this
private auto internalLowerBound(Range, alias pred, T)(SortedRange!(Range, pred) range, T x)
if(is(T : ElementType!Range))
@JakobOvrum
JakobOvrum / gist:535fb87526d8807851f8
Last active August 29, 2015 14:04
Equality and ordering
import std.algorithm, std.container, std.uni;
struct S
{
string str;
int opCmp(const S other) const
{
return icmp(str, other.str);
}
@JakobOvrum
JakobOvrum / gist:10972257
Created April 17, 2014 10:33
removeSpan overload set
import std.traits;
import std.range;
E[] removeSpan(E)(E[] arr, size_t from, size_t to) @trusted
if (isMutable!E && !hasElaborateAssign!E)
{
import core.exception : RangeError;
import core.stdc.string : memmove;
import std.exception : enforceEx;
@JakobOvrum
JakobOvrum / gist:8968977
Created February 13, 2014 03:00
std.random.uniform for enums
import rand = std.random;
import std.traits;
E uniform(E, RNG)(ref RNG rng)
if (is(E == enum) && rand.isUniformRNG!RNG)
{
static if (is(E B == enum))
alias BaseType = B;
else
static assert(false);
@JakobOvrum
JakobOvrum / gist:7881811
Created December 9, 2013 22:03
Approximation of C#'s `using` statement in D
interface IDisposable
{
void dispose();
}
auto using(T : IDisposable, CtorArgs...)(auto ref CtorArgs ctorArgs)
if(is(T == class) && __traits(compiles, new T(ctorArgs)))
{
static struct Result
{
@JakobOvrum
JakobOvrum / gist:7165288
Last active December 26, 2015 14:19
Demonstration of template pattern matching
// Takes instances of `Foo`
struct Foo(T){ T t; }
string takesFoo(T)(T t) if(is(T == Foo!U, U))
{
static if(is(T == Foo!U, U))
return U.stringof;
else
static assert(false);