Merge pull request #931 from spevans/pr_warning_fixes

diff --git a/CoreFoundation/Base.subproj/CFInternal.h b/CoreFoundation/Base.subproj/CFInternal.h
index a716f90..59106bd 100644
--- a/CoreFoundation/Base.subproj/CFInternal.h
+++ b/CoreFoundation/Base.subproj/CFInternal.h
@@ -341,6 +341,12 @@
 
 CF_EXPORT void *__CFConstantStringClassReference[];
 
+#if __CF_BIG_ENDIAN__
+#define CF_CONST_STRING_INFO {0x00, 0x00, 0x07, 0xc8}
+#elif __CF_LITTLE_ENDIAN__
+#define CF_CONST_STRING_INFO {0xc8, 0x07, 0x00, 0x00}
+#endif
+
 #if DEPLOYMENT_TARGET_LINUX
 #define CONST_STRING_SECTION __attribute__((section(".cfstr.data")))
 #else
@@ -348,11 +354,11 @@
 #endif
 
 #define CONST_STRING_DECL(S, V) \
-const struct __CFConstStr __##S CONST_STRING_SECTION = {{(uintptr_t)&__CFConstantStringClassReference, _CF_CONSTANT_OBJECT_STRONG_RC, 0, {0xc8, 0x07, 0x00, 0x00}}, (uint8_t *)(V), sizeof(V) - 1}; \
+const struct __CFConstStr __##S CONST_STRING_SECTION = {{(uintptr_t)&__CFConstantStringClassReference, _CF_CONSTANT_OBJECT_STRONG_RC, 0, CF_CONST_STRING_INFO}, (uint8_t *)(V), sizeof(V) - 1}; \
 const CFStringRef S = (CFStringRef)&__##S;
 
 #define PE_CONST_STRING_DECL(S, V) \
-const static struct __CFConstStr __##S CONST_STRING_SECTION = {{(uintptr_t)&__CFConstantStringClassReference, _CF_CONSTANT_OBJECT_STRONG_RC, 0, {0xc8, 0x07, 0x00, 0x00}}, (uint8_t *)(V), sizeof(V) - 1}; \
+const static struct __CFConstStr __##S CONST_STRING_SECTION = {{(uintptr_t)&__CFConstantStringClassReference, _CF_CONSTANT_OBJECT_STRONG_RC, 0, CF_CONST_STRING_INFO}, (uint8_t *)(V), sizeof(V) - 1}; \
 CF_PRIVATE const CFStringRef S = (CFStringRef)&__##S;
 
 
@@ -366,33 +372,24 @@
 struct CF_CONST_STRING {
     CFRuntimeBase _base;
     uint8_t *_ptr;
-    uint32_t _length;
+#if defined(__LP64__) && defined(__BIG_ENDIAN__)
+	uint64_t _length;
+#else
+	uint32_t _length;
+#endif
 };
 
 CF_EXPORT int __CFConstantStringClassReference[];
 
 /* CFNetwork also has a copy of the CONST_STRING_DECL macro (for use on platforms without constant string support in cc); please warn cfnetwork-core@group.apple.com of any necessary changes to this macro. -- REW, 1/28/2002 */
 
-#if __CF_BIG_ENDIAN__
-
 #define CONST_STRING_DECL(S, V)			\
-static struct CF_CONST_STRING __ ## S ## __ = {{(uintptr_t)&__CFConstantStringClassReference, {0x00, 0x00, 0x07, 0xc8}}, (uint8_t *)V, sizeof(V) - 1}; \
+static struct CF_CONST_STRING __ ## S ## __ = {{(uintptr_t)&__CFConstantStringClassReference, CF_CONST_STRING_INFO}, (uint8_t *)V, sizeof(V) - 1}; \
 const CFStringRef S = (CFStringRef) & __ ## S ## __;
 #define PE_CONST_STRING_DECL(S, V)			\
