Index: include/__bit_reference
===================================================================
--- include/__bit_reference	(revision 140328)
+++ include/__bit_reference	(working copy)
@@ -687,12 +687,12 @@
 
 // swap_ranges
 
-template <class _C1, class _C2>
-__bit_iterator<_C2, false>
-__swap_ranges_aligned(__bit_iterator<_C1, false> __first, __bit_iterator<_C1, false> __last,
-                      __bit_iterator<_C2, false> __result)
+template <class __C1, class __C2>
+__bit_iterator<__C2, false>
+__swap_ranges_aligned(__bit_iterator<__C1, false> __first, __bit_iterator<__C1, false> __last,
+                      __bit_iterator<__C2, false> __result)
 {
-    typedef __bit_iterator<_C1, false> _I1;
+    typedef __bit_iterator<__C1, false> _I1;
     typedef  typename _I1::difference_type difference_type;
     typedef typename _I1::__storage_type __storage_type;
     static const unsigned __bits_per_word = _I1::__bits_per_word;
@@ -737,12 +737,12 @@
     return __result;
 }
 
-template <class _C1, class _C2>
-__bit_iterator<_C2, false>
-__swap_ranges_unaligned(__bit_iterator<_C1, false> __first, __bit_iterator<_C1, false> __last,
-                        __bit_iterator<_C2, false> __result)
+template <class __C1, class __C2>
+__bit_iterator<__C2, false>
+__swap_ranges_unaligned(__bit_iterator<__C1, false> __first, __bit_iterator<__C1, false> __last,
+                        __bit_iterator<__C2, false> __result)
 {
-    typedef __bit_iterator<_C1, false> _I1;
+    typedef __bit_iterator<__C1, false> _I1;
     typedef  typename _I1::difference_type difference_type;
     typedef typename _I1::__storage_type __storage_type;
     static const unsigned __bits_per_word = _I1::__bits_per_word;
@@ -837,11 +837,11 @@
     return __result;
 }
 
-template <class _C1, class _C2>
+template <class __C1, class __C2>
 inline _LIBCPP_INLINE_VISIBILITY
