]> git.tdb.fi Git - builder.git/blob - source/pattern.cpp
Save caches before starting the build
[builder.git] / source / pattern.cpp
1 #include <stdexcept>
2 #include "pattern.h"
3
4 using namespace std;
5
6 Pattern::Pattern(const string &pat)
7 {
8         string::size_type percent = pat.find('%');
9         if(percent==string::npos)
10                 throw invalid_argument("No percent sign in pattern");
11         prefix = pat.substr(0, percent);
12         suffix = pat.substr(percent+1);
13 }
14
15 string Pattern::apply(const string &body) const
16 {
17         string result = body;
18         if(body.compare(0, prefix.size(), prefix))
19                 result = prefix+result;
20         if(body.size()<=suffix.size() || body.compare(body.size()-suffix.size(), suffix.size(), suffix))
21                 result += suffix;
22         return result;
23 }
24
25 list<string> Pattern::apply_list(const list<Pattern> &patterns, const string &body)
26 {
27         list<string> result;
28         for(list<Pattern>::const_iterator i=patterns.begin(); i!=patterns.end(); ++i)
29                 result.push_back(i->apply(body));
30         return result;
31 }