We have only touched the surface of what's available in Phobos, D's standard library. We've seen a handful of functions scattered throughout the book, with the most detail being given in Chapter 7, Composing Functional Pipelines with Algorithms and Ranges, where we discussed the library's algorithms and other range-based functions. In this section, we'll take a quick hop around some of the more notable modules and packages in Phobos.
The std.container
package contains modules in a collection of data structures, such as linked lists and a red-black tree. All of the containers share a similar, range-based API. Though the containers are usable now, the state of the package has been in a sort of limbo for some time, awaiting the arrival of a std.allocator
package so that custom allocators can be used with each of the containers. At the time of writing, std.allocator
has been approved by community review for inclusion in the std.experimental
Phobos package. It was once expected that this event would trigger more work on the modules in std.container
. However, much has been learned about ranges and collections in the years since std.container
was first introduced, so it is more likely that a new package will be added to Phobos, using the new allocators, to replace it. Still, std.container
is usable today. Refer to http://dlang.org/phobos/std_container.html.
An alternative to std.container
is the third-party containers
package. Developed and maintained by a company called EMSI (Economic Modeling Specialists, Intl.), this company uses D in production and the containers
package is battle-tested there. The containers in this library do not use the GC and are already making use of the future std.allocator
package. Visit https://github.com/economicmodeling/containers for details.
std.datetime
provides a variety of date and time functions and objects. Here, you'll find a number of ways to represent points in time, intervals of time, ranges over intervals of time, and time zones. There are also function templates that take any number of functions to execute for benchmarking. There's also the MonoTime
type, which we saw earlier in this chapter, providing high-precision timing. In all, there are a lot of things in this module to dig through, but it's all well-documented and none of it is difficult to use. Refer to the documentation at http://dlang.org/phobos/std_datetime.html for details.
This package contains modules that provide implementations of various hashing functions. At the time of writing, implementations exist for CRC, MD5, RIPEMD-160, SHA1 and SHA2. The base API can be found in the std.digest.digest
module. All implementations follow this API. You can read more about this at http://dlang.org/phobos/std_digest_digest.html.
This package serves as a staging ground for new Phobos modules and packages. The idea is that each proposed module (or package) goes through a brief review period in the forums, where community members can vote whether it is suitable for inclusion in std.experimental
. If approved, it remains in the package, while users put it through the paces, find the weaknesses, and look for ways to improve it. At some point, the package goes up for review again in the forums, where users will voice their opinions, before coming up for a vote on final inclusion into Phobos proper.
This entire module is geared around doing one thing: processing command-line options. For such a seemingly small task, it's quite a popular module. There's little reason for anyone writing a D program to manually process command line options these days, except for special-case needs not covered by this module. This example shows how easy it is:
void main(string[] args) { import std.getopt; import std.stdio : writeln; bool printMeaning; int repeatCount = 1; auto helpInfo = getopt( args, "print-meaning", "Print the meaning of life.", &printMeaning, "repeat-count", "Number of times to repeat the meaning of life.", &repeatCount ); if(helpInfo.helpWanted) { defaultGetoptPrinter( "This program can show you the meaning of life, if you tell it to.", helpInfo.options ); return; } if(printMeaning && repeatCount > 0) { for(int i=0; i<repeatCount; ++i) writeln("The meaning of life is 42."); } else { writeln("You have opted not to learn the meaning of life."); } }
Executing this program with -h
or -help
will print the help message, This program can show you the meaning of life, if you tell it to
, along with each option and its description. To see the meaning of life, execute it with the --print-meaning
switch. To print the message ten times, execute the following (assuming the executable is named options
):
options --print-meaning --repeat-count=10
Refer to http://dlang.org/phobos/std_getopt.html for more information.
This module provides the means to launch new processes and pipe data between them, with a number of different options for doing so. For example, spawnProcess
launches a new process, with the option of assigning it standard input, output, and error streams, then returns immediately, allowing the child process to continue running. The execute
function launches a new process, but blocks until the process is finished. Similarly-named functions, such as spawnShell
and executeShell
, behave in the same way, except that they accept arbitrary string commands, which they run through the system's default command shell. For all the details on std.process
, refer to http://dlang.org/phobos/std_process.html.
There are certain types of networked applications for which a dependency on vibe.d might be overkill. Sometimes, a simple thread-per-client or selector model of networking is more appropriate. For those situations, std.socket
provides types analogous to those found in the well-known Berkeley Sockets API, which is the basis for Posix Sockets and a version of which exists in the WinSock API for Windows. Anyone familiar with Berkeley Sockets, or the venerable java.net
API, should feel right at home with std.socket
. There you'll find a base Socket
type, with subclasses called TcpSocket
and UdpSocket
for communicating via the TCP and UDP protocols. Socket
provides member functions for setting socket options, and there are several enumerations to assist. There is also a SocketSet
type that allows using the selector model of connection management. You can read more about std.socket
at http://dlang.org/phobos/std_socket.html.
There are four modules in Phobos related to character encodings: std.ascii
, std.encoding
, std.uni
, and std.utf
.
The std.ascii
module consists of a number of functions that work with ASCII characters. Some examples are toUpper
, toLower
, isUpper
, isWhite
, and so on. All of the functions accept Unicode characters, but the is*
functions will return false
upon encountering them and the to*
functions will do nothing. Unicode-enabled counterparts to the functions in this module are found in std.uni
, along with several other Unicode algorithms and data structures. Refer to http://dlang.org/phobos/std_ascii.html and http://dlang.org/phobos/std_uni.html.
std.utf
exposes functions for encoding and decoding strings to and from the three Unicode encodings, UTF-8, UTF-16, and UTF-32. std.encoding
is where you can find functions for transcoding between UTF-8, UTF-16, UTF-32, ASCII, ISO-8869-1, and WINDOWS-1252. Visit http://dlang.org/phobos/std_utf.html and http://dlang.org/phobos/std_encoding.html for more.
In addition to bindings for the C standard library, there are some system-specific bindings living in DRuntime under the core.sys
package. There, you'll find the following subpackages: freebsd
, linux
, osx
, posix
, solaris
, and windows
. With these, you should have everything you need for programming on each supported system. The windows
package for years was woefully inadequate, but at the time of this writing, a third-party Windows API binding project has been merged into the DRuntime repository and should be released with DMD 2.069. This is a fairly complete binding that allows you to develop Win32 GUI applications with D. Now, you should be able to pick up Petzold's Programming Windows or Stevens's Advanced Programming in the Unix Environment and write all of the examples in D. If you find anything missing, submit a bug report at https://issues.dlang.org/ or a pull request at https://github.com/D-Programming-Language/DRuntime.
18.216.47.169