Skip to content

Commit

Permalink
For std.math and std.stdio, use more selective imports and document t…
Browse files Browse the repository at this point in the history
…he current symbols imported at the module level, done by checking with ddmd.
  • Loading branch information
joakim-noah committed May 25, 2016
1 parent 088f7bf commit 11d3bf6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 43 deletions.
31 changes: 18 additions & 13 deletions std/math.d
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,9 @@ version (Win64)
version = Win64_DMD_InlineAsm;
}

import core.bitop;
import core.math;
import core.stdc.math;
import std.traits;
static import core.math;
static import core.stdc.math;
import std.traits;// CommonType, isFloatingPoint, isIntegral, isSigned, isUnsigned, Largest, Unqual

version(LDC)
{
Expand Down Expand Up @@ -161,7 +160,7 @@ else version(D_InlineAsm_X86_64)

version(unittest)
{
import core.stdc.stdio;
import core.stdc.stdio : sprintf;

static if (real.sizeof > double.sizeof)
enum uint useDigits = 16;
Expand Down Expand Up @@ -2558,7 +2557,8 @@ unittest

unittest
{
import std.meta, std.typecons;
import std.meta : AliasSeq;
import std.typecons : tuple, Tuple;

foreach (T; AliasSeq!(real, double, float))
{
Expand Down Expand Up @@ -2645,6 +2645,7 @@ unittest
int ilogb(T)(const T x) @trusted pure nothrow @nogc
if (isFloatingPoint!T)
{
import core.bitop : bsr;
alias F = floatTraits!T;

union floatBits
Expand Down Expand Up @@ -2785,6 +2786,7 @@ int ilogb(T)(const T x) @trusted pure nothrow @nogc
int ilogb(T)(const T x) @safe pure nothrow @nogc
if (isIntegral!T && isUnsigned!T)
{
import core.bitop : bsr;
if (x == 0)
return FP_ILOGB0;
else
Expand All @@ -2797,6 +2799,7 @@ int ilogb(T)(const T x) @safe pure nothrow @nogc
int ilogb(T)(const T x) @safe pure nothrow @nogc
if (isIntegral!T && isSigned!T)
{
import std.traits : Unsigned;
// Note: abs(x) can not be used because the return type is not Unsigned and
// the return value would be wrong for x == int.min
Unsigned!T absx = x>=0 ? x : -x;
Expand All @@ -2808,7 +2811,8 @@ alias FP_ILOGBNAN = core.stdc.math.FP_ILOGBNAN;

@trusted nothrow @nogc unittest
{
import std.meta, std.typecons;
import std.meta : AliasSeq;
import std.typecons : Tuple;
foreach (F; AliasSeq!(float, double, real))
{
alias T = Tuple!(F, int);
Expand Down Expand Up @@ -2881,7 +2885,7 @@ float ldexp(float n, int exp) @safe pure nothrow @nogc { return ldexp(cast(real)
///
@nogc @safe pure nothrow unittest
{
import std.meta;
import std.meta : AliasSeq;
foreach (T; AliasSeq!(float, double, real))
{
T r;
Expand Down Expand Up @@ -4902,7 +4906,7 @@ bool isNaN(X)(X x) @nogc @trusted pure nothrow

@safe pure nothrow @nogc unittest
{
import std.meta;
import std.meta : AliasSeq;

foreach (T; AliasSeq!(float, double, real))
{
Expand Down Expand Up @@ -5086,7 +5090,7 @@ bool isSubnormal(X)(X x) @trusted pure nothrow @nogc
///
@safe pure nothrow @nogc unittest
{
import std.meta;
import std.meta : AliasSeq;

foreach (T; AliasSeq!(float, double, real))
{
Expand Down Expand Up @@ -5326,7 +5330,7 @@ R copysign(R, X)(X to, R from) @trusted pure nothrow @nogc

@safe pure nothrow @nogc unittest
{
import std.meta;
import std.meta : AliasSeq;

foreach (X; AliasSeq!(float, double, real, int, long))
{
Expand Down Expand Up @@ -5907,6 +5911,7 @@ real fma(real x, real y, real z) @safe pure nothrow @nogc { return (x * y) + z;
Unqual!F pow(F, G)(F x, G n) @nogc @trusted pure nothrow
if (isFloatingPoint!(F) && isIntegral!(G))
{
import std.traits : Unsigned;
real p = 1.0, v = void;
Unsigned!(Unqual!G) m = n;
if (n < 0)
Expand Down Expand Up @@ -6908,7 +6913,7 @@ private real polyImpl(real x, in real[] A) @trusted pure nothrow @nogc
*/
bool approxEqual(T, U, V)(T lhs, U rhs, V maxRelDiff, V maxAbsDiff = 1e-5)
{
import std.range;
import std.range.primitives : empty, front, isInputRange, popFront;
static if (isInputRange!T)
{
static if (isInputRange!U)
Expand Down Expand Up @@ -7301,7 +7306,7 @@ unittest

unittest
{
import std.meta;
import std.meta : AliasSeq;
foreach (T; AliasSeq!(float, double, real))
{
T[] values = [-cast(T)NaN(20), -cast(T)NaN(10), -T.nan, -T.infinity,
Expand Down
70 changes: 40 additions & 30 deletions std/stdio.d
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ Authors: $(WEB digitalmars.com, Walter Bright),
module std.stdio;

public import core.stdc.stdio;
import std.typecons;// Flag
import std.stdiobase;
import core.stdc.stddef;// wchar_t
import std.range.primitives;// empty, front, isBidirectionalRange
import std.traits;// Unqual, isSomeChar, isSomeString
import std.algorithm.mutation; // copy
import std.range.primitives;// ElementEncodingType, front, isBidirectionalRange, isInputRange
import std.stdiobase;
import std.traits;// isSomeChar, isSomeString, Unqual
import std.typecons;// Flag


/++
Expand Down Expand Up @@ -281,6 +282,7 @@ public:
import std.conv : text;
import std.exception : enforce;
import std.format : formattedRead;
import std.range.primitives : empty;
import std.string : chomp;

enforce(file.isOpen, "ByRecord: File must be open");
Expand Down Expand Up @@ -464,7 +466,7 @@ file.
*/
void opAssign(File rhs) @safe
{
import std.algorithm : swap;
import std.algorithm.mutation : swap;

swap(this, rhs);
}
Expand Down Expand Up @@ -1250,8 +1252,8 @@ Removes the lock over the specified file segment.
static void runForked(void delegate() code)
{
import core.stdc.stdlib : exit;
import core.sys.posix.unistd;
import core.sys.posix.sys.wait;
import core.sys.posix.sys.wait : wait;
import core.sys.posix.unistd : fork;
int child, status;
if ((child = fork()) == 0)
{
Expand Down Expand Up @@ -1430,7 +1432,7 @@ void main()
unittest
{
static import std.file;
import std.algorithm : equal;
import std.algorithm.comparison : equal;
import std.meta : AliasSeq;

auto deleteme = testFilename();
Expand Down Expand Up @@ -1622,8 +1624,9 @@ is recommended if you want to process a complete file.
if (isSomeChar!C && is(Unqual!C == C) && !is(C == enum) &&
isBidirectionalRange!R && is(typeof(terminator.front == dchar.init)))
{
import std.algorithm : endsWith, swap;
import std.range.primitives : back;
import std.algorithm.mutation : swap;
import std.algorithm.searching : endsWith;
import std.range.primitives : back, empty;

auto last = terminator.back;
C[] buf2;
Expand Down Expand Up @@ -1779,7 +1782,7 @@ Allows to directly use range operations on lines of a file.
struct ByLine(Char, Terminator)
{
private:
import std.typecons;
import std.typecons : RefCounted, RefCountedAutoInitialize;

/* Ref-counting stops the source range's Impl
* from getting out of sync after the range is copied, e.g.
Expand Down Expand Up @@ -1847,7 +1850,8 @@ Allows to directly use range operations on lines of a file.

void popFront()
{
import std.algorithm : endsWith;
import std.algorithm.searching : endsWith;
import std.range.primitives : empty;
assert(file.isOpen);
line = buffer;
file.readln(line, terminator);
Expand Down Expand Up @@ -1975,7 +1979,7 @@ the contents may well have changed).
private struct ByLineCopy(Char, Terminator)
{
private:
import std.typecons;
import std.typecons : RefCounted, RefCountedAutoInitialize;

/* Ref-counting stops the source range's ByLineCopyImpl
* from getting out of sync after the range is copied, e.g.
Expand Down Expand Up @@ -2104,8 +2108,7 @@ $(XREF file,readText)
unittest
{
static import std.file;
import std.algorithm : equal;
import std.range;
import std.algorithm.comparison : equal;

//printf("Entering test at line %d\n", __LINE__);
scope(failure) printf("Failed test at line %d\n", __LINE__);
Expand All @@ -2125,8 +2128,9 @@ $(XREF file,readText)
void test(Terminator)(string txt, in string[] witness,
KeepTerminator kt, Terminator term, bool popFirstLine = false)
{
import std.array : array;
import std.conv : text;
import std.algorithm : sort;
import std.algorithm.sorting : sort;
import std.range.primitives : walkLength;

uint i;
Expand Down Expand Up @@ -2183,8 +2187,8 @@ $(XREF file,readText)

unittest
{
import std.algorithm : equal;
import std.range;
import std.algorithm.comparison : equal;
import std.range : drop, take;

version(Win64)
{
Expand Down Expand Up @@ -2275,6 +2279,7 @@ $(XREF file,readText)
unittest
{
static import std.file;
import std.typecons : tuple;

// prepare test file
auto testFile = testFilename();
Expand Down Expand Up @@ -2318,7 +2323,7 @@ $(XREF file,readText)

this(File file, ubyte[] buffer)
{
import std.exception;
import std.exception : enforce;
enforce(buffer.length, "size must be larger than 0");
file_ = file;
chunk_ = buffer;
Expand Down Expand Up @@ -2685,6 +2690,7 @@ See $(LREF byChunk) for an example.
// the file mode on destruction, it is RefCounted on Windows.
struct BinaryWriterImpl(bool locking)
{
import std.traits : hasIndirections;
private:
FILE* fps;
string name;
Expand Down Expand Up @@ -2830,10 +2836,11 @@ void main()

unittest
{
import std.algorithm : copy, reverse;
import std.algorithm.mutation : reverse;
static import std.file;
import std.exception : collectException;
import std.range : only, retro, put;
import std.range : only, retro;
import std.range.primitives : put;
import std.string : format;

auto deleteme = testFilename();
Expand Down Expand Up @@ -3054,7 +3061,8 @@ unittest
unittest
{
static import std.file;
import std.range;
import std.range : chain, only, repeat;
import std.range.primitives : isOutputRange;

auto deleteme = testFilename();
scope(exit) std.file.remove(deleteme);
Expand Down Expand Up @@ -3126,7 +3134,7 @@ struct LockingTextReader

void opAssign(LockingTextReader r)
{
import std.algorithm : swap;
import std.algorithm.mutation : swap;
swap(this, r);
}

Expand Down Expand Up @@ -3197,7 +3205,8 @@ unittest
unittest // bugzilla 13686
{
static import std.file;
import std.algorithm : equal;
import std.algorithm.comparison : equal;
import std.utf : byDchar;

auto deleteme = testFilename();
std.file.write(deleteme, "Тест");
Expand All @@ -3207,7 +3216,6 @@ unittest // bugzilla 13686
File(deleteme).readf("%s", &s);
assert(s == "Тест");

import std.utf;
auto ltr = LockingTextReader(File(deleteme)).byDchar;
assert(equal(ltr, "Тест".byDchar));
}
Expand Down Expand Up @@ -3324,6 +3332,7 @@ void writeln(T...)(T args)
!isAggregateType!(typeof(args[0])))
{
import std.exception : enforce;
import std.traits : isStaticArray;

// Specialization for strings - a very frequent case
auto w = .trustedStdout.lockingTextWriter();
Expand Down Expand Up @@ -4173,9 +4182,9 @@ en.cppreference.com/w/c/io#Narrow_and_wide_orientation,
orientation).
*/
void toFile(T)(T data, string fileName)
if (is(typeof(std.algorithm.mutation.copy(data, stdout.lockingBinaryWriter))))
if (is(typeof(copy(data, stdout.lockingBinaryWriter))))
{
std.algorithm.mutation.copy(data, File(fileName, "wb").lockingBinaryWriter);
copy(data, File(fileName, "wb").lockingBinaryWriter);
}

unittest
Expand Down Expand Up @@ -4317,8 +4326,6 @@ unittest
// roll our own appender, but with "safe" arrays
private struct ReadlnAppender
{
import core.stdc.string;

char[] buf;
size_t pos;
bool safeAppend = false;
Expand Down Expand Up @@ -4354,6 +4361,7 @@ private struct ReadlnAppender
}
void reserve(size_t n)
{
import core.stdc.string : memcpy;
if (!reserveWithoutAllocating(n))
{
size_t ncap = buf.length * 2 + 128 + n;
Expand Down Expand Up @@ -4381,6 +4389,7 @@ private struct ReadlnAppender
}
void putonly(char[] b)
{
import core.stdc.string : memcpy;
assert(pos == 0); // assume this is the only put call
if (reserveWithoutAllocating(b.length))
memcpy(buf.ptr + pos, b.ptr, b.length);
Expand Down Expand Up @@ -4692,13 +4701,14 @@ private size_t readlnImpl(FILE* fps, ref char[] buf, dchar terminator, File.Orie
}
else version (Posix)
{
import std.utf : encode;
buf.length = 0;
for (int c; (c = FGETWC(fp)) != -1; )
{
if ((c & ~0x7F) == 0)
buf ~= cast(char)c;
else
std.utf.encode(buf, cast(dchar)c);
encode(buf, cast(dchar)c);
if (c == terminator)
break;
}
Expand Down

0 comments on commit 11d3bf6

Please sign in to comment.