T calcIntersection(T)(const T[] arrays...)

Implementation of the intersection operation for arrays.

Example

int[] arr = calcIntersection([3, 4, 1, -15], [9, 3, 1], [1, 99, 3]);
assert(arr == [1, 3]);

T calcUnion(T)(const T[] arrays...)

Implementation of the union operation for arrays.

Example

int[] arr = calcUnion([3, 4, 1, -15], [9, 3, 1], [1, 99, 3]);
assert(arr == [-15, 1, 3, 4, 9, 99]);

T calcDifference(T)(const T[] arrays...)

Implementation of the relative complement for arrays.

Example

int[] arr = calcDifference([3, 4, 1, -15], [9, 3, 1]);
assert(arr == [-15, 4]);

@safe
pure string removeDuplicateConsecutiveSubstring(string s, string substring)

The function removes consecutive string duplicates.

Example

string s = removeDuplicateConsecutiveSubstring("AAAAbooeAAAuuvAww", "A");
assert(s == "AbooeAuuvAww");
s = removeDuplicateConsecutiveSubstring("AAAAbcooeAAAgguvAww", "AA");
assert(s == "AAbcooeAAAgguvAww");

ref T[] removeDuplicate(T)(ref T[] arr)

The function removes duplicate elements from an array.

Example

int[] values = [1, 3, 1, 7, 2, 19, 5, 2, 2];
removeDuplicate(values);
assert(values == [1, 3, 7, 2, 19, 5]);

auto assocArrayItems(V, K)(V[K] aarray)

Returns array of tuples with pairs of keys and values from associative array.

auto sortAssocArrayItemsByKeys(V, K)(V[K] aarray, Flag!"desc" desc = No.desc)

Makes key-sorted array of tuples with pairs of keys and values from associative array.

Parameters

aarray

Associative array.

desc

Flag for sorting (descending/ascending). By default, No.desc.

Returns

Key-sorted array of tuples with elements of the associative array.

auto sortAssocArrayItemsByValues(V, K)(V[K] aarray, Flag!"desc" desc = No.desc)

Makes value-sorted array of tuples with pairs of keys and values from associative array.

Parameters

aarray

Associative array.

desc

Flag for sorting (descending/ascending). By default, No.desc.

Returns

Value-sorted array of tuples with elements of the associative array.

deprecated("Use sortAssocArrayItemsByKeys and sortAssocArrayItemsByValues")
void orderAssociativeArray(alias by, V, K)(in V[K] aarray, out K[] orderedKeys, out V[] orderedValues) if (by == "values" || by == "keys")

The function arranges the associative array by the required sorting method (string "by" is equal to "values" or "keys"), returns an array of keys and an array of values.

deprecated("Use sortAssocArrayItemsByKeys")
void orderAssociativeArrayByKeys(V, K)(in V[K] aarray, out K[] orderedKeys, out V[] orderedValues)

The function arranges the associative array by keys, returns an array of keys and an array of values.

deprecated("sortAssocArrayItemsByValues")
void orderAssociativeArrayByValues(V, K)(in V[K] aarray, out K[] orderedKeys, out V[] orderedValues)

The function arranges the associative array by values, returns an array of keys and an array of values.

V[K] mixAssociativeArrays(K, V)(V[K] firstArray, V[K] secondArray)

This function combines two associative arrays, returns new associative array. If two arrays have the same keys, the values from the first array are used.

@safe
pure string getOptionValue(string[] args, string flag, string altFlag = "")

Function to get a value by parameter. Used to process command line arguments.

Parameters

args

Command line arguments.

flag

Parameter with "-" or "--".

altFlag

Synonym for the flat. By default, empty.

Returns

The value of the required parameter.

@safe
pure string extractOptionValue(ref string[] args, string flag, string altFlag = "")

Function for extracting of value by flag. Unlike getOptionValue(), this function changes the passed args, removing the flag with a value.

Parameters

args

Command line arguments. Changes while the function is running.

flag

Parameter with "-" or "--".

altFlag

Synonym for the flat. By default, empty.

Returns

The value of the required parameter.

