[lib] format

This commit is contained in:
tangxifan 2023-01-02 12:41:24 -08:00
parent 994402ec9a
commit 77b64a21d4
2 changed files with 24 additions and 23 deletions

View File

@ -99,20 +99,21 @@ std::vector<size_t> StringToken::find_positions(const char& delim) const {
return anchors; return anchors;
} }
std::vector<std::string> StringToken::split_by_chunks(const char& chunk_delim, const bool& split_odd_chunk) const { std::vector<std::string> StringToken::split_by_chunks(
const char& chunk_delim, const bool& split_odd_chunk) const {
size_t chunk_idx_mod = 0; size_t chunk_idx_mod = 0;
if (split_odd_chunk) { if (split_odd_chunk) {
chunk_idx_mod = 1; chunk_idx_mod = 1;
} }
std::vector<std::string> tokens; std::vector<std::string> tokens;
/* There are pairs of quotes, identify the chunk which should be split*/ /* There are pairs of quotes, identify the chunk which should be split*/
std::vector<std::string> token_chunks = split(chunk_delim); std::vector<std::string> token_chunks = split(chunk_delim);
for (size_t ichunk = 0; ichunk < token_chunks.size(); ichunk++) { for (size_t ichunk = 0; ichunk < token_chunks.size(); ichunk++) {
/* Chunk with even index (including the first) is always out of two quote -> Split! /* Chunk with even index (including the first) is always out of two quote ->
* Chunk with odd index is always between two quotes -> Do not split! * Split! Chunk with odd index is always between two quotes -> Do not split!
*/ */
if (ichunk % 2 == chunk_idx_mod) { if (ichunk % 2 == chunk_idx_mod) {
StringToken chunk_tokenizer(token_chunks[ichunk]); StringToken chunk_tokenizer(token_chunks[ichunk]);
for (std::string curr_token : chunk_tokenizer.split()) { for (std::string curr_token : chunk_tokenizer.split()) {
tokens.push_back(curr_token); tokens.push_back(curr_token);
} }

View File

@ -27,28 +27,28 @@ class StringToken {
std::vector<std::string> split(const char* delim) const; std::vector<std::string> split(const char* delim) const;
std::vector<std::string> split(const std::vector<char>& delim) const; std::vector<std::string> split(const std::vector<char>& delim) const;
std::vector<std::string> split(); std::vector<std::string> split();
/** @brief Find the position (i-th charactor) in a string for a given delimiter, it will return a list of positions /** @brief Find the position (i-th charactor) in a string for a given
* For example, to find the position of all quotes (") in a string: * delimiter, it will return a list of positions For example, to find the
* "we" are good * position of all quotes (") in a string: "we" are good The following code is
* The following code is suggested: * suggested: StringToken tokenizer("\"we\" are good"); std::vector<size_t>
* StringToken tokenizer("\"we\" are good"); * anchors = tokenizer.find_positions('\"') The following vector will be
* std::vector<size_t> anchors = tokenizer.find_positions('\"') * returned: [0, 3] */
* The following vector will be returned:
* [0, 3] */
std::vector<size_t> find_positions(const char& delim) const; std::vector<size_t> find_positions(const char& delim) const;
/** @brief split the string for each chunk. This is useful where there are chunks of substring should not be splitted by the given delimiter /** @brief split the string for each chunk. This is useful where there are
* For example, to split the string with quotes (") in a string: * chunks of substring should not be splitted by the given delimiter For
* source "cmdA --opt1 val1;cmdB --opt2 val2" --verbose * example, to split the string with quotes (") in a string: source "cmdA
* where the string between the two quotes should not be splitted * --opt1 val1;cmdB --opt2 val2" --verbose where the string between the two
* The following code is suggested: * quotes should not be splitted The following code is suggested: StringToken
* StringToken tokenizer("source \"cmdA --opt1 val1;cmdB --opt2 val2\" --verbose"); * tokenizer("source \"cmdA --opt1 val1;cmdB --opt2 val2\" --verbose");
* std::vector<std::string> tokenizer.split_by_chunks('\"', true); * std::vector<std::string> tokenizer.split_by_chunks('\"', true);
* The following vector will be returned: * The following vector will be returned:
* ["source" "cmdA --opt1 val1;cmdB --opt2 val2" "--verbose"] * ["source" "cmdA --opt1 val1;cmdB --opt2 val2" "--verbose"]
* *
* .. note:: The option ``split_odd_chunk`` is useful when the chunk delimiter appears at the beginning of the string. * .. note:: The option ``split_odd_chunk`` is useful when the chunk delimiter
* appears at the beginning of the string.
*/ */
std::vector<std::string> split_by_chunks(const char& chunk_delim, const bool& split_odd_chunk = false) const; std::vector<std::string> split_by_chunks(
const char& chunk_delim, const bool& split_odd_chunk = false) const;
public: /* Public Mutators */ public: /* Public Mutators */
void set_data(const std::string& data); void set_data(const std::string& data);