-__bit_iterator<_C2, false>
-swap_ranges(__bit_iterator<_C1, false> __first1, __bit_iterator<_C1, false> __last1,
-            __bit_iterator<_C2, false> __first2)
+__bit_iterator<__C2, false>
+swap_ranges(__bit_iterator<__C1, false> __first1, __bit_iterator<__C1, false> __last1,
+            __bit_iterator<__C2, false> __first2)
 {
     if (__first1.__ctz_ == __first2.__ctz_)
         return __swap_ranges_aligned(__first1, __last1, __first2);
@@ -1213,15 +1213,15 @@
     template <class _D, bool _IC> friend __bit_iterator<_D, false> copy_backward(__bit_iterator<_D, _IC> __first,
                                                                                  __bit_iterator<_D, _IC> __last,
                                                                                  __bit_iterator<_D, false> __result);
-    template <class _C1, class _C2>friend __bit_iterator<_C2, false> __swap_ranges_aligned(__bit_iterator<_C1, false>,
-                                                                                           __bit_iterator<_C1, false>,
-                                                                                           __bit_iterator<_C2, false>);
-    template <class _C1, class _C2>friend __bit_iterator<_C2, false> __swap_ranges_unaligned(__bit_iterator<_C1, false>,
-                                                                                             __bit_iterator<_C1, false>,
-                                                                                             __bit_iterator<_C2, false>);
-    template <class _C1, class _C2>friend __bit_iterator<_C2, false> swap_ranges(__bit_iterator<_C1, false>,
-                                                                                 __bit_iterator<_C1, false>,
-                                                                                 __bit_iterator<_C2, false>);
+    template <class __C1, class __C2>friend __bit_iterator<__C2, false> __swap_ranges_aligned(__bit_iterator<__C1, false>,
+                                                                                           __bit_iterator<__C1, false>,
+                                                                                           __bit_iterator<__C2, false>);
+    template <class __C1, class __C2>friend __bit_iterator<__C2, false> __swap_ranges_unaligned(__bit_iterator<__C1, false>,
+                                                                                             __bit_iterator<__C1, false>,
+                                                                                             __bit_iterator<__C2, false>);
+    template <class __C1, class __C2>friend __bit_iterator<__C2, false> swap_ranges(__bit_iterator<__C1, false>,
+                                                                                 __bit_iterator<__C1, false>,
+                                                                                 __bit_iterator<__C2, false>);
     template <class _D> friend __bit_iterator<_D, false> rotate(__bit_iterator<_D, false>,
                                                                 __bit_iterator<_D, false>,
                                                                 __bit_iterator<_D, false>);
Index: include/locale
===================================================================
--- include/locale	(revision 140328)
+++ include/locale	(working copy)
@@ -186,7 +186,9 @@
 #endif
 #include <cstdlib>
 #include <ctime>
-#if !_WIN32
+#if _WIN32
+#include <support/win32/support.h> // vasprintf
+#else // _WIN32
 #include <nl_types.h>
 #endif  // !_WIN32
 
@@ -3711,10 +3713,14 @@
 typename messages<_CharT>::catalog
 messages<_CharT>::do_open(const basic_string<char>& __nm, const locale&) const
 {
+#if _WIN32
+    return -1;
+#else // _WIN32
     catalog __cat = reinterpret_cast<catalog>(catopen(__nm.c_str(), NL_CAT_LOCALE));
     if (__cat != -1)
         __cat = static_cast<catalog>((static_cast<size_t>(__cat) >> 1));
     return __cat;
+#endif // _WIN32
 }
 
 template <class _CharT>
@@ -3722,6 +3728,9 @@
 messages<_CharT>::do_get(catalog __c, int __set, int __msgid,
                          const string_type& __dflt) const
 {
+#if _WIN32
+    return __dflt;
+#else // _WIN32
     string __ndflt;
     __narrow_to_utf8<sizeof(char_type)*__CHAR_BIT__>()(back_inserter(__ndflt),
                                                        __dflt.c_str(),
@@ -3734,16 +3743,19 @@
     __widen_from_utf8<sizeof(char_type)*__CHAR_BIT__>()(back_inserter(__w),
                                                         __n, __n + strlen(__n));
     return __w;
+#endif // _WIN32
 }
 
 template <class _CharT>
 void
 messages<_CharT>::do_close(catalog __c) const
 {
+#if !_WIN32
     if (__c != -1)
         __c <<= 1;
     nl_catd __cat = reinterpret_cast<nl_catd>(__c);
     catclose(__cat);
+#endif // !_WIN32
 }
 
 extern template class messages<char>;
Index: include/support/win32/support.h
===================================================================
--- include/support/win32/support.h	(revision 140328)
+++ include/support/win32/support.h	(working copy)
@@ -12,4 +12,12 @@
    Functions and constants used in libc++ that are missing from the Windows C library.
   */
 
+#if __MINGW32__
+#include <stdio.h>
+#define swprintf snwprintf
+#endif // __MINGW32__
 int vasprintf( char **sptr, const char *__restrict__ fmt , va_list ap );
+size_t mbsnrtowcs( wchar_t *__restrict__ dst, const char **__restrict__ src,
+                   size_t nmc, size_t len, mbstate_t *__restrict__ ps );
+size_t wcsnrtombs( char *__restrict__ dst, const wchar_t **__restrict__ src,
+                   size_t nwc, size_t len, mbstate_t *__restrict__ ps );
Index: include/support/win32/locale.h
===================================================================
--- include/support/win32/locale.h	(revision 140328)
+++ include/support/win32/locale.h	(working copy)
@@ -18,13 +18,44 @@
 #define strtold_l _strtod_l
 #define isdigit_l _isdigit_l
 #define isxdigit_l _isxdigit_l
-#define newlocale _create_locale
+#define strcoll_l _strcoll_l
+#define strxfrm_l _strxfrm_l
+#define wcscoll_l _wcscoll_l
+#define wcsxfrm_l _wcsxfrm_l
+#define toupper_l _toupper_l
+#define tolower_l _tolower_l
+#define iswspace_l _iswspace_l
+#define iswprint_l _iswprint_l
+#define iswcntrl_l _iswcntrl_l
+#define iswupper_l _iswupper_l
+#define iswlower_l _iswlower_l
+#define iswalpha_l _iswalpha_l
+#define iswdigit_l _iswdigit_l
+#define iswpunct_l _iswpunct_l
+#define iswxdigit_l _iswxdigit_l
+#define towupper_l _towupper_l
+#define towlower_l _towlower_l
+#define strftime_l _strftime_l
+inline int isblank_l( int c, locale_t /*loc*/ )
+{
+    return ( c == ' ' || c == '\t' );
+}
+inline int iswblank_l( wint_t c, locale_t /*loc*/ )
+{
+    return ( c == L' ' || c == L'\t' );
+}
 #define freelocale _free_locale
+// ignore base; it is always 0 in libc++ code
+inline locale_t newlocale( int mask, const char * locale, locale_t /*base*/ )
+{
+    return _create_locale( mask, locale );
+}
+
 // FIXME: first call _configthreadlocale(_ENABLE_PER_THREAD_LOCALE) somewhere
 // FIXME: return types are different, need to make locale_t from char*
 inline locale_t uselocale(locale_t newloc)
 {
-    return newlocale( LC_ALL, setlocale(LC_ALL, newloc->locinfo->lc_category[LC_ALL].locale) );
+    return _create_locale( LC_ALL, setlocale(LC_ALL, newloc->locinfo->lc_category[LC_ALL].locale) );
 }
 
 #define LC_COLLATE_MASK _M_COLLATE
@@ -33,5 +64,9 @@
 #define LC_NUMERIC_MASK _M_NUMERIC
 #define LC_TIME_MASK _M_TIME
 #define LC_MESSAGES_MASK _M_MESSAGES
-
-enum { NL_SETD=0, NL_CAT_LOCALE=1 };
+#define LC_ALL_MASK (  LC_COLLATE_MASK \
+                     | LC_CTYPE_MASK \
+                     | LC_MESSAGES_MASK \
+                     | LC_MONETARY_MASK \
+                     | LC_NUMERIC_MASK \
+                     | LC_TIME_MASK )
Index: src/locale.cpp
===================================================================
--- src/locale.cpp	(revision 140328)
+++ src/locale.cpp	(working copy)
@@ -19,7 +19,11 @@
 #include "cstring"
 #include "cwctype"
 #include "__sso_allocator"
+#if _WIN32
+#include <locale.h>
+#else // _WIN32
 #include <langinfo.h>
+#endif // _!WIN32
 #include <stdlib.h>
 
 #ifdef _LIBCPP_STABLE_APPLE_ABI
@@ -5568,16 +5572,29 @@
         __frac_digits_ = lc->int_frac_digits;
     else
         __frac_digits_ = base::do_frac_digits();
+#if _WIN32
+    if (lc->p_sign_posn == 0)
+#else // _WIN32
     if (lc->int_p_sign_posn == 0)
+#endif //_WIN32
         __positive_sign_ = "()";
     else
         __positive_sign_ = lc->positive_sign;
+#if _WIN32
+    if(lc->n_sign_posn == 0)
+#else // _WIN32
     if (lc->int_n_sign_posn == 0)
+#endif // _WIN32
         __negative_sign_ = "()";
     else
         __negative_sign_ = lc->negative_sign;
+#if _WIN32
+    __init_pat(__pos_format_, lc->p_cs_precedes, lc->p_sep_by_space, lc->p_sign_posn);
+    __init_pat(__neg_format_, lc->n_cs_precedes, lc->n_sep_by_space, lc->n_sign_posn);
+#else
     __init_pat(__pos_format_, lc->int_p_cs_precedes, lc->int_p_sep_by_space, lc->int_p_sign_posn);
     __init_pat(__neg_format_, lc->int_n_cs_precedes, lc->int_n_sep_by_space, lc->int_n_sign_posn);
+#endif // _WIN32
 }
 
 template<>
