diff options
Diffstat (limited to 'src/boost/libs/regex/example/snippets')
20 files changed, 2289 insertions, 0 deletions
diff --git a/src/boost/libs/regex/example/snippets/captures_example.cpp b/src/boost/libs/regex/example/snippets/captures_example.cpp new file mode 100644 index 00000000..d1ff1e9f --- /dev/null +++ b/src/boost/libs/regex/example/snippets/captures_example.cpp @@ -0,0 +1,68 @@ +/* + * + * Copyright (c) 2003-2004 + * John Maddock + * + * Use, modification and distribution are subject to the + * Boost Software License, Version 1.0. (See accompanying file + * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + * + */ + + /* + * LOCATION: see http://www.boost.org for most recent version. + * FILE captures_example.cpp + * VERSION see <boost/version.hpp> + * DESCRIPTION: Demonstrate the behaviour of captures. + */ + +#include <boost/regex.hpp> +#include <iostream> + + +void print_captures(const std::string& regx, const std::string& text) +{ + boost::regex e(regx); + boost::smatch what; + std::cout << "Expression: \"" << regx << "\"\n"; + std::cout << "Text: \"" << text << "\"\n"; + if(boost::regex_match(text, what, e, boost::match_extra)) + { + unsigned i, j; + std::cout << "** Match found **\n Sub-Expressions:\n"; + for(i = 0; i < what.size(); ++i) + std::cout << " $" << i << " = \"" << what[i] << "\"\n"; + std::cout << " Captures:\n"; + for(i = 0; i < what.size(); ++i) + { + std::cout << " $" << i << " = {"; + for(j = 0; j < what.captures(i).size(); ++j) + { + if(j) + std::cout << ", "; + else + std::cout << " "; + std::cout << "\"" << what.captures(i)[j] << "\""; + } + std::cout << " }\n"; + } + } + else + { + std::cout << "** No Match found **\n"; + } +} + +int main(int , char* []) +{ + print_captures("(([[:lower:]]+)|([[:upper:]]+))+", "aBBcccDDDDDeeeeeeee"); + print_captures("a(b+|((c)*))+d", "abd"); + print_captures("(.*)bar|(.*)bah", "abcbar"); + print_captures("(.*)bar|(.*)bah", "abcbah"); + print_captures("^(?:(\\w+)|(?>\\W+))*$", "now is the time for all good men to come to the aid of the party"); + print_captures("^(?>(\\w+)\\W*)*$", "now is the time for all good men to come to the aid of the party"); + print_captures("^(\\w+)\\W+(?>(\\w+)\\W+)*(\\w+)$", "now is the time for all good men to come to the aid of the party"); + print_captures("^(\\w+)\\W+(?>(\\w+)\\W+(?:(\\w+)\\W+){0,2})*(\\w+)$", "now is the time for all good men to come to the aid of the party"); + return 0; +} + diff --git a/src/boost/libs/regex/example/snippets/credit_card_example.cpp b/src/boost/libs/regex/example/snippets/credit_card_example.cpp new file mode 100644 index 00000000..1620d8cc --- /dev/null +++ b/src/boost/libs/regex/example/snippets/credit_card_example.cpp @@ -0,0 +1,67 @@ +/* + * + * Copyright (c) 1998-2002 + * John Maddock + * + * Use, modification and distribution are subject to the + * Boost Software License, Version 1.0. (See accompanying file + * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + * + */ + + /* + * LOCATION: see http://www.boost.org for most recent version. + * FILE credit_card_example.cpp + * VERSION see <boost/version.hpp> + * DESCRIPTION: Credit card number formatting code. + */ + +#include <boost/regex.hpp> +#include <string> + +bool validate_card_format(const std::string& s) +{ + static const boost::regex e("(\\d{4}[- ]){3}\\d{4}"); + return boost::regex_match(s, e); +} + +const boost::regex e("\\A(\\d{3,4})[- ]?(\\d{4})[- ]?(\\d{4})[- ]?(\\d{4})\\z"); +const std::string machine_format("\\1\\2\\3\\4"); +const std::string human_format("\\1-\\2-\\3-\\4"); + +std::string machine_readable_card_number(const std::string& s) +{ + return boost::regex_replace(s, e, machine_format, boost::match_default | boost::format_sed); +} + +std::string human_readable_card_number(const std::string& s) +{ + return boost::regex_replace(s, e, human_format, boost::match_default | boost::format_sed); +} + +#include <iostream> +using namespace std; + +int main() +{ + string s[4] = { "0000111122223333", "0000 1111 2222 3333", + "0000-1111-2222-3333", "000-1111-2222-3333", }; + int i; + for(i = 0; i < 4; ++i) + { + cout << "validate_card_format(\"" << s[i] << "\") returned " << validate_card_format(s[i]) << endl; + } + for(i = 0; i < 4; ++i) + { + cout << "machine_readable_card_number(\"" << s[i] << "\") returned " << machine_readable_card_number(s[i]) << endl; + } + for(i = 0; i < 4; ++i) + { + cout << "human_readable_card_number(\"" << s[i] << "\") returned " << human_readable_card_number(s[i]) << endl; + } + return 0; +} + + + + diff --git a/src/boost/libs/regex/example/snippets/icu_example.cpp b/src/boost/libs/regex/example/snippets/icu_example.cpp new file mode 100644 index 00000000..6d1fc3be --- /dev/null +++ b/src/boost/libs/regex/example/snippets/icu_example.cpp @@ -0,0 +1,188 @@ +/* + * + * Copyright (c) 2004 + * John Maddock + * + * Use, modification and distribution are subject to the + * Boost Software License, Version 1.0. (See accompanying file + * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + * + */ + + /* + * LOCATION: see http://www.boost.org for most recent version. + * FILE mfc_example.cpp + * VERSION see <boost/version.hpp> + * DESCRIPTION: examples of using Boost.Regex with MFC and ATL string types. + */ + +#include <boost/regex/config.hpp> + +#ifdef BOOST_HAS_ICU + +#include <boost/regex/icu.hpp> +#include <iostream> +#include <assert.h> + +// +// Find out if *password* meets our password requirements, +// as defined by the regular expression *requirements*. +// +bool is_valid_password(const U_NAMESPACE_QUALIFIER UnicodeString& password, const U_NAMESPACE_QUALIFIER UnicodeString& requirements) +{ + return boost::u32regex_match(password, boost::make_u32regex(requirements)); +} + +// +// Extract filename part of a path from a UTF-8 encoded std::string and return the result +// as another std::string: +// +std::string get_filename(const std::string& path) +{ + boost::u32regex r = boost::make_u32regex("(?:\\A|.*\\\\)([^\\\\]+)"); + boost::smatch what; + if(boost::u32regex_match(path, what, r)) + { + // extract $1 as a std::string: + return what.str(1); + } + else + { + throw std::runtime_error("Invalid pathname"); + } +} + +U_NAMESPACE_QUALIFIER UnicodeString extract_greek(const U_NAMESPACE_QUALIFIER UnicodeString& text) +{ + // searches through some UTF-16 encoded text for a block encoded in Greek, + // this expression is imperfect, but the best we can do for now - searching + // for specific scripts is actually pretty hard to do right. + boost::u32regex r = boost::make_u32regex(L"[\\x{370}-\\x{3FF}](?:[^[:L*:]]|[\\x{370}-\\x{3FF}])*"); + boost::u16match what; + if(boost::u32regex_search(text, what, r)) + { + // extract $0 as a UnicodeString: + return U_NAMESPACE_QUALIFIER UnicodeString(what[0].first, what.length(0)); + } + else + { + throw std::runtime_error("No Greek found!"); + } +} + +void enumerate_currencies(const std::string& text) +{ + // enumerate and print all the currency symbols, along + // with any associated numeric values: + const char* re = + "([[:Sc:]][[:Cf:][:Cc:][:Z*:]]*)?" + "([[:Nd:]]+(?:[[:Po:]][[:Nd:]]+)?)?" + "(?(1)" + "|(?(2)" + "[[:Cf:][:Cc:][:Z*:]]*" + ")" + "[[:Sc:]]" + ")"; + boost::u32regex r = boost::make_u32regex(re); + boost::u32regex_iterator<std::string::const_iterator> i(boost::make_u32regex_iterator(text, r)), j; + while(i != j) + { + std::cout << (*i)[0] << std::endl; + ++i; + } +} + +void enumerate_currencies2(const std::string& text) +{ + // enumerate and print all the currency symbols, along + // with any associated numeric values: + const char* re = + "([[:Sc:]][[:Cf:][:Cc:][:Z*:]]*)?" + "([[:Nd:]]+(?:[[:Po:]][[:Nd:]]+)?)?" + "(?(1)" + "|(?(2)" + "[[:Cf:][:Cc:][:Z*:]]*" + ")" + "[[:Sc:]]" + ")"; + boost::u32regex r = boost::make_u32regex(re); + boost::u32regex_token_iterator<std::string::const_iterator> + i(boost::make_u32regex_token_iterator(text, r, 1)), j; + while(i != j) + { + std::cout << *i << std::endl; + ++i; + } +} + + +// +// Take a credit card number as a string of digits, +// and reformat it as a human readable string with "-" +// separating each group of four digit;, +// note that we're mixing a UTF-32 regex, with a UTF-16 +// string and a UTF-8 format specifier, and it still all +// just works: +// +const boost::u32regex e = boost::make_u32regex("\\A(\\d{3,4})[- ]?(\\d{4})[- ]?(\\d{4})[- ]?(\\d{4})\\z"); +const char* human_format = "$1-$2-$3-$4"; + +U_NAMESPACE_QUALIFIER UnicodeString human_readable_card_number(const U_NAMESPACE_QUALIFIER UnicodeString& s) +{ + return boost::u32regex_replace(s, e, human_format); +} + + +int main() +{ + // password checks using u32regex_match: + U_NAMESPACE_QUALIFIER UnicodeString pwd = "abcDEF---"; + U_NAMESPACE_QUALIFIER UnicodeString pwd_check = "(?=.*[[:lower:]])(?=.*[[:upper:]])(?=.*[[:punct:]]).{6,}"; + bool b = is_valid_password(pwd, pwd_check); + assert(b); + pwd = "abcD-"; + b = is_valid_password(pwd, pwd_check); + assert(!b); + // filename extraction with u32regex_match: + std::string file = "abc.hpp"; + file = get_filename(file); + assert(file == "abc.hpp"); + file = "c:\\a\\b\\c\\d.h"; + file = get_filename(file); + assert(file == "d.h"); + + // Greek text extraction with u32regex_search: + const UChar t[] = { + 'S', 'o', 'm', 'e', ' ', 'w', 'h', 'e', 'r', 'e', ' ', 'i', 'n', 0x0391, 0x039D, 0x0395, 0x0398, 0x0391, 0 + }; + const UChar g[] = { + 0x0391, 0x039D, 0x0395, 0x0398, 0x0391, 0 + }; + U_NAMESPACE_QUALIFIER UnicodeString text = t; + U_NAMESPACE_QUALIFIER UnicodeString greek = extract_greek(text); + assert(greek == g); + + // extract currency symbols with associated value, use iterator interface: + std::string text2 = " $100.23 or \xC2\xA3""198.12 "; // \xC2\xA3 is the pound sign encoded in UTF-8 + enumerate_currencies(text2); + enumerate_currencies2(text2); + + U_NAMESPACE_QUALIFIER UnicodeString credit_card_number = "1234567887654321"; + credit_card_number = human_readable_card_number(credit_card_number); + assert(credit_card_number == "1234-5678-8765-4321"); + return 0; +} + +#else + +#include <iostream> + +int main() +{ + std::cout << "<NOTE>ICU support not enabled, feature unavailable</NOTE>"; + return 0; +} + + +#endif + diff --git a/src/boost/libs/regex/example/snippets/mfc_example.cpp b/src/boost/libs/regex/example/snippets/mfc_example.cpp new file mode 100644 index 00000000..4b3ae62b --- /dev/null +++ b/src/boost/libs/regex/example/snippets/mfc_example.cpp @@ -0,0 +1,162 @@ +/* + * + * Copyright (c) 2004 + * John Maddock + * + * Use, modification and distribution are subject to the + * Boost Software License, Version 1.0. (See accompanying file + * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + * + */ + + /* + * LOCATION: see http://www.boost.org for most recent version. + * FILE mfc_example.cpp + * VERSION see <boost/version.hpp> + * DESCRIPTION: examples of using Boost.Regex with MFC and ATL string types. + */ + +#ifdef TEST_MFC + +#include <boost/regex/mfc.hpp> +#include <cstringt.h> +#include <atlstr.h> +#include <assert.h> +#include <tchar.h> +#include <iostream> + +#ifdef _UNICODE +#define cout wcout +#endif + +// +// Find out if *password* meets our password requirements, +// as defined by the regular expression *requirements*. +// +bool is_valid_password(const CString& password, const CString& requirements) +{ + return boost::regex_match(password, boost::make_regex(requirements)); +} + +// +// Extract filename part of a path from a CString and return the result +// as another CString: +// +CString get_filename(const CString& path) +{ + boost::tregex r(__T("(?:\\A|.*\\\\)([^\\\\]+)")); + boost::tmatch what; + if(boost::regex_match(path, what, r)) + { + // extract $1 as a CString: + return CString(what[1].first, what.length(1)); + } + else + { + throw std::runtime_error("Invalid pathname"); + } +} + +CString extract_postcode(const CString& address) +{ + // searches throw address for a UK postcode and returns the result, + // the expression used is by Phil A. on www.regxlib.com: + boost::tregex r(__T("^(([A-Z]{1,2}[0-9]{1,2})|([A-Z]{1,2}[0-9][A-Z]))\\s?([0-9][A-Z]{2})$")); + boost::tmatch what; + if(boost::regex_search(address, what, r)) + { + // extract $0 as a CString: + return CString(what[0].first, what.length()); + } + else + { + throw std::runtime_error("No postcode found"); + } +} + +void enumerate_links(const CString& html) +{ + // enumerate and print all the <a> links in some HTML text, + // the expression used is by Andew Lee on www.regxlib.com: + boost::tregex r(__T("href=[\"\']((http:\\/\\/|\\.\\/|\\/)?\\w+(\\.\\w+)*(\\/\\w+(\\.\\w+)?)*(\\/|\\?\\w*=\\w*(&\\w*=\\w*)*)?)[\"\']")); + boost::tregex_iterator i(boost::make_regex_iterator(html, r)), j; + while(i != j) + { + std::cout << (*i)[1] << std::endl; + ++i; + } +} + +void enumerate_links2(const CString& html) +{ + // enumerate and print all the <a> links in some HTML text, + // the expression used is by Andew Lee on www.regxlib.com: + boost::tregex r(__T("href=[\"\']((http:\\/\\/|\\.\\/|\\/)?\\w+(\\.\\w+)*(\\/\\w+(\\.\\w+)?)*(\\/|\\?\\w*=\\w*(&\\w*=\\w*)*)?)[\"\']")); + boost::tregex_token_iterator i(boost::make_regex_token_iterator(html, r, 1)), j; + while(i != j) + { + std::cout << *i << std::endl; + ++i; + } +} + +// +// Take a credit card number as a string of digits, +// and reformat it as a human readable string with "-" +// separating each group of four digits: +// +const boost::tregex e(__T("\\A(\\d{3,4})[- ]?(\\d{4})[- ]?(\\d{4})[- ]?(\\d{4})\\z")); +const CString human_format = __T("$1-$2-$3-$4"); + +CString human_readable_card_number(const CString& s) +{ + return boost::regex_replace(s, e, human_format); +} + + +int main() +{ + // password checks using regex_match: + CString pwd = "abcDEF---"; + CString pwd_check = "(?=.*[[:lower:]])(?=.*[[:upper:]])(?=.*[[:punct:]]).{6,}"; + bool b = is_valid_password(pwd, pwd_check); + assert(b); + pwd = "abcD-"; + b = is_valid_password(pwd, pwd_check); + assert(!b); + + // filename extraction with regex_match: + CString file = "abc.hpp"; + file = get_filename(file); + assert(file == "abc.hpp"); + file = "c:\\a\\b\\c\\d.h"; + file = get_filename(file); + assert(file == "d.h"); + + // postcode extraction with regex_search: + CString address = "Joe Bloke, 001 Somestreet, Somewhere,\nPL2 8AB"; + CString postcode = extract_postcode(address); + assert(postcode = "PL2 8NV"); + + // html link extraction with regex_iterator: + CString text = "<dt><a href=\"syntax_perl.html\">Perl Regular Expressions</a></dt><dt><a href=\"syntax_extended.html\">POSIX-Extended Regular Expressions</a></dt><dt><a href=\"syntax_basic.html\">POSIX-Basic Regular Expressions</a></dt>"; + enumerate_links(text); + enumerate_links2(text); + + CString credit_card_number = "1234567887654321"; + credit_card_number = human_readable_card_number(credit_card_number); + assert(credit_card_number == "1234-5678-8765-4321"); + return 0; +} + +#else + +#include <iostream> + +int main() +{ + std::cout << "<NOTE>MFC support not enabled, feature unavailable</NOTE>"; + return 0; +} + +#endif diff --git a/src/boost/libs/regex/example/snippets/partial_regex_grep.cpp b/src/boost/libs/regex/example/snippets/partial_regex_grep.cpp new file mode 100644 index 00000000..9934ebc5 --- /dev/null +++ b/src/boost/libs/regex/example/snippets/partial_regex_grep.cpp @@ -0,0 +1,109 @@ +/* + * + * Copyright (c) 1998-2002 + * John Maddock + * + * Use, modification and distribution are subject to the + * Boost Software License, Version 1.0. (See accompanying file + * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + * + */ + + /* + * LOCATION: see http://www.boost.org for most recent version. + * FILE partial_regex_grep.cpp + * VERSION see <boost/version.hpp> + * DESCRIPTION: Search example using partial matches. + */ + +#include <boost/regex.hpp> +#include <iostream> +#include <fstream> +#include <sstream> +#include <string> +#include <cstring> + +#ifdef BOOST_NO_STDC_NAMESPACE +namespace std{ using ::memmove; } +#endif + +// match some kind of html tag: +boost::regex e("<[^>]*>"); +// count how many: +unsigned int tags = 0; +// saved position of partial match: +const char* next_pos = 0; + +bool grep_callback(const boost::match_results<const char*>& m) +{ + if(m[0].matched == false) + { + // save position and return: + next_pos = m[0].first; + } + else + ++tags; + return true; +} + +void search(std::istream& is) +{ + char buf[4096]; + next_pos = buf + sizeof(buf); + bool have_more = true; + while(have_more) + { + // how much do we copy forward from last try: + std::ptrdiff_t leftover = (buf + sizeof(buf)) - next_pos; + // and how much is left to fill: + std::ptrdiff_t size = next_pos - buf; + // copy forward whatever we have left: + std::memmove(buf, next_pos, leftover); + // fill the rest from the stream: + is.read(buf + leftover, size); + std::streamsize read = is.gcount(); + // check to see if we've run out of text: + have_more = read == size; + // reset next_pos: + next_pos = buf + sizeof(buf); + // and then grep: + boost::regex_grep<bool(*)(const boost::cmatch&), const char*>(grep_callback, + static_cast<const char*>(buf), + static_cast<const char*>(buf + read + leftover), + e, + boost::match_default | boost::match_partial); + } +} + +int main(int argc, char* argv[]) +{ + if(argc > 1) + { + for(int i = 1; i < argc; ++i) + { + std::ifstream fs(argv[i]); + if(fs.bad()) continue; + search(fs); + fs.close(); + } + } + else + { + std::string one("<META NAME=\"keywords\" CONTENT=\"regex++, regular expressions, regular expression library, C++\">"); + std::string what; + while(what.size() < 10000) + { + what.append(one); + what.append(13, ' '); + } + std::stringstream ss; + ss.str(what); + search(ss); + } + std::cout << "total tag count was " << tags << std::endl; + return 0; +} + + + + diff --git a/src/boost/libs/regex/example/snippets/partial_regex_iterate.cpp b/src/boost/libs/regex/example/snippets/partial_regex_iterate.cpp new file mode 100644 index 00000000..62f67dd2 --- /dev/null +++ b/src/boost/libs/regex/example/snippets/partial_regex_iterate.cpp @@ -0,0 +1,118 @@ +/* + * + * Copyright (c) 1998-2007 + * John Maddock + * + * Use, modification and distribution are subject to the + * Boost Software License, Version 1.0. (See accompanying file + * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + * + */ + + /* + * LOCATION: see http://www.boost.org for most recent version. + * FILE partial_regex_iterate.cpp + * VERSION see <boost/version.hpp> + * DESCRIPTION: Search example using partial matches. + */ + +#include <boost/regex.hpp> +#include <iostream> +#include <fstream> +#include <sstream> +#include <string> +#include <cstring> + +#ifdef BOOST_NO_STDC_NAMESPACE +namespace std{ using ::memmove; } +#endif + +// match some kind of html tag: +boost::regex e("<[^>]*>"); +// count how many: +unsigned int tags = 0; + +void search(std::istream& is) +{ + // buffer we'll be searching in: + char buf[4096]; + // saved position of end of partial match: + const char* next_pos = buf + sizeof(buf); + // flag to indicate whether there is more input to come: + bool have_more = true; + + while(have_more) + { + // how much do we copy forward from last try: + std::ptrdiff_t leftover = (buf + sizeof(buf)) - next_pos; + // and how much is left to fill: + std::ptrdiff_t size = next_pos - buf; + // copy forward whatever we have left: + std::memmove(buf, next_pos, leftover); + // fill the rest from the stream: + is.read(buf + leftover, size); + std::streamsize read = is.gcount(); + // check to see if we've run out of text: + have_more = read == size; + // reset next_pos: + next_pos = buf + sizeof(buf); + // and then iterate: + boost::cregex_iterator a( + buf, + buf + read + leftover, + e, + boost::match_default | boost::match_partial); + boost::cregex_iterator b; + + while(a != b) + { + if((*a)[0].matched == false) + { + // Partial match, save position and break: + next_pos = (*a)[0].first; + break; + } + else + { + // full match: + ++tags; + } + + // move to next match: + ++a; + } + } +} + +int main(int argc, char* argv[]) +{ + if(argc > 1) + { + for(int i = 1; i < argc; ++i) + { + std::ifstream fs(argv[i]); + if(fs.bad()) continue; + search(fs); + fs.close(); + } + } + else + { + std::string one("<META NAME=\"keywords\" CONTENT=\"regex++, regular expressions, regular expression library, C++\">"); + std::string what; + while(what.size() < 10000) + { + what.append(one); + what.append(13, ' '); + } + std::stringstream ss; + ss.str(what); + search(ss); + } + std::cout << "total tag count was " << tags << std::endl; + return 0; +} + + + + diff --git a/src/boost/libs/regex/example/snippets/partial_regex_match.cpp b/src/boost/libs/regex/example/snippets/partial_regex_match.cpp new file mode 100644 index 00000000..096072bf --- /dev/null +++ b/src/boost/libs/regex/example/snippets/partial_regex_match.cpp @@ -0,0 +1,69 @@ +/* + * + * Copyright (c) 1998-2002 + * John Maddock + * + * Use, modification and distribution are subject to the + * Boost Software License, Version 1.0. (See accompanying file + * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + * + */ + + /* + * LOCATION: see http://www.boost.org for most recent version. + * FILE partial_regex_match.cpp + * VERSION see <boost/version.hpp> + * DESCRIPTION: regex_match example using partial matches. + */ + +#include <boost/regex.hpp> +#include <string> +#include <iostream> + +boost::regex e("(\\d{3,4})[- ]?(\\d{4})[- ]?(\\d{4})[- ]?(\\d{4})"); + +bool is_possible_card_number(const std::string& input) +{ + // + // return false for partial match, true for full match, or throw for + // impossible match based on what we have so far... + boost::match_results<std::string::const_iterator> what; + if(0 == boost::regex_match(input, what, e, boost::match_default | boost::match_partial)) + { + // the input so far could not possibly be valid so reject it: + throw std::runtime_error("Invalid data entered - this could not possibly be a valid card number"); + } + // OK so far so good, but have we finished? + if(what[0].matched) + { + // excellent, we have a result: + return true; + } + // what we have so far is only a partial match... + return false; +} + +int main(int argc, char* argv[]) +{ + try{ + std::string input; + if(argc > 1) + input = argv[1]; + else + std::cin >> input; + if(is_possible_card_number(input)) + { + std::cout << "Matched OK..." << std::endl; + } + else + std::cout << "Got a partial match..." << std::endl; + } + catch(const std::exception& e) + { + std::cout << e.what() << std::endl; + } + return 0; +} + + + diff --git a/src/boost/libs/regex/example/snippets/regex_grep_example_1.cpp b/src/boost/libs/regex/example/snippets/regex_grep_example_1.cpp new file mode 100644 index 00000000..2878f578 --- /dev/null +++ b/src/boost/libs/regex/example/snippets/regex_grep_example_1.cpp @@ -0,0 +1,131 @@ +/* + * + * Copyright (c) 1998-2002 + * John Maddock + * + * Use, modification and distribution are subject to the + * Boost Software License, Version 1.0. (See accompanying file + * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + * + */ + + /* + * LOCATION: see http://www.boost.org for most recent version. + * FILE regex_grep_example_1.cpp + * VERSION see <boost/version.hpp> + * DESCRIPTION: regex_grep example 1: searches a cpp file for class definitions. + */ + +#include <boost/regex.hpp> +#include <string> +#include <map> + +// purpose: +// takes the contents of a file in the form of a string +// and searches for all the C++ class definitions, storing +// their locations in a map of strings/int's + +typedef std::map<std::string, std::string::difference_type, std::less<std::string> > map_type; + +const char* re = + // possibly leading whitespace: + "^[[:space:]]*" + // possible template declaration: + "(template[[:space:]]*<[^;:{]+>[[:space:]]*)?" + // class or struct: + "(class|struct)[[:space:]]*" + // leading declspec macros etc: + "(" + "\\<\\w+\\>" + "(" + "[[:blank:]]*\\([^)]*\\)" + ")?" + "[[:space:]]*" + ")*" + // the class name + "(\\<\\w*\\>)[[:space:]]*" + // template specialisation parameters + "(<[^;:{]+>)?[[:space:]]*" + // terminate in { or : + "(\\{|:[^;\\{()]*\\{)"; + +boost::regex expression(re); + +class IndexClassesPred +{ + map_type& m; + std::string::const_iterator base; +public: + IndexClassesPred(map_type& a, std::string::const_iterator b) : m(a), base(b) {} + bool operator()(const boost::match_results<std::string::const_iterator>& what) + { + // what[0] contains the whole string + // what[5] contains the class name. + // what[6] contains the template specialisation if any. + // add class name and position to map: + m[std::string(what[5].first, what[5].second) + std::string(what[6].first, what[6].second)] = + what[5].first - base; + return true; + } +private: + IndexClassesPred& operator=(const IndexClassesPred&); +}; + +void IndexClasses(map_type& m, const std::string& file) +{ + std::string::const_iterator start, end; + start = file.begin(); + end = file.end(); + boost::regex_grep(IndexClassesPred(m, start), start, end, expression); +} + + +#include <fstream> +#include <iostream> + +using namespace std; + +void load_file(std::string& s, std::istream& is) +{ + s.erase(); + if(is.bad()) return; + s.reserve(static_cast<std::string::size_type>(is.rdbuf()->in_avail())); + char c; + while(is.get(c)) + { + if(s.capacity() == s.size()) + s.reserve(s.capacity() * 3); + s.append(1, c); + } +} + +int main(int argc, const char** argv) +{ + std::string text; + for(int i = 1; i < argc; ++i) + { + cout << "Processing file " << argv[i] << endl; + map_type m; + std::ifstream fs(argv[i]); + load_file(text, fs); + fs.close(); + IndexClasses(m, text); + cout << m.size() << " matches found" << endl; + map_type::iterator c, d; + c = m.begin(); + d = m.end(); + while(c != d) + { + cout << "class \"" << (*c).first << "\" found at index: " << (*c).second << endl; + ++c; + } + } + return 0; +} + + + + + + + diff --git a/src/boost/libs/regex/example/snippets/regex_grep_example_2.cpp b/src/boost/libs/regex/example/snippets/regex_grep_example_2.cpp new file mode 100644 index 00000000..b5222c2d --- /dev/null +++ b/src/boost/libs/regex/example/snippets/regex_grep_example_2.cpp @@ -0,0 +1,123 @@ +/* + * + * Copyright (c) 1998-2002 + * John Maddock + * + * Use, modification and distribution are subject to the + * Boost Software License, Version 1.0. (See accompanying file + * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + * + */ + + /* + * LOCATION: see http://www.boost.org for most recent version. + * FILE regex_grep_example_2.cpp + * VERSION see <boost/version.hpp> + * DESCRIPTION: regex_grep example 2: searches a cpp file for class definitions, + * using a global callback function. + */ + +#include <boost/regex.hpp> +#include <string> +#include <map> +#include <boost/regex.hpp> + +// purpose: +// takes the contents of a file in the form of a string +// and searches for all the C++ class definitions, storing +// their locations in a map of strings/int's + +typedef std::map<std::string, std::string::difference_type, std::less<std::string> > map_type; + +const char* re = + // possibly leading whitespace: + "^[[:space:]]*" + // possible template declaration: + "(template[[:space:]]*<[^;:{]+>[[:space:]]*)?" + // class or struct: + "(class|struct)[[:space:]]*" + // leading declspec macros etc: + "(" + "\\<\\w+\\>" + "(" + "[[:blank:]]*\\([^)]*\\)" + ")?" + "[[:space:]]*" + ")*" + // the class name + "(\\<\\w*\\>)[[:space:]]*" + // template specialisation parameters + "(<[^;:{]+>)?[[:space:]]*" + // terminate in { or : + "(\\{|:[^;\\{()]*\\{)"; + + +boost::regex expression(re); +map_type class_index; +std::string::const_iterator base; + +bool grep_callback(const boost::match_results<std::string::const_iterator>& what) +{ + // what[0] contains the whole string + // what[5] contains the class name. + // what[6] contains the template specialisation if any. + // add class name and position to map: + class_index[std::string(what[5].first, what[5].second) + std::string(what[6].first, what[6].second)] = + what[5].first - base; + return true; +} + +void IndexClasses(const std::string& file) +{ + std::string::const_iterator start, end; + start = file.begin(); + end = file.end(); + base = start; + boost::regex_grep(grep_callback, start, end, expression); +} + +#include <fstream> +#include <iostream> + +using namespace std; + +void load_file(std::string& s, std::istream& is) +{ + s.erase(); + if(is.bad()) return; + s.reserve(static_cast<std::string::size_type>(is.rdbuf()->in_avail())); + char c; + while(is.get(c)) + { + if(s.capacity() == s.size()) + s.reserve(s.capacity() * 3); + s.append(1, c); + } +} + +int main(int argc, const char** argv) +{ + std::string text; + for(int i = 1; i < argc; ++i) + { + cout << "Processing file " << argv[i] << endl; + std::ifstream fs(argv[i]); + load_file(text, fs); + fs.close(); + IndexClasses(text); + cout << class_index.size() << " matches found" << endl; + map_type::iterator c, d; + c = class_index.begin(); + d = class_index.end(); + while(c != d) + { + cout << "class \"" << (*c).first << "\" found at index: " << (*c).second << endl; + ++c; + } + class_index.erase(class_index.begin(), class_index.end()); + } + return 0; +} + + + diff --git a/src/boost/libs/regex/example/snippets/regex_grep_example_3.cpp b/src/boost/libs/regex/example/snippets/regex_grep_example_3.cpp new file mode 100644 index 00000000..9097dfda --- /dev/null +++ b/src/boost/libs/regex/example/snippets/regex_grep_example_3.cpp @@ -0,0 +1,153 @@ +/* + * + * Copyright (c) 1998-2002 + * John Maddock + * + * Use, modification and distribution are subject to the + * Boost Software License, Version 1.0. (See accompanying file + * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + * + */ + + /* + * LOCATION: see http://www.boost.org for most recent version. + * FILE regex_grep_example_3.cpp + * VERSION see <boost/version.hpp> + * DESCRIPTION: regex_grep example 3: searches a cpp file for class definitions, + * using a bound member function callback. + */ + +#include <boost/regex.hpp> +#include <string> +#include <map> +#include <functional> +#include <boost/detail/workaround.hpp> + +// purpose: +// takes the contents of a file in the form of a string +// and searches for all the C++ class definitions, storing +// their locations in a map of strings/int's + +typedef std::map<std::string, std::string::difference_type, std::less<std::string> > map_type; + +const char* re = + // possibly leading whitespace: + "^[[:space:]]*" + // possible template declaration: + "(template[[:space:]]*<[^;:{]+>[[:space:]]*)?" + // class or struct: + "(class|struct)[[:space:]]*" + // leading declspec macros etc: + "(" + "\\<\\w+\\>" + "(" + "[[:blank:]]*\\([^)]*\\)" + ")?" + "[[:space:]]*" + ")*" + // the class name + "(\\<\\w*\\>)[[:space:]]*" + // template specialisation parameters + "(<[^;:{]+>)?[[:space:]]*" + // terminate in { or : + "(\\{|:[^;\\{()]*\\{)"; + + +class class_index +{ + boost::regex expression; + map_type index; + std::string::const_iterator base; + + bool grep_callback(boost::match_results<std::string::const_iterator> what); +public: + map_type& get_map() { return index; } + void IndexClasses(const std::string& file); + class_index() + : expression(re) {} +}; + +bool class_index::grep_callback(boost::match_results<std::string::const_iterator> what) +{ + // what[0] contains the whole string + // what[5] contains the class name. + // what[6] contains the template specialisation if any. + // add class name and position to map: + index[std::string(what[5].first, what[5].second) + std::string(what[6].first, what[6].second)] = + what[5].first - base; + return true; +} + +void class_index::IndexClasses(const std::string& file) +{ + std::string::const_iterator start, end; + start = file.begin(); + end = file.end(); + base = start; +#if BOOST_WORKAROUND(_MSC_VER, < 1300) && !defined(_STLP_VERSION) + boost::regex_grep(std::bind1st(std::mem_fun1(&class_index::grep_callback), this), + start, + end, + expression); +#elif defined(BOOST_NO_CXX98_BINDERS) + boost::regex_grep(std::bind(&class_index::grep_callback, this, std::placeholders::_1), + start, + end, + expression); +#else + boost::regex_grep(std::bind1st(std::mem_fun(&class_index::grep_callback), this), + start, + end, + expression); +#endif +} + + +#include <fstream> +#include <iostream> + +using namespace std; + +void load_file(std::string& s, std::istream& is) +{ + s.erase(); + if(is.bad()) return; + s.reserve(static_cast<std::string::size_type>(is.rdbuf()->in_avail())); + char c; + while(is.get(c)) + { + if(s.capacity() == s.size()) + s.reserve(s.capacity() * 3); + s.append(1, c); + } +} + +int main(int argc, const char** argv) +{ + std::string text; + for(int i = 1; i < argc; ++i) + { + cout << "Processing file " << argv[i] << endl; + std::ifstream fs(argv[i]); + load_file(text, fs); + fs.close(); + class_index idx; + idx.IndexClasses(text); + cout << idx.get_map().size() << " matches found" << endl; + map_type::iterator c, d; + c = idx.get_map().begin(); + d = idx.get_map().end(); + while(c != d) + { + cout << "class \"" << (*c).first << "\" found at index: " << (*c).second << endl; + ++c; + } + } + return 0; +} + + + + + + diff --git a/src/boost/libs/regex/example/snippets/regex_grep_example_4.cpp b/src/boost/libs/regex/example/snippets/regex_grep_example_4.cpp new file mode 100644 index 00000000..533d896e --- /dev/null +++ b/src/boost/libs/regex/example/snippets/regex_grep_example_4.cpp @@ -0,0 +1,155 @@ +/* + * + * Copyright (c) 1998-2002 + * John Maddock + * + * Use, modification and distribution are subject to the + * Boost Software License, Version 1.0. (See accompanying file + * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + * + */ + + /* + * LOCATION: see http://www.boost.org for most recent version. + * FILE regex_grep_example_4.cpp + * VERSION see <boost/version.hpp> + * DESCRIPTION: regex_grep example 4: searches a cpp file for class definitions, + * using a C++ Builder closure as a callback. + */ + +#ifdef __BORLANDC__ + +#include <boost/regex.hpp> +#include <string> +#include <map> +#include <functional> + +// purpose: +// takes the contents of a file in the form of a string +// and searches for all the C++ class definitions, storing +// their locations in a map of strings/int's + +typedef std::map<std::string, int, std::less<std::string> > map_type; + +const char* re = + // possibly leading whitespace: + "^[[:space:]]*" + // possible template declaration: + "(template[[:space:]]*<[^;:{]+>[[:space:]]*)?" + // class or struct: + "(class|struct)[[:space:]]*" + // leading declspec macros etc: + "(" + "\\<\\w+\\>" + "(" + "[[:blank:]]*\\([^)]*\\)" + ")?" + "[[:space:]]*" + ")*" + // the class name + "(\\<\\w*\\>)[[:space:]]*" + // template specialisation parameters + "(<[^;:{]+>)?[[:space:]]*" + // terminate in { or : + "(\\{|:[^;\\{()]*\\{)"; + + +class class_index +{ + boost::regex expression; + map_type index; + std::string::const_iterator base; + typedef boost::match_results<std::string::const_iterator> arg_type; + + bool grep_callback(const boost::match_results<std::string::const_iterator>& what); +public: + map_type& get_map() { return index; } + typedef bool (__closure* grep_callback_type)(const arg_type&); + void IndexClasses(const std::string& file); + class_index() + : index(), + expression(re) + {} +}; + +bool class_index::grep_callback(const boost::match_results<std::string::const_iterator>& what) +{ + // what[0] contains the whole string + // what[5] contains the class name. + // what[6] contains the template specialisation if any. + // add class name and position to map: + index[std::string(what[5].first, what[5].second) + std::string(what[6].first, what[6].second)] = + what[5].first - base; + return true; +} + +void class_index::IndexClasses(const std::string& file) +{ + std::string::const_iterator start, end; + start = file.begin(); + end = file.end(); + base = start; + class_index::grep_callback_type cl = &(this->grep_callback); + boost::regex_grep(cl, + start, + end, + expression); +} + + +#include <fstream> +#include <iostream> + +using namespace std; + +void load_file(std::string& s, std::istream& is) +{ + s.erase(); + if(is.bad()) return; + s.reserve(is.rdbuf()->in_avail()); + char c; + while(is.get(c)) + { + if(s.capacity() == s.size()) + s.reserve(s.capacity() * 3); + s.append(1, c); + } +} + +int main(int argc, const char** argv) +{ + std::string text; + for(int i = 1; i < argc; ++i) + { + cout << "Processing file " << argv[i] << endl; + std::ifstream fs(argv[i]); + load_file(text, fs); + fs.close(); + class_index i; + i.IndexClasses(text); + cout << i.get_map().size() << " matches found" << endl; + map_type::iterator c, d; + c = i.get_map().begin(); + d = i.get_map().end(); + while(c != d) + { + cout << "class \"" << (*c).first << "\" found at index: " << (*c).second << endl; + ++c; + } + } + return 0; +} + +#else // __BORLANDC__ + +int main() +{ + return 0; +} + + +#endif + + + + diff --git a/src/boost/libs/regex/example/snippets/regex_iterator_example.cpp b/src/boost/libs/regex/example/snippets/regex_iterator_example.cpp new file mode 100644 index 00000000..101052f1 --- /dev/null +++ b/src/boost/libs/regex/example/snippets/regex_iterator_example.cpp @@ -0,0 +1,114 @@ +/* + * + * Copyright (c) 2003 + * John Maddock + * + * Use, modification and distribution are subject to the + * Boost Software License, Version 1.0. (See accompanying file + * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + * + */ + + /* + * LOCATION: see http://www.boost.org for most recent version. + * FILE regex_iterator_example_2.cpp + * VERSION see <boost/version.hpp> + * DESCRIPTION: regex_iterator example 2: searches a cpp file for class definitions, + * using global data. + */ + +#include <boost/regex.hpp> +#include <string> +#include <map> +#include <fstream> +#include <iostream> + +using namespace std; + +// purpose: +// takes the contents of a file in the form of a string +// and searches for all the C++ class definitions, storing +// their locations in a map of strings/int's + +typedef std::map<std::string, std::string::difference_type, std::less<std::string> > map_type; + +const char* re = + // possibly leading whitespace: + "^[[:space:]]*" + // possible template declaration: + "(template[[:space:]]*<[^;:{]+>[[:space:]]*)?" + // class or struct: + "(class|struct)[[:space:]]*" + // leading declspec macros etc: + "(" + "\\<\\w+\\>" + "(" + "[[:blank:]]*\\([^)]*\\)" + ")?" + "[[:space:]]*" + ")*" + // the class name + "(\\<\\w*\\>)[[:space:]]*" + // template specialisation parameters + "(<[^;:{]+>)?[[:space:]]*" + // terminate in { or : + "(\\{|:[^;\\{()]*\\{)"; + + +boost::regex expression(re); +map_type class_index; + +bool regex_callback(const boost::match_results<std::string::const_iterator>& what) +{ + // what[0] contains the whole string + // what[5] contains the class name. + // what[6] contains the template specialisation if any. + // add class name and position to map: + class_index[what[5].str() + what[6].str()] = what.position(5); + return true; +} + +void load_file(std::string& s, std::istream& is) +{ + s.erase(); + if(is.bad()) return; + s.reserve(static_cast<std::string::size_type>(is.rdbuf()->in_avail())); + char c; + while(is.get(c)) + { + if(s.capacity() == s.size()) + s.reserve(s.capacity() * 3); + s.append(1, c); + } +} + +int main(int argc, const char** argv) +{ + std::string text; + for(int i = 1; i < argc; ++i) + { + cout << "Processing file " << argv[i] << endl; + std::ifstream fs(argv[i]); + load_file(text, fs); + fs.close(); + // construct our iterators: + boost::sregex_iterator m1(text.begin(), text.end(), expression); + boost::sregex_iterator m2; + std::for_each(m1, m2, ®ex_callback); + // copy results: + cout << class_index.size() << " matches found" << endl; + map_type::iterator c, d; + c = class_index.begin(); + d = class_index.end(); + while(c != d) + { + cout << "class \"" << (*c).first << "\" found at index: " << (*c).second << endl; + ++c; + } + class_index.erase(class_index.begin(), class_index.end()); + } + return 0; +} + + + diff --git a/src/boost/libs/regex/example/snippets/regex_match_example.cpp b/src/boost/libs/regex/example/snippets/regex_match_example.cpp new file mode 100644 index 00000000..dd10de4d --- /dev/null +++ b/src/boost/libs/regex/example/snippets/regex_match_example.cpp @@ -0,0 +1,105 @@ +/* + * + * Copyright (c) 1998-2002 + * John Maddock + * + * Use, modification and distribution are subject to the + * Boost Software License, Version 1.0. (See accompanying file + * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + * + */ + + /* + * LOCATION: see http://www.boost.org for most recent version. + * FILE regex_match_example.cpp + * VERSION see <boost/version.hpp> + * DESCRIPTION: ftp based regex_match example. + */ + +#include <boost/regex.hpp> +#include <cstdlib> +#include <stdlib.h> +#include <string> +#include <iostream> + +using namespace std; +using namespace boost; + +regex expression("^([0-9]+)(\\-| |$)(.*)$"); + +// process_ftp: +// on success returns the ftp response code, and fills +// msg with the ftp response message. +int process_ftp(const char* response, std::string* msg) +{ + cmatch what; + if(regex_match(response, what, expression)) + { + // what[0] contains the whole string + // what[1] contains the response code + // what[2] contains the separator character + // what[3] contains the text message. + if(msg) + msg->assign(what[3].first, what[3].second); + return ::atoi(what[1].first); + } + // failure did not match + if(msg) + msg->erase(); + return -1; +} + +#if defined(BOOST_MSVC) || (defined(__BORLANDC__) && (__BORLANDC__ == 0x550)) +// +// problem with std::getline under MSVC6sp3 +istream& getline(istream& is, std::string& s) +{ + s.erase(); + char c = static_cast<char>(is.get()); + while(c != '\n') + { + s.append(1, c); + c = static_cast<char>(is.get()); + } + return is; +} +#endif + +int main(int argc, const char*[]) +{ + std::string in, out; + do + { + if(argc == 1) + { + cout << "enter test string" << endl; + getline(cin, in); + if(in == "quit") + break; + } + else + in = "100 this is an ftp message text"; + int result; + result = process_ftp(in.c_str(), &out); + if(result != -1) + { + cout << "Match found:" << endl; + cout << "Response code: " << result << endl; + cout << "Message text: " << out << endl; + } + else + { + cout << "Match not found" << endl; + } + cout << endl; + } while(argc == 1); + return 0; +} + + + + + + + + diff --git a/src/boost/libs/regex/example/snippets/regex_merge_example.cpp b/src/boost/libs/regex/example/snippets/regex_merge_example.cpp new file mode 100644 index 00000000..cb8b493c --- /dev/null +++ b/src/boost/libs/regex/example/snippets/regex_merge_example.cpp @@ -0,0 +1,137 @@ +/* + * + * Copyright (c) 1998-2002 + * John Maddock + * + * Use, modification and distribution are subject to the + * Boost Software License, Version 1.0. (See accompanying file + * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + * + */ + + /* + * LOCATION: see http://www.boost.org for most recent version. + * FILE regex_merge_example.cpp + * VERSION see <boost/version.hpp> + * DESCRIPTION: regex_merge example: + * converts a C++ file to syntax highlighted HTML. + */ + +#include <boost/regex.hpp> +#include <iostream> +#include <fstream> +#include <sstream> +#include <string> +#include <iterator> +#include <fstream> +#include <iostream> + +// purpose: +// takes the contents of a file and transform to +// syntax highlighted code in html format + +boost::regex e1, e2; +extern const char* expression_text; +extern const char* format_string; +extern const char* pre_expression; +extern const char* pre_format; +extern const char* header_text; +extern const char* footer_text; + +void load_file(std::string& s, std::istream& is) +{ + s.erase(); + if(is.bad()) return; + s.reserve(static_cast<std::string::size_type>(is.rdbuf()->in_avail())); + char c; + while(is.get(c)) + { + if(s.capacity() == s.size()) + s.reserve(s.capacity() * 3); + s.append(1, c); + } +} + +int main(int argc, const char** argv) +{ + try{ + e1.assign(expression_text); + e2.assign(pre_expression); + for(int i = 1; i < argc; ++i) + { + std::cout << "Processing file " << argv[i] << std::endl; + std::ifstream fs(argv[i]); + std::string in; + load_file(in, fs); + fs.close(); + std::string out_name = std::string(argv[i]) + std::string(".htm"); + std::ofstream os(out_name.c_str()); + os << header_text; + // strip '<' and '>' first by outputting to a + // temporary string stream + std::ostringstream t(std::ios::out | std::ios::binary); + std::ostream_iterator<char> oi(t); + boost::regex_merge(oi, in.begin(), in.end(), e2, pre_format, boost::match_default | boost::format_all); + // then output to final output stream + // adding syntax highlighting: + std::string s(t.str()); + std::ostream_iterator<char> out(os); + boost::regex_merge(out, s.begin(), s.end(), e1, format_string, boost::match_default | boost::format_all); + os << footer_text; + os.close(); + } + } + catch(...) + { return -1; } + return 0; +} + +const char* pre_expression = "(<)|(>)|\\r"; +const char* pre_format = "(?1<)(?2>)"; + + +const char* expression_text = // preprocessor directives: index 1 + "(^[[:blank:]]*#(?:[^\\\\\\n]|\\\\[^\\n[:punct:][:word:]]*[\\n[:punct:][:word:]])*)|" + // comment: index 2 + "(//[^\\n]*|/\\*.*?\\*/)|" + // literals: index 3 + "\\<([+-]?(?:(?:0x[[:xdigit:]]+)|(?:(?:[[:digit:]]*\\.)?[[:digit:]]+(?:[eE][+-]?[[:digit:]]+)?))u?(?:(?:int(?:8|16|32|64))|L)?)\\>|" + // string literals: index 4 + "('(?:[^\\\\']|\\\\.)*'|\"(?:[^\\\\\"]|\\\\.)*\")|" + // keywords: index 5 + "\\<(__asm|__cdecl|__declspec|__export|__far16|__fastcall|__fortran|__import" + "|__pascal|__rtti|__stdcall|_asm|_cdecl|__except|_export|_far16|_fastcall" + "|__finally|_fortran|_import|_pascal|_stdcall|__thread|__try|asm|auto|bool" + "|break|case|catch|cdecl|char|class|const|const_cast|continue|default|delete" + "|do|double|dynamic_cast|else|enum|explicit|extern|false|float|for|friend|goto" + "|if|inline|int|long|mutable|namespace|new|operator|pascal|private|protected" + "|public|register|reinterpret_cast|return|short|signed|sizeof|static|static_cast" + "|struct|switch|template|this|throw|true|try|typedef|typeid|typename|union|unsigned" + "|using|virtual|void|volatile|wchar_t|while)\\>" + ; + +const char* format_string = "(?1<font color=\"#008040\">$&</font>)" + "(?2<I><font color=\"#000080\">$&</font></I>)" + "(?3<font color=\"#0000A0\">$&</font>)" + "(?4<font color=\"#0000FF\">$&</font>)" + "(?5<B>$&</B>)"; + +const char* header_text = "<HTML>\n<HEAD>\n" + "<TITLE>Auto-generated html formated source</TITLE>\n" + "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=windows-1252\">\n" + "</HEAD>\n" + "<BODY LINK=\"#0000ff\" VLINK=\"#800080\" BGCOLOR=\"#ffffff\">\n" + "<P> </P>\n<PRE>"; + +const char* footer_text = "</PRE>\n</BODY>\n\n"; + + + + + + + + + + + diff --git a/src/boost/libs/regex/example/snippets/regex_replace_example.cpp b/src/boost/libs/regex/example/snippets/regex_replace_example.cpp new file mode 100644 index 00000000..19331ab8 --- /dev/null +++ b/src/boost/libs/regex/example/snippets/regex_replace_example.cpp @@ -0,0 +1,138 @@ +/* + * + * Copyright (c) 1998-2002 + * John Maddock + * + * Use, modification and distribution are subject to the + * Boost Software License, Version 1.0. (See accompanying file + * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + * + */ + + /* + * LOCATION: see http://www.boost.org for most recent version. + * FILE regex_replace_example.cpp + * VERSION see <boost/version.hpp> + * DESCRIPTION: regex_replace example: + * converts a C++ file to syntax highlighted HTML. + */ + +#include <boost/regex.hpp> +#include <iostream> +#include <fstream> +#include <sstream> +#include <string> +#include <iterator> +#include <fstream> +#include <iostream> + +// purpose: +// takes the contents of a file and transform to +// syntax highlighted code in html format + +boost::regex e1, e2; +extern const char* expression_text; +extern const char* format_string; +extern const char* pre_expression; +extern const char* pre_format; +extern const char* header_text; +extern const char* footer_text; + +void load_file(std::string& s, std::istream& is) +{ + s.erase(); + if(is.bad()) return; + s.reserve(static_cast<std::string::size_type>(is.rdbuf()->in_avail())); + char c; + while(is.get(c)) + { + if(s.capacity() == s.size()) + s.reserve(s.capacity() * 3); + s.append(1, c); + } +} + +int main(int argc, const char** argv) +{ + try{ + e1.assign(expression_text); + e2.assign(pre_expression); + for(int i = 1; i < argc; ++i) + { + std::cout << "Processing file " << argv[i] << std::endl; + std::ifstream fs(argv[i]); + std::string in; + load_file(in, fs); + fs.close(); + std::string out_name = std::string(argv[i]) + std::string(".htm"); + std::ofstream os(out_name.c_str()); + os << header_text; + // strip '<' and '>' first by outputting to a + // temporary string stream + std::ostringstream t(std::ios::out | std::ios::binary); + std::ostream_iterator<char> oi(t); + boost::regex_replace(oi, in.begin(), in.end(), e2, pre_format, boost::match_default | boost::format_all); + // then output to final output stream + // adding syntax highlighting: + std::string s(t.str()); + std::ostream_iterator<char> out(os); + boost::regex_replace(out, s.begin(), s.end(), e1, format_string, boost::match_default | boost::format_all); + os << footer_text; + os.close(); + } + } + catch(...) + { return -1; } + return 0; +} + +const char* pre_expression = "(<)|(>)|(&)|\\r"; +const char* pre_format = "(?1<)(?2>)(?3&)"; + + +const char* expression_text = // preprocessor directives: index 1 + "(^[[:blank:]]*#(?:[^\\\\\\n]|\\\\[^\\n[:punct:][:word:]]*[\\n[:punct:][:word:]])*)|" + // comment: index 2 + "(//[^\\n]*|/\\*.*?\\*/)|" + // literals: index 3 + "\\<([+-]?(?:(?:0x[[:xdigit:]]+)|(?:(?:[[:digit:]]*\\.)?[[:digit:]]+(?:[eE][+-]?[[:digit:]]+)?))u?(?:(?:int(?:8|16|32|64))|L)?)\\>|" + // string literals: index 4 + "('(?:[^\\\\']|\\\\.)*'|\"(?:[^\\\\\"]|\\\\.)*\")|" + // keywords: index 5 + "\\<(__asm|__cdecl|__declspec|__export|__far16|__fastcall|__fortran|__import" + "|__pascal|__rtti|__stdcall|_asm|_cdecl|__except|_export|_far16|_fastcall" + "|__finally|_fortran|_import|_pascal|_stdcall|__thread|__try|asm|auto|bool" + "|break|case|catch|cdecl|char|class|const|const_cast|continue|default|delete" + "|do|double|dynamic_cast|else|enum|explicit|extern|false|float|for|friend|goto" + "|if|inline|int|long|mutable|namespace|new|operator|pascal|private|protected" + "|public|register|reinterpret_cast|return|short|signed|sizeof|static|static_cast" + "|struct|switch|template|this|throw|true|try|typedef|typeid|typename|union|unsigned" + "|using|virtual|void|volatile|wchar_t|while)\\>" + ; + +const char* format_string = "(?1<font color=\"#008040\">$&</font>)" + "(?2<I><font color=\"#000080\">$&</font></I>)" + "(?3<font color=\"#0000A0\">$&</font>)" + "(?4<font color=\"#0000FF\">$&</font>)" + "(?5<B>$&</B>)"; + +const char* header_text = "<HTML>\n<HEAD>\n" + "<TITLE>Auto-generated html formated source</TITLE>\n" + "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=windows-1252\">\n" + "</HEAD>\n" + "<BODY LINK=\"#0000ff\" VLINK=\"#800080\" BGCOLOR=\"#ffffff\">\n" + "<P> </P>\n<PRE>"; + +const char* footer_text = "</PRE>\n</BODY>\n\n"; + + + + + + + + + + + + diff --git a/src/boost/libs/regex/example/snippets/regex_search_example.cpp b/src/boost/libs/regex/example/snippets/regex_search_example.cpp new file mode 100644 index 00000000..ee9cd90f --- /dev/null +++ b/src/boost/libs/regex/example/snippets/regex_search_example.cpp @@ -0,0 +1,129 @@ +/* + * + * Copyright (c) 1998-2002 + * John Maddock + * + * Use, modification and distribution are subject to the + * Boost Software License, Version 1.0. (See accompanying file + * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + * + */ + + /* + * LOCATION: see http://www.boost.org for most recent version. + * FILE regex_search_example.cpp + * VERSION see <boost/version.hpp> + * DESCRIPTION: regex_search example: searches a cpp file for class definitions. + */ + +#include <boost/regex.hpp> +#include <string> +#include <map> + +// purpose: +// takes the contents of a file in the form of a string +// and searches for all the C++ class definitions, storing +// their locations in a map of strings/int's + +typedef std::map<std::string, std::string::difference_type, std::less<std::string> > map_type; + +const char* re = + // possibly leading whitespace: + "^[[:space:]]*" + // possible template declaration: + "(template[[:space:]]*<[^;:{]+>[[:space:]]*)?" + // class or struct: + "(class|struct)[[:space:]]*" + // leading declspec macros etc: + "(" + "\\<\\w+\\>" + "(" + "[[:blank:]]*\\([^)]*\\)" + ")?" + "[[:space:]]*" + ")*" + // the class name + "(\\<\\w*\\>)[[:space:]]*" + // template specialisation parameters + "(<[^;:{]+>)?[[:space:]]*" + // terminate in { or : + "(\\{|:[^;\\{()]*\\{)"; + + +boost::regex expression(re); + +void IndexClasses(map_type& m, const std::string& file) +{ + std::string::const_iterator start, end; + start = file.begin(); + end = file.end(); + boost::match_results<std::string::const_iterator> what; + boost::match_flag_type flags = boost::match_default; + while(boost::regex_search(start, end, what, expression, flags)) + { + // what[0] contains the whole string + // what[5] contains the class name. + // what[6] contains the template specialisation if any. + // add class name and position to map: + m[std::string(what[5].first, what[5].second) + std::string(what[6].first, what[6].second)] = + what[5].first - file.begin(); + // update search position: + start = what[0].second; + // update flags: + flags |= boost::match_prev_avail; + flags |= boost::match_not_bob; + } +} + + +#include <iostream> +#include <fstream> + +using namespace std; + +void load_file(std::string& s, std::istream& is) +{ + s.erase(); + if(is.bad()) return; + s.reserve(static_cast<std::string::size_type>(is.rdbuf()->in_avail())); + char c; + while(is.get(c)) + { + if(s.capacity() == s.size()) + s.reserve(s.capacity() * 3); + s.append(1, c); + } +} + +int main(int argc, const char** argv) +{ + std::string text; + for(int i = 1; i < argc; ++i) + { + cout << "Processing file " << argv[i] << endl; + map_type m; + std::ifstream fs(argv[i]); + load_file(text, fs); + fs.close(); + IndexClasses(m, text); + cout << m.size() << " matches found" << endl; + map_type::iterator c, d; + c = m.begin(); + d = m.end(); + while(c != d) + { + cout << "class \"" << (*c).first << "\" found at index: " << (*c).second << endl; + ++c; + } + } + return 0; +} + + + + + + + + + diff --git a/src/boost/libs/regex/example/snippets/regex_split_example_1.cpp b/src/boost/libs/regex/example/snippets/regex_split_example_1.cpp new file mode 100644 index 00000000..df2ef96b --- /dev/null +++ b/src/boost/libs/regex/example/snippets/regex_split_example_1.cpp @@ -0,0 +1,76 @@ +/* + * + * Copyright (c) 1998-2002 + * John Maddock + * + * Use, modification and distribution are subject to the + * Boost Software License, Version 1.0. (See accompanying file + * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + * + */ + + /* + * LOCATION: see http://www.boost.org for most recent version. + * FILE regex_split_example_1.cpp + * VERSION see <boost/version.hpp> + * DESCRIPTION: regex_split example: split a string into tokens. + */ + + +#include <boost/regex.hpp> +#include <list> + + +unsigned tokenise(std::list<std::string>& l, std::string& s) +{ + return boost::regex_split(std::back_inserter(l), s); +} + +#include <iostream> +using namespace std; + + +#if defined(BOOST_MSVC) || (defined(__BORLANDC__) && (__BORLANDC__ == 0x550)) +// +// problem with std::getline under MSVC6sp3 +istream& getline(istream& is, std::string& s) +{ + s.erase(); + char c = static_cast<char>(is.get()); + while(c != '\n') + { + s.append(1, c); + c = static_cast<char>(is.get()); + } + return is; +} +#endif + + +int main(int argc, const char*[]) +{ + string s; + list<string> l; + do{ + if(argc == 1) + { + cout << "Enter text to split (or \"quit\" to exit): "; + getline(cin, s); + if(s == "quit") break; + } + else + s = "This is a string of tokens"; + unsigned result = tokenise(l, s); + cout << result << " tokens found" << endl; + cout << "The remaining text is: \"" << s << "\"" << endl; + while(l.size()) + { + s = *(l.begin()); + l.pop_front(); + cout << s << endl; + } + }while(argc == 1); + return 0; +} + + diff --git a/src/boost/libs/regex/example/snippets/regex_split_example_2.cpp b/src/boost/libs/regex/example/snippets/regex_split_example_2.cpp new file mode 100644 index 00000000..ad735022 --- /dev/null +++ b/src/boost/libs/regex/example/snippets/regex_split_example_2.cpp @@ -0,0 +1,86 @@ +/* + * + * Copyright (c) 1998-2002 + * John Maddock + * + * Use, modification and distribution are subject to the + * Boost Software License, Version 1.0. (See accompanying file + * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + * + */ + + /* + * LOCATION: see http://www.boost.org for most recent version. + * FILE regex_split_example_2.cpp + * VERSION see <boost/version.hpp> + * DESCRIPTION: regex_split example: spit out linked URL's. + */ + + +#include <boost/regex.hpp> +#include <list> +#include <fstream> +#include <iostream> +#include <iterator> + +boost::regex e("<\\s*A\\s+[^>]*href\\s*=\\s*\"([^\"]*)\"", + boost::regex::normal | boost::regbase::icase); + +void load_file(std::string& s, std::istream& is) +{ + s.erase(); + if(is.bad()) return; + // + // attempt to grow string buffer to match file size, + // this doesn't always work... + s.reserve(static_cast<std::string::size_type>(is.rdbuf()->in_avail())); + char c; + while(is.get(c)) + { + // use logarithmic growth stategy, in case + // in_avail (above) returned zero: + if(s.capacity() == s.size()) + s.reserve(s.capacity() * 3); + s.append(1, c); + } +} + +int main(int argc, char** argv) +{ + std::string s; + std::list<std::string> l; + int i; + for(i = 1; i < argc; ++i) + { + std::cout << "Findings URL's in " << argv[i] << ":" << std::endl; + s.erase(); + std::ifstream is(argv[i]); + load_file(s, is); + is.close(); + boost::regex_split(std::back_inserter(l), s, e); + while(l.size()) + { + s = *(l.begin()); + l.pop_front(); + std::cout << s << std::endl; + } + } + // + // alternative method: + // split one match at a time and output direct to + // cout via ostream_iterator<std::string>.... + // + for(i = 1; i < argc; ++i) + { + std::cout << "Findings URL's in " << argv[i] << ":" << std::endl; + s.erase(); + std::ifstream is(argv[i]); + load_file(s, is); + is.close(); + while(boost::regex_split(std::ostream_iterator<std::string>(std::cout), s, e, boost::match_default, 1)) std::cout << std::endl; + } + + return 0; +} + + diff --git a/src/boost/libs/regex/example/snippets/regex_token_iterator_eg_1.cpp b/src/boost/libs/regex/example/snippets/regex_token_iterator_eg_1.cpp new file mode 100644 index 00000000..f1ecb44d --- /dev/null +++ b/src/boost/libs/regex/example/snippets/regex_token_iterator_eg_1.cpp @@ -0,0 +1,72 @@ +/* + * + * Copyright (c) 12003 + * John Maddock + * + * Use, modification and distribution are subject to the + * Boost Software License, Version 1.0. (See accompanying file + * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + * + */ + + /* + * LOCATION: see http://www.boost.org for most recent version. + * FILE regex_token_iterator_example_1.cpp + * VERSION see <boost/version.hpp> + * DESCRIPTION: regex_token_iterator example: split a string into tokens. + */ + + +#include <boost/regex.hpp> + +#include <iostream> +using namespace std; + + +#if defined(BOOST_MSVC) || (defined(__BORLANDC__) && (__BORLANDC__ == 0x550)) +// +// problem with std::getline under MSVC6sp3 +istream& getline(istream& is, std::string& s) +{ + s.erase(); + char c = static_cast<char>(is.get()); + while(c != '\n') + { + s.append(1, c); + c = static_cast<char>(is.get()); + } + return is; +} +#endif + + +int main(int argc, const char*[]) +{ + string s; + do{ + if(argc == 1) + { + cout << "Enter text to split (or \"quit\" to exit): "; + getline(cin, s); + if(s == "quit") break; + } + else + s = "This is a string of tokens"; + + boost::regex re("\\s+"); + boost::sregex_token_iterator i(s.begin(), s.end(), re, -1); + boost::sregex_token_iterator j; + + unsigned count = 0; + while(i != j) + { + cout << *i++ << endl; + count++; + } + cout << "There were " << count << " tokens found." << endl; + + }while(argc == 1); + return 0; +} + + diff --git a/src/boost/libs/regex/example/snippets/regex_token_iterator_eg_2.cpp b/src/boost/libs/regex/example/snippets/regex_token_iterator_eg_2.cpp new file mode 100644 index 00000000..3458ce4d --- /dev/null +++ b/src/boost/libs/regex/example/snippets/regex_token_iterator_eg_2.cpp @@ -0,0 +1,89 @@ +/* + * + * Copyright (c) 2003 + * John Maddock + * + * Use, modification and distribution are subject to the + * Boost Software License, Version 1.0. (See accompanying file + * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + * + */ + + /* + * LOCATION: see http://www.boost.org for most recent version. + * FILE regex_token_iterator_example_2.cpp + * VERSION see <boost/version.hpp> + * DESCRIPTION: regex_token_iterator example: spit out linked URL's. + */ + + +#include <boost/regex.hpp> +#include <fstream> +#include <iostream> +#include <iterator> + +boost::regex e("<\\s*A\\s+[^>]*href\\s*=\\s*\"([^\"]*)\"", + boost::regex::normal | boost::regbase::icase); + +void load_file(std::string& s, std::istream& is) +{ + s.erase(); + if(is.bad()) return; + // + // attempt to grow string buffer to match file size, + // this doesn't always work... + s.reserve(static_cast<std::string::size_type>(is.rdbuf()->in_avail())); + char c; + while(is.get(c)) + { + // use logarithmic growth stategy, in case + // in_avail (above) returned zero: + if(s.capacity() == s.size()) + s.reserve(s.capacity() * 3); + s.append(1, c); + } +} + +int main(int argc, char** argv) +{ + std::string s; + int i; + for(i = 1; i < argc; ++i) + { + std::cout << "Findings URL's in " << argv[i] << ":" << std::endl; + s.erase(); + std::ifstream is(argv[i]); + load_file(s, is); + is.close(); + boost::sregex_token_iterator i(s.begin(), s.end(), e, 1); + boost::sregex_token_iterator j; + while(i != j) + { + std::cout << *i++ << std::endl; + } + } + // + // alternative method: + // test the array-literal constructor, and split out the whole + // match as well as $1.... + // + for(i = 1; i < argc; ++i) + { + std::cout << "Findings URL's in " << argv[i] << ":" << std::endl; + s.erase(); + std::ifstream is(argv[i]); + load_file(s, is); + is.close(); + const int subs[] = {1, 0,}; + boost::sregex_token_iterator i(s.begin(), s.end(), e, subs); + boost::sregex_token_iterator j; + while(i != j) + { + std::cout << *i++ << std::endl; + } + } + + return 0; +} + + |