Minor cleanup of aidl.cpp

1) Move some helper functions to parse_helpers.cpp
2) Refactor some helper functions to take advantage of C++ built ins
3) Add unittest coverage of refactored logic
4) clang-format -i -style=Google parse_helpers.cpp
5) Move remaining helper functions of aidl.cpp into anonymous namespace

Change-Id: I3a2cf7be113ac7f71a1a3502b12a8ed910509546
diff --git a/parse_helpers.cpp b/parse_helpers.cpp
new file mode 100644
index 0000000..0aae3a0
--- /dev/null
+++ b/parse_helpers.cpp
@@ -0,0 +1,89 @@
+/*
+ * Copyright (C) 2015, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "parse_helpers.h"
+
+#include <algorithm>
+#include <cctype>
+#include <cstring>
+#include <stdlib.h>
+#include <string>
+#include <vector>
+
+#include "aidl_language.h"
+
+namespace android {
+namespace aidl {
+
+char* parse_import_statement(const char* text) {
+  const char* end;
+  int len;
+
+  while (isspace(*text)) {
+    text++;
+  }
+  while (!isspace(*text)) {
+    text++;
+  }
+  while (isspace(*text)) {
+    text++;
+  }
+  end = text;
+  while (!isspace(*end) && *end != ';') {
+    end++;
+  }
+  len = end - text;
+
+  char* rv = (char*)malloc(len + 1);
+  memcpy(rv, text, len);
+  rv[len] = '\0';
+
+  return rv;
+}
+
+int convert_direction(const char* direction) {
+  if (direction == NULL) {
+    return IN_PARAMETER;
+  }
+  if (0 == strcmp(direction, "in")) {
+    return IN_PARAMETER;
+  }
+  if (0 == strcmp(direction, "out")) {
+    return OUT_PARAMETER;
+  }
+  return INOUT_PARAMETER;
+}
+
+bool is_java_keyword(const char* str) {
+  static const std::vector<std::string> kJavaKeywords{
+      "abstract",   "assert",       "boolean",   "break",      "byte",
+      "case",       "catch",        "char",      "class",      "const",
+      "continue",   "default",      "do",        "double",     "else",
+      "enum",       "extends",      "final",     "finally",    "float",
+      "for",        "goto",         "if",        "implements", "import",
+      "instanceof", "int",          "interface", "long",       "native",
+      "new",        "package",      "private",   "protected",  "public",
+      "return",     "short",        "static",    "strictfp",   "super",
+      "switch",     "synchronized", "this",      "throw",      "throws",
+      "transient",  "try",          "void",      "volatile",   "while",
+      "true",       "false",        "null",
+  };
+  return std::find(kJavaKeywords.begin(), kJavaKeywords.end(), str) !=
+      kJavaKeywords.end();
+}
+
+}  // namespace android
+}  // namespace aidl