Skip to content

Commit a52f36b

Browse files
committed
mostly add utility functions for backslash handling to MiscUtils.h
1 parent 16d7e98 commit a52f36b

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

library/Core.cpp

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ using std::string;
105105
// FIXME: A lot of code in one file, all doing different things... there's something fishy about it.
106106

107107
static bool parseKeySpec(std::string keyspec, int *psym, int *pmod, std::string *pfocus = nullptr);
108-
size_t loadScriptFiles(Core* core, color_ostream& out, const std::vector<std::string>& prefix, const std::filesystem::path& folder);
108+
static size_t loadScriptFiles(Core* core, color_ostream& out, std::span<const std::string> prefix, const std::filesystem::path& folder);
109109

110110
namespace DFHack {
111111

@@ -316,7 +316,7 @@ static std::string dfhack_version_desc()
316316
return s.str();
317317
}
318318

319-
static bool init_run_script(color_ostream &out, lua_State *state, const std::string& pcmd, std::span<const std::string> pargs)
319+
static bool init_run_script(color_ostream &out, lua_State *state, const std::string& pcmd, const std::span<const std::string> pargs)
320320
{
321321
if (!lua_checkstack(state, pargs.size()+10))
322322
return false;
@@ -329,7 +329,7 @@ static bool init_run_script(color_ostream &out, lua_State *state, const std::str
329329
return true;
330330
}
331331

332-
static command_result runLuaScript(color_ostream &out, std::string name, std::span<const std::string> args)
332+
static command_result runLuaScript(color_ostream &out, std::string name, const std::span<const std::string> args)
333333
{
334334
auto init_fn = [n = std::move(name), args](color_ostream& out, lua_State* state) -> bool {
335335
return init_run_script(out, state, n, args);
@@ -673,14 +673,14 @@ void tags_helper(color_ostream &con, const std::string &tag) {
673673
}
674674
}
675675

676-
void ls_helper(color_ostream &con, const std::vector<std::string> &params) {
676+
static void ls_helper(color_ostream &con, const std::span<const std::string> params) {
677677
std::vector<std::string> filter;
678678
bool skip_tags = false;
679679
bool show_dev_commands = false;
680-
std::string exclude_strs = "";
680+
std::string exclude_strs;
681681

682682
bool in_exclude = false;
683-
for (auto str : params) {
683+
for (const auto& str : params) {
684684
if (in_exclude)
685685
exclude_strs = str;
686686
else if (str == "--notags")
@@ -733,14 +733,10 @@ command_result Core::runCommand(color_ostream &con, const std::string &first_, s
733733
if (first.empty())
734734
return CR_NOT_IMPLEMENTED;
735735

736-
if (first.find('\\') != std::string::npos)
736+
if (has_backslashes(first))
737737
{
738738
con.printerr("Replacing backslashes with forward slashes in \"%s\"\n", first.c_str());
739-
for (size_t i = 0; i < first.size(); i++)
740-
{
741-
if (first[i] == '\\')
742-
first[i] = '/';
743-
}
739+
replace_backslashes_with_forwardslashes(first);
744740
}
745741

746742
// let's see what we actually got
@@ -842,14 +838,10 @@ command_result Core::runCommand(color_ostream &con, const std::string &first_, s
842838
{
843839
for (auto& part : parts)
844840
{
845-
if (part.find('\\') != std::string::npos)
841+
if (has_backslashes(part))
846842
{
847843
con.printerr("Replacing backslashes with forward slashes in \"%s\"\n", part.c_str());
848-
for (char& c : part)
849-
{
850-
if (c == '\\')
851-
c = '/';
852-
}
844+
replace_backslashes_with_forwardslashes(part);
853845
}
854846

855847
part = GetAliasCommand(part, true);
@@ -920,7 +912,7 @@ command_result Core::runCommand(color_ostream &con, const std::string &first_, s
920912
if (!plug)
921913
continue;
922914

923-
if (parts.size() && std::ranges::find(parts, key) == std::ranges::end(parts))
915+
if (parts.size() && std::ranges::find(parts, key) == parts.end())
924916
continue;
925917

926918
color_value color;
@@ -2168,7 +2160,7 @@ static void getFilesWithPrefixAndSuffix(const std::filesystem::path& folder, con
21682160
}
21692161
}
21702162

2171-
size_t loadScriptFiles(Core* core, color_ostream& out, const std::vector<std::string>& prefix, const std::filesystem::path& folder) {
2163+
size_t loadScriptFiles(Core* core, color_ostream& out, const std::span<const std::string> prefix, const std::filesystem::path& folder) {
21722164
static const std::string suffix = ".init";
21732165
std::vector<std::filesystem::path> scriptFiles;
21742166
for ( const auto& p : prefix ) {

library/include/MiscUtils.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,19 @@ static inline std::string &trim(std::string &s) {
475475
return ltrim(rtrim(s));
476476
}
477477

478+
static inline bool has_backslashes(const std::string_view str)
479+
{
480+
return (str.find('\\') != std::string::npos);
481+
}
482+
483+
static inline void replace_backslashes_with_forwardslashes(std::string& str)
484+
{
485+
for (auto& c : str) {
486+
if (c == '\\')
487+
c = '/';
488+
}
489+
}
490+
478491
enum struct NumberFormatType : int32_t {
479492
DEFAULT = 0,
480493
ENGLISH,

0 commit comments

Comments
 (0)