Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extracted kentekenformatter + tests from other project
- Loading branch information
1 parent
3fadfe1
commit 825b6bc
Showing
9 changed files
with
328 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/kenteken-formatter.xcodeproj/xcuserdata/ | ||
/kenteken-formatter.xcodeproj/project.xcworkspace/xcuserdata/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// NSString+KentekenFormatter.h | ||
// | ||
// Created by Mathijs Kadijk on 03-09-12. | ||
// Copyright (c) 2012 Wrep. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
typedef NS_ENUM(NSInteger, WRPKentekenSidecode) { | ||
WRPKentekenSidecodeBijzondereToelating = -3, | ||
WRPKentekenSidecodeInternationaalGerechtshof = -2, | ||
WRPKentekenSidecodeKoninklijkHuis = -1, | ||
WRPKentekenSidecodeUnknown = 0, | ||
WRPKentekenSidecode1 = 1, | ||
WRPKentekenSidecode2 = 2, | ||
WRPKentekenSidecode3 = 3, | ||
WRPKentekenSidecode4 = 4, | ||
WRPKentekenSidecode5 = 5, | ||
WRPKentekenSidecode6 = 6, | ||
WRPKentekenSidecode7 = 7, | ||
WRPKentekenSidecode8 = 8, | ||
WRPKentekenSidecode9 = 9, | ||
WRPKentekenSidecode10 = 10 | ||
}; | ||
|
||
@interface NSString (KentekenFormatter) | ||
|
||
|
||
/** | ||
Format een string naar een Nederlands kenteken. Sidecode 1 t/m 10 worden ondersteund. | ||
@discussion Corrupte kentekens worden zo goed en zo kwaad als het gaat geformat, dit zodat deze functie ook nuttig is om te gebruiken tijdens het invoeren van de gebruiker. Voor meer informatie over kenteken formaten en sidecodes zie http://nl.wikipedia.org/wiki/Nederlands_kenteken | ||
*/ | ||
- (NSString *)kentekenFormat; | ||
|
||
/** | ||
Check welke sidecode dit kenteken is. | ||
@discussion Voor meer informatie over kenteken sidecodes zie http://nl.wikipedia.org/wiki/Nederlands_kenteken | ||
*/ | ||
- (WRPKentekenSidecode)kentekenSidecode; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// | ||
// NSString+KentekenFormatter.m | ||
// | ||
// Created by Mathijs Kadijk on 03-09-12. | ||
// Copyright (c) 2012 Wrep. All rights reserved. | ||
// | ||
|
||
#import "NSString+KentekenFormatter.h" | ||
|
||
@implementation NSString (KentekenFormatter) | ||
|
||
- (NSString *)kentekenFormat | ||
{ | ||
// If string if empty just return an empty string | ||
if ([self length] == 0) | ||
{ | ||
return @""; | ||
} | ||
|
||
// Strip non-alphanumeric characters | ||
NSCharacterSet *nonAlphaNumericCharacters = [[NSCharacterSet alphanumericCharacterSet] invertedSet]; | ||
NSString *workingString = [[self componentsSeparatedByCharactersInSet:nonAlphaNumericCharacters] componentsJoinedByString:@""]; | ||
|
||
// Uppercase the string | ||
workingString = [workingString uppercaseString]; | ||
|
||
// Initialize some variables | ||
char curChar = [workingString characterAtIndex:0]; | ||
BOOL lastCharWasNumber = (curChar >= '0' && curChar <= '9'); | ||
int charsOfTheSameTypeInARow = 0; | ||
|
||
// Loop through all characters | ||
for (int i = 0; i < [workingString length]; i++) | ||
{ | ||
curChar = [workingString characterAtIndex:i]; | ||
BOOL curCharIsNumber = (curChar >= '0' && curChar <= '9'); | ||
|
||
if (curCharIsNumber != lastCharWasNumber) | ||
{ | ||
// We are switching from letters to numbers or vice versa, add dash and reset counter | ||
workingString = [workingString stringByReplacingCharactersInRange:NSMakeRange(i, 0) withString:@"-"]; | ||
charsOfTheSameTypeInARow = 0; | ||
} | ||
else | ||
{ | ||
// Count the number of letters/numbers in a row | ||
charsOfTheSameTypeInARow++; | ||
|
||
if (charsOfTheSameTypeInARow >= 4) | ||
{ | ||
// There are 4 of the same type of characters in a row, add dash between number 2 and 3 and set the counter to the correct amount | ||
workingString = [workingString stringByReplacingCharactersInRange:NSMakeRange(i-1, 0) withString:@"-"]; | ||
charsOfTheSameTypeInARow = 1; | ||
} | ||
} | ||
|
||
// Save the type of this character for the next iteration | ||
lastCharWasNumber = curCharIsNumber; | ||
} | ||
|
||
// Return the result | ||
return workingString; | ||
} | ||
|
||
- (WRPKentekenSidecode)kentekenSidecode | ||
{ | ||
// Initialize sidecode regexps | ||
NSArray *sidecodeExpressions = @[ | ||
@{@"regexp" : @"^ZZ-[0-9]{2}-[0-9]{2}$", @"sidecode" : @(WRPKentekenSidecodeBijzondereToelating)}, | ||
@{@"regexp" : @"^CDJ-[0-9]{3}$", @"sidecode" : @(WRPKentekenSidecodeInternationaalGerechtshof)}, | ||
@{@"regexp" : @"^AA-[0-9]{2,3}(-[0-9]{2})?$", @"sidecode" : @(WRPKentekenSidecodeKoninklijkHuis)}, | ||
@{@"regexp" : @"^[A-Z]{2}-[0-9]{2}-[0-9]{2}$", @"sidecode" : @(WRPKentekenSidecode1)}, | ||
@{@"regexp" : @"^[0-9]{2}-[0-9]{2}-[A-Z]{2}$", @"sidecode" : @(WRPKentekenSidecode2)}, | ||
@{@"regexp" : @"^[0-9]{2}-[A-Z]{2}-[0-9]{2}$", @"sidecode" : @(WRPKentekenSidecode3)}, | ||
@{@"regexp" : @"^[A-Z]{2}-[0-9]{2}-[A-Z]{2}$", @"sidecode" : @(WRPKentekenSidecode4)}, | ||
@{@"regexp" : @"^[A-Z]{2}-[A-Z]{2}-[0-9]{2}$", @"sidecode" : @(WRPKentekenSidecode5)}, | ||
@{@"regexp" : @"^[0-9]{2}-[A-Z]{2}-[A-Z]{2}$", @"sidecode" : @(WRPKentekenSidecode6)}, | ||
@{@"regexp" : @"^[0-9]{2}-[A-Z]{3}-[0-9]{1}$", @"sidecode" : @(WRPKentekenSidecode7)}, | ||
@{@"regexp" : @"^[0-9]{1}-[A-Z]{3}-[0-9]{2}$", @"sidecode" : @(WRPKentekenSidecode8)}, | ||
@{@"regexp" : @"^[A-Z]{2}-[0-9]{3}-[A-Z]{1}$", @"sidecode" : @(WRPKentekenSidecode9)}, | ||
@{@"regexp" : @"^[A-Z]{1}-[0-9]{3}-[A-Z]{2}$", @"sidecode" : @(WRPKentekenSidecode10)}, | ||
]; | ||
|
||
// Format kenteken | ||
NSString *kentekenString = [self kentekenFormat]; | ||
|
||
// Walk through all sidecode options | ||
for (NSDictionary *sidecodeInfo in sidecodeExpressions) | ||
{ | ||
// Initialize regexp | ||
NSError *error; | ||
NSRegularExpression *regexp = [NSRegularExpression regularExpressionWithPattern:sidecodeInfo[@"regexp"] | ||
options:0 | ||
error:&error]; | ||
|
||
#if DEBUG | ||
// Check for initialization errors | ||
if (error) | ||
{ | ||
NSLog(@"NSString+KentekenFormatter could not initialize regular expression: %@", sidecodeInfo[@"regexp"]); | ||
} | ||
#endif | ||
|
||
// Calculate if this string matches the regexp | ||
NSRange matchedRange = [regexp rangeOfFirstMatchInString:kentekenString | ||
options:0 | ||
range:NSMakeRange(0, kentekenString.length)]; | ||
|
||
// If we have a match... | ||
if (matchedRange.location != NSNotFound) | ||
{ | ||
// Return the correct sidecode | ||
return [sidecodeInfo[@"sidecode"] integerValue]; | ||
} | ||
} | ||
|
||
// No matched, so sidecode unknown | ||
return WRPKentekenSidecodeUnknown; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
kenteken-formatter.xcodeproj/project.xcworkspace/contents.xcworkspacedata
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// | ||
// KentekenFormatterTests.h | ||
// Schaderegistratie | ||
// | ||
// Created by Mathijs Kadijk on 03-09-12. | ||
// Copyright (c) 2012 Bultman. All rights reserved. | ||
// | ||
|
||
#import <SenTestingKit/SenTestingKit.h> | ||
|
||
@interface KentekenFormatterTests : SenTestCase { | ||
NSArray *kentekenData; | ||
} | ||
|
||
@end |
Oops, something went wrong.