-
Notifications
You must be signed in to change notification settings - Fork 0
/
BarUtilities.m
69 lines (50 loc) · 2.67 KB
/
BarUtilities.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
// BarUtilities.m
// Bartender
//
// Created by Tom Houpt on 7/19/20.
//
#include "BarUtilities.h"
@implementation NSString (ParsingBarTenderLabelExtensions)
- (NSArray *)parseAsBarTenderLabel; {
// parse a string from a barcode in the format <expt_code><subject_index><item_code>
// where expt_code is all alpha, subject_index is all numberic, and item_code is all alpha
// if there is no item_code, item_code is set to "_na"
// returns an NSArray @[ exptCode, subjectIndex, itemCode];
// NB: Barcode Code39 indicates lowercase letters with a "+" prefix, eg. "g" -> "+G"
// so we want to recognize alpha as A-Z and the + sign
// other ASCII characters use other escape prefixes, like "%" and "/" and "$",
// but those are outside our valid label characters
// https://en.wikipedia.org/wiki/Code_39
NSRange searchedRange = NSMakeRange(0, [self length]);
NSString *pattern = @"([A-Z\\+]+)([0-9]+)([A-Z\\+]+)*";
NSError *error = nil;
NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern: pattern options:NSRegularExpressionCaseInsensitive error:&error];
NSUInteger numberOfMatches = [regex numberOfMatchesInString:self options:0 range: searchedRange];
if (0 == numberOfMatches) { return nil; }
NSTextCheckingResult* match = [regex firstMatchInString:self options:0 range: searchedRange];
NSUInteger numberOfRanges = [match numberOfRanges];
if (4 > numberOfRanges){ return nil; }
/*
for (NSUInteger j = 0; j < numberOfRanges; j++) {
NSLog(@"%ld loc: %ld len: %ld", j, [match rangeAtIndex:j].location, [match rangeAtIndex:j].length);
}
*/
// NSString* matchText = [self substringWithRange:[match range]];
NSString* exptCode = nil;
NSString* subjectIndex = nil;
NSString* itemCode = @"_na";
if ( NSNotFound != [match rangeAtIndex:1].location && 0 != [match rangeAtIndex:1].length){
exptCode = [[self substringWithRange:[match rangeAtIndex:1]] stringByReplacingOccurrencesOfString:@"+" withString:@""];
}
if ( NSNotFound != [match rangeAtIndex:2].location && 0 != [match rangeAtIndex:2].length){
subjectIndex = [[self substringWithRange:[match rangeAtIndex:2]] stringByReplacingOccurrencesOfString:@"+" withString:@""];
}
if ( NSNotFound != [match rangeAtIndex:3].location && 0 != [match rangeAtIndex:3].length){
itemCode = [[self substringWithRange:[match rangeAtIndex:3]] stringByReplacingOccurrencesOfString:@"+" withString:@""];
}
// NSLog(@"Match Text: %@ %@ %@ %@", matchText, exptCode,subjectIndex,itemCode );
NSArray *label_codes = @[ exptCode, subjectIndex, itemCode];
return label_codes;
}
@end