Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions std/internal/cstring.d
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,6 @@ Helper functions for working with $(I C strings).
This module is intended to provide fast, safe and garbage free
way to work with $(I C strings).

Example:
---
version(Posix):

import core.stdc.stdlib: free;
import core.sys.posix.stdlib: setenv;
import std.exception: enforce;

void setEnvironment(in char[] name, in char[] value)
{ enforce(setenv(name.tempCString(), value.tempCString(), 1) != -1); }
---
---
version(Windows):

import core.sys.windows.windows: SetEnvironmentVariableW;
import std.exception: enforce;

void setEnvironment(in char[] name, in char[] value)
{ enforce(SetEnvironmentVariableW(name.tempCStringW(), value.tempCStringW())); }
---

Copyright: Denis Shelomovskij 2013-2014

License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
Expand All @@ -36,6 +15,28 @@ COREREF = $(HTTP dlang.org/phobos/core_$1.html#$2, $(D core.$1.$2))
*/
module std.internal.cstring;

///
unittest
{
version(Posix)
{
import core.stdc.stdlib: free;
import core.sys.posix.stdlib: setenv;
import std.exception: enforce;

void setEnvironment(in char[] name, in char[] value)
{ enforce(setenv(name.tempCString(), value.tempCString(), 1) != -1); }
}

version(Windows)
{
import core.sys.windows.windows: SetEnvironmentVariableW;
import std.exception: enforce;

void setEnvironment(in char[] name, in char[] value)
{ enforce(SetEnvironmentVariableW(name.tempCStringW(), value.tempCStringW())); }
}
}

import std.traits;
import std.range;
Expand Down
121 changes: 64 additions & 57 deletions std/signals.d
Original file line number Diff line number Diff line change
Expand Up @@ -80,63 +80,6 @@ extern (C) void rt_detachDisposeEvent( Object obj, DisposeEvt evt );
* Mixin to create a signal within a class object.
*
* Different signals can be added to a class by naming the mixins.
*
* Example:
---
import std.signals;
import std.stdio;

class Observer
{ // our slot
void watch(string msg, int i)
{
writefln("Observed msg '%s' and value %s", msg, i);
}
}

class Foo
{
int value() { return _value; }

int value(int v)
{
if (v != _value)
{ _value = v;
// call all the connected slots with the two parameters
emit("setting new value", v);
}
return v;
}

// Mix in all the code we need to make Foo into a signal
mixin Signal!(string, int);

private :
int _value;
}

void main()
{
Foo a = new Foo;
Observer o = new Observer;

a.value = 3; // should not call o.watch()
a.connect(&o.watch); // o.watch is the slot
a.value = 4; // should call o.watch()
a.disconnect(&o.watch); // o.watch is no longer a slot
a.value = 5; // so should not call o.watch()
a.connect(&o.watch); // connect again
a.value = 6; // should call o.watch()
destroy(o); // destroying o should automatically disconnect it
a.value = 7; // should not call o.watch()
}
---
* which should print:
* <pre>
* Observed msg 'setting new value' and value 4
* Observed msg 'setting new value' and value 6
* </pre>
*
*/

mixin template Signal(T1...)
Expand Down Expand Up @@ -271,6 +214,70 @@ mixin template Signal(T1...)
size_t slots_idx; // used length of slots[]
}

///
unittest
{
import std.signals;

int observedMessageCounter = 0;

class Observer
{ // our slot
void watch(string msg, int value)
{
switch(observedMessageCounter++)
{
case 0:
assert(msg == "setting new value");
assert(value == 4);
break;
case 1:
assert(msg == "setting new value");
assert(value == 6);
break;
default:
assert(0, "Unknown observation");
}
}
}

class Foo
{
int value() { return _value; }

int value(int v)
{
if (v != _value)
{ _value = v;
// call all the connected slots with the two parameters
emit("setting new value", v);
}
return v;
}

// Mix in all the code we need to make Foo into a signal
mixin Signal!(string, int);

private :
int _value;
}

Foo a = new Foo;
Observer o = new Observer;

a.value = 3; // should not call o.watch()
a.connect(&o.watch); // o.watch is the slot
a.value = 4; // should call o.watch()
a.disconnect(&o.watch); // o.watch is no longer a slot
a.value = 5; // so should not call o.watch()
a.connect(&o.watch); // connect again
a.value = 6; // should call o.watch()
destroy(o); // destroying o should automatically disconnect it
a.value = 7; // should not call o.watch()

assert(observedMessageCounter == 2);
}

