Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
SIP_parser_with_std_regex_need_help_to_improve_it.cc
//http://rextester.com/l/cpp_online_compiler_gcc // http://coliru.stacked-crooked.com/ // std::regex RFC 3261 SIP: Session Initiation Protocol #include <iostream> #include <string> #include <regex> int main () { std::string alpha("[a-zA-Z]"); std::string digit("[0-9]"); std::string hexdig("[0-9a-fA-F]"); std::string alphanum(alpha + "|" + digit); std::string reserved("[;\\/\\?\\:@&=\\+\\$\\,]"); std::string mark("[\\-_\\.!~\\*'\\(\\)]"); std::string unreserved(alphanum + "|" + mark); std::string escaped("%" + hexdig + "{2}"); std::string sp("\\s"); std::string htab("\t"); std::string wsp("[" + sp + "\t\v\f]"); std::string crlf("[\r\n]"); std::string lws(wsp + "*" + crlf + "?" + wsp + "+"); std::string sws("[" + lws + "]?"); std::string hcolon("[" + sp + "\t]*:" + sws); std::string utf8_cont("[\x80-\xBF]"); std::string utf8_nonascii( "[\xC0-\xDF]" + utf8_cont + "|[\xE0-\xEF]" + utf8_cont + "{2}" + "|[\xF0-\xF7]" + utf8_cont + "{3}" + "|[\xF8-\xFB]" + utf8_cont + "{4}" + "|[\xFC-\xFD]" + utf8_cont + "{5}"); std::string text_utf8char("[\x21-\x7E]|" + utf8_nonascii); std::string text_utf8_trim(text_utf8char + "+[" + lws +"*" + text_utf8char +"]*"); std::string lhex("[0-9a-f]"); std::string token("(" + alphanum + "|\\-|\\.|!|%|\\*|_|\\+|`|'|~)+"); std::string token_not_dot("(" + alphanum + "\\-!%\\*_\\+`'~)+"); // TODO Not used std::string separator("[\\(\\)/<>@,;\\:\\\\\"/\\[\\]\\?=\\{\\}\\s\t]"); std::string word("(" + alphanum + "|\\-|\\.|!|%|\\*|_|\\+|`|'|~|\\(|\\)|<|>|\\:|\\\|\"|\\[|\\]|\\?|=|\\{|\\})+"); std::string star(sws + "*" + sws); std::string slash(sws + "/" + sws); std::string sequal(sws + "=" + sws); std::string lparen(sws + "*(" + sws); std::string rparen(sws + ")" + sws); std::string raquot(sws + ">" + sws); std::string laquot(sws + "<" + sws); std::string comma(sws + "," + sws); std::string semi(sws + ";" + sws); std::string colon(sws + ":" + sws); std::string ldquot(sws + "\""); std::string rdquot("\"" + sws); std::string ctext("[\x21-\x27]|[\x2A-\x5B]|[\x5D-\x7E]|" + utf8_nonascii + "|" + lws); std::string quoted_pair("\\[\x00-\x09]|[\x0B-\x0C]|[\x0E-\x7F]"); std::string comment(lparen + "(" + ctext + "|" + quoted_pair + ")*" + rparen); std::string qdtext(lws + "|[\x00-\x09]|[\x0B-\x0C]|[\x0E-\x7F]"); std::string quoted_string(sws + "\"(" + qdtext + "|" + quoted_pair + ")*\""); std::string port(digit + "+"); std::string hex4(hexdig + "{1,4}"); std::string hexseq(hex4 + "(:hex4)*"); std::string hexpart(hexseq + "|" + hexseq + "::" + hexseq + "?|::" + hexseq + "?"); std::string ipv4address(digit + "{1,3}\\." + digit + "{1,3}\\." + digit + "{1,3}\\." + digit + "{1,3}"); std::string ipv6address(hexpart + "+(:" + ipv4address + ")?"); std::string ipv6reference("\\[" + ipv6address + "\\]"); std::string toplabel("(" + alpha + "|" + alpha + "(" + alphanum + "|\\-)*" + alphanum + ")+"); std::string domainlabel("(" + alphanum + "|" + alphanum + "(" + alphanum + "|\\-)*" + alphanum + ")+"); std::string hostname("(" + domainlabel + "\\.)*" + toplabel + "\\.?"); std::string host(hostname + "|" + ipv4address + "|" + ipv6reference); std::string hostport("(" + host + ")(:" + port + ")?"); std::string user_unreserved("[&=\\+\\$,;\\?\\/]"); std::string password("(" + unreserved + "|" + escaped + "|[&=\\+\\$,])*"); std::string user("(" + unreserved + "|" + escaped + "|" + user_unreserved + ")+"); std::string telephone_digit("[" + hexdig + "|[\\)]?|[\\*]?|[\\-]?|[\\.]?|[#]?]"); std::string telephone_subscriber("[\\+]?([" + telephone_digit + "|[\\(]?" + telephone_digit + "]*)"); std::string userinfo("(" + user + "|" + telephone_subscriber + ")(:" + password + ")?@"); std::string hnv_unreserved("[\\[\\]\\/\\?\\:\\+\\$]"); std::string hname("(" + hnv_unreserved + "|" + unreserved + "|" + escaped + ")+"); std::string hvalue("(" + hnv_unreserved + "|" + unreserved + "|" + escaped + ")*"); std::string header(hname + "=" + hvalue); std::string headers("\\?" + header + "(&" + header + ")*"); std::string param_unreserved("[\\[\\]\\/\\:&\\+\\$]"); std::string paramchar("(" + param_unreserved + "|" + unreserved + "|" + escaped + ")"); std::string pname("(" + paramchar + ")+"); std::string pvalue("(" + paramchar + ")+"); std::string other_param(pname + "(=" + pvalue + ")?"); std::string method("INVITE|ACK|OPTIONS|BYE|CANCEL|REGISTER|MESSAGE|PRACK|" + token); std::string method_param("method=" + method); std::string ttl(digit + "{1,3}"); std::string ttl_param("ttl=(" + ttl + ")"); std::string maddr_param("maddr=" + host); std::string lr_param("lr"); std::string transport_param("transport=(udp|tcp|sctp|tls)"); std::string other_transport(token); std::string other_user(token); std::string user_param("user=phone|ip|" + other_user); std::string uri_parameter(transport_param + "|" + user_param + "|" + method_param + "|" + ttl_param + "|" + maddr_param + "|" + lr_param + "|" + other_\ param); std::string uri_parameters("(;" + uri_parameter + ")*"); std::string sip_uri("sip:(" + userinfo + ")?" + hostport + uri_parameters + "(" + headers + ")?"); std::string sips_uri("sips:(" + userinfo + ")?" + hostport + uri_parameters + "(" + headers + ")?"); std::string reason_phrase("(" + reserved + "|" + unreserved + "|" + escaped + "|" + utf8_nonascii + "|" + utf8_cont + "|" + sp + "|" + htab + ")*"); std::string sip_version("SIP/" + digit + "+\\." + digit + "+"); std::string request_uri(sip_uri + "|" + sips_uri); // TODO To be continued std::string request_line("(" + method + ")" + sp + "(" + request_uri + ")" + sp + "(" + sip_version + ")" + crlf); // RFC: method + sp + request_uri + sp + sip_version + crlf); std::string status_line("(" + sip_version + ")" + sp + "(" + digit + "{3})" + sp + "(" + reason_phrase + ")" + crlf); std::string message_header( "Accept" "|Accept-Encoding" "|Accept-Language" "|Alert-Info" "|Allow" "|Authentication-Info" "|Authorization" "|Call-ID" "|Call-Info" "|Contact" "|Content-Disposition" "|Content-Encoding" "|Content-Language" "|Content-Length" "|Content-Type" "|CSeq" "|Date" "|Error-Info" "|Expires" "|From" "|In-Reply-To" "|Max-Forwards" "|MIME-Version" "|Min-Expires" "|Organization" "|Priority" "|Proxy-Authenticate" "|Proxy-Authorization" "|Proxy-Require" "|Record-Route" "|Reply-To" "|Require" "|Retry-After" "|Route" "|Server" "|Subject" "|Supported" "|Timestamp" "|To" "|Unsupported" "|User-Agent" "|Via" "|Warning" "|WWW-Authenticate" "|extension-header" ); std::string message_body("[[0-9a-fA-F]{2}]*"); std::string request(request_line + "|[" + message_header + "]*" + crlf + "[" + message_body + "]?"); std::string response(status_line + "|[" + message_header + "]*" + crlf + "[" + message_body + "]?"); std::string sip_message("[" + request + "]|[" + response + "]"); //std::string s ("£$% subject sugfw pud adsd969612npsdjsj "); //std::regex e (alphanum, std::regex::ECMAScript); //std::regex e (std::string("(" + alphanum + ")+"), std::regex::ECMAScrip | std::regex_constants::extendedt); // //std::string s (" asdf;iasgy "); //std::regex e (reserved, std::regex::ECMAScript | std::regex_constants::extended); // //std::string s (" asdf~ia*sgy sdv-sdb "); //std::regex e (mark, std::regex::ECMAScript | std::regex_constants::extended); // //std::string s ("asdf ia\tskdj\vldhvvqh;\fewveqr;v"); //std::regex e (wsp, std::regex::ECMAScript | std::regex_constants::extended); // /*std::string s ("asdf ia\tskdj\vldh\r\nvvqh;\fewveqr;v"); std::regex e ("[" + wsp + "*" + crlf + "?]", std::regex::ECMAScript | std::regex_constants::extended); // std::sregex_iterator begin = std::sregex_iterator(s.begin(), s.end(), e); std::sregex_iterator end = std::sregex_iterator(); std::cout << "string object with " << std::distance(begin, end) << " matches\n"; std::cout << "the matches were: " << std::endl; for (std::sregex_iterator it = begin; it != end; ++it) { std::cout << "[" << it->str() << "] at position " << it->position() << std::endl; }*/ //std::string s ("dflusydalyu \t \r\n\tskdjbsdnvv"); //std::regex e ("(" + wsp + "*" + crlf + "?" + wsp + "+)", std::regex::ECMAScript | std::regex_constants::extended); // //std::string s ("dflusydalyu \t \r\n\tskdjbsdnvv"); //std::regex e (lws, std::regex::ECMAScript | std::regex_constants::extended); // //std::string s ("dflusydalyu \t \r\n\tskdjbsdnvv"); //std::regex e (sws, std::regex::ECMAScript | std::regex_constants::extended); // //std::locale old; //std::locale::global(std::locale("en_US.UTF-8")); //std::string s (" dads "); //std::regex e ("[\x21-\x8A]+", std::regex::ECMAScript | std::regex_constants::extended); // //std::string s (" fewe35f25wef wef-wec.com (owewec) <AYDG7394SDd.woefu-wekff> "); //std::regex e (token, std::regex::ECMAScript); // //std::string s (" voip.example.com:600 "); //std::regex e ("(" + toplabel + ")+", std::regex::ECMAScript | std::regex_constants::extended); // //std::string s (" [27ff:fe:b68:192.168.1.2] "); //std::string s (" [2134::1234:4567:24:1236:2444:6] "); //std::regex e (hexseq, std::regex::ECMAScript | std::regex_constants::extended); // //std::string s (" voip.example.com:600 "); //std::regex e (hostname, std::regex::ECMAScript | std::regex_constants::extended); // //std::string s (" voip.example.com:600 "); //std::string s (" 192.168.1.2:600 "); //std::regex e (hostport, std::regex::ECMAScript | std::regex_constants::extended); // //std::string s (" 123$~€567& "); //std::regex e (password, std::regex::ECMAScript | std::regex_constants::extended); // //std::string s (" john_doe "); //std::regex e (user, std::regex::ECMAScript | std::regex_constants::extended); // //std::string s (" +3349487674# "); //std::regex e ("(" + telephone_digit + ")+", std::regex::ECMAScript | std::regex_constants::extended); // //std::string s (" john_doe:123$~567&@ "); //std::regex e ("(" + user + ")(:" + password + ")?@", std::regex::ECMAScript | std::regex_constants::extended); //std::string s (" user1:123$~567&@ "); //std::regex e ("(" + user + ")(:" + password + ")?@", std::regex::ECMAScript | std::regex_constants::extended); // //std::string s (" transport=tcp "); //std::string s (" transport=udp "); //std::regex e (transport_param, std::regex::ECMAScript | std::regex_constants::extended); // //std::string s (" method=INVITE "); //std::regex e (method_param, std::regex::ECMAScript | std::regex_constants::extended); // //std::string s (" ttl=25 "); //std::regex e (ttl_param, std::regex::ECMAScript | std::regex_constants::extended); // //std::string s (";icid-generated-at=192.1.1.10;orig-ioi=testetsi.org"); //std::regex e (uri_parameters, std::regex::ECMAScript | std::regex_constants::extended); // //std::string s (" sip:123@192.1.2.3 "); //std::string s(" sip:user1@etsi.org "); //std::regex e (sip_uri, std::regex::ECMAScript | std::regex_constants::extended); // //std::string s(" sip:user1@etsi.org "); //std::regex e (request_uri, std::regex::ECMAScript | std::regex_constants::extended); // //std::string s(" MESSAGE sip:user1@etsi.org SIP/2.0\r\n "); //std::regex e (sip_version, std::regex::ECMAScript | std::regex_constants::extended); // std::string s(" MESSAGE sip:user1@etsi.org SIP/2.0\r\n "); std::regex e (request_line, std::regex::ECMAScript); //std::regex e ("(" + method + ")" + sp + "(" + request_uri + ")" + sp + "(" + sip_version + ")" + crlf, std::regex::ECMAScript | std::regex_constants::extended); // //std::string s(" SIP/2.0 100 Trying\r\n "); //std::regex e (status_line, std::regex::ECMAScript | std::regex_constants::extended); //std::regex e ("(" + method + ")" + sp + "(" + request_uri + ")" + sp + "(" + sip_version + ")" + crlf, std::regex::ECMAScript | std::regex_constants::extended); // std::smatch matches; if(std::regex_search(s, matches, e)) { std::cout << "Match found\n"; for (size_t i = 0; i < matches.size(); ++i) { if (matches[i].str().length() != 0) { std::cout << i << ": '" << matches[i].str() << "'" << std::endl; } } } else { std::cout << "Match not found\n"; } std::sregex_iterator begin = std::sregex_iterator(s.begin(), s.end(), e); std::sregex_iterator end = std::sregex_iterator(); //std::locale::global(old); std::cout << "string object with " << std::distance(begin, end) << " matches\n"; std::cout << "the matches were: " << std::endl; for (std::sregex_iterator it = begin; it != end; ++it) { std::cout << "[" << it->str() << "] at position " << it->position() << " with size [" << it->size() << "]" << std::endl; } std::cout << std::endl; return 0; }
run
|
edit
|
history
|
help
0
顺序表的实现——静态分配
pointconcat
NonparaH
said
Bitwise - Check power of 2 or not
Beadandó
pangram
Odwrócone podciągi
C++ Solar eclipse program(Shoushi integrated) 1644 - 1785 from Ideone( Date: August 14, 2014 )
spiral traversal of a matrix