Example

string[] args = "app -a value1 value2 -b parameter --nothing".split;
assert("parameter" == args.getOptionValue("-b"));
assert("value1" == args.getOptionValue("-a"));
assert("" == args.getOptionValue("--nothing"));
assert(args == "app -a value1 value2 -b parameter --nothing".split);

assert("parameter" == args.extractValueForFlag("-b"));
assert("app -a value1 value2 --nothing".split == args);
assert("value1" == args.extractValueForFlag("-a"));
assert("app value2 --nothing".split == args);

args = "-a value1 value2 -b parameter --nothing".split;
assert("parameter" == args.getOptionValue("-b"));
assert("value1" == args.getOptionValue("-a"));
assert("" == args.getOptionValue("--nothing"));
assert("parameter" == args.extractValueForFlag("-b"));
assert("-a value1 value2 --nothing".split == args);
assert("value1" == args.extractValueForFlag("-a"));
assert("value2 --nothing".split == args);

args = `build --file=/home/user/Project_1/Makefile`.split;
assert("/home/user/Project_1/Makefile" == args.getOptionValue("--file"));

args = "app -a - value2 -b parameter --nothing".split;
assert("-" == args.extractValueForFlag("-a"));

// cases with --
args = "app -c name -f -- -strange_name -q question".split;
assert("name" == args.extractValueForFlag("-c"));
assert("-strange_name" == args.extractValueForFlag("-f"));
assert("" == args.extractValueForFlag("-q"));
args = "app -c -- name".split;
assert("name" == args.extractValueForFlag("-c"));

@safe
pure string[] getOptionRange(string[] args, string flag, string altFlag = "")

Function to get values as array by flag.

Parameters

args

Command line arguments.

flag

Parameter with "--".

altFlag

Synonym for the flat. By default, empty.

Returns

array of values after the flag.

@safe
pure string[] extractOptionRange(ref string[] args, string flag, string altFlag = "")

Function for extracting of value as array by flag. Unlike getOptionRange(), this function changes the passed arguments (args), removing the flag with a value.

Parameters

args

Command line arguments. Changes while the function is running.

flag

Parameter with "--".

altFlag

Synonym for the flat. By default, empty.

Returns

array of values after the flag.

Example

string[] args = "app -a value1 value2 -b parameter --nothing".split;
assert(["value1", "value2"] == getOptionRange(args, "-a"));
assert(["parameter"] == getOptionRange(args, "-b"));
string[] nothing = getOptionRange(args, "--nothing");
assert(nothing.empty);

args = "app --alt value1 value2 -b parameter --nothing".split;
assert(["value1", "value2"] == getOptionRange(args, "-a", "--alt"));

args = "app -a -b thing".split;
string[] vacuum = getOptionRange(args, "-a");
assert(vacuum.empty);

args = "app -a value1 value2 -b parameter --nothing".split;
assert("value1 value2".split == extractOptionRange(args, "-a"));
assert("app -b parameter --nothing".split == args);

args = "-a value1 value2 -b parameter --nothing".split;
assert("value1 value2".split == extractOptionRange(args, "-a"));
assert("-b parameter --nothing".split == args);

args = "-a value1 - value3 -b parameter --nothing".split;
assert("value1 - value3".split == extractOptionRange(args, "-a"));

// cases with --
args = "app -c name -f -- -strange_name x y -q Q".split;
assert("-strange_name x y -q Q".split == extractOptionRange(args, "-f"));
args = "app -f -- -strange_name".split;
assert(["-strange_name"] == extractOptionRange(args, "-f"));
args = "app -f -- -strange_name --".split;
assert("-strange_name --".split == extractOptionRange(args, "-f"));
args = "app -f name --".split;
assert(["name"] == extractOptionRange(args, "-f"));
args = "app -f name -- name2".split;
assert(["name", "name2"] == extractOptionRange(args, "-f"));

@safe
pure string getOctalForm(ulong value)

The function converts integer number into an octal form, returns as a string.

Example

short eight = 8;
assert(getOctalForm(eight) == "10");
assert(getOctalForm(33_261) == "100755");

