From 39ba8e3927570905dca8c50e48a4c09febfe44bc Mon Sep 17 00:00:00 2001
From: dd86k
Date: Sat, 30 Mar 2024 21:05:46 -0400
Subject: [PATCH] Fix truncated entries on print
Relative paths is annoying with dirEntries
---
src/main.d | 6 +++---
src/utils.d | 36 +++++++++++++++++++++++++++++++++---
2 files changed, 36 insertions(+), 6 deletions(-)
diff --git a/src/main.d b/src/main.d
index 3fc9243..21e03a4 100644
--- a/src/main.d
+++ b/src/main.d
@@ -398,7 +398,7 @@ ubyte[] hashFile(Digest digest, ref File file)
// NOTE: This is called from another thread
void mtDirEntry(DirEntry entry, immutable(void)* uobj)
{
- string path = entry.name[2..$];
+ string path = fixpath( entry.name );
if (entry.isDir())
{
@@ -412,7 +412,7 @@ void mtDirEntry(DirEntry entry, immutable(void)* uobj)
{
Digest digest = cast(Digest)uobj; // Get thread-assigned instance
digest.reset();
- printHash(hashFile(digest, entry.name), path);
+ printHash(hashFile(digest, path), path);
}
catch (Exception ex)
{
@@ -567,7 +567,7 @@ int processList(string path, bool autodetect, Style style)
void processAgainstEntry(DirEntry entry, immutable(void)* uobj)
{
- string path = entry.name[2..$];
+ string path = fixpath( entry.name );
if (entry.isDir())
{
diff --git a/src/utils.d b/src/utils.d
index 9ef993b..1f8be5d 100644
--- a/src/utils.d
+++ b/src/utils.d
@@ -6,9 +6,10 @@
module utils;
import std.conv : text;
+import std.datetime : Duration, dur;
import std.format.read : formattedRead;
import std.string : toStringz;
-import std.datetime : Duration, dur;
+import std.path : dirSeparator;
import core.stdc.stdio : sscanf;
import core.stdc.ctype : toupper;
@@ -17,15 +18,25 @@ import core.stdc.ctype : toupper;
// Use OS functions (GetFileAttributes/GetFileType, stat_t, etc.)
/// Parse string into a 32-bit unsigned integer.
-/// Params: input =
+/// Params: input = String user input.
/// Returns: Unformatted number.
-uint cparse(string input)
+int cparse(string input)
{
int u = void;
if (sscanf(input.toStringz, "%i", &u) != 1)
throw new Exception("Could not parse input");
return cast(uint)u;
}
+unittest
+{
+ import std.conv : octal;
+ assert(cparse("0") == 0);
+ assert(cparse("1") == 1);
+ assert(cparse("010") == octal!10);
+ assert(cparse("0x10") == 0x10);
+ assert(cparse("0x7fffffff") == 0x7fff_ffff);
+ assert(cparse("0x80000000") == 0x8000_0000);
+}
private enum
{
@@ -267,4 +278,23 @@ unittest
assert(compareList("ABC", &cmp) == 3);
assert(compareList("ABCD", &cmp) == 6);
assert(compareList("ABCDE", &cmp) == 10);
+}
+
+// Better than the dirSeparator string
+private immutable string pf = "."~dirSeparator;
+
+// Fixes the annoying relative path that dirEntries *might* introduce.
+// Safer than preemptively truncating it.
+string fixpath(string entry)
+{
+ if (entry.length <= pf.length)
+ return entry;
+
+ return entry[0..pf.length] != pf ? entry : entry[pf.length..$];
+}
+unittest
+{
+ assert(fixpath("a") == "a");
+ assert(fixpath("abc") == "abc");
+ assert(fixpath(pf~"abc") == "abc");
}
\ No newline at end of file