forked from gnachman/iTerm2
/
iTermRule.m
99 lines (86 loc) · 2.29 KB
/
iTermRule.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// iTermRule.m
// iTerm
//
// Created by George Nachman on 6/24/14.
//
//
#import "iTermRule.h"
@interface iTermRule()
@property(nonatomic, copy) NSString *username;
@property(nonatomic, copy) NSString *hostname;
@property(nonatomic, copy) NSString *path;
@end
@implementation iTermRule
+ (instancetype)ruleWithString:(NSString *)string {
// hostname
// username@
// username@hostname
// username@hostname:path
// username@*:path
// hostname:path
// /path
NSString *username = nil;
NSString *hostname = nil;
NSString *path = nil;
NSUInteger atSign = [string rangeOfString:@"@"].location;
NSUInteger colon = [string rangeOfString:@":"].location;
if (atSign != NSNotFound) {
// user@host[:path]
username = [string substringToIndex:atSign];
if (colon != NSNotFound && colon < atSign) {
// malformed, like foo:bar@baz
colon = NSNotFound;
} else if (colon != NSNotFound) {
// user@host:path
hostname = [string substringWithRange:NSMakeRange(atSign + 1, colon - atSign - 1)];
} else if (colon == NSNotFound) {
// user@host
hostname = [string substringFromIndex:atSign + 1];
}
}
if (colon != NSNotFound) {
// [user@]host:path
path = [string substringFromIndex:colon + 1];
} else if (atSign == NSNotFound && [string hasPrefix:@"/"]) {
// /path
path = string;
} else if (atSign == NSNotFound && colon == NSNotFound) {
// host
hostname = string;
}
if ([hostname isEqualToString:@"*"]) {
// user@*:path or *:path
hostname = nil;
}
iTermRule *rule = [[[iTermRule alloc] init] autorelease];
rule.username = username;
rule.hostname = hostname;
rule.path = path;
return rule;
}
- (void)dealloc {
[_hostname release];
[_username release];
[_path release];
[super dealloc];
}
- (int)scoreForHostname:(NSString *)hostname
username:(NSString *)username
path:(NSString *)path {
const int kHostMatchScore = 4;
const int kUserMatchScore = 2;
const int kPathMatchScore = 1;
int score = 0;
if ([hostname isEqualToString:self.hostname]) {
score |= kHostMatchScore;
}
if ([username isEqualToString:self.username]) {
score |= kUserMatchScore;
}
if ([path isEqualToString:self.path]) {
score |= kPathMatchScore;
}
return score;
}
@end