-static struct CF_CONST_STRING __ ## S ## __ = {{(uintptr_t)&__CFConstantStringClassReference, {0x00, 0x00, 0x07, 0xc8}}, (uint8_t *)V, sizeof(V) - 1}; \
+static struct CF_CONST_STRING __ ## S ## __ = {{(uintptr_t)&__CFConstantStringClassReference, CF_CONST_STRING_INFO}, (uint8_t *)V, sizeof(V) - 1}; \
 CF_PRIVATE const CFStringRef S = (CFStringRef) & __ ## S ## __;
 
-#elif __CF_LITTLE_ENDIAN__
-
-#define CONST_STRING_DECL(S, V)			\
-static struct CF_CONST_STRING __ ## S ## __ = {{(uintptr_t)&__CFConstantStringClassReference, {0xc8, 0x07, 0x00, 0x00}}, (uint8_t *)(V), sizeof(V) - 1}; \
-const CFStringRef S = (CFStringRef) & __ ## S ## __;
-#define PE_CONST_STRING_DECL(S, V)			\
-static struct CF_CONST_STRING __ ## S ## __ = {{(uintptr_t)&__CFConstantStringClassReference, {0xc8, 0x07, 0x00, 0x00}}, (uint8_t *)(V), sizeof(V) - 1}; \
-CF_PRIVATE const CFStringRef S = (CFStringRef) & __ ## S ## __;
-
-#endif
-
 #endif // __CONSTANT_CFSTRINGS__
 
 CF_EXPORT bool __CFOASafe;
diff --git a/CoreFoundation/String.subproj/CFCharacterSet.c b/CoreFoundation/String.subproj/CFCharacterSet.c
index e5deb33..ed98865 100644
--- a/CoreFoundation/String.subproj/CFCharacterSet.c
+++ b/CoreFoundation/String.subproj/CFCharacterSet.c
@@ -216,10 +216,10 @@
         UInt32 lastCharMask;
 
         length = firstCharIndex % sizeof(UInt32);
-        firstCharMask = (((((UInt32)0xFF) << (firstChar & (BITSPERBYTE - 1))) & 0xFF) << (((sizeof(UInt32) - 1) - length) * BITSPERBYTE)) | (((UInt32)0xFFFFFFFF) >> ((length + 1) * BITSPERBYTE));
+        firstCharMask = (((((UInt32)0xFF) << (firstChar & (BITSPERBYTE - 1))) & 0xFF) << (((sizeof(UInt32) - 1) - length) * BITSPERBYTE)) | (UInt32)(((UInt64)0xFFFFFFFF) >> ((length + 1) * BITSPERBYTE));
         
         length = lastCharIndex % sizeof(UInt32);
-        lastCharMask = ((((UInt32)0xFF) >> ((BITSPERBYTE - 1) - (lastChar & (BITSPERBYTE - 1)))) << (((sizeof(UInt32) - 1) - length) * BITSPERBYTE)) | (((UInt32)0xFFFFFFFF) << ((sizeof(UInt32) - length) * BITSPERBYTE));
+        lastCharMask = ((((UInt32)0xFF) >> ((BITSPERBYTE - 1) - (lastChar & (BITSPERBYTE - 1)))) << (((sizeof(UInt32) - 1) - length) * BITSPERBYTE)) | (UInt32)(((UInt64)0xFFFFFFFF) << ((sizeof(UInt32) - length) * BITSPERBYTE));
 
         firstCharIndex = firstChar >> LOG_BPLW;
         lastCharIndex = lastChar >> LOG_BPLW;
diff --git a/CoreFoundation/URL.subproj/CFURLComponents.c b/CoreFoundation/URL.subproj/CFURLComponents.c
index c1732e4..5756ea8 100644
--- a/CoreFoundation/URL.subproj/CFURLComponents.c
+++ b/CoreFoundation/URL.subproj/CFURLComponents.c
@@ -74,6 +74,7 @@
     if (instance->_userComponent) CFRelease(instance->_userComponent);
     if (instance->_passwordComponent) CFRelease(instance->_passwordComponent);
     if (instance->_hostComponent) CFRelease(instance->_hostComponent);
