Skip to content

Commit 2c6ac9b

Browse files
committed
Slight refactoring to match Swift extension
1 parent c15bc8b commit 2c6ac9b

File tree

4 files changed

+99
-108
lines changed

4 files changed

+99
-108
lines changed

TwitterSearch/TwitterSearch/NSString+URLEncoding.h

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,38 +35,47 @@
3535

3636
/**
3737
An NSString category that provides percent encoding of URL
38-
strings.
38+
strings following RFC 3986 or the W3C HTML specification.
3939
*/
4040

4141
@interface NSString (URLEncoding)
4242

43-
/**--------------------------------------
44-
* @name Instance Methods
45-
* --------------------------------------
46-
*/
47-
4843
/**
4944
Returns a new string made from the receiver by replacing characters which are
50-
reserved in a URL query with percent encoded characters (see RFC 3986).
51-
52-
@param plusForSpace A boolean flag which when set replaces spaces by a '+'
53-
otherwise they are percent escaped as %20.
54-
45+
reserved in a URI query with percent encoded characters.
46+
47+
@discussion The following characters are not considered reserved in a URI query
48+
by RFC 3986:
49+
50+
- Alpha "a...z" "A...Z"
51+
- Numberic "0...9"
52+
- Unreserved "-._~"
53+
54+
In addition the reserved characters "/" and "?" have no reserved purpose in the
55+
query component of a URI so do not need to be percent escaped.
56+
5557
@return Returns the encoded string, or nil if the transformation is not possible.
5658
*/
5759

58-
- (nullable NSString *)stringByAddingPercentEncodingForURLQuery:(BOOL)plusForSpace;
60+
- (nullable NSString *)stringByAddingPercentEncodingForRFC3986;
5961

6062
/**
6163
Returns a new string made from the receiver by replacing characters which are
62-
reserved in a URL encoded form with percent encoded characters.
64+
reserved in HTML forms (media type application/x-www-form-urlencoded) with
65+
percent encoded characters.
66+
67+
@discussion The W3C HTML5 specification, section 4.10.22.5 URL-encoded form
68+
data percent encodes all characters except the following:
69+
70+
- Space (0x20) is replaced by a "+" (0x2B)
71+
- Bytes in the range 0x2A, 0x2D, 0x2E, 0x30-0x39, 0x41-0x5A, 0x5F, 0x61-0x7A
72+
(alphanumeric + "*-._")
6373
64-
@discussion Unlike -stringByAddingPercentEncodingForURLQuery: this method
65-
does not escape '*', spaces are always replaced by '+' and the '~' is not
66-
an allowed character so it is percent escaped.
74+
@param plusForSpace Boolean, when true replaces space with a '+'
75+
otherwise uses percent encoding (%20). Default is false.
6776
6877
@return Returns the encoded string, or nil if the transformation is not possible.
6978
*/
7079

71-
- (nullable NSString *)stringByAddingPercentEncodingForURLFormData;
80+
- (nullable NSString *)stringByAddingPercentEncodingForFormData:(BOOL)plusForSpace;
7281
@end

TwitterSearch/TwitterSearch/NSString+URLEncoding.m

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,26 @@
3535

3636
@implementation NSString (URLEncoding)
3737

