| 242 | namespace |
| 243 | { |
| 244 | bool StringMatch(juce::String pattern, juce::String target) |
| 245 | { |
| 246 | /*if (pattern.endsWithChar('*')) |
| 247 | { |
| 248 | ppattern = pattern.removeCharacters("*"); |
| 249 | return target.startsWith(pattern); |
| 250 | } |
| 251 | else |
| 252 | { |
| 253 | return pattern == target; |
| 254 | }*/ |
| 255 | |
| 256 | int m = pattern.length(); |
| 257 | int n = target.length(); |
| 258 | |
| 259 | // empty pattern can only match with |
| 260 | // empty string |
| 261 | if (m == 0) |
| 262 | return (n == 0); |
| 263 | |
| 264 | // lookup table for storing results of |
| 265 | // subproblems |
| 266 | std::vector<std::vector<bool>> lookup(n + 1, std::vector<bool>(m + 1)); |
| 267 | |
| 268 | // empty pattern can match with empty string |
| 269 | lookup[0][0] = true; |
| 270 | |
| 271 | // Only '*' can match with empty string |
| 272 | for (int j = 1; j <= m; j++) |
| 273 | if (pattern[j - 1] == '*') |
| 274 | lookup[0][j] = lookup[0][j - 1]; |
| 275 | |
| 276 | // fill the table in bottom-up fashion |
| 277 | for (int i = 1; i <= n; i++) |
| 278 | { |
| 279 | for (int j = 1; j <= m; j++) |
| 280 | { |
| 281 | // Two cases if we see a '*' |
| 282 | // a) We ignore ‘*’ character and move |
| 283 | // to next character in the pattern, |
| 284 | // i.e., ‘*’ indicates an empty sequence. |
| 285 | // b) '*' character matches with ith |
| 286 | // character in input |
| 287 | if (pattern[j - 1] == '*') |
| 288 | lookup[i][j] = lookup[i][j - 1] || lookup[i - 1][j]; |
| 289 | |
| 290 | else if (target[i - 1] == pattern[j - 1]) |
| 291 | lookup[i][j] = lookup[i - 1][j - 1]; |
| 292 | |
| 293 | // If characters don't match |
| 294 | else |
| 295 | lookup[i][j] = false; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | return lookup[n][m]; |
| 300 | } |
| 301 | } |