Skip to content

Commit

Permalink
Merge pull request #2727 from 9il/stdio
Browse files Browse the repository at this point in the history
std.stdio: clean imports
  • Loading branch information
MartinNowak committed Nov 17, 2014
2 parents bc138d4 + e678d31 commit 1fecd4e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 27 deletions.
29 changes: 16 additions & 13 deletions std/conv.d
Expand Up @@ -27,6 +27,12 @@ import std.range.constraints;
import std.traits;
import std.typetuple;

private string convFormat(Char, Args...)(in Char[] fmt, Args args)
{
import std.string : format;
return std.string.format(fmt, args);
}

/* ************* Exceptions *************** */

/**
Expand Down Expand Up @@ -1395,11 +1401,10 @@ T toImpl(T, S)(S value)

static if (isStaticArray!T)
{
import std.string : format;
import std.exception : enforce;
auto res = to!(E[])(value);
enforce!ConvException(T.length == res.length,
format("Length mismatch when converting to static array: %s vs %s", T.length, res.length));
convFormat("Length mismatch when converting to static array: %s vs %s", T.length, res.length));
return res[0 .. T.length];
}
else
Expand Down Expand Up @@ -1770,8 +1775,7 @@ T toImpl(T, S)(S value)
if (Member == value)
return Member;
}
import std.string : format;
throw new ConvException(format("Value (%s) does not match any member value of enum '%s'", value, T.stringof));
throw new ConvException(convFormat("Value (%s) does not match any member value of enum '%s'", value, T.stringof));
}

@safe pure unittest
Expand Down Expand Up @@ -3896,7 +3900,7 @@ private template emplaceImpl(T)
ref UT emplaceImpl()(ref UT chunk)
{
static assert (is(typeof({static T i;})),
format("Cannot emplace a %1$s because %1$s.this() is annotated with @disable.", T.stringof));
convFormat("Cannot emplace a %1$s because %1$s.this() is annotated with @disable.", T.stringof));

return emplaceInitializer(chunk);
}
Expand All @@ -3905,7 +3909,7 @@ private template emplaceImpl(T)
ref UT emplaceImpl(Arg)(ref UT chunk, auto ref Arg arg)
{
static assert(is(typeof({T t = arg;})),
format("%s cannot be emplaced from a %s.", T.stringof, Arg.stringof));
convFormat("%s cannot be emplaced from a %s.", T.stringof, Arg.stringof));

static if (isStaticArray!T)
{
Expand Down Expand Up @@ -3976,7 +3980,7 @@ private template emplaceImpl(T)
.emplaceImpl!E(chunk[i], arg);
}
else
static assert(0, format("Sorry, this implementation doesn't know how to emplace a %s with a %s", T.stringof, Arg.stringof));
static assert(0, convFormat("Sorry, this implementation doesn't know how to emplace a %s with a %s", T.stringof, Arg.stringof));

return chunk;
}
Expand Down Expand Up @@ -4042,11 +4046,11 @@ private template emplaceImpl(T)
{
//We can't emplace. Try to diagnose a disabled postblit.
static assert(!(Args.length == 1 && is(Args[0] : T)),
format("Cannot emplace a %1$s because %1$s.this(this) is annotated with @disable.", T.stringof));
convFormat("Cannot emplace a %1$s because %1$s.this(this) is annotated with @disable.", T.stringof));

//We can't emplace.
static assert(false,
format("%s cannot be emplaced from %s.", T.stringof, Args[].stringof));
convFormat("%s cannot be emplaced from %s.", T.stringof, Args[].stringof));
}

return chunk;
Expand All @@ -4069,7 +4073,7 @@ private deprecated("Using static opCall for emplace is deprecated. Plase use emp
ref T emplaceOpCaller(T, Args...)(ref T chunk, auto ref Args args)
{
static assert (is(typeof({T t = T.opCall(args);})),
format("%s.opCall does not return adequate data for construction.", T.stringof));
convFormat("%s.opCall does not return adequate data for construction.", T.stringof));
return emplaceImpl!T(chunk, chunk.opCall(args));
}

Expand Down Expand Up @@ -4938,13 +4942,12 @@ unittest

private void testEmplaceChunk(void[] chunk, size_t typeSize, size_t typeAlignment, string typeName)
{
import std.string : format;
import std.exception : enforce;
enforce!ConvException(chunk.length >= typeSize,
format("emplace: Chunk size too small: %s < %s size = %s",
convFormat("emplace: Chunk size too small: %s < %s size = %s",
chunk.length, typeName, typeSize));
enforce!ConvException((cast(size_t) chunk.ptr) % typeAlignment == 0,
format("emplace: Misaligned memory block (0x%X): it must be %s-byte aligned for type %s",
convFormat("emplace: Misaligned memory block (0x%X): it must be %s-byte aligned for type %s",
chunk.ptr, typeAlignment, typeName));
}

Expand Down
60 changes: 48 additions & 12 deletions std/stdio.d
Expand Up @@ -16,15 +16,20 @@ Authors: $(WEB digitalmars.com, Walter Bright),
*/
module std.stdio;

