Fix circular dependencies between optional, array_view, and rtc_base.

This splits things out of rtc_base and makes dependencies explicit.

Bug: webrtc:6828
Change-Id: Id521896c3c43595349021c857bec216e429a0c8d
Reviewed-on: https://webrtc-review.googlesource.com/32780
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Commit-Queue: Patrik Höglund <phoglund@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#21264}
diff --git a/rtc_base/stringutils.h b/rtc_base/stringutils.h
index 95e36ee..b0898c0 100644
--- a/rtc_base/stringutils.h
+++ b/rtc_base/stringutils.h
@@ -19,6 +19,7 @@
 #if defined(WEBRTC_WIN)
 #include <malloc.h>
 #include <wchar.h>
+#include <windows.h>
 #define alloca _alloca
 #endif  // WEBRTC_WIN
 
@@ -295,6 +296,43 @@
 
 #endif  // WEBRTC_WIN
 
+///////////////////////////////////////////////////////////////////////////////
+// UTF helpers (Windows only)
+///////////////////////////////////////////////////////////////////////////////
+
+#if defined(WEBRTC_WIN)
+
+inline std::wstring ToUtf16(const char* utf8, size_t len) {
+  int len16 = ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len),
+                                    nullptr, 0);
+  wchar_t* ws = STACK_ARRAY(wchar_t, len16);
+  ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len), ws, len16);
+  return std::wstring(ws, len16);
+}
+
+inline std::wstring ToUtf16(const std::string& str) {
+  return ToUtf16(str.data(), str.length());
+}
+
+inline std::string ToUtf8(const wchar_t* wide, size_t len) {
+  int len8 = ::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len),
+                                   nullptr, 0, nullptr, nullptr);
+  char* ns = STACK_ARRAY(char, len8);
+  ::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len), ns, len8,
+                        nullptr, nullptr);
+  return std::string(ns, len8);
+}
+
+inline std::string ToUtf8(const wchar_t* wide) {
+  return ToUtf8(wide, wcslen(wide));
+}
+
+inline std::string ToUtf8(const std::wstring& wstr) {
+  return ToUtf8(wstr.data(), wstr.length());
+}
+
+#endif  // WEBRTC_WIN
+
 // Replaces all occurrences of "search" with "replace".
 void replace_substrs(const char *search,
                      size_t search_len,