C++ (Qt)class parse_param{public: parse_param(); parse_param(const std::string &tokens, const std::string &delims); void add_quote(char bra, char ket); void set_delims(const std::string &delims); void set_tokens(const std::string &tokens); friend class string_parser; private: std::string m_bra_list; std::string m_ket_list; std::string m_tokens; std::string m_delims;};
C++ (Qt)enum class split_behavior { keep_empty_parts, skip_empty_parts }; class string_parser{public: typedef std::string::size_type size_type; string_parser(const parse_param ¶m); size_type next_token(const std::string &str, size_type pos = 0, split_behavior behavior = split_behavior::skip_empty_parts) const; size_type next_quote(const std::string &str, size_type pos = 0, split_behavior behavior = split_behavior::skip_empty_parts) const; size_type matched_length() const; char token() const; std::string result() const; static std::string trim(const std::string &str, const std::string &delim = " \t\n"); static size_type npos(); private: const parse_param &m_param; mutable char m_token; mutable size_type m_mathed_length; mutable std::string m_result; size_type find_first_token(const std::string &str, size_type pos = 0) const; static size_type skip_quote(const std::string &str, char bra, char ket, size_type pos = 0);};
C++ (Qt)std::string str = "a, b, , c , {d,e {f}}";parse_param param(",.", " \t");param.add_quote('{', '}'); string_parser parser(param);size_t pos = 0;while ((pos = parser.next_token(str, pos, mbib::split_behavior::skip_empty_parts)) != std::string::npos) { std::cout << std::left << parser.result() << std::setw(10) << parser.token() << std::endl; pos += parser.matched_length();}
Bash"a"",""b"",""c"",""{d,e {f}}"""
C++ (Qt)std::string str = "a, b, , c , {d,e {f}}";parse_param param(",.", " \t");param.add_quote('{', '}'); string_parser parser(param);size_t pos = 0;while ((pos = parser.next_token(str, pos, mbib::split_behavior::keep_empty_parts)) != std::string::npos) { std::cout << std::left << parser.result() << std::setw(10) << parser.token() << std::endl; pos += parser.matched_length();}
Bash"a"",""b"","""",""c"",""{d,e {f}}"""
C++ (Qt)std::string str = "{a, b},{}{} , c , {d,e {f}}";parse_param param(",.", " \t");param.add_quote('{', '}'); string_parser parser(param);size_t pos = 0;while ((pos = parser.next_token(str, pos, mbib::split_behavior::skip_empty_parts)) != std::string::npos) { std::cout << std::left << parser.result() << std::endl; pos += parser.matched_length();}
Bash"a, b""d,e {f}"
C++ (Qt)#include <iostream>#include <ctime>#include "string_parser.h"#include "QStringParser.h"#include <QDebug> int main(){ QString src="Westerholt, K. {and Sprungmann, D.} and Zabel, H. and Brucas, R. and Hj\"orvarsson, B. and Tikhonov, D. A. and Garifullin, I. A."; std::string std_src = src.toStdString(); const long N = 1000000; parse_param param(",.", " \t"); param.add_quote('{', '}'); string_parser parser(param); clock_t tStart = clock(); for (long i = 0; i < N; ++i) { size_t pos = 0; while ((pos = parser.next_token(std_src, pos, split_behavior::keep_empty_parts)) != std::string::npos) { //std::cout << parser.result() << std::endl << parser.token() << std::endl; pos += parser.matched_length(); } } std::cout << "m_ax: time (sec) = " << (float)(clock() - tStart) / CLOCKS_PER_SEC << std::endl; CParseParam cparam(" \t", ",."); cparam.AddQuote(100, "{", "}"); QStringRef temp; tStart = clock(); for (long i = 0; i < N; ++i) { QStringParser cparser(src, cparam); while(cparser.NextToken(temp)) {} } std::cout << "igors: time (sec) = " << (float)(clock() - tStart) / CLOCKS_PER_SEC << std::endl; return 0;}
Bashm_ax: time (sec) = 11.72igors: time (sec) = 8.93
C++ (Qt) QString src = "\"test, (\",test)"; CParseParam cparam(" \t", ",."); cparam.AddQuote(10, "(", ")"); cparam.AddQuote(10, "\"", "\""); QStringRef temp; QStringParser cparser(src, cparam); while(cparser.NextToken(temp)) { qDebug() << temp; }
Bash""test, (",test)"
Bash""test, ("" "," "test)"
C++ (Qt)QString src = "[test(],test)]";