Boost::string or Boost::regex

Source: Internet
Author: User

Sometimes the following questions are encountered when writing code

If you have a text file that includes content similar to the C language, there is a line in the following format:

Layout (local_size_x = A,local_size_y = b, local_size_z = c) in;

When the portion marked with blue (layout, local_size_x, local_size_y, Local_size_z, in) is keyword, the italic part (A, B, c) is a number with the data type unsigned int, write a function To extract the value of a, B, and C from the file. Where the file name is called the input parameter, the return value of the function is the extracted a,b,c three values.

Like what. For example one of the following text files, the output expected by the program is (16, 16, 1)

#version 430 Core

Layout (local_size_x = +, local_size_y = 1) in;

void Main (void)

{

Imagestore (Utexture, IVEC2 (Gl_globalinvocationid.xy), VEC4 (0, 0, 0, 0));

}

When analyzing text, you need to be aware of the following points:

A. We have a text that has only one layout statement for defining local_size_x. Local_size_y and Local_size_z. And there is no error in the syntax of this statement.

B. The user can stare out some code by//or/*...*/method.

C. Users are able to define macros using # define;

D. local_size_x,local_size_y. The default value for Local_size_z is 1, which is defined by the local_size_x and local_size_y. Local_size_z can be omitted, or local_size_y and local_size_z can be omitted if the local_size_x is defined.

Like what. The return value for parsing such as the following text should be (32, 16, 1).

#version 430 Core

#define Local_x32

Layout (local_size_x = +, local_size_y = 2) in;

Layout (local_size_x = local_x, local_size_y = +) in;

void Main (void)

{

Imagestore (Utexture, IVEC2 (Gl_globalinvocationid.xy), VEC4 (0, 0, 0, 0));

}

wrote a code with boost::string.

#include <iostream> #include <fstream> #include <map> #include <vector> #include <boost/ tuple/tuple.hpp> #include <boost/lexical_cast.hpp> #include <boost/algorithm/string.hpp> #include <boost/utility/string_ref.hpp>class ctest{public:ctest (int vx = 1, int vY = 1, int vZ = 1): m_x (VX), m_y (VY), M_z (v Z) {}~ctest () {}//*********************************************************************************//function: void ParseText (const char* vfilename) {std::vector<std::string> strvec;preprocess (Vfilename, STRVEC);/*for (int i=0; I<strvec.size (); ++i) {std::cout << strvec[i] << Std::endl;} */processlayout (Strvec);} Function:void Printmember ( ) const{std::cout << m_x << "<< m_y <<" << m_z << Std::endl;} Function:boost::tuples:: TupLe<int, int, int> getValue () Const{return boost::make_tuple (m_x, m_y, m_z);} Private://*********************************************************************************//function:void Preprocess (const char* vfilename, std::vector<std::string>& vostrvec) {std::ifstream Ifs (vFileName); IFS) {std::cout << "Can not open the file" << vfilename << std::endl;exit (-1);} std::string linestr;while (Getline (Ifs, linestr)) {if (Linestr.find ("//")! = Std::string::npos) {Std::string::iterator End = Linestr.begin () +linestr.find ("//"), if (linestr.begin () = End) Vostrvec.push_back (std::string (), End));} else if (Linestr.find ("/*")! = Std::string::npos) {while (Getline (Ifs, linestr)) {if (Linestr.find ("* *")! = std::string:: NPOs) break;}} else if (linestr.size () > 0) vostrvec.push_back (LINESTR);} Ifs.close ();} Function:void Processlayout (const STD::VECTOR&LT;STD::STRING&GT;&Amp Vstrvec) {std::map<std::string, int> datamap;for (unsigned int i=0; i<vstrvec.size (); ++i) {if (vstrvec[i].find ("#define")! = Std::string::npos) processdefine (Vstrvec[i], DataMap); else if (Vstrvec[i].find ("layout")! = std::string :: NPOs) Processlayout (Vstrvec[i], DataMap);}} Function:void Processdefine (const std::string& vsorcestring, std::map<std::string, int>& voDataMap) {typedef boost:: Split_iterator<std::string::const_iterator> Split_string_itearor; Split_string_itearor BGN, End;std::vector<std::string> strvec;for (BGN = Boost::algorithm::make_split_iterator (Vsorcestring, Boost::algorithm::token_finder (boost::is_any_of (""))); Bgn! = End; ++BGN) {if (*BGN). Size () >0) Strvec.push_back (std::string ((*BGN). Begin (), (*BGN). End ()));} for (int i=0; i<strvec.size (); ++i)//{//std::cout << strvec[i] << std::endl;//}vodatamap[strvec[1]] = Boost::lexical_cast<int> (strvec[2]);} void Processlayout (const std::string& vsorcestring, std::map<std::string, int>& vDataMap) {typedef boost ::split_iterator<std::string::const_iterator> Split_string_itearor; Split_string_itearor BGN, End;std::vector<std::string> strvec;for (BGN = Boost::algorithm::make_split_iterator (Vsorcestring, Boost::algorithm::token_finder (Boost::is_any_of ("(,); ="))); Bgn! = End; ++BGN) {if (*BGN). Size () >0) Strvec.push_back (std::string ((*BGN). Begin (), (*BGN). End ()));} /*for (int i=0; i<strvec.size (); ++i) {std::cout << "[" << Strvec[i] << "]";} Std::cout << std::endl;*/if (strvec.size () >= 4) {if (strvec[2][0] >= ' 0 ' && strvec[2][1] <= ' 9 ') {m_ X = boost::lexical_cast<int> (strvec[2]);} Else{if (Vdatamap.find (strvec[2]) = = Vdatamap.end ()) {std::cout << "somethind if wrong \ n"; exit (1);} m_x = vdatamap[strvec[2]];}} if (Strvec.size () >= 6) {if (strvec[4][0] >= ' 0 ' && strvec[4][0] <= ' 9 ') {m_y = boost::lexical_cast<int> (Strvec[4]);} Else{if (Vdatamap.find (strvec[4]) = = Vdatamap.end ()) {std::cout << "somethind if wrong \ n"; exit (1);} M_y = Vdatamap[strvec[4]];}} if (Strvec.size () >= 8) {if (strvec[6][0] >= ' 0 ' && strvec[6][1] <= ' 9 ') {m_z = boost::lexical_cast<int& gt; (Strvec[6]);} Else{if (Vdatamap.find (strvec[6]) = = Vdatamap.end ()) {std::cout << "somethind if wrong \ n"; exit (1);} M_z = Vdatamap[strvec[6]];}} Private:int m_x;int m_y;int m_z;}; int main () {CTest Test; Test.parsetext ("test.txt"); test. Printmember (); GetChar (); return 0;}


