Merge remote-tracking branch 'aosp/upstream' into update_kati

* aosp/upstream:
  Fix crash due to std::string_view change
  Remove AppendString

Change-Id: Ic53c45ab9e39d378d44fbd8e35637a0be72ad34d
diff --git a/src/dep.cc b/src/dep.cc
index 5ba59fe..0391cb7 100644
--- a/src/dep.cc
+++ b/src/dep.cc
@@ -39,10 +39,9 @@
 static std::vector<std::unique_ptr<DepNode>> g_dep_node_pool;
 
 static Symbol ReplaceSuffix(Symbol s, Symbol newsuf) {
-  std::string r;
-  AppendString(StripExt(s.str()), &r);
+  std::string r{StripExt(s.str())};
   r += '.';
-  AppendString(newsuf.str(), &r);
+  r += newsuf.str();
   return Intern(r);
 }
 
diff --git a/src/func.cc b/src/func.cc
index 4af6662..2345808 100644
--- a/src/func.cc
+++ b/src/func.cc
@@ -134,11 +134,11 @@
     size_t found = str.find(pat, index);
     if (found == std::string::npos)
       break;
-    AppendString(std::string_view(str).substr(index, found - index), s);
-    AppendString(repl, s);
+    s->append(std::string_view(str).substr(index, found - index));
+    s->append(repl);
     index = found + pat.size();
   }
-  AppendString(std::string_view(str).substr(index), s);
+  s->append(std::string_view(str).substr(index));
 }
 
 void FindstringFunc(const std::vector<Value*>& args,
@@ -147,7 +147,7 @@
   const std::string&& find = args[0]->Eval(ev);
   const std::string&& in = args[1]->Eval(ev);
   if (in.find(find) != std::string::npos)
-    AppendString(find, s);
+    s->append(find);
 }
 
 void FilterFunc(const std::vector<Value*>& args,
@@ -238,7 +238,7 @@
   for (std::string_view tok : WordScanner(text)) {
     n--;
     if (n == 0) {
-      AppendString(tok, s);
+      s->append(tok);
       break;
     }
   }
@@ -297,7 +297,7 @@
   WordScanner ws(text);
   auto begin = ws.begin();
   if (begin != ws.end()) {
-    AppendString(*begin, s);
+    s->append(*begin);
   }
 }
 
@@ -309,7 +309,7 @@
   for (std::string_view tok : WordScanner(text)) {
     last = tok;
   }
-  AppendString(last, s);
+  s->append(last);
 }
 
 void JoinFunc(const std::vector<Value*>& args, Evaluator* ev, std::string* s) {
@@ -322,8 +322,8 @@
   for (iter1 = ws1.begin(), iter2 = ws2.begin();
        iter1 != ws1.end() && iter2 != ws2.end(); ++iter1, ++iter2) {
     ww.Write(*iter1);
-    // Use |AppendString| not to append extra ' '.
-    AppendString(*iter2, s);
+    // Use append to not append extra ' '.
+    s->append(*iter2);
   }
   for (; iter1 != ws1.end(); ++iter1)
     ww.Write(*iter1);
@@ -413,7 +413,7 @@
   WordWriter ww(s);
   for (std::string_view tok : WordScanner(text)) {
     ww.Write(pre);
-    AppendString(tok, s);
+    s->append(tok);
   }
 }
 
@@ -486,7 +486,7 @@
 void ValueFunc(const std::vector<Value*>& args, Evaluator* ev, std::string* s) {
   const std::string&& var_name = args[0]->Eval(ev);
   Var* var = ev->LookupVar(Intern(var_name));
-  AppendString(std::string(var->String()), s);
+  s->append(std::string(var->String()));
 }
 
 void EvalFunc(const std::vector<Value*>& args, Evaluator* ev, std::string*) {
@@ -1003,8 +1003,8 @@
     Var* v = ev->PeekVar(sym);
     const Loc& loc = v->Location();
     ww.Write(loc.filename ? loc.filename : "<unknown>");
-    AppendString(":", s);
-    AppendString(std::to_string(loc.lineno > 0 ? loc.lineno : 0), s);
+    s->append(":");
+    s->append(std::to_string(loc.lineno > 0 ? loc.lineno : 0));
   }
 }
 
diff --git a/src/regen.cc b/src/regen.cc
index 0f2bb1c..458c4cd 100644
--- a/src/regen.cc
+++ b/src/regen.cc
@@ -202,7 +202,8 @@
     int num_envs = LOAD_INT(fp);
     for (int i = 0; i < num_envs; i++) {
       LOAD_STRING(fp, &s);
-      std::string_view val(getenv(s.c_str()));
+      const char* val_c_str = getenv(s.c_str());
+      std::string_view val(val_c_str ? val_c_str : "");
       LOAD_STRING(fp, &s2);
       if (val != s2) {
         if (g_flags.dump_kati_stamp) {
diff --git a/src/strutil.cc b/src/strutil.cc
index ff240ae..0a222ad 100644
--- a/src/strutil.cc
+++ b/src/strutil.cc
@@ -93,7 +93,7 @@
 
 void WordWriter::Write(std::string_view s) {
   MaybeAddWhitespace();
-  AppendString(s, out_);
+  out_->append(s);
 }
 
 ScopedTerminator::ScopedTerminator(std::string_view s)
@@ -105,10 +105,6 @@
   const_cast<char*>(s_.data())[s_.size()] = c_;
 }
 
-void AppendString(std::string_view str, std::string* out) {
-  out->append(str.begin(), str.end());
-}
-
 bool HasPrefix(std::string_view str, std::string_view prefix) {
   ssize_t size_diff = str.size() - prefix.size();
   return size_diff >= 0 && str.substr(0, prefix.size()) == prefix;
@@ -170,10 +166,10 @@
                           std::string* out) const {
   if (percent_index_ == std::string::npos) {
     if (str == pat_) {
-      AppendString(subst, out);
+      out->append(subst);
       return;
     } else {
-      AppendString(str, out);
+      out->append(str);
       return;
     }
   }
@@ -181,17 +177,16 @@
   if (MatchImpl(str)) {
     size_t subst_percent_index = subst.find('%');
     if (subst_percent_index == std::string::npos) {
-      AppendString(subst, out);
+      out->append(subst);
       return;
     } else {
-      AppendString(subst.substr(0, subst_percent_index), out);
-      AppendString(str.substr(percent_index_, str.size() - pat_.size() + 1),
-                   out);
-      AppendString(subst.substr(subst_percent_index + 1), out);
+      out->append(subst.substr(0, subst_percent_index));
+      out->append(str.substr(percent_index_, str.size() - pat_.size() + 1));
+      out->append(subst.substr(subst_percent_index + 1));
       return;
     }
   }
-  AppendString(str, out);
+  out->append(str);
 }
 
 void Pattern::AppendSubstRef(std::string_view str,
@@ -351,7 +346,7 @@
     *o = buf;
     *o += '/';
   }
-  AppendString(s, o);
+  o->append(s);
   NormalizePath(o);
 }
 
diff --git a/src/strutil.h b/src/strutil.h
index fe35d5e..e199e14 100644
--- a/src/strutil.h
+++ b/src/strutil.h
@@ -78,8 +78,6 @@
   return r;
 }
 
-void AppendString(std::string_view str, std::string* out);
-
 bool HasPrefix(std::string_view str, std::string_view prefix);
 
 bool HasSuffix(std::string_view str, std::string_view suffix);