aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2020-08-13 16:02:13 -0700
committerChris Robinson <[email protected]>2020-08-13 16:02:13 -0700
commitaafcb1f6fc220bbebdb023d6b6d2277f94085904 (patch)
tree61d95bddaf21c2bfe7bdca0272c713e8b4fb8102
parent8e7199cbb60a4682a3d4c28d5746c1890c40e983 (diff)
Simplify StrSubst in makemhr
-rw-r--r--utils/makemhr/makemhr.cpp63
1 files changed, 27 insertions, 36 deletions
diff --git a/utils/makemhr/makemhr.cpp b/utils/makemhr/makemhr.cpp
index 96823745..6307868f 100644
--- a/utils/makemhr/makemhr.cpp
+++ b/utils/makemhr/makemhr.cpp
@@ -89,6 +89,7 @@
#endif
#include "alfstream.h"
+#include "alspan.h"
#include "alstring.h"
#include "loaddef.h"
#include "loadsofa.h"
@@ -161,42 +162,31 @@ enum ChannelIndex : uint {
* pattern string are replaced with the replacement string. The result is
* truncated if necessary.
*/
-static int StrSubst(const char *in, const char *pat, const char *rep, const size_t maxLen, char *out)
+static std::string StrSubst(al::span<const char> in, const al::span<const char> pat,
+ const al::span<const char> rep)
{
- size_t inLen, patLen, repLen;
- size_t si, di;
- int truncated;
-
- inLen = strlen(in);
- patLen = strlen(pat);
- repLen = strlen(rep);
- si = 0;
- di = 0;
- truncated = 0;
- while(si < inLen && di < maxLen)
+ std::string ret;
+ ret.reserve(in.size() + pat.size());
+
+ while(in.size() >= pat.size())
{
- if(patLen <= inLen-si)
+ if(al::strncasecmp(in.data(), pat.data(), pat.size()) == 0)
{
- if(al::strncasecmp(&in[si], pat, patLen) == 0)
- {
- if(repLen > maxLen-di)
- {
- repLen = maxLen - di;
- truncated = 1;
- }
- strncpy(&out[di], rep, repLen);
- si += patLen;
- di += repLen;
- }
+ in = in.subspan(pat.size());
+ ret.append(rep.data(), rep.size());
+ }
+ else
+ {
+ size_t endpos{1};
+ while(endpos < in.size() && in[endpos] != pat.front())
+ ++endpos;
+ ret.append(in.data(), endpos);
+ in = in.subspan(endpos);
}
- out[di] = in[si];
- si++;
- di++;
}
- if(si < inLen)
- truncated = 1;
- out[di] = '\0';
- return !truncated;
+ ret.append(in.data(), in.size());
+
+ return ret;
}
@@ -1395,7 +1385,6 @@ static int ProcessDefinition(const char *inName, const uint outRate, const Chann
const int surface, const double limit, const uint truncSize, const HeadModelT model,
const double radius, const char *outName)
{
- char rateStr[8+1], expName[MAX_PATH_LEN];
HrirDataT hData;
fprintf(stdout, "Using %u thread%s.\n", numThreads, (numThreads==1)?"":"s");
@@ -1487,10 +1476,12 @@ static int ProcessDefinition(const char *inName, const uint outRate, const Chann
NormalizeHrirs(&hData);
fprintf(stdout, "Calculating impulse delays...\n");
CalculateHrtds(model, (radius > DEFAULT_CUSTOM_RADIUS) ? radius : hData.mRadius, &hData);
- snprintf(rateStr, sizeof(rateStr), "%u", hData.mIrRate);
- StrSubst(outName, "%r", rateStr, sizeof(expName), expName);
- fprintf(stdout, "Creating MHR data set %s...\n", expName);
- return StoreMhr(&hData, expName);
+
+ const auto rateStr = std::to_string(hData.mIrRate);
+ const auto expName = StrSubst({outName, strlen(outName)}, {"%r", 2},
+ {rateStr.data(), rateStr.size()});
+ fprintf(stdout, "Creating MHR data set %s...\n", expName.c_str());
+ return StoreMhr(&hData, expName.c_str());
}
static void PrintHelp(const char *argv0, FILE *ofile)