Modified: trunk/Source/WebCore/platform/mac/WebCoreNSURLExtras.mm (236766 => 236767)
--- trunk/Source/WebCore/platform/mac/WebCoreNSURLExtras.mm 2018-10-02 22:14:52 UTC (rev 236766)
+++ trunk/Source/WebCore/platform/mac/WebCoreNSURLExtras.mm 2018-10-02 22:34:09 UTC (rev 236767)
@@ -44,7 +44,7 @@
#define HOST_NAME_BUFFER_LENGTH 2048
#define URL_BYTES_BUFFER_LENGTH 2048
-typedef void (* StringRangeApplierFunction)(NSString *string, NSRange range, void *context);
+typedef void (* StringRangeApplierFunction)(NSString *, NSRange, RetainPtr<NSMutableArray>&);
static uint32_t IDNScriptWhiteList[(USCRIPT_CODE_LIMIT + 31) / 32];
@@ -631,7 +631,7 @@
return !host ? string : host;
}
-static void collectRangesThatNeedMapping(NSString *string, NSRange range, void *context, BOOL encode)
+static void collectRangesThatNeedMapping(NSString *string, NSRange range, RetainPtr<NSMutableArray>& array, BOOL encode)
{
// Generally, we want to optimize for the case where there is one host name that does not need mapping.
// Therefore, we use nil to indicate no mapping here and an empty array to indicate error.
@@ -641,39 +641,32 @@
if (!error && !needsMapping)
return;
- __strong NSMutableArray **array = (__strong NSMutableArray **)context;
- if (!*array)
- *array = [[NSMutableArray alloc] init];
-
+ if (!array)
+ array = adoptNS([NSMutableArray new]);
+
if (!error)
- [*array addObject:[NSValue valueWithRange:range]];
+ [array addObject:[NSValue valueWithRange:range]];
}
-static void collectRangesThatNeedEncoding(NSString *string, NSRange range, void *context)
+static void collectRangesThatNeedEncoding(NSString *string, NSRange range, RetainPtr<NSMutableArray>& array)
{
- return collectRangesThatNeedMapping(string, range, context, YES);
+ return collectRangesThatNeedMapping(string, range, array, YES);
}
-static void collectRangesThatNeedDecoding(NSString *string, NSRange range, void *context)
+static void collectRangesThatNeedDecoding(NSString *string, NSRange range, RetainPtr<NSMutableArray>& array)
{
- return collectRangesThatNeedMapping(string, range, context, NO);
+ return collectRangesThatNeedMapping(string, range, array, NO);
}
-static inline NSCharacterSet *retain(NSCharacterSet *charset)
+static void applyHostNameFunctionToMailToURLString(NSString *string, StringRangeApplierFunction f, RetainPtr<NSMutableArray>& array)
{
- CFRetain(charset);
- return charset;
-}
-
-static void applyHostNameFunctionToMailToURLString(NSString *string, StringRangeApplierFunction f, void *context)
-{
// In a mailto: URL, host names come after a '@' character and end with a '>' or ',' or '?' character.
// Skip quoted strings so that characters in them don't confuse us.
// When we find a '?' character, we are past the part of the URL that contains host names.
- static NSCharacterSet *hostNameOrStringStartCharacters = retain([NSCharacterSet characterSetWithCharactersInString:@"\"@?"]);
- static NSCharacterSet *hostNameEndCharacters = retain([NSCharacterSet characterSetWithCharactersInString:@">,?"]);
- static NSCharacterSet *quotedStringCharacters = retain([NSCharacterSet characterSetWithCharactersInString:@"\"\\"]);
+ static NeverDestroyed<RetainPtr<NSCharacterSet>> hostNameOrStringStartCharacters = [NSCharacterSet characterSetWithCharactersInString:@"\"@?"];
+ static NeverDestroyed<RetainPtr<NSCharacterSet>> hostNameEndCharacters = [NSCharacterSet characterSetWithCharactersInString:@">,?"];
+ static NeverDestroyed<RetainPtr<NSCharacterSet>> quotedStringCharacters = [NSCharacterSet characterSetWithCharactersInString:@"\"\\"];
unsigned stringLength = [string length];
NSRange remaining = NSMakeRange(0, stringLength);
@@ -680,7 +673,7 @@
while (1) {
// Find start of host name or of quoted string.
- NSRange hostNameOrStringStart = [string rangeOfCharacterFromSet:hostNameOrStringStartCharacters options:0 range:remaining];
+ NSRange hostNameOrStringStart = [string rangeOfCharacterFromSet:hostNameOrStringStartCharacters.get().get() options:0 range:remaining];
if (hostNameOrStringStart.location == NSNotFound)
return;
@@ -694,7 +687,7 @@
if (c == '@') {
// Find end of host name.
unsigned hostNameStart = remaining.location;
- NSRange hostNameEnd = [string rangeOfCharacterFromSet:hostNameEndCharacters options:0 range:remaining];
+ NSRange hostNameEnd = [string rangeOfCharacterFromSet:hostNameEndCharacters.get().get() options:0 range:remaining];
BOOL done;
if (hostNameEnd.location == NSNotFound) {
hostNameEnd.location = stringLength;
@@ -706,7 +699,7 @@
}
// Process host name range.
- f(string, NSMakeRange(hostNameStart, hostNameEnd.location - hostNameStart), context);
+ f(string, NSMakeRange(hostNameStart, hostNameEnd.location - hostNameStart), array);
if (done)
return;
@@ -714,7 +707,7 @@
// Skip quoted string.
ASSERT(c == '"');
while (1) {
- NSRange escapedCharacterOrStringEnd = [string rangeOfCharacterFromSet:quotedStringCharacters options:0 range:remaining];
+ NSRange escapedCharacterOrStringEnd = [string rangeOfCharacterFromSet:quotedStringCharacters.get().get() options:0 range:remaining];
if (escapedCharacterOrStringEnd.location == NSNotFound)
return;
@@ -738,7 +731,7 @@
}
}
-static void applyHostNameFunctionToURLString(NSString *string, StringRangeApplierFunction f, void *context)
+static void applyHostNameFunctionToURLString(NSString *string, StringRangeApplierFunction f, RetainPtr<NSMutableArray>& array)
{
// Find hostnames. Too bad we can't use any real URL-parsing code to do this,
// but we have to do it before doing all the %-escaping, and this is the only
@@ -747,7 +740,7 @@
// Maybe we should implement this using a character buffer instead?
if (protocolIs(string, "mailto")) {
- applyHostNameFunctionToMailToURLString(string, f, context);
+ applyHostNameFunctionToMailToURLString(string, f, array);
return;
}
@@ -760,19 +753,19 @@
return;
// Check that all characters before the :// are valid scheme characters.
- static NSCharacterSet *nonSchemeCharacters = retain([[NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-."] invertedSet]);
- if ([string rangeOfCharacterFromSet:nonSchemeCharacters options:0 range:NSMakeRange(0, separatorRange.location)].location != NSNotFound)
+ static NeverDestroyed<RetainPtr<NSCharacterSet>> nonSchemeCharacters = [[NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-."] invertedSet];
+ if ([string rangeOfCharacterFromSet:nonSchemeCharacters.get().get() options:0 range:NSMakeRange(0, separatorRange.location)].location != NSNotFound)
return;
unsigned stringLength = [string length];
- static NSCharacterSet *hostTerminators = retain([NSCharacterSet characterSetWithCharactersInString:@":/?#"]);
+ static NeverDestroyed<RetainPtr<NSCharacterSet>> hostTerminators = [NSCharacterSet characterSetWithCharactersInString:@":/?#"];
// Start after the separator.
unsigned authorityStart = NSMaxRange(separatorRange);
// Find terminating character.
- NSRange hostNameTerminator = [string rangeOfCharacterFromSet:hostTerminators options:0 range:NSMakeRange(authorityStart, stringLength - authorityStart)];
+ NSRange hostNameTerminator = [string rangeOfCharacterFromSet:hostTerminators.get().get() options:0 range:NSMakeRange(authorityStart, stringLength - authorityStart)];
unsigned hostNameEnd = hostNameTerminator.location == NSNotFound ? stringLength : hostNameTerminator.location;
// Find "@" for the start of the host name.
@@ -779,10 +772,10 @@
NSRange userInfoTerminator = [string rangeOfString:@"@" options:0 range:NSMakeRange(authorityStart, hostNameEnd - authorityStart)];
unsigned hostNameStart = userInfoTerminator.location == NSNotFound ? authorityStart : NSMaxRange(userInfoTerminator);
- f(string, NSMakeRange(hostNameStart, hostNameEnd - hostNameStart), context);
+ return f(string, NSMakeRange(hostNameStart, hostNameEnd - hostNameStart), array);
}
-static NSString *mapHostNames(NSString *string, BOOL encode)
+static RetainPtr<NSString> mapHostNames(NSString *string, BOOL encode)
{
// Generally, we want to optimize for the case where there is one host name that does not need mapping.
@@ -790,19 +783,17 @@
return string;
// Make a list of ranges that actually need mapping.
- NSMutableArray *hostNameRanges = nil;
+ RetainPtr<NSMutableArray> hostNameRanges;
StringRangeApplierFunction f = encode ? collectRangesThatNeedEncoding : collectRangesThatNeedDecoding;
- applyHostNameFunctionToURLString(string, f, &hostNameRanges);
+ applyHostNameFunctionToURLString(string, f, hostNameRanges);
if (!hostNameRanges)
return string;
- if (![hostNameRanges count]) {
- [hostNameRanges release];
+ if (![hostNameRanges count])
return nil;
- }
// Do the mapping.
- NSMutableString *mutableCopy = [string mutableCopy];
+ auto mutableCopy = adoptNS([string mutableCopy]);
unsigned i = [hostNameRanges count];
while (i--) {
NSRange hostNameRange = [[hostNameRanges objectAtIndex:i] rangeValue];
@@ -809,14 +800,13 @@
NSString *mappedHostName = encode ? encodeHostNameWithRange(string, hostNameRange) : decodeHostNameWithRange(string, hostNameRange);
[mutableCopy replaceCharactersInRange:hostNameRange withString:mappedHostName];
}
- [hostNameRanges release];
- return [mutableCopy autorelease];
+ return mutableCopy;
}
-static NSString *stringByTrimmingWhitespace(NSString *string)
+static RetainPtr<NSString> stringByTrimmingWhitespace(NSString *string)
{
- NSMutableString *trimmed = [[string mutableCopy] autorelease];
- CFStringTrimWhitespace((__bridge CFMutableStringRef)trimmed);
+ auto trimmed = adoptNS([string mutableCopy]);
+ CFStringTrimWhitespace((__bridge CFMutableStringRef)trimmed.get());
return trimmed;
}
@@ -912,18 +902,18 @@
if (!string)
return nil;
- string = mapHostNames(stringByTrimmingWhitespace(string), YES);
- if (!string)
+ auto mappedString = mapHostNames(stringByTrimmingWhitespace(string).get(), YES);
+ if (!mappedString)
return nil;
// Let's check whether the URL is bogus.
- URL url { URL { nsURL }, string };
+ URL url { URL { nsURL }, mappedString.get() };
if (!url.createCFURL())
return nil;
// FIXME: https://bugs.webkit.org/show_bug.cgi?id=186057
// We should be able to use url.createCFURL instead of using directly CFURL parsing routines.
- NSData *data = ""
+ NSData *data = ""
if (!data)
return [NSURL URLWithString:@""];
@@ -1158,7 +1148,7 @@
*q = '\0';
// Check string to see if it can be converted to display using UTF-8
- NSString *result = [NSString stringWithUTF8String:after.data()];
+ RetainPtr<NSString> result = [NSString stringWithUTF8String:after.data()];
if (!result) {
// Could not convert to UTF-8.
// Convert characters greater than 0x7f to escape sequences.
@@ -1185,13 +1175,13 @@
if (mayNeedHostNameDecoding) {
// FIXME: Is it good to ignore the failure of mapHostNames and keep result intact?
- NSString *mappedResult = mapHostNames(result, NO);
+ auto mappedResult = mapHostNames(result.get(), NO);
if (mappedResult)
result = mappedResult;
}
result = [result precomposedStringWithCanonicalMapping];
- return CFBridgingRelease(createStringWithEscapedUnsafeCharacters((__bridge CFStringRef)result));
+ return CFBridgingRelease(createStringWithEscapedUnsafeCharacters((__bridge CFStringRef)result.get()));
}
BOOL isUserVisibleURL(NSString *string)
@@ -1238,17 +1228,14 @@
NSRange colon = [string rangeOfString:@":"];
if (colon.location != NSNotFound && colon.location > 0) {
NSRange scheme = {0, colon.location};
- static NSCharacterSet *InverseSchemeCharacterSet = nil;
- if (!InverseSchemeCharacterSet) {
- /*
- This stuff is very expensive. 10-15 msec on a 2x1.2GHz. If not cached it swamps
- everything else when adding items to the autocomplete DB. Makes me wonder if we
- even need to enforce the character set here.
- */
- NSString *acceptableCharacters = @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+.-";
- InverseSchemeCharacterSet = [[[NSCharacterSet characterSetWithCharactersInString:acceptableCharacters] invertedSet] retain];
- }
- NSRange illegals = [string rangeOfCharacterFromSet:InverseSchemeCharacterSet options:0 range:scheme];
+ /*
+ This stuff is very expensive. 10-15 msec on a 2x1.2GHz. If not cached it swamps
+ everything else when adding items to the autocomplete DB. Makes me wonder if we
+ even need to enforce the character set here.
+ */
+ NSString *acceptableCharacters = @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+.-";
+ static NeverDestroyed<RetainPtr<NSCharacterSet>> InverseSchemeCharacterSet([[NSCharacterSet characterSetWithCharactersInString:acceptableCharacters] invertedSet]);
+ NSRange illegals = [string rangeOfCharacterFromSet:InverseSchemeCharacterSet.get().get() options:0 range:scheme];
if (illegals.location == NSNotFound)
return scheme;
}
@@ -1258,7 +1245,7 @@
BOOL looksLikeAbsoluteURL(NSString *string)
{
// Trim whitespace because _web_URLWithString allows whitespace.
- return rangeOfURLScheme(stringByTrimmingWhitespace(string)).location != NSNotFound;
+ return rangeOfURLScheme(stringByTrimmingWhitespace(string).get()).location != NSNotFound;
}
} // namespace WebCore