refactor duplication (shouldSkip and skip_name) into a utility function

R=caryclark@google.com, reed@google.com

Committed: https://code.google.com/p/skia/source/detail?r=10280

Review URL: https://codereview.chromium.org/19807005

git-svn-id: http://skia.googlecode.com/svn/trunk@10317 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/bench/benchmain.cpp b/bench/benchmain.cpp
index c0f6da9..1ace7d1 100644
--- a/bench/benchmain.cpp
+++ b/bench/benchmain.cpp
@@ -22,6 +22,7 @@
 #include "SkBenchLogger.h"
 #include "SkBenchmark.h"
 #include "SkCanvas.h"
+#include "SkCommandLineFlags.h"
 #include "SkDeferredCanvas.h"
 #include "SkDevice.h"
 #include "SkColorPriv.h"
@@ -270,38 +271,6 @@
     return -1;
 }
 
-static bool skip_name(const SkTDArray<const char*> array, const char name[]) {
-    // FIXME: this duplicates the logic in skia_test.cpp, gmmain.cpp -- consolidate
-    int count = array.count();
-    size_t testLen = strlen(name);
-    bool anyExclude = count == 0;
-    for (int i = 0; i < array.count(); ++i) {
-        const char* matchName = array[i];
-        size_t matchLen = strlen(matchName);
-        bool matchExclude, matchStart, matchEnd;
-        if ((matchExclude = matchName[0] == '~')) {
-            anyExclude = true;
-            matchName++;
-            matchLen--;
-        }
-        if ((matchStart = matchName[0] == '^')) {
-            matchName++;
-            matchLen--;
-        }
-        if ((matchEnd = matchName[matchLen - 1] == '$')) {
-            matchLen--;
-        }
-        if (matchStart ? (!matchEnd || matchLen == testLen)
-                && strncmp(name, matchName, matchLen) == 0
-                : matchEnd ? matchLen <= testLen
-                && strncmp(name + testLen - matchLen, matchName, matchLen) == 0
-                : strstr(name, matchName) != 0) {
-            return matchExclude;
-        }
-    }
-    return !anyExclude;
-}
-
 static void help() {
     SkString configsStr;
     static const size_t kConfigCount = SK_ARRAY_COUNT(gConfigs);
@@ -771,7 +740,7 @@
         }
 
         // only run benchmarks if their name contains matchStr
-        if (skip_name(fMatches, bench->getName())) {
+        if (SkCommandLineFlags::ShouldSkip(fMatches, bench->getName())) {
             continue;
         }
 
diff --git a/gm/gmmain.cpp b/gm/gmmain.cpp
index 01eaaeb..958a511 100644
--- a/gm/gmmain.cpp
+++ b/gm/gmmain.cpp
@@ -1410,38 +1410,6 @@
     return -1;
 }
 
-static bool skip_name(SkCommandLineFlags::StringArray array, const char name[]) {
-    // FIXME: this duplicates the logic in test/skia_test.cpp -- consolidate
-    int count = array.count();
-    size_t testLen = strlen(name);
-    bool anyExclude = count == 0;
-    for (int i = 0; i < array.count(); ++i) {
-        const char* matchName = array[i];
-        size_t matchLen = strlen(matchName);
-        bool matchExclude, matchStart, matchEnd;
-        if ((matchExclude = matchName[0] == '~')) {
-            anyExclude = true;
-            matchName++;
-            matchLen--;
-        }
-        if ((matchStart = matchName[0] == '^')) {
-            matchName++;
-            matchLen--;
-        }
-        if ((matchEnd = matchName[matchLen - 1] == '$')) {
-            matchLen--;
-        }
-        if (matchStart ? (!matchEnd || matchLen == testLen)
-                && strncmp(name, matchName, matchLen) == 0
-                : matchEnd ? matchLen <= testLen
-                && strncmp(name + testLen - matchLen, matchName, matchLen) == 0
-                : strstr(name, matchName) != 0) {
-            return matchExclude;
-        }
-    }
-    return !anyExclude;
-}
-
 namespace skiagm {
 #if SK_SUPPORT_GPU
 SkAutoTUnref<GrContext> gGrContext;
@@ -2076,7 +2044,12 @@
         }
 
         const char* shortName = gm->shortName();
