Write regex string to lex.yy.cpp by escaping \ and ", prevent trigraphs, very long strings are represented by character arrays
| 3122 | |
| 3123 | /// Write regex string to lex.yy.cpp by escaping \ and ", prevent trigraphs, very long strings are represented by character arrays |
| 3124 | void Reflex::write_regex(const std::string *condition, const std::string& regex) |
| 3125 | { |
| 3126 | // output a string if start condition == NULL (--regexp-file option) or when the string is not too long |
| 3127 | if (!condition || |
| 3128 | #ifdef OS_WIN |
| 3129 | regex.size() <= 16384 |
| 3130 | #else |
| 3131 | regex.size() <= 65536 |
| 3132 | #endif |
| 3133 | ) |
| 3134 | { |
| 3135 | if (condition) |
| 3136 | *out << " static const char *REGEX_" << *condition << " = "; |
| 3137 | *out << "\""; |
| 3138 | int c = '\0'; |
| 3139 | for (std::string::const_iterator i = regex.begin(); i != regex.end(); ++i) |
| 3140 | { |
| 3141 | if (*i == '\\' || *i == '"' || (*i == '?' && c == '?')) |
| 3142 | *out << "\\"; |
| 3143 | *out << *i; |
| 3144 | c = *i; |
| 3145 | } |
| 3146 | *out << "\""; |
| 3147 | } |
| 3148 | else |
| 3149 | { |
| 3150 | if (condition) |
| 3151 | *out << " static const char REGEX_" << *condition << "[" << regex.size() + 1 << "] = "; |
| 3152 | *out << "{ "; |
| 3153 | for (std::string::const_iterator i = regex.begin(); i != regex.end(); ++i) |
| 3154 | { |
| 3155 | if (*i == '\\') |
| 3156 | *out << "'\\\\',"; |
| 3157 | else if (*i == '\'') |
| 3158 | *out << "'\\'',"; |
| 3159 | else if (std::isprint(static_cast<unsigned char>(*i))) |
| 3160 | *out << "'" << *i << "', "; |
| 3161 | else |
| 3162 | *out << static_cast<int>(*i) << ", "; |
| 3163 | } |
| 3164 | *out << "0 }"; |
| 3165 | } |
| 3166 | if (condition) |
| 3167 | *out << ";\n"; |
| 3168 | } |