+    if (instance->_portComponent) CFRelease(instance->_portComponent);
     if (instance->_pathComponent) CFRelease(instance->_pathComponent);
     if (instance->_queryComponent) CFRelease(instance->_queryComponent);
     if (instance->_fragmentComponent) CFRelease(instance->_fragmentComponent);
@@ -197,10 +198,12 @@
     __CFLock(&components->_lock);
     memory->_lock = CFLockInit;
     memory->_urlString = components->_urlString ? CFStringCreateCopy(alloc, components->_urlString) : NULL;
+    memory->_parseInfo = components->_parseInfo;
     
     memory->_schemeComponentValid = components->_schemeComponentValid;
     memory->_userComponentValid = components->_userComponentValid;
     memory->_hostComponentValid = components->_hostComponentValid;
+    memory->_passwordComponentValid = components->_passwordComponentValid;
     memory->_portComponentValid = components->_portComponentValid;
     memory->_pathComponentValid = components->_pathComponentValid;
     memory->_queryComponentValid = components->_queryComponentValid;
@@ -305,7 +308,9 @@
 CF_EXPORT CFURLRef _CFURLComponentsCopyURLRelativeToURL(CFURLComponentsRef components, CFURLRef relativeToURL) {
     CFStringRef urlString = _CFURLComponentsCopyString(components);
     if (urlString) {
-        return CFURLCreateWithString(kCFAllocatorSystemDefault, urlString, relativeToURL);
+        CFURLRef url = CFURLCreateWithString(kCFAllocatorSystemDefault, urlString, relativeToURL);
+        CFRelease(urlString);
+        return url;
     } else {
         return NULL;
     }
@@ -1135,6 +1140,7 @@
                 CFArrayAppendValue(intermediateResult, entry);
                 CFRelease(entry);
                 CFRelease(nameString);
+                CFRelease(valueString);
             }
             else {
                 // at end of query while parsing the name string
diff --git a/Foundation/NSHTTPCookie.swift b/Foundation/NSHTTPCookie.swift
index b2c629f..358b3df 100644
--- a/Foundation/NSHTTPCookie.swift
+++ b/Foundation/NSHTTPCookie.swift
@@ -184,10 +184,8 @@
     /// <tr>
     ///     <td>HTTPCookiePropertyKey.path</td>
     ///     <td>NSString</td>
-    ///     <td>NO</td>
-    ///     <td>Path for the cookie. Inferred from the value for
-    ///     HTTPCookiePropertyKey.originURL if not provided. Default is "/".
-    ///     </td>
+    ///     <td>YES</td>
+    ///     <td>Path for the cookie</td>
     /// </tr>
     /// <tr>
     ///     <td>HTTPCookiePropertyKey.port</td>
diff --git a/Foundation/NSJSONSerialization.swift b/Foundation/NSJSONSerialization.swift
index 36120e3..5aff5b2 100644
--- a/Foundation/NSJSONSerialization.swift
+++ b/Foundation/NSJSONSerialization.swift
@@ -284,6 +284,8 @@
 //MARK: - JSONSerializer
 private struct JSONWriter {
     
+    private let maxUIntLength = String(describing: UInt.max).characters.count
+    private let maxIntLength = String(describing: Int.max).characters.count
     var indent = 0
     let pretty: Bool
     let writer: (String?) -> Void
@@ -309,6 +311,10 @@
             try serializeString(str)
         case let boolValue as Bool:
             serializeBool(boolValue)
+        case let num as Int:
+            try serializeInt(value: num)
+        case let num as UInt:
+            try serializeUInt(value: num)
         case let array as Array<Any>:
             try serializeArray(array)
         case let dict as Dictionary<AnyHashable, Any>:
@@ -322,6 +328,92 @@
         }
     }
 
