[2444] | 1 |
|
---|
| 2 | #ifndef STRINGFORMAT_H_
|
---|
| 3 | #define STRINGFORMAT_H_
|
---|
| 4 |
|
---|
| 5 | #include "boost/xpressive/xpressive.hpp"
|
---|
| 6 |
|
---|
[2452] | 7 | namespace ariba {
|
---|
| 8 | namespace utility {
|
---|
| 9 | namespace string_format {
|
---|
| 10 |
|
---|
[2444] | 11 | using boost::xpressive::sregex;
|
---|
| 12 |
|
---|
[2452] | 13 | class regex_nav {
|
---|
| 14 | private:
|
---|
| 15 | typedef boost::xpressive::smatch _match;
|
---|
| 16 | typedef _match::nested_results_type nested_results;
|
---|
| 17 | typedef nested_results::const_iterator nested_iterator;
|
---|
| 18 | const _match& match;
|
---|
| 19 |
|
---|
[2444] | 20 | public:
|
---|
[2452] | 21 | regex_nav(const _match& match) :
|
---|
| 22 | match(match) {
|
---|
| 23 | }
|
---|
[2444] | 24 |
|
---|
[2452] | 25 | regex_nav() :
|
---|
| 26 | match(*((const _match*) NULL)) {
|
---|
| 27 | }
|
---|
[2444] | 28 |
|
---|
[2452] | 29 | bool matched() const {
|
---|
| 30 | return &match != NULL;
|
---|
| 31 | }
|
---|
[2444] | 32 |
|
---|
[2452] | 33 | regex_nav operator[] (const sregex& type) const {
|
---|
| 34 | const nested_results& nr = match.nested_results();
|
---|
| 35 | for (nested_iterator i = nr.begin(); i != nr.end(); i++) {
|
---|
| 36 | if (i->regex_id() == type.regex_id()) return regex_nav(*i);
|
---|
| 37 | }
|
---|
| 38 | return regex_nav();
|
---|
| 39 | }
|
---|
[2444] | 40 |
|
---|
[2452] | 41 | regex_nav operator[](int index) const {
|
---|
| 42 | const nested_results& nr = match.nested_results();
|
---|
| 43 | for (nested_iterator i = nr.begin(); i != nr.end() && index >= 0; i++) {
|
---|
| 44 | if (index == 0) return regex_nav(*i);
|
---|
| 45 | index--;
|
---|
| 46 | }
|
---|
| 47 | return regex_nav();
|
---|
| 48 | }
|
---|
[2444] | 49 |
|
---|
[2452] | 50 | int size() const {
|
---|
| 51 | return match.nested_results().size();
|
---|
| 52 | }
|
---|
[2444] | 53 |
|
---|
[2452] | 54 | std::string str() const {
|
---|
| 55 | if (!matched()) return std::string("<no match>");
|
---|
| 56 | return match[0].str();
|
---|
| 57 | }
|
---|
| 58 | };
|
---|
[2444] | 59 |
|
---|
[2452] | 60 | // regex: string
|
---|
| 61 | extern const sregex rstring;
|
---|
[2444] | 62 |
|
---|
[2452] | 63 | // regex: base64 encoding
|
---|
| 64 | extern const sregex rbase64;
|
---|
[2444] | 65 |
|
---|
[2452] | 66 | // regex: raw alphabet
|
---|
| 67 | extern const sregex rchars;
|
---|
| 68 |
|
---|
| 69 | // regex: integer
|
---|
| 70 | extern const sregex rint;
|
---|
| 71 |
|
---|
| 72 | // regex: binary label
|
---|
| 73 | extern const sregex rlabel;
|
---|
| 74 |
|
---|
| 75 | // regex: dot separated identifier
|
---|
| 76 | extern const sregex rid;
|
---|
| 77 |
|
---|
| 78 | // regex: "leaf" data
|
---|
| 79 | extern const sregex rdata;
|
---|
| 80 |
|
---|
| 81 | // regex: fields
|
---|
| 82 | extern const sregex rfield_label;
|
---|
| 83 | extern const sregex rfield;
|
---|
| 84 | extern const sregex rfields;
|
---|
| 85 |
|
---|
| 86 | // regex: objects
|
---|
| 87 | extern const sregex robject_data;
|
---|
| 88 | extern const sregex robject_id;
|
---|
| 89 | extern const sregex robject;
|
---|
| 90 | extern const sregex robjects;
|
---|
| 91 |
|
---|
| 92 | }}}
|
---|
| 93 |
|
---|
[2444] | 94 | #endif /* STRINGFORMAT_H_ */
|
---|