-
-
Notifications
You must be signed in to change notification settings - Fork 704
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Range-ify std.file remove, getSize #3431
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -620,27 +620,70 @@ private void renameImpl(const(char)[] f, const(char)[] t, const(FSChar)* fromz, | |
|
|
||
| /*************************************************** | ||
| Delete file $(D name). | ||
|
|
||
| Params: | ||
| name = string or range of characters representing the file name | ||
|
|
||
| Throws: $(D FileException) on error. | ||
| */ | ||
| void remove(in char[] name) @trusted | ||
| void remove(R)(R name) | ||
| if (isInputRange!R && isSomeChar!(ElementEncodingType!R) || isSomeString!R) | ||
| { | ||
| static if (isNarrowString!R && is(Unqual!(ElementEncodingType!R) == char)) | ||
| removeImpl(name, name.tempCString!FSChar()); | ||
| else | ||
| removeImpl(null, name.tempCString!FSChar()); | ||
| } | ||
|
|
||
| private void removeImpl(const(char)[] name, const(FSChar)* namez) @trusted | ||
| { | ||
| version(Windows) | ||
| { | ||
| cenforce(DeleteFileW(name.tempCStringW()), name); | ||
| cenforce(DeleteFileW(namez), name, namez); | ||
| } | ||
| else version(Posix) | ||
| { | ||
| import core.stdc.stdio; | ||
|
|
||
| cenforce(core.stdc.stdio.remove(name.tempCString()) == 0, | ||
| if (!name) | ||
| { | ||
| import core.stdc.string : strlen; | ||
| auto len = strlen(namez); | ||
| name = namez[0 .. len]; | ||
| } | ||
| cenforce(core.stdc.stdio.remove(namez) == 0, | ||
| "Failed to remove file " ~ name); | ||
| } | ||
| } | ||
|
|
||
| version(Windows) private WIN32_FILE_ATTRIBUTE_DATA getFileAttributesWin(in char[] name) @trusted | ||
| version(Windows) private WIN32_FILE_ATTRIBUTE_DATA getFileAttributesWin(R)(R name) | ||
| if (isInputRange!R && isSomeChar!(ElementEncodingType!R) || isSomeString!R) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here, no need for isSomeString |
||
| { | ||
| auto namez = name.tempCString!FSChar(); | ||
|
|
||
| WIN32_FILE_ATTRIBUTE_DATA fad = void; | ||
|
|
||
| static if (isNarrowString!R && is(Unqual!(ElementEncodingType!R) == char)) | ||
| { | ||
| WIN32_FILE_ATTRIBUTE_DATA fad; | ||
| enforce(GetFileAttributesExW(name.tempCStringW(), GET_FILEEX_INFO_LEVELS.GetFileExInfoStandard, &fad), new FileException(name.idup)); | ||
| static void getFA(const(char)[] name, const(FSChar)* namez, out WIN32_FILE_ATTRIBUTE_DATA fad) @trusted | ||
| { | ||
| enforce(GetFileAttributesExW(namez, GET_FILEEX_INFO_LEVELS.GetFileExInfoStandard, &fad), | ||
| new FileException(name.idup)); | ||
| } | ||
| getFA(name, namez, fad); | ||
| } | ||
| else | ||
| { | ||
| static void getFA(const(FSChar)* namez, out WIN32_FILE_ATTRIBUTE_DATA fad) @trusted | ||
| { | ||
| import core.stdc.wchar_ : wcslen; | ||
| import std.conv : to; | ||
|
|
||
| enforce(GetFileAttributesExW(namez, GET_FILEEX_INFO_LEVELS.GetFileExInfoStandard, &fad), | ||
| new FileException(namez[0 .. wcslen(namez)].to!string)); | ||
| } | ||
| getFA(namez, fad); | ||
| } | ||
| return fad; | ||
| } | ||
|
|
||
|
|
@@ -655,9 +698,13 @@ version(Windows) private ulong makeUlong(DWORD dwLow, DWORD dwHigh) @safe pure n | |
| /*************************************************** | ||
| Get size of file $(D name) in bytes. | ||
|
|
||
| Params: | ||
| name = string or range of characters representing the file name | ||
|
|
||
| Throws: $(D FileException) on error (e.g., file not found). | ||
| */ | ||
| ulong getSize(in char[] name) @safe | ||
| ulong getSize(R)(R name) | ||
| if (isInputRange!R && isSomeChar!(ElementEncodingType!R) || isSomeString!R) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here, no need for isSomeString |
||
| { | ||
| version(Windows) | ||
| { | ||
|
|
@@ -666,16 +713,18 @@ ulong getSize(in char[] name) @safe | |
| } | ||
| else version(Posix) | ||
| { | ||
| static auto trustedStat(in char[] path, stat_t* buf) @trusted | ||
| { | ||
| return stat(path.tempCString(), buf); | ||
| } | ||
| static stat_t* ptrOfLocalVariable(return ref stat_t buf) @trusted | ||
| auto namez = name.tempCString(); | ||
|
|
||
| static trustedStat(const(FSChar)* namez, out stat_t buf) @trusted | ||
| { | ||
| return &buf; | ||
| return stat(namez, &buf); | ||
| } | ||
| static if (isNarrowString!R && is(Unqual!(ElementEncodingType!R) == char)) | ||
| alias names = name; | ||
| else | ||
| string names = null; | ||
| stat_t statbuf = void; | ||
| cenforce(trustedStat(name, ptrOfLocalVariable(statbuf)) == 0, name); | ||
| cenforce(trustedStat(namez, statbuf) == 0, names, namez); | ||
| return statbuf.st_size; | ||
| } | ||
| } | ||
|
|
@@ -688,7 +737,8 @@ ulong getSize(in char[] name) @safe | |
| assert(getSize(deleteme) == 1); | ||
| // create a file of size 3 | ||
| write(deleteme, "abc"); | ||
| assert(getSize(deleteme) == 3); | ||
| import std.utf : byChar; | ||
| assert(getSize(deleteme.byChar) == 3); | ||
| } | ||
|
|
||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Forgot to mention - this is enough:
if (isInputRange!R && isSomeChar!(ElementEncodingType!R))There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was used to strings not being random access ranges!