@safe
pure string getBinaryForm(ulong value)

The function converts integer number into an binary form.

ssize_t getIndex(T)(T[] haystack, T needle)

The function returns the index of the array element by value.

Parameters

haystack

The array in which the search is performed.

needle

Element value in the array.

Returns

The index of the array element. -1 if the element was not found.

ssize_t getIndexOfLast(T)(const T[] haystack, T needle)

The function returns the index of the last occurrence of the specified value in the array.

Parameters

haystack

The array in which the search is performed.

needle

Element value in the array.

Returns

The index of the last occurrence. -1 if the element was not found.

T[] removeElementsByContent(T)(T[] haystack, T needle)

The function returns an array without unnecessary elements.

Example

auto arr = [1, 8, 17, 4, 23, 8, 8, 3, 13, 19, 8];
auto res = arr.removeElementsByContent(8);
assert(res == [1, 17, 4, 23, 3, 13, 19]);

auto lines = ["", "straw", "", "", "one more straw", "", ""];
auto straws = lines.removeElementsByContent("");
assert(straws == ["straw", "one more straw"]);

@safe
pure string urlEncode(in string[string] values)

Encode associative array using www-form-urlencoding (copied from std.uri).

Parameters

values

An associative array containing the values to be encoded.

Returns

A string encoded using www-form-urlencoding.

@safe
pure double convJSONValueToDouble(std.json.JSONValue json)

The function converts JSONValue to double.

alias J = JSONAttr

Structure for working with UDA. Used in serializeToJSON/deserializeFromJSON.

JSONValue serializeToJSON(T)(T obj)

Simple serialization of structures to JSON.

T deserializeFromJSON(T)(JSONValue jsonValue)

Simple deserialization of JSON to structures.

bool isAmong(alias pred = (a, b) => a == b, Value, Values...)(const Value value, const Values values) 
if (Values.length != 0)

The function similar to functions "canFind" and "among".

bool isAmong(alias pred = (a, b) => a == b, T1, T2)(const T1 value, const T2[] arr)

The function similar to functions "canFind" and "among".

T[] makeFilledArray(T)(size_t len, T filler)

Creates new array with specified content.

Example

assert(makeFilledArray(4, 5) == [5, 5, 5, 5]);
assert(makeFilledArray(10, ' ') == "          ");

@safe
pure string stripLeft(string line, in char symbol = ' ') nothrow

Cuts off consecutive unnecessary characters from the left side.

Returns

truncated string.

@safe
pure string stripLeftChars(string line, in char[] symbols) nothrow

Cuts off consecutive unnecessary characters from the left side.

Returns

truncated string.

@safe
pure string stripRight(string line, in char symbol = ' ') nothrow

Cuts off consecutive unnecessary characters from the right side.

Returns

truncated string.

@safe
pure string stripRightChars(string line, in char[] symbols) nothrow

Cuts off consecutive unnecessary characters from the right side.

Returns

truncated string.

@safe
pure string trimPrefix(string line, string prefix)

Removes the specified prefix from the input string.

@safe
pure string trimPostfix(string line, string postfix)

Removes the specified postfix from the input string.

size_t countElements(R, E)(R range, E value) nothrow pure @safe 
if (isInputRange!R)

Counts the number of required elements in a range.

Parameters

range

Some input range.

value

Value to find.

Returns

number of required elements.

T[] divideByElemNumber(T)(T arr, size_t n) 
if (std.traits.isArray!T)

Divides an array: makes a new array of arrays containing elements from source array. Each subarray contains specified number of elements.

Parameters

arr

Source array.

n

Number of elements in each new subarray.

T[] copyArray(T)(inout(T)[] arr)

Creates a new dynamic array of the same size and copies the contents of the given array into it. Unlike the built-in dup function, this function supports arrays with complex const or immutable structures.

Parameters

arr

The dynamic array.

Returns

A copy of the required array.

The module contains some useful functions for data processing, including functions for working with dynamic arrays and associative arrays, functions for working with command line arguments, operations on arrays as sets, some functions for working with strings, JSON, etc.

Public imports