| 363 | explicit Impl(const std::string& p) : pattern_(std::regex(PatternToRegex(p))) {} |
| 364 | |
| 365 | static std::string PatternToRegex(const std::string& p) { |
| 366 | std::string special_chars = "()[]{}+-|^$\\.&~# \t\n\r\v\f"; |
| 367 | std::string transformed; |
| 368 | auto it = p.begin(); |
| 369 | while (it != p.end()) { |
| 370 | if (*it == '\\') { |
| 371 | transformed += '\\'; |
| 372 | if (++it != p.end()) { |
| 373 | transformed += *it; |
| 374 | } |
| 375 | } else if (*it == '*') { |
| 376 | transformed += "[^/]*"; |
| 377 | } else if (*it == '?') { |
| 378 | transformed += "[^/]"; |
| 379 | } else if (special_chars.find(*it) != std::string::npos) { |
| 380 | transformed += "\\"; |
| 381 | transformed += *it; |
| 382 | } else { |
| 383 | transformed += *it; |
| 384 | } |
| 385 | it++; |
| 386 | } |
| 387 | return transformed; |
| 388 | } |
| 389 | }; |
| 390 | |
| 391 | Globber::Globber(std::string pattern) : impl_(new Impl(pattern)) {} |