It's just a question that can be written in Boost::regex.

#include <string> #include <fstream> #include <iostream> #include <boost\regex.hpp> #include <boost\algorithm\string\split.hpp> #include <boost\algorithm\string\regex.hpp> #include <boost\ algorithm\string\classification.hpp>//********************************************************************* function:unsigned int Convertstring2ui (const std::string& vstring {unsigned int value = 0;for (unsigned int i=0; i<vstring.length (); i++) {value = value*10 + vstring.at (i)-' 0 ';} return Value;} //************************************************************************************************************* Function:void readcontentfromfile (const char* vfilename, std::string& vocontent) {Std::ifstream InFile (     Vfilename); char* pcontent = Null;if (InFile) {infile.seekg (0, Infile.end); unsigned int numcharacter = unsigned int (INFILE.TELLG ()); Pcontent = new CHAR[NUMCHARACTER+1];INFILE.SEEKG (0, Std::ios::beg);NT I=0;while (! Infile.eof ()) {if (Infile.read (&pcontent[i], sizeof (char))) i++;} Pcontent[i] = ' + '; vocontent = std::string (pcontent);} Delete[] pcontent;} //************************************************************************************************************* Function:void deletecomments (std::string& viostring) {Boost::regex Commentregex ("(//.*?\\n) | ( /\\*.*? (\\*) +/) viostring = Boost::regex_replace (viostring, Commentregex, "", Boost::regex_constants::match_not_dot_ newline);} //************************************************************************************************************* Function:void Replacemacro (std::string& viostring) {boost::smatch Macrostring;boost::regex MacroRegex ("^# Define (\\s) + ([a-za-z_0-9\\ (\ \)]+) (\\s) + ([a-za-z_0-9\\ (\ \)]+)]; Std::string::const_iterator Start = Viostring.begin (); Std::string::const_iterator End = Viostring.end ();std::vector<std::string> MacroSet, Valueset;while (Boost::regex_search (Start, End, macrostring, Macroregex, Boost::regex_constants::match_not_null|boost::regex_constants::match_not_dot_newline)) {Start = MacroString[0]. Second Macroset.push_back (Macrostring[2].str ()); Valueset.push_back (Macrostring[4].str ());} _assert (macroset.size () = = Valueset.size ()); for (unsigned int i=0; i<macroset.size (); i++) {viostring = Boost::regex_ Replace (viostring, Boost::regex (macroset.at (i)), valueset.at (i));}} //************************************************************************************************************* Function:void dumpnums (const std::string& vcontent, unsigned int& voA, unsigned int& voB, unsigned INT&A mp VOC) {VoA = VoB = VoC = 1;boost::regex Matchregex ("Layout \ \ (local_size_x = ([0-9]+) (, local_size_y = ([0-9]+) (, local_size _z = ([0-9]+)]?)? \) in; "); Boost::smatch Matchstring;boost::regex_search (vcontent, matchstring, matchregex); VoA = Convertstring2ui (MatchString [1].str ()); if (! Matchstring[3].str (). empty ()) {VoB = Convertstring2ui (Matchstring[3].str ()); MATCHSTRING[5]. STR (). empty ()) VoC = Convertstring2ui (Matchstring[5].str ());}} //************************************************************************************************************* Function:void parsefile (const std::string& vfilename, unsigned int& voA, unsigned int& voB, unsigned int & VoC) {std::string content;readcontentfromfile (vfilename.c_str (), content);d eletecomments (content); Replacemacro (content);d umpnums (content, VoA, VoB, VoC);} void Installmemoryleakdetector () {#if defined (DEBUG) | defined (_DEBUG) _crtsetdbgflag (_CRTDBG_ALLOC_MEM_DF | _crtdbg_ LEAK_CHECK_DF);//_crtbreakalloc = 955; #endif}int Main (int argc, char** argv) {installmemoryleakdetector (); _assert ( ARGC >= 2); const std::string FileName (argv[1]), unsigned int A = 0, B = 0, C = 0; Parsefile (FileName, A, B, c), Std::cout << a << "<< B <<" "<< C << Std::endl;retur n 0;}




Boost::string or Boost::regex

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.