-
Notifications
You must be signed in to change notification settings - Fork 0
/
XYCharacterEscaper.m
117 lines (90 loc) · 2.09 KB
/
XYCharacterEscaper.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//
// XYCharacterEscaper.m
// Demo
//
// Created by hzyuxiaohua on 15/11/17.
// Copyright © 2015年 X Co., Ltd. All rights reserved.
//
#import "XYCharacterEscaper.h"
@interface XYCharacterEscaper ()
@property (nonatomic, copy) NSString* source;
@end
@implementation XYCharacterEscaper
- (NSString *)value
{
return _source;
}
+ (instancetype)escaperWithSource:(NSString *)source
{
XYCharacterEscaper* escaper = [XYCharacterEscaper new];
[escaper setSource:source];
return escaper;
}
- (XYEscaperHandler)escapeSemicolon
{
return ^() {
[self escape:@";" into:@"\\;"];
return self;
};
}
- (XYEscaperHandler)unescapeSemicolon
{
return ^ () {
[self unescape:@"\\;" into:@";"];
return self;
};
}
- (XYEscaperHandler)escapeColon
{
return ^ () {
[self escape:@":" into:@"\\:"];
return self;
};
}
- (XYEscaperHandler)unescapeColon
{
return ^ () {
[self unescape:@"\\:" into:@":"];
return self;
};
}
- (XYEscaperHandler)escapeComma
{
return ^ () {
[self escape:@"," into:@"\\,"];
return self;
};
}
- (XYEscaperHandler)unescapeComma
{
return ^ () {
[self unescape:@"\\," into:@","];
return self;
};
}
- (XYEscaperHandler)escapeCTLF
{
return ^ () {
[self escape:@"\n" into:@"\\n"];
[self escape:@"\r" into:@"\\r"];
return self;
};
}
- (XYEscaperHandler)unescapeCTLF
{
return ^ () {
[self unescape:@"\\n" into:@"\n"];
[self unescape:@"\\r" into:@"\r"];
return self;
};
}
- (void)escape:(NSString *)character into:(NSString *)escaped_character
{
_source = [_source stringByReplacingOccurrencesOfString:escaped_character withString:character];
_source = [_source stringByReplacingOccurrencesOfString:character withString:escaped_character];
}
- (void)unescape:(NSString *)escaped_character into:(NSString *)character
{
_source = [_source stringByReplacingOccurrencesOfString:escaped_character withString:character];
}
@end