Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace literal "NSDebugDescription" error keys #2442

Merged
merged 1 commit into from
Aug 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CoreFoundation/PlugIn.subproj/CFBundle.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <CoreFoundation/CFSet.h>
#include <CoreFoundation/CFURLAccess.h>
#include <CoreFoundation/CFError.h>
#include <CoreFoundation/CFError_Private.h>
#include <string.h>
#include <CoreFoundation/CFPriv.h>
#include "CFInternal.h"
Expand Down Expand Up @@ -1122,7 +1123,7 @@ CF_PRIVATE CFErrorRef _CFBundleCreateErrorDebug(CFAllocatorRef allocator, CFBund
numKeys++;
}
if (debugString) {
userInfoKeys[numKeys] = CFSTR("NSDebugDescription");
userInfoKeys[numKeys] = kCFErrorDebugDescriptionKey;
userInfoValues[numKeys] = debugString;
numKeys++;
}
Expand Down
36 changes: 18 additions & 18 deletions Foundation/JSONSerialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ open class JSONSerialization : NSObject {
return value
}
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: [
"NSDebugDescription" : "JSON text did not start with array or object and option to allow fragments not set."
NSDebugDescriptionErrorKey : "JSON text did not start with array or object and option to allow fragments not set."
])
}

Expand Down Expand Up @@ -353,7 +353,7 @@ private struct JSONWriter {
let num = __SwiftValue.store(obj) as! NSNumber
writer(num.description)
default:
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: ["NSDebugDescription" : "Invalid object cannot be serialized"])
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: [NSDebugDescriptionErrorKey : "Invalid object cannot be serialized"])
}
}

Expand Down Expand Up @@ -390,7 +390,7 @@ private struct JSONWriter {

private func serializeFloat<T: FloatingPoint & LosslessStringConvertible>(_ num: T) throws {
guard num.isFinite else {
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: ["NSDebugDescription" : "Invalid number value (\(num)) in JSON write"])
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: [NSDebugDescriptionErrorKey : "Invalid number value (\(num)) in JSON write"])
}
var str = num.description
if str.hasSuffix(".0") {
Expand Down Expand Up @@ -460,7 +460,7 @@ private struct JSONWriter {
if let key = key as? String {
try serializeString(key)
} else {
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: ["NSDebugDescription" : "NSDictionary key must be NSString"])
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: [NSDebugDescriptionErrorKey : "NSDictionary key must be NSString"])
}
pretty ? writer(" : ") : writer(":")
try serializeJSON(value)
Expand All @@ -470,7 +470,7 @@ private struct JSONWriter {
let elems = try dict.sorted(by: { a, b in
guard let a = a.key as? String,
let b = b.key as? String else {
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: ["NSDebugDescription" : "NSDictionary key must be NSString"])
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: [NSDebugDescriptionErrorKey : "NSDictionary key must be NSString"])
}
let options: NSString.CompareOptions = [.numeric, .caseInsensitive, .forcedOrdering]
let range: Range<String.Index> = a.startIndex..<a.endIndex
Expand Down Expand Up @@ -593,7 +593,7 @@ private struct JSONReader {

guard let chunk = String(data: Data(bytes: buffer.baseAddress!.advanced(by: begin), count: byteLength), encoding: encoding) else {
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: [
"NSDebugDescription" : "Unable to convert data to a string using the detected encoding. The data may be corrupt."
NSDebugDescriptionErrorKey : "Unable to convert data to a string using the detected encoding. The data may be corrupt."
])
}
return chunk
Expand Down Expand Up @@ -627,7 +627,7 @@ private struct JSONReader {
switch self.source.takeASCII(input) {
case nil:
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: [
"NSDebugDescription" : "Unexpected end of file during JSON parse."
NSDebugDescriptionErrorKey : "Unexpected end of file during JSON parse."
])
case let (taken, index)? where taken == ascii:
return index
Expand Down Expand Up @@ -686,22 +686,22 @@ private struct JSONReader {
}
else {
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: [
"NSDebugDescription" : "Invalid escape sequence at position \(source.distanceFromStart(currentIndex))"
NSDebugDescriptionErrorKey : "Invalid escape sequence at position \(source.distanceFromStart(currentIndex))"
])
}
default:
currentIndex = index
}
}
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: [
"NSDebugDescription" : "Unexpected end of file during string parse."
NSDebugDescriptionErrorKey : "Unexpected end of file during string parse."
])
}

func parseEscapeSequence(_ input: Index) throws -> (String, Index)? {
guard let (byte, index) = source.takeASCII(input) else {
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: [
"NSDebugDescription" : "Early end of unicode escape sequence around character"
NSDebugDescriptionErrorKey : "Early end of unicode escape sequence around character"
])
}
let output: String
Expand Down Expand Up @@ -741,7 +741,7 @@ private struct JSONReader {
// Trail surrogate must come after lead surrogate
throw CocoaError.error(.propertyListReadCorrupt,
userInfo: [
"NSDebugDescription" : """
NSDebugDescriptionErrorKey : """
Unable to convert unicode escape sequence (no high-surrogate code point) \
to UTF8-encoded character at position \(source.distanceFromStart(input))
"""
Expand All @@ -752,7 +752,7 @@ private struct JSONReader {
UTF16.isTrailSurrogate(trailCodeUnit) else {
throw CocoaError.error(.propertyListReadCorrupt,
userInfo: [
"NSDebugDescription" : """
NSDebugDescriptionErrorKey : """
Unable to convert unicode escape sequence (no low-surrogate code point) \
to UTF8-encoded character at position \(source.distanceFromStart(input))
"""
Expand Down Expand Up @@ -852,7 +852,7 @@ private struct JSONReader {
guard nextASCII() else { return true }
} else {
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue,
userInfo: ["NSDebugDescription" : "Numbers must start with a 1-9 at character \(input)." ])
userInfo: [NSDebugDescriptionErrorKey : "Numbers must start with a 1-9 at character \(input)." ])
}

if ascii == JSONReader.DECIMAL_SEPARATOR {
Expand All @@ -861,7 +861,7 @@ private struct JSONReader {
guard nextASCII() else { return true }
} else if JSONReader.allDigits.contains(ascii) {
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue,
userInfo: ["NSDebugDescription" : "Leading zeros not allowed at character \(input)." ])
userInfo: [NSDebugDescriptionErrorKey : "Leading zeros not allowed at character \(input)." ])
}

digitCount = string.count - (isInteger ? 0 : 1) - (isNegative ? 1 : 0)
Expand Down Expand Up @@ -982,17 +982,17 @@ private struct JSONReader {
func parseObjectMember(_ input: Index, options opt: JSONSerialization.ReadingOptions) throws -> (String, Any, Index)? {
guard let (name, index) = try parseString(input) else {
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: [
"NSDebugDescription" : "Missing object key at location \(source.distanceFromStart(input))"
NSDebugDescriptionErrorKey : "Missing object key at location \(source.distanceFromStart(input))"
])
}
guard let separatorIndex = try consumeStructure(Structure.NameSeparator, input: index) else {
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: [
"NSDebugDescription" : "Invalid separator at location \(source.distanceFromStart(index))"
NSDebugDescriptionErrorKey : "Invalid separator at location \(source.distanceFromStart(index))"
])
}
guard let (value, finalIndex) = try parseValue(separatorIndex, options: opt) else {
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: [
"NSDebugDescription" : "Invalid value at location \(source.distanceFromStart(separatorIndex))"
NSDebugDescriptionErrorKey : "Invalid value at location \(source.distanceFromStart(separatorIndex))"
])
}

Expand Down Expand Up @@ -1023,7 +1023,7 @@ private struct JSONReader {
}
}
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: [
"NSDebugDescription" : "Badly formed array at location \(source.distanceFromStart(index))"
NSDebugDescriptionErrorKey : "Badly formed array at location \(source.distanceFromStart(index))"
])
}
}
Expand Down
2 changes: 1 addition & 1 deletion Foundation/NSCoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ open class NSCoder : NSObject {
}

open func failWithError(_ error: Error) {
if let debugDescription = (error as? NSError)?.userInfo["NSDebugDescription"] {
if let debugDescription = (error as? NSError)?.userInfo[NSDebugDescriptionErrorKey] {
NSLog("*** NSKeyedUnarchiver.init: \(debugDescription)")
} else {
NSLog("*** NSKeyedUnarchiver.init: decoding error")
Expand Down
2 changes: 1 addition & 1 deletion Foundation/NSKeyedUnarchiver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ open class NSKeyedUnarchiver : NSCoder {

private func _decodingError(_ code: CocoaError.Code, withDescription description: String) -> NSError {
return NSError(domain: NSCocoaErrorDomain,
code: code.rawValue, userInfo: [ "NSDebugDescription" : description ])
code: code.rawValue, userInfo: [ NSDebugDescriptionErrorKey : description ])
}

private func _replacementObject(_ decodedObject: Any?) -> Any? {
Expand Down
8 changes: 4 additions & 4 deletions Foundation/NSString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1284,15 +1284,15 @@ extension NSString {
let bytePtr = readResult.bytes.bindMemory(to: UInt8.self, capacity: readResult.length)
guard let cf = CFStringCreateWithBytes(kCFAllocatorDefault, bytePtr, readResult.length, CFStringConvertNSStringEncodingToEncoding(numericCast(enc)), true) else {
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.fileReadInapplicableStringEncoding.rawValue, userInfo: [
"NSDebugDescription" : "Unable to create a string using the specified encoding."
NSDebugDescriptionErrorKey : "Unable to create a string using the specified encoding."
])
}
var str: String?
if String._conditionallyBridgeFromObjectiveC(cf._nsObject, result: &str) {
self.init(str!)
} else {
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.fileReadInapplicableStringEncoding.rawValue, userInfo: [
"NSDebugDescription" : "Unable to bridge CFString to String."
NSDebugDescriptionErrorKey : "Unable to bridge CFString to String."
])
}
}
Expand Down Expand Up @@ -1339,15 +1339,15 @@ extension NSString {
guard let cf = CFStringCreateWithBytes(kCFAllocatorDefault, bytePtr + offset, readResult.length - offset,
CFStringConvertNSStringEncodingToEncoding(numericCast(encoding)), true) else {
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.fileReadInapplicableStringEncoding.rawValue, userInfo: [
"NSDebugDescription" : "Unable to create a string using the specified encoding."
NSDebugDescriptionErrorKey : "Unable to create a string using the specified encoding."
])
}
var str: String?
if String._conditionallyBridgeFromObjectiveC(cf._nsObject, result: &str) {
self.init(str!)
} else {
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.fileReadInapplicableStringEncoding.rawValue, userInfo: [
"NSDebugDescription" : "Unable to bridge CFString to String."
NSDebugDescriptionErrorKey : "Unable to bridge CFString to String."
])
}
enc?.pointee = encoding
Expand Down