clover: move tokenize function to algorithm

Reviewed-by: Francisco Jerez <currojerez@riseup.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/4974>
This commit is contained in:
Serge Martin 2020-09-27 14:12:46 +02:00 committed by Marge Bot
parent ee5b46fcfd
commit fd3209a974
3 changed files with 49 additions and 45 deletions

View File

@ -30,7 +30,6 @@
#include <vector>
#include <fstream>
#include <iostream>
#include <sstream>
namespace clover {
namespace llvm {
@ -40,49 +39,6 @@ namespace clover {
throw std::forward<E>(e);
}
inline std::vector<std::string>
tokenize(const std::string &s) {
std::vector<std::string> ss;
std::ostringstream oss;
// OpenCL programs can pass a quoted argument, most frequently the
// include path. This is useful so that path containing spaces is
// treated as a single argument instead of being split by the spaces.
// Additionally, the argument should also be unquoted before being
// passed to the compiler. We avoid using std::string::replace here to
// remove quotes, as the single and double quote characters can be a
// part of the file name.
bool escape_next = false;
bool in_quote_double = false;
bool in_quote_single = false;
for (auto c : s) {
if (escape_next) {
oss.put(c);
escape_next = false;
} else if (c == '\\') {
escape_next = true;
} else if (c == '"' && !in_quote_single) {
in_quote_double = !in_quote_double;
} else if (c == '\'' && !in_quote_double) {
in_quote_single = !in_quote_single;
} else if (c != ' ' || in_quote_single || in_quote_double) {
oss.put(c);
} else if (oss.tellp() > 0) {
ss.emplace_back(oss.str());
oss.str("");
}
}
if (oss.tellp() > 0)
ss.emplace_back(oss.str());
if (in_quote_double || in_quote_single)
throw invalid_build_options_error();
return ss;
}
inline std::string
as_string(const std::vector<char> &v) {
return { v.begin(), v.end() };

View File

@ -696,7 +696,7 @@ module
clover::spirv::link_program(const std::vector<module> &modules,
const device &dev, const std::string &opts,
std::string &r_log) {
std::vector<std::string> options = clover::llvm::tokenize(opts);
std::vector<std::string> options = tokenize(opts);
bool create_library = false;

View File

@ -24,6 +24,7 @@
#define CLOVER_UTIL_ALGORITHM_HPP
#include <algorithm>
#include <sstream>
#include <stdexcept>
#include "util/range.hpp"
@ -214,6 +215,53 @@ namespace clover {
r.erase(i, e);
}
///
/// Build a vector of string from a space separated string
/// quoted parts content is preserved and unquoted
///
inline std::vector<std::string>
tokenize(const std::string &s) {
std::vector<std::string> ss;
std::ostringstream oss;
// OpenCL programs can pass a quoted argument, most frequently the
// include path. This is useful so that path containing spaces is
// treated as a single argument instead of being split by the spaces.
// Additionally, the argument should also be unquoted before being
// passed to the compiler. We avoid using std::string::replace here to
// remove quotes, as the single and double quote characters can be a
// part of the file name.
bool escape_next = false;
bool in_quote_double = false;
bool in_quote_single = false;
for (auto c : s) {
if (escape_next) {
oss.put(c);
escape_next = false;
} else if (c == '\\') {
escape_next = true;
} else if (c == '"' && !in_quote_single) {
in_quote_double = !in_quote_double;
} else if (c == '\'' && !in_quote_double) {
in_quote_single = !in_quote_single;
} else if (c != ' ' || in_quote_single || in_quote_double) {
oss.put(c);
} else if (oss.tellp() > 0) {
ss.emplace_back(oss.str());
oss.str("");
}
}
if (oss.tellp() > 0)
ss.emplace_back(oss.str());
if (in_quote_double || in_quote_single)
throw invalid_build_options_error();
return ss;
}
///
/// Build a \a sep separated string from a vector of T
///