+    private func serializeUInt(value: UInt) throws {
+        if value == 0 {
+            writer("0")
+            return
+        }
+        var array: [UInt] = []
+        var stringResult = ""
+        //Maximum length of an UInt
+        array.reserveCapacity(maxUIntLength)
+        stringResult.reserveCapacity(maxUIntLength)
+        var number = value
+        
+        while number != 0 {
+            array.append(number % 10)
+            number /= 10
+        }
+        
+        /*
+         Step backwards through the array and append the values to the string. This way the values are appended in the correct order.
+         */
+        var counter = array.count
+        while counter > 0 {
+            counter -= 1
+            let digit: UInt = array[counter]
+            switch digit {
+            case 0: stringResult.append("0")
+            case 1: stringResult.append("1")
+            case 2: stringResult.append("2")
+            case 3: stringResult.append("3")
+            case 4: stringResult.append("4")
+            case 5: stringResult.append("5")
+            case 6: stringResult.append("6")
+            case 7: stringResult.append("7")
+            case 8: stringResult.append("8")
+            case 9: stringResult.append("9")
+            default: fatalError()
+            }
+        }
+        
+        writer(stringResult)
+    }
+    
+    private func serializeInt(value: Int) throws {
+        if value == 0 {
+            writer("0")
+            return
+        }
+        var array: [Int] = []
+        var stringResult = ""
+        array.reserveCapacity(maxIntLength)
+        //Account for a negative sign
+        stringResult.reserveCapacity(maxIntLength + 1)
+        var number = value
+        
+        while number != 0 {
+            array.append(number % 10)
+            number /= 10
+        }
+        //If negative add minus sign before adding any values
+        if value < 0 {
+            stringResult.append("-")
+        }
+        /*
+         Step backwards through the array and append the values to the string. This way the values are appended in the correct order.
+         */
+        var counter = array.count
+        while counter > 0 {
+            counter -= 1
+            let digit = array[counter]
+            switch digit {
+            case 0: stringResult.append("0")
+            case 1, -1: stringResult.append("1")
+            case 2, -2: stringResult.append("2")
+            case 3, -3: stringResult.append("3")
+            case 4, -4: stringResult.append("4")
+            case 5, -5: stringResult.append("5")
+            case 6, -6: stringResult.append("6")
+            case 7, -7: stringResult.append("7")
+            case 8, -8: stringResult.append("8")
+            case 9, -9: stringResult.append("9")
+            default: fatalError()
+            }
+        }
+        writer(stringResult)
+    }
+    
     func serializeString(_ str: String) throws {
         writer("\"")
         for scalar in str.unicodeScalars {
diff --git a/Foundation/NSLocale.swift b/Foundation/NSLocale.swift
index 52becd1..6bc7873 100644
--- a/Foundation/NSLocale.swift
+++ b/Foundation/NSLocale.swift
@@ -97,7 +97,7 @@
     open class var availableLocaleIdentifiers: [String] {
         var identifiers = Array<String>()
         for obj in CFLocaleCopyAvailableLocaleIdentifiers()._nsObject {
-            identifiers.append((obj as! NSString)._swiftObject)
+            identifiers.append(obj as! String)
         }
         return identifiers
     }
diff --git a/TestFoundation/TestNSJSONSerialization.swift b/TestFoundation/TestNSJSONSerialization.swift
index 0e4e09f..86616f9 100644
--- a/TestFoundation/TestNSJSONSerialization.swift
+++ b/TestFoundation/TestNSJSONSerialization.swift
@@ -863,6 +863,10 @@
             ("test_nested_array", test_nested_array),
             ("test_nested_dictionary", test_nested_dictionary),
             ("test_serialize_number", test_serialize_number),
+            ("test_serialize_IntMax", test_serialize_IntMax),
+            ("test_serialize_IntMin", test_serialize_IntMin),
+            ("test_serialize_UIntMax", test_serialize_UIntMax),
+            ("test_serialize_UIntMin", test_serialize_UIntMin),
             ("test_serialize_stringEscaping", test_serialize_stringEscaping),
             ("test_jsonReadingOffTheEndOfBuffers", test_jsonReadingOffTheEndOfBuffers),
             ("test_jsonObjectToOutputStreamBuffer", test_jsonObjectToOutputStreamBuffer),
@@ -1054,6 +1058,26 @@
         XCTAssertEqual(try trySerialize(json), "[false,true]")
     }
     
+    func test_serialize_IntMax() {
+        let json: [Any] = [Int.max]
+        XCTAssertEqual(try trySerialize(json), "[\(Int.max)]")
+    }
+    
+    func test_serialize_IntMin() {
+        let json: [Any] = [Int.min]
+        XCTAssertEqual(try trySerialize(json), "[\(Int.min)]")
+    }
+    
+    func test_serialize_UIntMax() {
+        let json: [Any] = [UInt.max]
+        XCTAssertEqual(try trySerialize(json), "[\(UInt.max)]")
+    }
+    
+    func test_serialize_UIntMin() {
+        let json: [Any] = [UInt.min]
+        XCTAssertEqual(try trySerialize(json), "[\(UInt.min)]")
+    }
+    
     func test_serialize_stringEscaping() {
         var json = ["foo"]
         XCTAssertEqual(try trySerialize(json), "[\"foo\"]")
diff --git a/TestFoundation/TestNSLocale.swift b/TestFoundation/TestNSLocale.swift
index a061c03..bfd69ad 100644
--- a/TestFoundation/TestNSLocale.swift
+++ b/TestFoundation/TestNSLocale.swift
@@ -20,7 +20,8 @@
         return [
             ("test_constants", test_constants),
             ("test_Identifier", test_Identifier),
-            ("test_copy", test_copy)
+            ("test_copy", test_copy),
+            ("test_availableIdentifiers", test_availableIdentifiers),
         ]
     }
 
@@ -107,4 +108,8 @@
 
         XCTAssertTrue(locale == localeCopy)
     }