-        if (skip_name(FLAGS_match, shortName)) {
+
+        SkTDArray<const char*> matchStrs;
+        for (int i = 0; i < FLAGS_match.count(); ++i) {
+            matchStrs.push(FLAGS_match[i]);
+        }
+        if (SkCommandLineFlags::ShouldSkip(matchStrs, shortName)) {
             continue;
         }
 
diff --git a/gyp/bench.gyp b/gyp/bench.gyp
index 27f80ce..040f7a4 100644
--- a/gyp/bench.gyp
+++ b/gyp/bench.gyp
@@ -19,6 +19,7 @@
       'dependencies': [
         'skia_lib.gyp:skia_lib',
         'bench_timer',
+        'flags.gyp:flags',
       ],
       'conditions': [
         ['skia_gpu == 1',
diff --git a/tests/skia_test.cpp b/tests/skia_test.cpp
index da63d42..7a8308f 100644
--- a/tests/skia_test.cpp
+++ b/tests/skia_test.cpp
@@ -159,44 +159,6 @@
     int32_t* fFailCount;
 };
 
-/* Takes a list of the form [~][^]match[$]
-   ~ causes a matching test to always be skipped
-   ^ requires the start of the test to match
-   $ requires the end of the test to match
-   ^ and $ requires an exact match
-   If a test does not match any list entry, it is skipped unless some list entry starts with ~
- */
-static bool shouldSkip(const char* testName) {
-    int count = FLAGS_match.count();
-    size_t testLen = strlen(testName);
-    bool anyExclude = count == 0;
-    for (int index = 0; index < count; ++index) {
-        const char* matchName = FLAGS_match[index];
-        size_t matchLen = strlen(matchName);
-        bool matchExclude, matchStart, matchEnd;
-        if ((matchExclude = matchName[0] == '~')) {
-            anyExclude = true;
-            matchName++;
-            matchLen--;
-        }
-        if ((matchStart = matchName[0] == '^')) {
-            matchName++;
-            matchLen--;
-        }
-        if ((matchEnd = matchName[matchLen - 1] == '$')) {
-            matchLen--;
-        }
-        if (matchStart ? (!matchEnd || matchLen == testLen)
-                && strncmp(testName, matchName, matchLen) == 0
-                : matchEnd ? matchLen <= testLen
-                && strncmp(testName + testLen - matchLen, matchName, matchLen) == 0
-                : strstr(testName, matchName) != 0) {
-            return matchExclude;
-        }
-    }
-    return !anyExclude;
-}
-
 int tool_main(int argc, char** argv);
 int tool_main(int argc, char** argv) {
     SkCommandLineFlags::SetUsage("");
@@ -244,9 +206,16 @@
     int total = 0;
     int toRun = 0;
     Test* test;
+
+    SkTDArray<const char*> matchStrs;
+    for(int i = 0; i < FLAGS_match.count(); ++i) {
+        matchStrs.push(FLAGS_match[i]);
+    }
+
     while ((test = iter.next()) != NULL) {
         SkAutoTDelete<Test> owned(test);
-        if(!shouldSkip(test->getName())) {
+
+        if(!SkCommandLineFlags::ShouldSkip(matchStrs, test->getName())) {
             toRun++;
         }
         total++;
@@ -262,7 +231,7 @@
     SkTArray<Test*> unsafeTests;  // Always passes ownership to an SkTestRunnable
     for (int i = 0; i < total; i++) {
         SkAutoTDelete<Test> test(iter.next());
-        if (shouldSkip(test->getName())) {
+        if (SkCommandLineFlags::ShouldSkip(matchStrs, test->getName())) {
             ++skipCount;
         } else if (!test->isThreadsafe()) {
             unsafeTests.push_back() = test.detach();
diff --git a/tools/flags/SkCommandLineFlags.cpp b/tools/flags/SkCommandLineFlags.cpp
index ed8db8e..b088b4e 100644
--- a/tools/flags/SkCommandLineFlags.cpp
+++ b/tools/flags/SkCommandLineFlags.cpp
@@ -303,3 +303,34 @@
         exit(0);
     }
 }
+
+bool SkCommandLineFlags::ShouldSkip(const SkTDArray<const char*>& strings, const char* name) {
+    int count = strings.count();
+    size_t testLen = strlen(name);
+    bool anyExclude = count == 0;
+    for (int i = 0; i < strings.count(); ++i) {
+        const char* matchName = strings[i];
+        size_t matchLen = strlen(matchName);
+        bool matchExclude, matchStart, matchEnd;
+        if ((matchExclude = matchName[0] == '~')) {
+            anyExclude = true;
+            matchName++;
+            matchLen--;
+        }
+        if ((matchStart = matchName[0] == '^')) {
+            matchName++;
+            matchLen--;
+        }
+        if ((matchEnd = matchName[matchLen - 1] == '$')) {
+            matchLen--;
+        }
+        if (matchStart ? (!matchEnd || matchLen == testLen)
+                && strncmp(name, matchName, matchLen) == 0
+                : matchEnd ? matchLen <= testLen
+                && strncmp(name + testLen - matchLen, matchName, matchLen) == 0
+                : strstr(name, matchName) != 0) {
+            return matchExclude;
+        }
+    }
+    return !anyExclude;
+}
diff --git a/tools/flags/SkCommandLineFlags.h b/tools/flags/SkCommandLineFlags.h
index d0e7450..51af933 100644
--- a/tools/flags/SkCommandLineFlags.h
+++ b/tools/flags/SkCommandLineFlags.h
@@ -10,6 +10,7 @@
 
 #include "SkString.h"
 #include "SkTArray.h"
+#include "SkTDArray.h"
 
 /**
  *  Including this file (and compiling SkCommandLineFlags.cpp) provides command line
@@ -90,7 +91,6 @@
  *  strings) so that a flag can be followed by multiple parameters.
  */
 
-
 class SkFlagInfo;
 
 class SkCommandLineFlags {
@@ -108,6 +108,15 @@
      */
     static void Parse(int argc, char** argv);
 
+    /* Takes a list of the form [~][^]match[$]
+     ~ causes a matching test to always be skipped
+     ^ requires the start of the test to match
+     $ requires the end of the test to match
+     ^ and $ requires an exact match
+     If a test does not match any list entry, it is skipped unless some list entry starts with ~
+    */
+    static bool ShouldSkip(const SkTDArray<const char*>& strings, const char* name);
+
     /**
      *  Custom class for holding the arguments for a string flag.
      *  Publicly only has accessors so the strings cannot be modified.