Reduce StringPiece usage a bit.

Various easy cases, in case we want to move towards making the StringPiece
constructors explicit.

Change-Id: I49e5efc5b787f847ab4cb12d66d1d16aed03fa67
diff --git a/src/check_jni.cc b/src/check_jni.cc
index 1e08022..84c7376 100644
--- a/src/check_jni.cc
+++ b/src/check_jni.cc
@@ -120,16 +120,15 @@
   // such as NewByteArray.
   // If -verbose:third-party-jni is on, we want to log any JNI function calls
   // made by a third-party native method.
-  std::string classNameStr(MethodHelper(method).GetDeclaringClassDescriptor());
-  if (!vm->trace.empty() && classNameStr.find(vm->trace) != std::string::npos) {
+  std::string className(MethodHelper(method).GetDeclaringClassDescriptor());
+  if (!vm->trace.empty() && className.find(vm->trace) != std::string::npos) {
     return true;
   }
   if (VLOG_IS_ON(third_party_jni)) {
     // Return true if we're trying to log all third-party JNI activity and 'method' doesn't look
     // like part of Android.
-    StringPiece className(classNameStr);
     for (size_t i = 0; gBuiltInPrefixes[i] != NULL; ++i) {
-      if (className.starts_with(gBuiltInPrefixes[i])) {
+      if (StartsWith(className, gBuiltInPrefixes[i])) {
         return false;
       }
     }
diff --git a/src/dex2oat.cc b/src/dex2oat.cc
index f8c342e..23c7416 100644
--- a/src/dex2oat.cc
+++ b/src/dex2oat.cc
@@ -126,7 +126,7 @@
     while (image_classes_file->good()) {
       std::string dot;
       std::getline(*image_classes_file.get(), dot);
-      if (StringPiece(dot).starts_with("#") || dot.empty()) {
+      if (StartsWith(dot, "#") || dot.empty()) {
         continue;
       }
       std::string descriptor(DotToDescriptor(dot.c_str()));
diff --git a/src/jni_internal.cc b/src/jni_internal.cc
index 0921afb..8b61226 100644
--- a/src/jni_internal.cc
+++ b/src/jni_internal.cc
@@ -2668,8 +2668,7 @@
   Runtime::Options options;
   for (int i = 0; i < args->nOptions; ++i) {
     JavaVMOption* option = &args->options[i];
-    options.push_back(std::make_pair(StringPiece(option->optionString),
-                                     option->extraInfo));
+    options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
   }
   bool ignore_unrecognized = args->ignoreUnrecognized;
   Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
diff --git a/src/runtime.cc b/src/runtime.cc
index cfc4c59..99dc0d8 100644
--- a/src/runtime.cc
+++ b/src/runtime.cc
@@ -235,12 +235,12 @@
   return 0;
 }
 
-size_t ParseIntegerOrDie(const StringPiece& s) {
-  StringPiece::size_type colon = s.find(':');
-  if (colon == StringPiece::npos) {
+size_t ParseIntegerOrDie(const std::string& s) {
+  std::string::size_type colon = s.find(':');
+  if (colon == std::string::npos) {
     LOG(FATAL) << "Missing integer: " << s;
   }
-  const char* begin = &s.data()[colon + 1];
+  const char* begin = &s[colon + 1];
   char* end;
   size_t result = strtoul(begin, &end, 10);
   if (begin == end || *end != '\0') {
@@ -293,12 +293,12 @@
   parsed->hook_abort_ = abort;
 
   for (size_t i = 0; i < options.size(); ++i) {
-    const StringPiece& option = options[i].first;
+    const std::string option(options[i].first);
     if (true && options[0].first == "-Xzygote") {
       LOG(INFO) << "option[" << i << "]=" << option;
     }
-    if (option.starts_with("-Xbootclasspath:")) {
-      parsed->boot_class_path_ = option.substr(strlen("-Xbootclasspath:")).data();
+    if (StartsWith(option, "-Xbootclasspath:")) {
+      parsed->boot_class_path_ = option.substr(strlen("-Xbootclasspath:"));
     } else if (option == "-classpath" || option == "-cp") {
       // TODO: support -Djava.class.path
       i++;
@@ -307,21 +307,20 @@
         LOG(FATAL) << "Missing required class path value for " << option;
         return NULL;
       }
-      const StringPiece& value = options[i].first;
-      parsed->class_path_ = value.data();
-    } else if (option.starts_with("-Ximage:")) {
-      parsed->image_ = option.substr(strlen("-Ximage:")).data();
-    } else if (option.starts_with("-Xcheck:jni")) {
+      parsed->class_path_ = options[i].first;
+    } else if (StartsWith(option, "-Ximage:")) {
+      parsed->image_ = option.substr(strlen("-Ximage:"));
+    } else if (StartsWith(option, "-Xcheck:jni")) {
       parsed->check_jni_ = true;
-    } else if (option.starts_with("-Xrunjdwp:") || option.starts_with("-agentlib:jdwp=")) {
-      std::string tail(option.substr(option[1] == 'X' ? 10 : 15).ToString());
+    } else if (StartsWith(option, "-Xrunjdwp:") || StartsWith(option, "-agentlib:jdwp=")) {
+      std::string tail(option.substr(option[1] == 'X' ? 10 : 15));
       if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
         LOG(FATAL) << "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
                    << "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n";
         return NULL;
       }
-    } else if (option.starts_with("-Xms")) {
-      size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
+    } else if (StartsWith(option, "-Xms")) {
+      size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).c_str(), 1024);
       if (size == 0) {
         if (ignore_unrecognized) {
           continue;
@@ -331,8 +330,8 @@
         return NULL;
       }
       parsed->heap_initial_size_ = size;
-    } else if (option.starts_with("-Xmx")) {
-      size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024);
+    } else if (StartsWith(option, "-Xmx")) {
+      size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).c_str(), 1024);
       if (size == 0) {
         if (ignore_unrecognized) {
           continue;
@@ -342,8 +341,8 @@
         return NULL;
       }
       parsed->heap_maximum_size_ = size;
-    } else if (option.starts_with("-XX:HeapGrowthLimit=")) {
-      size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).data(), 1024);
+    } else if (StartsWith(option, "-XX:HeapGrowthLimit=")) {
+      size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).c_str(), 1024);
       if (size == 0) {
         if (ignore_unrecognized) {
           continue;
@@ -353,8 +352,8 @@
         return NULL;
       }
       parsed->heap_growth_limit_ = size;
-    } else if (option.starts_with("-Xss")) {
-      size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
+    } else if (StartsWith(option, "-Xss")) {
+      size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).c_str(), 1);
       if (size == 0) {
         if (ignore_unrecognized) {
           continue;
@@ -364,17 +363,17 @@
         return NULL;
       }
       parsed->stack_size_ = size;
-    } else if (option.starts_with("-D")) {
-      parsed->properties_.push_back(option.substr(strlen("-D")).data());
-    } else if (option.starts_with("-Xjnitrace:")) {
-      parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:")).data();
+    } else if (StartsWith(option, "-D")) {
+      parsed->properties_.push_back(option.substr(strlen("-D")));
+    } else if (StartsWith(option, "-Xjnitrace:")) {
+      parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:"));
     } else if (option == "compiler") {
       parsed->is_compiler_ = true;
     } else if (option == "-Xzygote") {
       parsed->is_zygote_ = true;
-    } else if (option.starts_with("-verbose:")) {
+    } else if (StartsWith(option, "-verbose:")) {
       std::vector<std::string> verbose_options;
-      Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options);
+      Split(option.substr(strlen("-verbose:")), ',', verbose_options);
       for (size_t i = 0; i < verbose_options.size(); ++i) {
         if (verbose_options[i] == "class") {
           gLogVerbosity.class_linker = true;
@@ -400,14 +399,14 @@
           LOG(WARNING) << "Ignoring unknown -verbose option: " << verbose_options[i];
         }
       }
-    } else if (option.starts_with("-Xjnigreflimit:")) {
+    } else if (StartsWith(option, "-Xjnigreflimit:")) {
       parsed->jni_globals_max_ = ParseIntegerOrDie(option);
-    } else if (option.starts_with("-Xlockprofthreshold:")) {
+    } else if (StartsWith(option, "-Xlockprofthreshold:")) {
       parsed->lock_profiling_threshold_ = ParseIntegerOrDie(option);
-    } else if (option.starts_with("-Xstacktracefile:")) {
+    } else if (StartsWith(option, "-Xstacktracefile:")) {
 // always show stack traces in debug builds
 #ifdef NDEBUG
-      parsed->stack_trace_file_ = option.substr(strlen("-Xstacktracefile:")).data();
+      parsed->stack_trace_file_ = option.substr(strlen("-Xstacktracefile:"));
 #endif
     } else if (option == "sensitiveThread") {
       parsed->hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(options[i].second);
diff --git a/src/runtime.h b/src/runtime.h
index e34a577..b284b46 100644
--- a/src/runtime.h
+++ b/src/runtime.h
@@ -53,7 +53,7 @@
 class Runtime {
  public:
 
-  typedef std::vector<std::pair<StringPiece, const void*> > Options;
+  typedef std::vector<std::pair<std::string, const void*> > Options;
 
   class ParsedOptions {
    public:
@@ -111,9 +111,6 @@
     return instance_;
   }
 
-  // Compiles a dex file.
-  static void Compile(const StringPiece& filename);
-
   // Aborts semi-cleanly. Used in the implementation of LOG(FATAL), which most
   // callers should prefer.
   // This isn't marked ((noreturn)) because then gcc will merge multiple calls
diff --git a/src/utils.cc b/src/utils.cc
index 201834c..09a01c6 100644
--- a/src/utils.cc
+++ b/src/utils.cc
@@ -374,10 +374,12 @@
   return descriptor;
 }
 
-std::string DescriptorToDot(const StringPiece& descriptor) {
+std::string DescriptorToDot(const char* descriptor) {
+  size_t length = strlen(descriptor);
+  DCHECK_GT(length, 0U);
   DCHECK_EQ(descriptor[0], 'L');
-  DCHECK_EQ(descriptor[descriptor.size()-1], ';');
-  std::string dot(descriptor.substr(1, descriptor.size() - 2).ToString());
+  DCHECK_EQ(descriptor[length - 1], ';');
+  std::string dot(descriptor + 1, length - 2);
   std::replace(dot.begin(), dot.end(), '/', '.');
   return dot;
 }
@@ -670,6 +672,10 @@
 template std::string Join<const char*>(std::vector<const char*>& strings, char separator);
 template std::string Join<char*>(std::vector<char*>& strings, char separator);
 
+bool StartsWith(const std::string& s, const char* prefix) {
+  return s.compare(0, strlen(prefix), prefix) == 0;
+}
+
 void SetThreadName(const char* threadName) {
   ANNOTATE_THREAD_NAME(threadName); // For tsan.
 
@@ -760,7 +766,7 @@
   std::string art_cache(StringPrintf("%s/art-cache", GetAndroidData()));
 
   if (!OS::DirectoryExists(art_cache.c_str())) {
-    if (StringPiece(art_cache).starts_with("/tmp/")) {
+    if (StartsWith(art_cache, "/tmp/")) {
       int result = mkdir(art_cache.c_str(), 0700);
       if (result != 0) {
         LOG(FATAL) << "Failed to create art-cache directory " << art_cache;
diff --git a/src/utils.h b/src/utils.h
index 74a9802..39a5d4f 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -165,6 +165,9 @@
   return result;
 }
 
+// Tests whether 's' starts with 'prefix'.
+bool StartsWith(const std::string& s, const char* prefix);
+
 // Used to implement PrettyClass, PrettyField, PrettyMethod, and PrettyTypeOf,
 // one of which is probably more useful to you.
 // Returns a human-readable equivalent of 'descriptor'. So "I" would be "int",
@@ -213,7 +216,7 @@
 std::string DotToDescriptor(const char* class_name);
 
 // Turn "Ljava/lang/String;" into "java.lang.String".
-std::string DescriptorToDot(const StringPiece& descriptor);
+std::string DescriptorToDot(const char* descriptor);
 
 // Tests for whether 's' is a valid class name in the three common forms:
 bool IsValidBinaryClassName(const char* s);  // "java.lang.String"