@@ -5698,7 +5715,11 @@
         __frac_digits_ = lc->int_frac_digits;
     else
         __frac_digits_ = base::do_frac_digits();
+#if _WIN32
+    if (lc->p_sign_posn == 0)
+#else // _WIN32
     if (lc->int_p_sign_posn == 0)
+#endif // _WIN32
         __positive_sign_ = L"()";
     else
     {
@@ -5714,7 +5735,11 @@
         wbe = wbuf + j;
         __positive_sign_.assign(wbuf, wbe);
     }
+#if _WIN32
+    if (lc->n_sign_posn == 0)
+#else // _WIN32
     if (lc->int_n_sign_posn == 0)
+#endif // _WIN32
         __negative_sign_ = L"()";
     else
     {
@@ -5730,8 +5755,13 @@
         wbe = wbuf + j;
         __negative_sign_.assign(wbuf, wbe);
     }
+#if _WIN32
+    __init_pat(__pos_format_, lc->p_cs_precedes, lc->p_sep_by_space, lc->p_sign_posn);
+    __init_pat(__neg_format_, lc->n_cs_precedes, lc->n_sep_by_space, lc->n_sign_posn);
+#else // _WIN32
     __init_pat(__pos_format_, lc->int_p_cs_precedes, lc->int_p_sep_by_space, lc->int_p_sign_posn);
     __init_pat(__neg_format_, lc->int_n_cs_precedes, lc->int_n_sep_by_space, lc->int_n_sign_posn);
