Skip to content

Commit

Permalink
Remove StringRef alias (#951)
Browse files Browse the repository at this point in the history
  • Loading branch information
ken-matsui authored Jul 21, 2024
1 parent b08abd5 commit f32c760
Show file tree
Hide file tree
Showing 47 changed files with 348 additions and 292 deletions.
47 changes: 26 additions & 21 deletions src/Algos.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
#include <cctype>
#include <memory>
#include <string>
#include <string_view>
#include <thread>
#include <utility>

std::string
toUpper(const StringRef str) noexcept {
toUpper(const std::string_view str) noexcept {
std::string res;
for (const unsigned char c : str) {
res += static_cast<char>(std::toupper(c));
Expand All @@ -21,7 +22,7 @@ toUpper(const StringRef str) noexcept {
}

std::string
toMacroName(const StringRef name) noexcept {
toMacroName(const std::string_view name) noexcept {
std::string macroName;
for (const unsigned char c : name) {
if (std::isalpha(c)) {
Expand All @@ -36,15 +37,15 @@ toMacroName(const StringRef name) noexcept {
}

int
execCmd(const StringRef cmd) noexcept {
execCmd(const std::string_view cmd) noexcept {
logger::debug("Running `", cmd, '`');
const int status = std::system(cmd.data());
const int exitCode = status >> 8;
return exitCode;
}

static std::pair<std::string, int>
getCmdOutputImpl(const StringRef cmd) {
getCmdOutputImpl(const std::string_view cmd) {
constexpr usize bufferSize = 128;
std::array<char, bufferSize> buffer{};
std::string output;
Expand All @@ -67,7 +68,7 @@ getCmdOutputImpl(const StringRef cmd) {
}

std::string
getCmdOutput(const StringRef cmd, const usize retry) {
getCmdOutput(const std::string_view cmd, const usize retry) {
logger::debug("Running `", cmd, '`');

int exitCode = EXIT_SUCCESS;
Expand All @@ -87,7 +88,7 @@ getCmdOutput(const StringRef cmd, const usize retry) {
}

bool
commandExists(const StringRef cmd) noexcept {
commandExists(const std::string_view cmd) noexcept {
std::string checkCmd = "command -v ";
checkCmd += cmd;
checkCmd += " >/dev/null 2>&1";
Expand All @@ -96,7 +97,7 @@ commandExists(const StringRef cmd) noexcept {

// ref: https://wandbox.org/permlink/zRjT41alOHdwcf00
static usize
levDistance(const StringRef lhs, const StringRef rhs) {
levDistance(const std::string_view lhs, const std::string_view rhs) {
const usize lhsSize = lhs.size();
const usize rhsSize = rhs.size();

Expand Down Expand Up @@ -132,18 +133,22 @@ levDistance(const StringRef lhs, const StringRef rhs) {
}

static bool
equalsInsensitive(const StringRef lhs, const StringRef rhs) noexcept {
equalsInsensitive(
const std::string_view lhs, const std::string_view rhs
) noexcept {
return std::equal(
lhs.cbegin(), lhs.cend(), rhs.cbegin(), rhs.cend(),
[](char lhs, char rhs) { return std::tolower(lhs) == std::tolower(rhs); }
);
}

Option<StringRef>
findSimilarStr(const StringRef lhs, std::span<const StringRef> candidates) {
Option<std::string_view>
findSimilarStr(
const std::string_view lhs, std::span<const std::string_view> candidates
) {
// We need to check if `Candidates` has the exact case-insensitive string
// because the Levenshtein distance match does not care about it.
for (const StringRef str : candidates) {
for (const std::string_view str : candidates) {
if (equalsInsensitive(lhs, str)) {
return str;
}
Expand All @@ -155,8 +160,8 @@ findSimilarStr(const StringRef lhs, std::span<const StringRef> candidates) {
const usize length = lhs.size();
const usize maxDist = length < 3 ? length - 1 : length / 3;

Option<std::pair<StringRef, usize>> similarStr = None;
for (const StringRef str : candidates) {
Option<std::pair<std::string_view, usize>> similarStr = None;
for (const std::string_view str : candidates) {
const usize curDist = levDistance(lhs, str);
if (curDist <= maxDist) {
// The first similar string found || More similar string found
Expand Down Expand Up @@ -192,9 +197,9 @@ testLevDistance() {

void
testLevDistance2() {
constexpr StringRef str1 = "\nMäry häd ä little lämb\n\nLittle lämb\n";
constexpr StringRef str2 = "\nMary häd ä little lämb\n\nLittle lämb\n";
constexpr StringRef str3 = "Mary häd ä little lämb\n\nLittle lämb\n";
constexpr std::string_view str1 = "\nMäry häd ä little lämb\n\nLittle lämb\n";
constexpr std::string_view str2 = "\nMary häd ä little lämb\n\nLittle lämb\n";
constexpr std::string_view str3 = "Mary häd ä little lämb\n\nLittle lämb\n";

assertEq(levDistance(str1, str2), 2UL);
assertEq(levDistance(str2, str1), 2UL);
Expand All @@ -221,9 +226,9 @@ testLevDistance2() {

void
testFindSimilarStr() {
constexpr Arr<StringRef, 8> candidates{
"if", "ifdef", "ifndef", "elif", "else", "endif", "elifdef", "elifndef"
};
constexpr Arr<std::string_view, 8> candidates{ "if", "ifdef", "ifndef",
"elif", "else", "endif",
"elifdef", "elifndef" };

assertEq(findSimilarStr("id", candidates), "if"sv);
assertEq(findSimilarStr("ifd", candidates), "if"sv);
Expand All @@ -245,11 +250,11 @@ testFindSimilarStr() {

void
testFindSimilarStr2() {
constexpr Arr<StringRef, 2> candidates{ "aaab", "aaabc" };
constexpr Arr<std::string_view, 2> candidates{ "aaab", "aaabc" };
assertEq(findSimilarStr("aaaa", candidates), "aaab"sv);
assertEq(findSimilarStr("1111111111", candidates), None);

constexpr Arr<StringRef, 1> candidateS2{ "AAAA" };
constexpr Arr<std::string_view, 1> candidateS2{ "AAAA" };
assertEq(findSimilarStr("aaaa", candidateS2), "AAAA"sv);

pass();
Expand Down
16 changes: 9 additions & 7 deletions src/Algos.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
#include <span>
#include <sstream>
#include <string>
#include <string_view>
#include <utility>

std::string toUpper(StringRef str) noexcept;
std::string toMacroName(StringRef name) noexcept;
std::string toUpper(std::string_view str) noexcept;
std::string toMacroName(std::string_view name) noexcept;

int execCmd(StringRef cmd) noexcept;
std::string getCmdOutput(StringRef cmd, usize retry = 3);
bool commandExists(StringRef cmd) noexcept;
int execCmd(std::string_view cmd) noexcept;
std::string getCmdOutput(std::string_view cmd, usize retry = 3);
bool commandExists(std::string_view cmd) noexcept;

template <typename T>
Vec<std::string>
Expand Down Expand Up @@ -83,5 +84,6 @@ topoSort(
///
/// \returns a similar string if exists. If no similar string exists,
/// returns None.
Option<StringRef>
findSimilarStr(StringRef lhs, std::span<const StringRef> candidates);
Option<std::string_view> findSimilarStr(
std::string_view lhs, std::span<const std::string_view> candidates
);
32 changes: 17 additions & 15 deletions src/BuildConfig.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@
#include <span>
#include <sstream>
#include <string>
#include <string_view>
#include <tbb/blocked_range.h>
#include <tbb/parallel_for.h>
#include <tbb/spin_mutex.h>
#include <thread>

static constinit const StringRef TEST_OUT_DIR = "tests";
static constinit const StringRef PATH_FROM_OUT_DIR = "../../";
static constinit const std::string_view TEST_OUT_DIR = "tests";
static constinit const std::string_view PATH_FROM_OUT_DIR = "../../";

// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
static std::string OUT_DIR;
Expand Down Expand Up @@ -57,7 +58,7 @@ getOutDir() {
}

static Vec<fs::path>
listSourceFilePaths(const StringRef directory) {
listSourceFilePaths(const std::string_view directory) {
Vec<fs::path> sourceFilePaths;
for (const auto& entry : fs::recursive_directory_iterator(directory)) {
if (!SOURCE_FILE_EXTS.contains(entry.path().extension())) {
Expand Down Expand Up @@ -183,11 +184,11 @@ struct BuildConfig {

void emitVariable(std::ostream& os, const std::string& varName) const;
void emitMakefile(std::ostream& os) const;
void emitCompdb(StringRef baseDir, std::ostream& os) const;
void emitCompdb(std::string_view baseDir, std::ostream& os) const;
};

static void
emitDep(std::ostream& os, usize& offset, const StringRef dep) {
emitDep(std::ostream& os, usize& offset, const std::string_view dep) {
constexpr usize maxLineLen = 80;
if (offset + dep.size() + 2 > maxLineLen) { // 2 for space and \.
// \ for line continuation. \ is the 80th character.
Expand All @@ -200,7 +201,7 @@ emitDep(std::ostream& os, usize& offset, const StringRef dep) {

static void
emitTarget(
std::ostream& os, const StringRef target,
std::ostream& os, const std::string_view target,
const HashSet<std::string>& dependsOn,
const Option<std::string>& sourceFile = None,
const Vec<std::string>& commands = {}
Expand All @@ -213,12 +214,12 @@ emitTarget(
if (sourceFile.has_value()) {
emitDep(os, offset, sourceFile.value());
}
for (const StringRef dep : dependsOn) {
for (const std::string_view dep : dependsOn) {
emitDep(os, offset, dep);
}
os << '\n';

for (const StringRef cmd : commands) {
for (const std::string_view cmd : commands) {
os << '\t';
if (!cmd.starts_with('@')) {
os << "$(Q)";
Expand Down Expand Up @@ -286,7 +287,8 @@ BuildConfig::emitMakefile(std::ostream& os) const {
}

void
BuildConfig::emitCompdb(const StringRef baseDir, std::ostream& os) const {
BuildConfig::emitCompdb(const std::string_view baseDir, std::ostream& os)
const {
const fs::path baseDirPath = fs::canonical(baseDir);
const std::string indent1(2, ' ');
const std::string indent2(4, ' ');
Expand All @@ -299,7 +301,7 @@ BuildConfig::emitCompdb(const StringRef baseDir, std::ostream& os) const {
}

bool isCompileTarget = false;
for (const StringRef cmd : targetInfo.commands) {
for (const std::string_view cmd : targetInfo.commands) {
if (!cmd.starts_with("$(CXX)") && !cmd.starts_with("@$(CXX)")) {
continue;
}
Expand Down Expand Up @@ -392,7 +394,7 @@ parseMMOutput(const std::string& mmOutput, std::string& target) {
}

static bool
isUpToDate(const StringRef makefilePath) {
isUpToDate(const std::string_view makefilePath) {
if (!fs::exists(makefilePath)) {
return false;
}
Expand Down Expand Up @@ -443,7 +445,7 @@ containsTestCode(const std::string& sourceFile) {
}

static std::string
printfCmd(const StringRef header, const StringRef body) {
printfCmd(const std::string_view header, const std::string_view body) {
std::ostringstream oss;
logger::info(oss, header, body);
std::string msg = oss.str();
Expand Down Expand Up @@ -513,7 +515,7 @@ mapHeaderToObj(const fs::path& headerPath, const fs::path& buildOutDir) {
// depending header files for the source file.
static void
collectBinDepObjs( // NOLINT(misc-no-recursion)
HashSet<std::string>& deps, const StringRef sourceFileName,
HashSet<std::string>& deps, const std::string_view sourceFileName,
const HashSet<std::string>& objTargetDeps,
const HashSet<std::string>& buildObjTargets, const BuildConfig& config
) {
Expand Down Expand Up @@ -565,7 +567,7 @@ installDeps() {
}

static void
addDefine(const StringRef name, const StringRef value) {
addDefine(const std::string_view name, const std::string_view value) {
DEFINES += fmt::format(" -D{}='\"{}\"'", name, value);
}

Expand All @@ -586,7 +588,7 @@ setVariables(BuildConfig& config, const bool isDebug) {
if (profile.lto) {
CXXFLAGS += " -flto";
}
for (const StringRef flag : profile.cxxflags) {
for (const std::string_view flag : profile.cxxflags) {
CXXFLAGS += ' ';
CXXFLAGS += flag;
}
Expand Down
Loading

0 comments on commit f32c760

Please sign in to comment.