Created
September 4, 2014 01:49
-
-
Save SirTony/264dfac539c697b6728b to your computer and use it in GitHub Desktop.
A small collection of utility functions for the D programming language.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module util; | |
bool contains( T )( T[] a, T item ) | |
{ | |
foreach( x; a ) | |
if( x == item ) | |
return true; | |
return false; | |
} | |
bool contains( TKey, TValue )( TValue[TKey] map, TValue item ) | |
{ | |
foreach( k, v; map ) | |
if( v == item ) | |
return true; | |
return false; | |
} | |
bool containsKey( TKey, TValue )( TValue[TKey] map, TKey key ) | |
{ | |
foreach( k, v; map ) | |
if( k == key ) | |
return true; | |
return false; | |
} | |
TValue[] values( TKey, TValue )( TValue[TKey] map ) | |
{ | |
TValue[] values; | |
foreach( k, v; map ) | |
values ~= v; | |
return values; | |
} | |
R[] select( T, R )( T[] a, R delegate( T ) selector ) | |
{ | |
R[] ret; | |
foreach( x; a ) | |
ret ~= selector( x ); | |
return ret; | |
} | |
char toUpper( in char c ) | |
{ | |
if ( c >= 'a' && c <= 'z' ) | |
return cast( char )( c - 0x20 ); | |
else | |
return c; | |
} | |
char toLower( in char c ) | |
{ | |
if( c >= 'A' && c <= 'z' ) | |
return cast( char )( c + 0x20 ); | |
else | |
return c; | |
} | |
string titleCase( string s ) | |
{ | |
import std.uni : isAlpha; | |
bool capNext = true; | |
char[] n; | |
foreach( c; s.dup ) | |
{ | |
if( !c.isAlpha() ) | |
capNext = true; | |
else if( capNext ) | |
{ | |
n ~= c.toUpper(); | |
capNext = false; | |
continue; | |
} | |
n ~= c; | |
} | |
return n.idup; | |
} | |
//Alternative to std.algorithm.sort. | |
//Implements a simple, non-optomized, and unstable quicksort. | |
T[] qsort( T )( T[] arr, int delegate( T, T ) cmp ) | |
{ | |
if( arr.length < 2 ) | |
return arr; | |
T pivot = arr[0]; | |
T[] a, b; | |
for( int i = 1; i < arr.length; ++ i ) | |
{ | |
T x = arr[i]; | |
if( cmp( x, pivot ) < cmp( pivot, x ) ) | |
a ~= x; | |
else | |
b ~= x; | |
} | |
return qsort( a, cmp ) ~ pivot ~ qsort( b, cmp ); | |
} | |
//Alternative to std.algorithm.group | |
uint[T] group( T )( T[] a ) | |
{ | |
uint[T] counts; | |
foreach( x; a ) | |
counts[x] += 1; | |
return counts; | |
} | |
string padRight( in string s, char padChar, int len ) | |
{ | |
if( s.length >= len ) | |
return s; | |
int diff = len - s.length; | |
string n = s; | |
foreach( _; 0 .. diff ) | |
n ~= padChar; | |
return n; | |
} | |
string padLeft( in string s, char padChar, int len ) | |
{ | |
if( s.length >= len ) | |
return s; | |
int diff = len - s.length; | |
string n; | |
foreach( _; 0 .. diff ) | |
n ~= padChar; | |
return n ~ s; | |
} | |
string pad( in string s, char padChar, int len ) | |
{ | |
if( s.length >= len ) | |
return s; | |
int diff = len - s.length; | |
string n; | |
int leftPad = ( diff % 2 ) ? diff / 2 : ( diff - 1 ) / 2; | |
int rightPad = diff - leftPad; | |
foreach( _; 0 .. leftPad ) | |
n ~= padChar; | |
n ~= s; | |
foreach( _; 0 .. rightPad ) | |
n ~= padChar; | |
return n; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment