45 changes: 23 additions & 22 deletions std/regex/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ public alias StaticRegex(Char) = std.regex.internal.ir.StaticRegex!(Char);
@trusted public auto regex(S)(S[] patterns, const(char)[] flags="")
if (isSomeString!(S))
{
import std.functional;
import std.functional : memoize;
enum cacheSize = 8; //TODO: invent nice interface to control regex caching
S pat;
if (patterns.length > 1)
Expand Down Expand Up @@ -373,7 +373,7 @@ unittest
public auto regexImpl(S)(S pattern, const(char)[] flags="")
if (isSomeString!(S))
{
import std.regex.internal.parser;
import std.regex.internal.parser : Parser, CodeGen;
auto parser = Parser!(Unqual!(typeof(pattern)), CodeGen)(pattern, flags);
auto r = parser.program;
return r;
Expand Down Expand Up @@ -423,7 +423,7 @@ enum isRegexFor(RegEx, R) = is(RegEx == Regex!(BasicElementOf!R))
alias DataIndex = DIndex;
alias String = R;
private:
import std.conv;
import std.conv : text;
R _input;
int _nMatch;
enum smallString = 3;
Expand Down Expand Up @@ -463,7 +463,7 @@ private:

void newMatches(uint n)
{
import core.stdc.stdlib;
import core.stdc.stdlib : calloc;
if (n > smallString)
{
auto p = cast(Group!DataIndex*)enforce(
Expand Down Expand Up @@ -494,7 +494,7 @@ public:
}
~this()
{
import core.stdc.stdlib;
import core.stdc.stdlib : free;
if (!(_refcount & SMALL_MASK))
{
if (--_refcount == 0)
Expand Down Expand Up @@ -653,7 +653,7 @@ unittest
if (isSomeString!R)
{
private:
import core.stdc.stdlib;
import core.stdc.stdlib : malloc, free;
alias Char = BasicElementOf!R;
alias EngineType = Engine!Char;
EngineType _engine;
Expand Down Expand Up @@ -770,7 +770,7 @@ public:

private @trusted auto matchOnce(alias Engine, RegEx, R)(R input, RegEx re)
{
import core.stdc.stdlib;
import core.stdc.stdlib : malloc, free;
alias Char = BasicElementOf!R;
alias EngineType = Engine!Char;

Expand Down Expand Up @@ -888,22 +888,22 @@ private R replaceAllWith(alias output,
public auto match(R, RegEx)(R input, RegEx re)
if (isSomeString!R && is(RegEx == Regex!(BasicElementOf!R)))
{
import std.regex.internal.thompson;
import std.regex.internal.thompson : ThompsonMatcher;
return RegexMatch!(Unqual!(typeof(input)),ThompsonMatcher)(input, re);
}

///ditto
public auto match(R, String)(R input, String re)
if (isSomeString!R && isSomeString!String)
{
import std.regex.internal.thompson;
import std.regex.internal.thompson : ThompsonMatcher;
return RegexMatch!(Unqual!(typeof(input)),ThompsonMatcher)(input, regex(re));
}

public auto match(R, RegEx)(R input, RegEx re)
if (isSomeString!R && is(RegEx == StaticRegex!(BasicElementOf!R)))
{
import std.regex.internal.backtracking;
import std.regex.internal.backtracking : BacktrackingMatcher;
return RegexMatch!(Unqual!(typeof(input)),BacktrackingMatcher!true)(input, re);
}

Expand All @@ -928,30 +928,30 @@ public auto match(R, RegEx)(R input, RegEx re)
public auto matchFirst(R, RegEx)(R input, RegEx re)
if (isSomeString!R && is(RegEx == Regex!(BasicElementOf!R)))
{
import std.regex.internal.thompson;
import std.regex.internal.thompson : ThompsonMatcher;
return matchOnce!ThompsonMatcher(input, re);
}

///ditto
public auto matchFirst(R, String)(R input, String re)
if (isSomeString!R && isSomeString!String)
{
import std.regex.internal.thompson;
import std.regex.internal.thompson : ThompsonMatcher;
return matchOnce!ThompsonMatcher(input, regex(re));
}

///ditto
public auto matchFirst(R, String)(R input, String[] re...)
if (isSomeString!R && isSomeString!String)
{
import std.regex.internal.thompson;
import std.regex.internal.thompson : ThompsonMatcher;
return matchOnce!ThompsonMatcher(input, regex(re));
}

public auto matchFirst(R, RegEx)(R input, RegEx re)
if (isSomeString!R && is(RegEx == StaticRegex!(BasicElementOf!R)))
{
import std.regex.internal.backtracking;
import std.regex.internal.backtracking : BacktrackingMatcher;
return matchOnce!(BacktrackingMatcher!true)(input, re);
}

Expand Down Expand Up @@ -979,30 +979,30 @@ public auto matchFirst(R, RegEx)(R input, RegEx re)
public auto matchAll(R, RegEx)(R input, RegEx re)
if (isSomeString!R && is(RegEx == Regex!(BasicElementOf!R)))
{
import std.regex.internal.thompson;
import std.regex.internal.thompson : ThompsonMatcher;
return matchMany!ThompsonMatcher(input, re);
}

///ditto
public auto matchAll(R, String)(R input, String re)
if (isSomeString!R && isSomeString!String)
{
import std.regex.internal.thompson;
import std.regex.internal.thompson : ThompsonMatcher;
return matchMany!ThompsonMatcher(input, regex(re));
}

///ditto
public auto matchAll(R, String)(R input, String[] re...)
if (isSomeString!R && isSomeString!String)
{
import std.regex.internal.thompson;
import std.regex.internal.thompson : ThompsonMatcher;
return matchMany!ThompsonMatcher(input, regex(re));
}

public auto matchAll(R, RegEx)(R input, RegEx re)
if (isSomeString!R && is(RegEx == StaticRegex!(BasicElementOf!R)))
{
import std.regex.internal.backtracking;
import std.regex.internal.backtracking : BacktrackingMatcher;
return matchMany!(BacktrackingMatcher!true)(input, re);
}

Expand Down Expand Up @@ -1067,22 +1067,22 @@ public auto matchAll(R, RegEx)(R input, RegEx re)
public auto bmatch(R, RegEx)(R input, RegEx re)
if (isSomeString!R && is(RegEx == Regex!(BasicElementOf!R)))
{
import std.regex.internal.backtracking;
import std.regex.internal.backtracking : BacktrackingMatcher;
return RegexMatch!(Unqual!(typeof(input)), BacktrackingMatcher!false)(input, re);
}

///ditto
public auto bmatch(R, String)(R input, String re)
if (isSomeString!R && isSomeString!String)
{
import std.regex.internal.backtracking;
import std.regex.internal.backtracking : BacktrackingMatcher;
return RegexMatch!(Unqual!(typeof(input)), BacktrackingMatcher!false)(input, regex(re));
}

public auto bmatch(R, RegEx)(R input, RegEx re)
if (isSomeString!R && is(RegEx == StaticRegex!(BasicElementOf!R)))
{
import std.regex.internal.backtracking;
import std.regex.internal.backtracking : BacktrackingMatcher;
return RegexMatch!(Unqual!(typeof(input)),BacktrackingMatcher!true)(input, re);
}

Expand All @@ -1092,7 +1092,8 @@ package void replaceFmt(R, Capt, OutR)
if (isOutputRange!(OutR, ElementEncodingType!R[]) &&
isOutputRange!(OutR, ElementEncodingType!(Capt.String)[]))
{
import std.algorithm, std.conv;
import std.algorithm.searching : find;
import std.conv : text, parse;
import std.ascii : isDigit, isAlpha;
enum State { Normal, Dollar }
auto state = State.Normal;
Expand Down
2 changes: 1 addition & 1 deletion std/socket.d
Original file line number Diff line number Diff line change
Expand Up @@ -2329,7 +2329,7 @@ public:
{
version (Windows)
{
import std.algorithm;
import std.algorithm.searching : canFind;
return fds.canFind(s) ? 1 : 0;
}
else
Expand Down
26 changes: 19 additions & 7 deletions std/stdio.d
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ Throws: $(D ErrnoException) in case of error.
{
import std.exception : errnoEnforce;
import std.format : format;
import core.stdc.stdint : intptr_t;

// Create file descriptors from the handles
version (DIGITAL_MARS_STDIO)
Expand Down Expand Up @@ -782,15 +783,19 @@ Throws: $(D Exception) if the file is not opened or if the OS call fails.
*/
void sync() @trusted
{
import std.exception : enforce, errnoEnforce;
import std.exception : enforce;

enforce(isOpen, "Attempting to sync() an unopened file");

version (Windows)
{
import core.sys.windows.windows : FlushFileBuffers;
wenforce(FlushFileBuffers(windowsHandle), "FlushFileBuffers failed");
}
else
{
import core.sys.posix.unistd : fsync;
import std.exception : errnoEnforce;
errnoEnforce(fsync(fileno) == 0, "fsync failed");
}
}
Expand Down Expand Up @@ -1069,7 +1074,7 @@ Throws: $(D Exception) if the file is not opened.

version(Windows)
{
import core.sys.windows.windows;
import core.sys.windows.windows : ULARGE_INTEGER, OVERLAPPED, BOOL;

private BOOL lockImpl(alias F, Flags...)(ulong start, ulong length,
Flags flags)
Expand All @@ -1089,7 +1094,7 @@ Throws: $(D Exception) if the file is not opened.

private static T wenforce(T)(T cond, string str)
{
import std.windows.syserror;
import std.windows.syserror : sysErrorString, GetLastError;

if (cond) return cond;
throw new Exception(str ~ ": " ~ sysErrorString(GetLastError()));
Expand Down Expand Up @@ -1131,11 +1136,12 @@ $(UL
void lock(LockType lockType = LockType.readWrite,
ulong start = 0, ulong length = 0)
{
import std.exception : enforce, errnoEnforce;
import std.exception : enforce;

enforce(isOpen, "Attempting to call lock() on an unopened file");
version (Posix)
{
import std.exception : errnoEnforce;
import core.sys.posix.fcntl : F_RDLCK, F_SETLKW, F_WRLCK;
immutable short type = lockType == LockType.readWrite
? F_WRLCK : F_RDLCK;
Expand All @@ -1145,6 +1151,7 @@ $(UL
else
version(Windows)
{
import core.sys.windows.windows : LockFileEx, LOCKFILE_EXCLUSIVE_LOCK;
immutable type = lockType == LockType.readWrite ?
LOCKFILE_EXCLUSIVE_LOCK : 0;
wenforce(lockImpl!LockFileEx(start, length, type),
Expand All @@ -1163,11 +1170,12 @@ specified file segment was already locked.
bool tryLock(LockType lockType = LockType.readWrite,
ulong start = 0, ulong length = 0)
{
import std.exception : enforce, errnoEnforce;
import std.exception : enforce;

enforce(isOpen, "Attempting to call tryLock() on an unopened file");
version (Posix)
{
import std.exception : errnoEnforce;
import core.stdc.errno : EACCES, EAGAIN, errno;
import core.sys.posix.fcntl : F_RDLCK, F_SETLK, F_WRLCK;
immutable short type = lockType == LockType.readWrite
Expand All @@ -1181,6 +1189,8 @@ specified file segment was already locked.
else
version(Windows)
{
import core.sys.windows.windows : GetLastError, LockFileEx, LOCKFILE_EXCLUSIVE_LOCK,
ERROR_IO_PENDING, ERROR_LOCK_VIOLATION, LOCKFILE_FAIL_IMMEDIATELY;
immutable type = lockType == LockType.readWrite
? LOCKFILE_EXCLUSIVE_LOCK : 0;
immutable res = lockImpl!LockFileEx(start, length,
Expand All @@ -1200,18 +1210,20 @@ Removes the lock over the specified file segment.
*/
void unlock(ulong start = 0, ulong length = 0)
{
import std.exception : enforce, errnoEnforce;
import std.exception : enforce;

enforce(isOpen, "Attempting to call unlock() on an unopened file");
version (Posix)
{
import std.exception : errnoEnforce;
import core.sys.posix.fcntl : F_SETLK, F_UNLCK;
errnoEnforce(lockImpl(F_SETLK, F_UNLCK, start, length) != -1,
"Could not remove lock for file `"~_name~"'");
}
else
version(Windows)
{
import core.sys.windows.windows : UnlockFileEx;
wenforce(lockImpl!UnlockFileEx(start, length),
"Could not remove lock for file `"~_name~"'");
}
Expand Down Expand Up @@ -4175,7 +4187,7 @@ Initialize with a message and an error code.
{
import core.stdc.string : strerror;

auto s = core.stdc.string.strerror(errno);
auto s = strerror(errno);
}
auto sysmsg = to!string(s);
// If e is 0, we don't use the system error message. (The message
Expand Down
5 changes: 4 additions & 1 deletion std/stream.d
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,10 @@ interface OutputStream {

// not really abstract, but its instances will do nothing useful
class Stream : InputStream, OutputStream {
private import std.string, std.digest.crc, core.stdc.stdlib, core.stdc.stdio;
import std.string : toStringz;
import std.digest.crc : CRC32;
import core.stdc.stdlib : alloca;
import core.stdc.stdio : vsnprintf;

// stream abilities
bool readable = false; /// Indicates whether this stream can be read from.
Expand Down
5 changes: 1 addition & 4 deletions std/utf.d
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ debug (utf) import core.stdc.stdio : printf;
+/
class UTFException : Exception
{
import core.internal.string;
import core.internal.string : unsignedToTempString, UnsignedStringBuf;

uint[4] sequence;
size_t len;
Expand Down Expand Up @@ -1581,7 +1581,6 @@ version(unittest) private void testDecode(R)(R range,
size_t expectedIndex,
size_t line = __LINE__)
{
import std.exception;
import std.string : format;
import core.exception : AssertError;

Expand Down Expand Up @@ -1610,7 +1609,6 @@ version(unittest) private void testDecodeFront(R)(ref R range,
size_t expectedNumCodeUnits,
size_t line = __LINE__)
{
import std.exception;
import std.string : format;
import core.exception : AssertError;

Expand Down Expand Up @@ -1642,7 +1640,6 @@ version(unittest) private void testBothDecode(R)(R range,

version(unittest) private void testBadDecode(R)(R range, size_t index, size_t line = __LINE__)
{
import std.exception;
import std.string : format;
import core.exception : AssertError;

Expand Down
2 changes: 1 addition & 1 deletion std/windows/syserror.d
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ bool putSysError(Writer)(DWORD code, Writer w, /*WORD*/int langId = 0)

class WindowsException : Exception
{
import core.sys.windows.windows;
import core.sys.windows.windows : DWORD;

final @property DWORD code() { return _code; } /// $(D GetLastError)'s return value.
private DWORD _code;
Expand Down