public import core.stdc.stdio, std.string : KeepTerminator;
import core.vararg;
public import core.stdc.stdio;
static import core.stdc.stdio;
import std.typecons : Flag;
import std.stdiobase;
import core.stdc.errno, core.stdc.stddef, core.stdc.stdlib, core.memory,
core.stdc.string, core.stdc.wchar_, core.exception;
import std.range;
import std.traits : Unqual, isSomeChar, isAggregateType, isSomeString,
isIntegral, isBoolean, ParameterTypeTuple;
import core.stdc.errno, core.stdc.stddef, core.stdc.stdlib,
core.stdc.string, core.stdc.wchar_;
import std.range.constraints;
import std.traits;

/++
If flag $(D KeepTerminator) is set to $(D KeepTerminator.yes), then the delimiter
is included in the strings returned.
+/
alias KeepTerminator = Flag!"keepTerminator";

version (CRuntime_Microsoft)
{
Expand Down Expand Up @@ -1877,6 +1882,7 @@ $(XREF file,readText)
{
static import std.file;
import std.algorithm : equal;
import std.range;

//printf("Entering test at line %d\n", __LINE__);
scope(failure) printf("Failed test at line %d\n", __LINE__);
Expand Down Expand Up @@ -1954,6 +1960,7 @@ $(XREF file,readText)
unittest
{
import std.algorithm : equal;
import std.range;

version(Win64)
{
Expand Down Expand Up @@ -2084,14 +2091,24 @@ $(XREF file,readText)
@property nothrow
ubyte[] front()
{
version(assert) if (empty) throw new RangeError();
version(assert)
{
import core.exception : RangeError;
if (empty)
throw new RangeError();
}
return chunk_;
}

/// Ditto
void popFront()
{
version(assert) if (empty) throw new RangeError();
version(assert)
{
import core.exception : RangeError;
if (empty)
throw new RangeError();
}
prime();
}
}
Expand Down Expand Up @@ -2571,6 +2588,7 @@ unittest
unittest
{
static import std.file;
import std.range;

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

@property dchar front()
{
version(assert) if (empty) throw new RangeError();
version(assert)
{
import core.exception : RangeError;
if (empty)
throw new RangeError();
}
return _front;
}

Expand Down Expand Up @@ -2700,7 +2723,12 @@ struct LockingTextReader

void popFront()
{
version(assert) if (empty) throw new RangeError();
version(assert)
{
import core.exception : RangeError;
if (empty)
throw new RangeError();
}

// Pop the current front.
char[4] buf;
Expand Down Expand Up @@ -3666,7 +3694,8 @@ class StdioException : Exception
uint errno;

/**
Initialize with a message and an error code. */
Initialize with a message and an error code.
*/
this(string message, uint e = .errno)
{
import std.conv : to;
Expand Down Expand Up @@ -3763,6 +3792,9 @@ unittest
version (DIGITAL_MARS_STDIO)
private size_t readlnImpl(FILE* fps, ref char[] buf, dchar terminator = '\n')
{
import core.memory;
import std.array : appender, uninitializedArray;

FLOCK(fps);
scope(exit) FUNLOCK(fps);

Expand Down Expand Up @@ -3904,6 +3936,9 @@ private size_t readlnImpl(FILE* fps, ref char[] buf, dchar terminator = '\n')
version (MICROSOFT_STDIO)
private size_t readlnImpl(FILE* fps, ref char[] buf, dchar terminator = '\n')
{
import core.memory;
import std.array : appender, uninitializedArray;

FLOCK(fps);
scope(exit) FUNLOCK(fps);

Expand Down Expand Up @@ -3940,6 +3975,7 @@ private size_t readlnImpl(FILE* fps, ref char[] buf, dchar terminator = '\n')
version (GCC_IO)
private size_t readlnImpl(FILE* fps, ref char[] buf, dchar terminator = '\n')
{
import core.memory;
import std.utf : encode;

if (fwide(fps, 0) > 0)
Expand Down
4 changes: 2 additions & 2 deletions std/string.d
Expand Up @@ -264,7 +264,7 @@ pure nothrow unittest
/**
Flag indicating whether a search is case-sensitive.
*/
alias CaseSensitive = Flag!"CaseSensitive";
alias CaseSensitive = Flag!"caseSensitive";

/++
Returns the index of the first occurrence of $(D c) in $(D s). If $(D c)
Expand Down Expand Up @@ -1893,7 +1893,7 @@ S capitalize(S)(S s) @trusted pure
If $(D keepTerm) is set to $(D KeepTerminator.yes), then the delimiter
is included in the strings returned.
+/
alias KeepTerminator = Flag!"KeepTerminator";
alias KeepTerminator = Flag!"keepTerminator";
/// ditto
S[] splitLines(S)(S s, in KeepTerminator keepTerm = KeepTerminator.no) @safe pure
if (isSomeString!S)
Expand Down

0 comments on commit 1fecd4e

Please sign in to comment.