Skip to content

Commit

Permalink
Introduce readText and make stripUTF8BOM private
Browse files Browse the repository at this point in the history
  • Loading branch information
Geod24 committed Feb 22, 2024
1 parent 7541430 commit fc93c40
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion source/dub/generators/build.d
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ class BuildGenerator : ProjectGenerator {
const dbPathStr = dbPath.toNativeString();
Json db;
if (exists(dbPathStr)) {
const text = stripUTF8Bom(cast(string)readFile(dbPath));
const text = readText(dbPath);
db = parseJsonString(text, dbPathStr);
enforce(db.type == Json.Type.array, "Expected a JSON array in " ~ dbPathStr);
}
Expand Down
4 changes: 2 additions & 2 deletions source/dub/internal/utils.d
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ bool isWritableDir(NativePath p, bool create_if_missing = false)

Json jsonFromFile(NativePath file, bool silent_fail = false) {
if( silent_fail && !existsFile(file) ) return Json.emptyObject;
auto text = stripUTF8Bom(cast(string)readFile(file));
auto text = readText(file);
return parseJsonString(text, file.toNativeString());
}

Expand Down Expand Up @@ -451,7 +451,7 @@ version(DubUseCurl) {
}
}

string stripUTF8Bom(string str)
private string stripUTF8Bom(string str)
{
if( str.length >= 3 && str[0 .. 3] == [0xEF, 0xBB, 0xBF] )
return str[3 ..$];
Expand Down
6 changes: 6 additions & 0 deletions source/dub/internal/vibecompat/core/file.d
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ public ubyte[] readFile(NativePath path)
return cast(ubyte[]) std.file.read(path.toNativeString());
}

/// Returns the content of a file as text
public string readText(NativePath path)
{
return std.file.readText(path.toNativeString());
}

/**
Moves or renames a file.
*/
Expand Down
4 changes: 1 addition & 3 deletions source/dub/recipe/io.d
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ PackageRecipe readPackageRecipe(
PackageRecipe readPackageRecipe(NativePath filename,
in PackageName parent = PackageName.init, StrictMode mode = StrictMode.Ignore)
{
import dub.internal.utils : stripUTF8Bom;

string text = stripUTF8Bom(cast(string)readFile(filename));
string text = readText(filename);
return parsePackageRecipe(text, filename.toNativeString(), parent, null, mode);
}

Expand Down
17 changes: 14 additions & 3 deletions source/dub/test/base.d
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ package class TestPackageManager : PackageManager
SelectionsFile selected;
try
{
const content = cast(string) this.fs.readFile(selverfile);
const content = this.fs.readText(selverfile);
selected = parseConfigString!SelectionsFile(
content, selverfile.toNativeString());
}
Expand Down Expand Up @@ -380,7 +380,6 @@ package class TestPackageManager : PackageManager
Package parent = null, string version_ = null,
StrictMode mode = StrictMode.Ignore)
{
import dub.internal.utils : stripUTF8Bom;
if (recipe.empty)
recipe = this.findPackageFile(path);

Expand All @@ -392,7 +391,7 @@ package class TestPackageManager : PackageManager
const PackageName parent_name = parent
? PackageName(parent.name) : PackageName.init;

string text = stripUTF8Bom(cast(string)this.fs.readFile(recipe));
string text = this.fs.readText(recipe);
auto content = parsePackageRecipe(text, recipe.toNativeString(),
parent_name, null, mode);

Expand Down Expand Up @@ -748,6 +747,18 @@ public class FSEntry
return entry.content.dup;
}

/// Reads a file, returns the content as text
public string readText (NativePath path)
{
import std.utf : validate;

auto entry = this.lookup(path);
enforce(entry.type == Type.File, "Trying to read a directory");
// Ignore BOM: If it's needed for a test, add support for it.
validate(cast(const(char[])) entry.content);
return cast(string) entry.content.idup();
}

/// Write to this file
public void writeFile (NativePath path, const(char)[] data)
{
Expand Down

0 comments on commit fc93c40

Please sign in to comment.