// A function whose sole purpose is to get this module linked in
// so the unittest will run.
void linkin() { }
Expand Down
36 changes: 12 additions & 24 deletions std/string.d
Original file line number Diff line number Diff line change
Expand Up @@ -6228,30 +6228,6 @@ unittest
* in one of a known set of strings, and the program will helpfully
* autocomplete the string once sufficient characters have been
* entered that uniquely identify it.
* Example:
* ---
* import std.stdio;
* import std.string;
*
* void main()
* {
* static string[] list = [ "food", "foxy" ];
*
* auto abbrevs = std.string.abbrev(list);
*
* foreach (key, value; abbrevs)
* {
* writefln("%s => %s", key, value);
* }
* }
* ---
* produces the output:
* <pre>
* fox =&gt; foxy
* food =&gt; food
* foxy =&gt; foxy
* foo =&gt; food
* </pre>
*/

string[string] abbrev(string[] values) @safe pure
Expand Down Expand Up @@ -6303,6 +6279,18 @@ string[string] abbrev(string[] values) @safe pure
return result;
}

///
unittest
{
import std.string;

static string[] list = [ "food", "foxy" ];
auto abbrevs = std.string.abbrev(list);
assert(abbrevs == ["fox": "foxy", "food": "food",
"foxy": "foxy", "foo": "food"]);
}


@trusted pure unittest
{
import std.conv : to;
Expand Down
68 changes: 35 additions & 33 deletions std/uuid.d
Original file line number Diff line number Diff line change
Expand Up @@ -75,27 +75,6 @@ $(TR $(TDNW UUID namespaces)
* boost._uuid) from the Boost project with some minor additions and API
* changes for a more D-like API.
*
* Example:
* ------------------------
* UUID[] ids;
* ids ~= randomUUID();
* ids ~= md5UUID("test.name.123");
* ids ~= sha1UUID("test.name.123");
*
* foreach(entry; ids)
* {
* assert(entry.variant == UUID.Variant.rfc4122);
* }
*
* assert(ids[0].uuidVersion == UUID.Version.randomNumberBased);
* assert(ids[1].toString() == "22390768-cced-325f-8f0f-cfeaa19d0ccd");
* assert(ids[1].data == [34, 57, 7, 104, 204, 237, 50, 95, 143, 15, 207,
* 234, 161, 157, 12, 205]);
*
* UUID id;
* assert(id.empty);
*
* ------------------------
* Standards:
* $(LINK2 http://www.ietf.org/rfc/rfc4122.txt, RFC 4122)
*
Expand All @@ -118,6 +97,28 @@ $(TR $(TDNW UUID namespaces)
*/
module std.uuid;

///
unittest
{
import std.uuid;

UUID[] ids;
ids ~= randomUUID();
ids ~= md5UUID("test.name.123");
ids ~= sha1UUID("test.name.123");

foreach(entry; ids)
{
assert(entry.variant == UUID.Variant.rfc4122);
}
assert(ids[0].uuidVersion == UUID.Version.randomNumberBased);
assert(ids[1].toString() == "22390768-cced-325f-8f0f-cfeaa19d0ccd");
assert(ids[1].data == [34, 57, 7, 104, 204, 237, 50, 95, 143, 15, 207,
234, 161, 157, 12, 205]);
UUID id;
assert(id.empty);
}

import std.range.primitives;
import std.traits;

Expand Down Expand Up @@ -329,18 +330,6 @@ public struct UUID
* hyphens exactly like above.
*
* For a less strict parser, see $(LREF parseUUID)
*
* Example:
* -------------------------
* id = UUID("8AB3060E-2cba-4f23-b74c-b52db3bdfb46");
* assert(id.data == [138, 179, 6, 14, 44, 186, 79, 35, 183, 76,
* 181, 45, 179, 189, 251, 70]);
* assert(id.toString() == "8ab3060e-2cba-4f23-b74c-b52db3bdfb46");
*
* //Can also be used in CTFE, for example as UUID literals:
* enum ctfeID = UUID("8ab3060e-2cba-4f23-b74c-b52db3bdfb46");
* //here parsing is done at compile time, no runtime overhead!
* -------------------------
*/
this(T)(in T[] uuid) if(isSomeChar!(Unqual!T))
{
Expand Down Expand Up @@ -398,6 +387,19 @@ public struct UUID
UUIDParsingException.Reason.invalidChar, "Couldn't parse ubyte");
}

///
@safe pure unittest
{
auto id = UUID("8AB3060E-2cba-4f23-b74c-b52db3bdfb46");
assert(id.data == [138, 179, 6, 14, 44, 186, 79, 35, 183, 76,
181, 45, 179, 189, 251, 70]);
assert(id.toString() == "8ab3060e-2cba-4f23-b74c-b52db3bdfb46");

//Can also be used in CTFE, for example as UUID literals:
enum ctfeID = UUID("8ab3060e-2cba-4f23-b74c-b52db3bdfb46");
//here parsing is done at compile time, no runtime overhead!
}

@safe pure unittest
{
import std.exception;
Expand Down