+#endif // _WIN32
 }
 
 void __do_nothing(void*) {}
Index: src/string.cpp
===================================================================
--- src/string.cpp	(revision 140328)
+++ src/string.cpp	(working copy)
@@ -11,6 +11,9 @@
 #include "cstdlib"
 #include "cwchar"
 #include "cerrno"
+#if _WIN32
+#include "support/win32/support.h"
+#endif // _WIN32
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
Index: src/thread.cpp
===================================================================
--- src/thread.cpp	(revision 140328)
+++ src/thread.cpp	(working copy)
@@ -12,7 +12,9 @@
 #include "vector"
 #include "future"
 #include <sys/types.h>
+#if !_WIN32
 #include <sys/sysctl.h>
+#endif // _WIN32
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
Index: src/support/win32/support.cpp
===================================================================
--- src/support/win32/support.cpp	(revision 140328)
+++ src/support/win32/support.cpp	(working copy)
@@ -8,11 +8,17 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include <stddef.h> // size_t
+#include <stdlib.h> // malloc
+#include <stdio.h>  // vsprintf, vsnprintf
+#include <string.h> // strcpy, wcsncpy
+#include <wchar.h>  // mbstate_t
+
 int vasprintf( char **sptr, const char *__restrict__ fmt, va_list ap )
 {
-    *sptr = NULL
+    *sptr = NULL;
     int count = vsnprintf( *sptr, 0, fmt, ap );
-    if( (count >= 0) && ((*sptr = malloc(count+1)) != NULL) )
+    if( (count >= 0) && ((*sptr = (char*)malloc(count+1)) != NULL) )
     {
         vsprintf( *sptr, fmt, ap );
         sptr[count] = '\0';
@@ -20,3 +26,36 @@
 
     return count;
 }
+
+// FIXME: use wcrtomb and avoid copy
+// use mbsrtowcs which is available, first copy first nwc elements of src
+size_t mbsnrtowcs( wchar_t *__restrict__ dst, const char **__restrict__ src,
+                   size_t nmc, size_t len, mbstate_t *__restrict__ ps )
+{
+    char* local_src = new char[nmc+1];
+    char* nmcsrc = local_src;
+    strncpy( nmcsrc, *src, nmc );
+    nmcsrc[nmc] = '\0';
+    const size_t result = mbsrtowcs( dst, const_cast<const char **>(&nmcsrc), len, ps );
+    // propagate error
+    if( nmcsrc == NULL )
+        *src = NULL;
+    delete[] local_src;
+    return result;
+}
+// FIXME: use wcrtomb and avoid copy
+// use wcsrtombs which is available, first copy first nwc elements of src
+size_t wcsnrtombs( char *__restrict__ dst, const wchar_t **__restrict__ src,
+                   size_t nwc, size_t len, mbstate_t *__restrict__ ps )
+{
+    wchar_t* local_src = new wchar_t[nwc];
+    wchar_t* nwcsrc = local_src;
+    wcsncpy(nwcsrc, *src, nwc);
+    nwcsrc[nwc] = '\0';
+    const size_t result = wcsrtombs( dst, const_cast<const wchar_t **>(&nwcsrc), len, ps );
+    // propogate error
+    if( nwcsrc == NULL )
+        *src = NULL;
+    delete[] nwcsrc;
+    return result;
+}