+
+     func test_availableIdentifiers() {
+        XCTAssertNoThrow(Locale.availableIdentifiers)
+    }
 }
diff --git a/TestFoundation/TestNSXMLDocument.swift b/TestFoundation/TestNSXMLDocument.swift
index b160ff4..68e9bdb 100644
--- a/TestFoundation/TestNSXMLDocument.swift
+++ b/TestFoundation/TestNSXMLDocument.swift
@@ -36,8 +36,8 @@
                 ("test_processingInstruction", test_processingInstruction),
                 ("test_parseXMLString", test_parseXMLString),
                 ("test_prefixes", test_prefixes),
-                ("test_validation_success", test_validation_success),
-                ("test_validation_failure", test_validation_failure),
+                //                ("test_validation_success", test_validation_success),
+                //                ("test_validation_failure", test_validation_failure),
                 ("test_dtd", test_dtd),
                 ("test_documentWithDTD", test_documentWithDTD),
                 ("test_dtd_attributes", test_dtd_attributes)
@@ -56,7 +56,7 @@
                 ("test_processingInstruction", test_processingInstruction),
                 ("test_parseXMLString", test_parseXMLString),
                 ("test_prefixes", test_prefixes),
-                ("test_validation_success", test_validation_success),
+                //                ("test_validation_success", test_validation_success),
                 //                ("test_validation_failure", test_validation_failure),
                 ("test_dtd", test_dtd),
                 //                ("test_documentWithDTD", test_documentWithDTD),
@@ -289,6 +289,8 @@
         XCTAssertEqual(element.localName, "root")
     }
 
+    /*
+     * <rdar://31567922> Re-enable these tests in a way that does not depend on the internet.
     func test_validation_success() throws {
         let validString = "<?xml version=\"1.0\" standalone=\"yes\"?><!DOCTYPE foo [ <!ELEMENT foo (#PCDATA)> ]><foo>Hello world</foo>"
         do {
@@ -330,7 +332,7 @@
         } catch let error as NSError {
             XCTAssert((error.userInfo[NSLocalizedDescriptionKey] as! String).contains("Element true was declared EMPTY this one has content"))
         }
-    }
+    }*/
 
     func test_dtd() throws {
         let node = XMLNode.dtdNode(withXMLString:"<!ELEMENT foo (#PCDATA)>") as! XMLDTDNode