Skip to content

Instantly share code, notes, and snippets.

@RobertAudi
Last active December 29, 2015 12:22
Show Gist options
  • Save RobertAudi/5926772 to your computer and use it in GitHub Desktop.
Save RobertAudi/5926772 to your computer and use it in GitHub Desktop.
NSString category containing an Objective-C port of @henrik's slugalize helper method: https://github.com/henrik/slugalizer?source=c
//
// Created by Robert Audi on 07/04/13.
// Updated on 08/21/14
//
#import <Foundation/Foundation.h>
@interface NSString (RAInflections)
- (NSString *)slugalize;
@end
//
// Created by Robert Audi on 07/04/13.
// Updated on 08/21/14
//
#import "NSString+RAInflections.h"
@implementation NSString (RAInflections)
/**
* Port of the slugalized helper created by @henrik
* https://github.com/RobertAudi/slugalizer
*/
- (NSString *)slugalize
{
NSString *separator = @"-";
NSMutableString *slugalizedString = [NSMutableString string];
NSRange replaceRange = NSMakeRange(0, self.length);
// Remove all non ASCII characters
NSError *nonASCIICharsRegexError = nil;
NSRegularExpression *nonASCIICharsRegex = [NSRegularExpression regularExpressionWithPattern:@"[^\\x00-\\x7F]+"
options:0
error:&nonASCIICharsRegexError];
slugalizedString = [[nonASCIICharsRegex stringByReplacingMatchesInString:self
options:0
range:replaceRange
withTemplate:@""] mutableCopy];
// Turn non-slug characters into separators
NSError *nonSlugCharactersError = nil;
NSRegularExpression *nonSlugCharactersRegex = [NSRegularExpression regularExpressionWithPattern:@"[^a-z0-9\\-_\\+]+"
options:NSRegularExpressionCaseInsensitive
error:&nonSlugCharactersError];
slugalizedString = [[nonSlugCharactersRegex stringByReplacingMatchesInString:slugalizedString
options:0
range:replaceRange
withTemplate:separator] mutableCopy];
// No more than one of the separator in a row
NSError *repeatingSeparatorsError = nil;
NSRegularExpression *repeatingSeparatorsRegex = [NSRegularExpression regularExpressionWithPattern:[NSString stringWithFormat:@"%@{2,}", separator]
options:0
error:&repeatingSeparatorsError];
slugalizedString = [[repeatingSeparatorsRegex stringByReplacingMatchesInString:slugalizedString
options:0
range:replaceRange
withTemplate:separator] mutableCopy];
// Remove leading/trailing separator
slugalizedString = [[slugalizedString stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:separator]] mutableCopy];
return [slugalizedString lowercaseString];
}
@end
@7Cornelio
Copy link

The replaceRange in line 37 should be recalculated, as slugalizedString may be shorter than the original string. If the original string ends in a non-ascii character, it crashes at line 35

@nickvelloff
Copy link

Hi thanks for this. I fixed the range issue in my fork if you would like to update.

@jeremypiednoel
Copy link

Should do
replaceRange = NSMakeRange(0, slugalizedString.length);
after all stringByReplacingMatchesInString

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment