Skip to content

Commit

Permalink
Replaced RegexKitLite with modern system-provided NSRegularExpressions.
Browse files Browse the repository at this point in the history
  • Loading branch information
regexident committed Jun 8, 2015
1 parent aa0c970 commit 875db40
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 2,972 deletions.
9 changes: 4 additions & 5 deletions QuickLookStephenProject/GenerateThumbnailForURL.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
#include <QuickLook/QuickLook.h>

#import <Foundation/Foundation.h>
#import "RegexKitLite.h"

#import "QLSFileAttributes.h"


/**
* This dictionary is used for a file with no extension. It maps the MIME type
* (as returned by file(1)) onto an appropriate thumbnail badge.
Expand Down Expand Up @@ -72,10 +70,11 @@
// Does the filename match a known pattern? If so, use the appropriate badge.
if (!badge && [fileExtension isEqualToString:@""]) {
NSDictionary *map = filenameRegexToBadgeMap();

[map enumerateKeysAndObjectsUsingBlock:
^(NSString *regex, NSString *candidateBadge, BOOL *stop) {
if ([fileName rkl_isMatchedByRegex:regex]) {
^(NSString *pattern, NSString *candidateBadge, BOOL *stop) {
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
NSRange range = [regex rangeOfFirstMatchInString:fileName options:0 range:NSMakeRange(0, fileName.length)];
if (range.location != NSNotFound) {
badge = candidateBadge;
*stop = true;
}
Expand Down
21 changes: 8 additions & 13 deletions QuickLookStephenProject/QLSFileAttributes.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//

#import "QLSFileAttributes.h"
#import "RegexKitLite.h"

@interface QLSFileAttributes ()

Expand All @@ -19,18 +18,16 @@ @interface QLSFileAttributes ()

@implementation QLSFileAttributes

+ (instancetype)attributesForItemAtURL:(NSURL *)aURL
{
+ (instancetype)attributesForItemAtURL:(NSURL *)aURL {
NSString *magicString = [self magicStringForItemAtURL:aURL];
if (!magicString) return nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(\\S+/\\S+); charset=(\\S+)" options:0 error:nil];
NSTextCheckingResult *match = [regex firstMatchInString:magicString options:0 range:NSMakeRange(0, magicString.length)];

NSArray *matches = [magicString rkl_captureComponentsMatchedByRegex:
@"(\\S+/\\S+); charset=(\\S+)"];
if (!match) return nil;

if (![matches count]) return nil;

NSString *mimeType = matches[1];
NSString *charset = matches[2];
NSString *mimeType = [magicString substringWithRange:[match rangeAtIndex:1]];
NSString *charset = [magicString substringWithRange:[match rangeAtIndex:2]];

BOOL mimeTypeIsTextual = [self mimeTypeIsTextual:mimeType];

Expand All @@ -50,8 +47,7 @@ + (instancetype)attributesForItemAtURL:(NSURL *)aURL
// Private Methods
////////////////////////////////////////////////////////////////////////////////

+ (NSString *)magicStringForItemAtURL:(NSURL *)aURL
{
+ (NSString *)magicStringForItemAtURL:(NSURL *)aURL {
NSString *path = [aURL path];
NSParameterAssert(path);

Expand Down Expand Up @@ -94,8 +90,7 @@ + (NSString *)magicStringForItemAtURL:(NSURL *)aURL
* @return YES if mimeType contains "text", or if the mime type conforms to the
* public.text UTI.
*/
+ (BOOL)mimeTypeIsTextual:(NSString *)mimeType
{
+ (BOOL)mimeTypeIsTextual:(NSString *)mimeType {
NSArray *components = [mimeType componentsSeparatedByString:@"/"];
if (components.count != 2)
return NO;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

/* Begin PBXBuildFile section */
0107ABFF15C76F2900C65F1A /* QLSFileAttributes.m in Sources */ = {isa = PBXBuildFile; fileRef = 0107ABFD15C76F2900C65F1A /* QLSFileAttributes.m */; };
0149EB3B15F2E8E4003AB298 /* RegexKitLite.m in Sources */ = {isa = PBXBuildFile; fileRef = 0149EB3915F2E8E4003AB298 /* RegexKitLite.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
0149EB3E15F2E9A3003AB298 /* libicucore.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 0149EB3D15F2E9A3003AB298 /* libicucore.dylib */; };
2C05A19C06CAA52B00D84F6F /* GeneratePreviewForURL.m in Sources */ = {isa = PBXBuildFile; fileRef = 2C05A19B06CAA52B00D84F6F /* GeneratePreviewForURL.m */; };
61E3BCFB0870B4F2002186A0 /* GenerateThumbnailForURL.m in Sources */ = {isa = PBXBuildFile; fileRef = 61E3BCFA0870B4F2002186A0 /* GenerateThumbnailForURL.m */; };
Expand Down Expand Up @@ -37,9 +36,7 @@

/* Begin PBXFileReference section */
0107ABFC15C76F2900C65F1A /* QLSFileAttributes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QLSFileAttributes.h; sourceTree = "<group>"; };
0107ABFD15C76F2900C65F1A /* QLSFileAttributes.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QLSFileAttributes.m; sourceTree = "<group>"; };
0149EB3815F2E8E4003AB298 /* RegexKitLite.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RegexKitLite.h; sourceTree = "<group>"; };
0149EB3915F2E8E4003AB298 /* RegexKitLite.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RegexKitLite.m; sourceTree = "<group>"; };
0107ABFD15C76F2900C65F1A /* QLSFileAttributes.m */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.c.objc; path = QLSFileAttributes.m; sourceTree = "<group>"; tabWidth = 2; usesTabs = 0; };
0149EB3D15F2E9A3003AB298 /* libicucore.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libicucore.dylib; path = usr/lib/libicucore.dylib; sourceTree = SDKROOT; };
089C167EFE841241C02AAC07 /* English */ = {isa = PBXFileReference; fileEncoding = 10; lastKnownFileType = text.plist.strings; name = English; path = English.lproj/InfoPlist.strings; sourceTree = "<group>"; };
08FB77B6FE84183AC02AAC07 /* main.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = main.c; sourceTree = "<group>"; };
Expand Down Expand Up @@ -71,19 +68,9 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
0149EB3F15F2EC37003AB298 /* Third-Party */ = {
isa = PBXGroup;
children = (
0149EB3815F2E8E4003AB298 /* RegexKitLite.h */,
0149EB3915F2E8E4003AB298 /* RegexKitLite.m */,
);
name = "Third-Party";
sourceTree = "<group>";
};
089C166AFE841209C02AAC07 /* QuickLookStephen */ = {
isa = PBXGroup;
children = (
0149EB3F15F2EC37003AB298 /* Third-Party */,
08FB77AFFE84173DC02AAC07 /* Source */,
089C167CFE841241C02AAC07 /* Resources */,
089C1671FE841209C02AAC07 /* External Frameworks and Libraries */,
Expand Down Expand Up @@ -200,7 +187,6 @@
2C05A19C06CAA52B00D84F6F /* GeneratePreviewForURL.m in Sources */,
61E3BCFB0870B4F2002186A0 /* GenerateThumbnailForURL.m in Sources */,
0107ABFF15C76F2900C65F1A /* QLSFileAttributes.m in Sources */,
0149EB3B15F2E8E4003AB298 /* RegexKitLite.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down