38-
- (nullable NSString *)stringByAddingPercentEncodingForURLQuery:(BOOL)plusForSpace {
39-
NSString *unreserved = @"-._~";
40-
NSMutableCharacterSet *urlQueryAllowedCharacterSet = [NSMutableCharacterSet alphanumericCharacterSet];
41-
[urlQueryAllowedCharacterSet addCharactersInString:unreserved];
42-
38+
- (nullable NSString *)stringByAddingPercentEncodingForRFC3986 {
39+
NSString *unreserved = @"-._~/?";
40+
NSMutableCharacterSet *allowedCharacterSet = [NSMutableCharacterSet alphanumericCharacterSet];
41+
[allowedCharacterSet addCharactersInString:unreserved];
42+
return [self stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacterSet];
43+
}
44+
45+
- (nullable NSString *)stringByAddingPercentEncodingForFormData:(BOOL)plusForSpace {
46+
NSString *unreserved = @"*-._";
47+
NSMutableCharacterSet *allowedCharacterSet = [NSMutableCharacterSet alphanumericCharacterSet];
48+
[allowedCharacterSet addCharactersInString:unreserved];
4349
if (plusForSpace) {
44-
[urlQueryAllowedCharacterSet addCharactersInString:@" "];
50+
[allowedCharacterSet addCharactersInString:@" "];
4551
}
4652

47-
NSString *encodedString = [self stringByAddingPercentEncodingWithAllowedCharacters:urlQueryAllowedCharacterSet];
53+
NSString *encoded = [self stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacterSet];
4854
if (plusForSpace) {
49-
encodedString = [encodedString stringByReplacingOccurrencesOfString:@" " withString:@"+"];
55+
encoded = [encoded stringByReplacingOccurrencesOfString:@" " withString:@"+"];
5056
}
51-
return encodedString;
52-
}
53-
54-
- (nullable NSString *)stringByAddingPercentEncodingForURLFormData {
55-
NSString *unreserved = @"-._* ";
56-
NSMutableCharacterSet *urlQueryAllowedCharacterSet = [NSMutableCharacterSet alphanumericCharacterSet];
57-
[urlQueryAllowedCharacterSet addCharactersInString:unreserved];
58-
59-
NSString *encodedString = [self stringByAddingPercentEncodingWithAllowedCharacters:urlQueryAllowedCharacterSet];
60-
encodedString = [encodedString stringByReplacingOccurrencesOfString:@" " withString:@"+"];
61-
return encodedString;
57+
return encoded;
6258
}
6359

6460
@end

TwitterSearch/TwitterSearch/SearchViewController.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)ce
170170
- (void)loadQuery
171171
{
172172
self.searchState = UYLTwitterSearchStateLoading;
173-
NSString *encodedQuery = [self.query stringByAddingPercentEncodingForURLFormData];
173+
NSString *encodedQuery = [self.query stringByAddingPercentEncodingForFormData:NO];
174174
ACAccountType *accountType = [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
175175
[self.accountStore requestAccessToAccountsWithType:accountType
176176
options:NULL

TwitterSearch/TwitterSearchUnitTests/NSStringURLEncodingUnitTests.m

Lines changed: 57 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -47,108 +47,94 @@ - (void)tearDown {
4747
[super tearDown];
4848
}
4949

50-
- (void)testQueryAllowed {
51-
NSString *input = @"ABC123abc";
52-
NSString *output = [input stringByAddingPercentEncodingForURLQuery:NO];
53-
NSString *expected = @"ABC123abc";
54-
BOOL result = [expected isEqualToString:output];
55-
XCTAssertTrue(result, "Expected: %@ got %@",expected,output);
50+
- (void)testRFC3986AlphaNumericNotEncoded {
51+
NSString *input = @"abcdefghijklmnopqrstuvwxyz"
52+
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
53+
"0123456789";
54+
NSString *output = [input stringByAddingPercentEncodingForRFC3986];
55+
XCTAssertEqualObjects(input, output);
5656
}
5757

58-
- (void)testFormAllowed {
59-
NSString *input = @"ABC123abc";
60-
NSString *output = [input stringByAddingPercentEncodingForURLFormData];
61-
NSString *expected = @"ABC123abc";
62-
BOOL result = [expected isEqualToString:output];
63-
XCTAssertTrue(result, "Expected: %@ got %@",expected,output);
58+
- (void)testFormAlphaNumericNotEncoded {
59+
NSString *input = @"abcdefghijklmnopqrstuvwxyz"
60+
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
61+
"0123456789";
62+
NSString *output = [input stringByAddingPercentEncodingForFormData:NO];
63+
XCTAssertEqualObjects(input, output);
6464
}
6565

66-
- (void)testQuerySpaceIsPercentEncoded {
66+
- (void)testRFC3986UnreservedNotEncoded {
67+
NSString *input = @"-._~";
68+
NSString *output = [input stringByAddingPercentEncodingForRFC3986];
69+
XCTAssertEqualObjects(input, output);
70+
}
71+
72+
- (void)testRFC3986SlashQuestionNotEncoded {
73+
NSString *input = @"/?";
74+
NSString *output = [input stringByAddingPercentEncodingForRFC3986];
75+
XCTAssertEqualObjects(input, output);
76+
}
77+
78+
- (void)testFormUnreservedNotEncoded {
79+
NSString *input = @"*-._";
80+
NSString *output = [input stringByAddingPercentEncodingForFormData:NO];
81+
XCTAssertEqualObjects(input, output);
82+
}
83+
84+
- (void)testQuerySpacePercentEncoded {
6785
NSString *input = @"one two";
68-
NSString *output = [input stringByAddingPercentEncodingForURLQuery:NO];
86+
NSString *output = [input stringByAddingPercentEncodingForRFC3986];
6987
NSString *expected = @"one%20two";
70-
BOOL result = [expected isEqualToString:output];
71-
XCTAssertTrue(result, "Expected: %@ got %@",expected,output);
88+
XCTAssertEqualObjects(expected, output);
7289
}
7390

74-
- (void)testQuerySpaceIsPlusEncoded {
91+
- (void)testFormSpacePercentEncoded {
7592
NSString *input = @"one two";
76-
NSString *output = [input stringByAddingPercentEncodingForURLQuery:YES];
77-
NSString *expected = @"one+two";
78-
BOOL result = [expected isEqualToString:output];
79-
XCTAssertTrue(result, "Expected: %@ got %@",expected,output);
93+
NSString *output = [input stringByAddingPercentEncodingForFormData:NO];
94+
NSString *expected = @"one%20two";
95+
XCTAssertEqualObjects(expected, output);
8096
}
8197

82-
- (void)testFormSpaceIsPlusEncoded {
98+
- (void)testFormSpacePlusEncoded {
8399
NSString *input = @"one two";
84-
NSString *output = [input stringByAddingPercentEncodingForURLFormData];
100+
NSString *output = [input stringByAddingPercentEncodingForFormData:YES];
85101
NSString *expected = @"one+two";
86-
BOOL result = [expected isEqualToString:output];
87-
XCTAssertTrue(result, "Expected: %@ got %@",expected,output);
102+
XCTAssertEqualObjects(expected, output);
88103
}
89104

90-
- (void)testQueryPlusIsEncodedWhenUsingPlusForSpaces {
105+
- (void) testFormPlusIsPercentEncoded {
91106
NSString *input = @"one+two";
92-
NSString *output = [input stringByAddingPercentEncodingForURLQuery:YES];
107+
NSString *output = [input stringByAddingPercentEncodingForFormData:YES];
93108
NSString *expected = @"one%2Btwo";
94-
BOOL result = [expected isEqualToString:output];
95-
XCTAssertTrue(result, "Expected: %@ got %@",expected,output);
109+
XCTAssertEqualObjects(expected, output);
96110
}
97111

98-
- (void)testQueryPercentIsEncoded {
112+
- (void) testQueryPercentPercentEncoded {
99113
NSString *input = @"%";
100-
NSString *output = [input stringByAddingPercentEncodingForURLQuery:NO];
114+
NSString *output = [input stringByAddingPercentEncodingForRFC3986];
101115
NSString *expected = @"%25";
102-
BOOL result = [expected isEqualToString:output];
103-
XCTAssertTrue(result, "Expected: %@ got %@",expected,output);
116+
XCTAssertEqualObjects(expected, output);
104117
}
105118

106-
- (void)testFormPercentIsEncoded {
119+
- (void) testFormPercentPercentEncoded {
107120
NSString *input = @"%";
108-
NSString *output = [input stringByAddingPercentEncodingForURLFormData];
121+
NSString *output = [input stringByAddingPercentEncodingForFormData:NO];
109122
NSString *expected = @"%25";
110-
BOOL result = [expected isEqualToString:output];
111-
XCTAssertTrue(result, "Expected: %@ got %@",expected,output);
123+
XCTAssertEqualObjects(expected, output);
112124
}
113125

114-
- (void)testQueryReservedEncoded {
115-
NSString *input = @"!#$&'()*+,/:;=?@[]";
116-
NSString *output = [input stringByAddingPercentEncodingForURLQuery:NO];
117-
NSString *expected = @"%21%23%24%26%27%28%29%2A%2B%2C%2F%3A%3B%3D%3F%40%5B%5D";
118-
BOOL result = [expected isEqualToString:output];
119-
XCTAssertTrue(result, "Expected: %@ got %@",expected,output);
126+
- (void) testQueryReservedPercentEncoded {
127+
NSString *input = @"!#$&'()*+,:;=@[]";
128+
NSString *output = [input stringByAddingPercentEncodingForRFC3986];
129+
NSString *expected = @"%21%23%24%26%27%28%29%2A%2B%2C%3A%3B%3D%40%5B%5D";
130+
XCTAssertEqualObjects(expected, output);
120131
}
121132

122-
- (void)testFormReservedEncoded {
133+
- (void) testFormReservedPercentEncoded {
123134
NSString *input = @"!#$&'()+,/:;=?@[]";
124-
NSString *output = [input stringByAddingPercentEncodingForURLFormData];
135+
NSString *output = [input stringByAddingPercentEncodingForFormData:NO];
125136
NSString *expected = @"%21%23%24%26%27%28%29%2B%2C%2F%3A%3B%3D%3F%40%5B%5D";
126-
BOOL result = [expected isEqualToString:output];
127-
XCTAssertTrue(result, "Expected: %@ got %@",expected,output);
128-
}
129-
130-
- (void)testQueryUnreservedNotEncoded {
131-
NSString *input = @"-._~";
132-
NSString *output = [input stringByAddingPercentEncodingForURLQuery:NO];
133-
NSString *expected = @"-._~";
134-
BOOL result = [expected isEqualToString:output];
135-
XCTAssertTrue(result, "Expected: %@ got %@",expected,output);
136-
}
137-
138-
- (void)testFormUnreservedNotEncoded {
139-
NSString *input = @"-._*";
140-
NSString *output = [input stringByAddingPercentEncodingForURLFormData];
141-
NSString *expected = @"-._*";
142-
BOOL result = [expected isEqualToString:output];
143-
XCTAssertTrue(result, "Expected: %@ got %@",expected,output);
144-
}
145-
146-
- (void)testFormTidleEncoded {
147-
NSString *input = @"~";
148-
NSString *output = [input stringByAddingPercentEncodingForURLFormData];
149-
NSString *expected = @"%7E";
150-
BOOL result = [expected isEqualToString:output];
151-
XCTAssertTrue(result, "Expected: %@ got %@",expected,output);
137+
XCTAssertEqualObjects(expected, output);
152138
}
153139

154140
@end

0 commit comments

Comments
 (0)