Skip to content

Instantly share code, notes, and snippets.

@jerrykrinock
Created January 26, 2021 03:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jerrykrinock/82bf441bdd1ab78cbe769d8158b4df0f to your computer and use it in GitHub Desktop.
Save jerrykrinock/82bf441bdd1ab78cbe769d8158b4df0f to your computer and use it in GitHub Desktop.
How to replace deprecated CFURLCreateStringByAddingPercentEscapes() with -[NSString stringByAddingPercentEncodingWithAllowedCharacters:]
Question: Which NSCharacterSet should I pass to -[NSString stringByAddingPercentEncodingWithAllowedCharacters:]
so that it works exactly like the deprecated CFURLCreateStringByAddingPercentEscapes() did?
Answer: Either URLFragmentAllowedCharacterSet or URLQueryAllowedCharacterSet
Explanation:
That answer comes from the following program output, which is the result of running
the code below it.
Frag Host Pswd Path Qery User CFnd
0 0 0 0 0 0 0 00
0 0 0 0 0 0 0 01 
0 0 0 0 0 0 0 02 
0 0 0 0 0 0 0 03 
0 0 0 0 0 0 0 04 
0 0 0 0 0 0 0 05 
0 0 0 0 0 0 0 06 
0 0 0 0 0 0 0 07 
0 0 0 0 0 0 0 08 
0 0 0 0 0 0 0 09
0 0 0 0 0 0 0 0a
0 0 0 0 0 0 0 0b
0 0 0 0 0 0 0 0c
0 0 0 0 0 0 0 0d
0 0 0 0 0 0 0 0e 
0 0 0 0 0 0 0 0f 
0 0 0 0 0 0 0 10 
0 0 0 0 0 0 0 11 
0 0 0 0 0 0 0 12 
0 0 0 0 0 0 0 13 
0 0 0 0 0 0 0 14 
0 0 0 0 0 0 0 15 
0 0 0 0 0 0 0 16 
0 0 0 0 0 0 0 17 
0 0 0 0 0 0 0 18 
0 0 0 0 0 0 0 19 
0 0 0 0 0 0 0 1a 
0 0 0 0 0 0 0 1b 
0 0 0 0 0 0 0 1c 
0 0 0 0 0 0 0 1d 
0 0 0 0 0 0 0 1e 
0 0 0 0 0 0 0 1f 
0 0 0 0 0 0 0 20
1 1 1 1 1 1 1 21 !
0 0 0 0 0 0 0 22 "
0 0 0 0 0 0 0 23 #
1 1 1 1 1 1 1 24 $
0 0 0 0 0 0 0 25 %
1 1 1 1 1 1 1 26 &
1 1 1 1 1 1 1 27 '
1 1 1 1 1 1 1 28 (
1 1 1 1 1 1 1 29 )
1 1 1 1 1 1 1 2a *
1 1 1 1 1 1 1 2b +
1 1 1 1 1 1 1 2c ,
1 1 1 1 1 1 1 2d -
1 1 1 1 1 1 1 2e .
1 0 0 1 1 0 1 2f / Difference among the sets
1 1 1 1 1 1 1 30 0
1 1 1 1 1 1 1 31 1
1 1 1 1 1 1 1 32 2
1 1 1 1 1 1 1 33 3
1 1 1 1 1 1 1 34 4
1 1 1 1 1 1 1 35 5
1 1 1 1 1 1 1 36 6
1 1 1 1 1 1 1 37 7
1 1 1 1 1 1 1 38 8
1 1 1 1 1 1 1 39 9
1 1 0 1 1 0 1 3a : Difference among the sets
1 1 1 0 1 1 1 3b ; Difference among the sets
0 0 0 0 0 0 0 3c <
1 1 1 1 1 1 1 3d =
0 0 0 0 0 0 0 3e >
1 0 0 0 1 0 1 3f ? Difference among the sets
1 0 0 1 1 0 1 40 @ Difference among the sets
1 1 1 1 1 1 1 41 A
1 1 1 1 1 1 1 42 B
1 1 1 1 1 1 1 43 C
1 1 1 1 1 1 1 44 D
1 1 1 1 1 1 1 45 E
1 1 1 1 1 1 1 46 F
1 1 1 1 1 1 1 47 G
1 1 1 1 1 1 1 48 H
1 1 1 1 1 1 1 49 I
1 1 1 1 1 1 1 4a J
1 1 1 1 1 1 1 4b K
1 1 1 1 1 1 1 4c L
1 1 1 1 1 1 1 4d M
1 1 1 1 1 1 1 4e N
1 1 1 1 1 1 1 4f O
1 1 1 1 1 1 1 50 P
1 1 1 1 1 1 1 51 Q
1 1 1 1 1 1 1 52 R
1 1 1 1 1 1 1 53 S
1 1 1 1 1 1 1 54 T
1 1 1 1 1 1 1 55 U
1 1 1 1 1 1 1 56 V
1 1 1 1 1 1 1 57 W
1 1 1 1 1 1 1 58 X
1 1 1 1 1 1 1 59 Y
1 1 1 1 1 1 1 5a Z
0 1 0 0 0 0 0 5b [ Difference among the sets
0 0 0 0 0 0 0 5c \
0 1 0 0 0 0 0 5d ] Difference among the sets
0 0 0 0 0 0 0 5e ^
1 1 1 1 1 1 1 5f _
0 0 0 0 0 0 0 60 `
1 1 1 1 1 1 1 61 a
1 1 1 1 1 1 1 62 b
1 1 1 1 1 1 1 63 c
1 1 1 1 1 1 1 64 d
1 1 1 1 1 1 1 65 e
1 1 1 1 1 1 1 66 f
1 1 1 1 1 1 1 67 g
1 1 1 1 1 1 1 68 h
1 1 1 1 1 1 1 69 i
1 1 1 1 1 1 1 6a j
1 1 1 1 1 1 1 6b k
1 1 1 1 1 1 1 6c l
1 1 1 1 1 1 1 6d m
1 1 1 1 1 1 1 6e n
1 1 1 1 1 1 1 6f o
1 1 1 1 1 1 1 70 p
1 1 1 1 1 1 1 71 q
1 1 1 1 1 1 1 72 r
1 1 1 1 1 1 1 73 s
1 1 1 1 1 1 1 74 t
1 1 1 1 1 1 1 75 u
1 1 1 1 1 1 1 76 v
1 1 1 1 1 1 1 77 w
1 1 1 1 1 1 1 78 x
1 1 1 1 1 1 1 79 y
1 1 1 1 1 1 1 7a z
0 0 0 0 0 0 0 7b {
0 0 0 0 0 0 0 7c |
0 0 0 0 0 0 0 7d }
1 1 1 1 1 1 1 7e ~
0 0 0 0 0 0 0 7f 
Frag Host Pswd Path Qery User CFnd
Frag has 47 disallowed ASCII chars and MATCHES character set used by CFURLCreateStringByAddingPercentEscapes
Host has 48 disallowed ASCII chars and DIFFERS FROM character set used by CFURLCreateStringByAddingPercentEscapes
Pswd has 51 disallowed ASCII chars and DIFFERS FROM character set used by CFURLCreateStringByAddingPercentEscapes
Path has 49 disallowed ASCII chars and DIFFERS FROM character set used by CFURLCreateStringByAddingPercentEscapes
Qery has 47 disallowed ASCII chars and MATCHES character set used by CFURLCreateStringByAddingPercentEscapes
User has 51 disallowed ASCII chars and DIFFERS FROM character set used by CFURLCreateStringByAddingPercentEscapes
## Objective-C Code which generated the above output:
const char* const labels[] = {"Frag", "Host", "Pswd", "Path", "Qery", "User", "CFnd"};
char header[35];
NSInteger disallowedCounts[] = {0,0,0,0,0,0};
sprintf(header, "%s %s %s %s %s %s %s", labels[0], labels[1], labels[2], labels[3], labels[4], labels[5], labels[6]);
printf("\n%s\n", header);
BOOL matches[6] = {YES,YES,YES,YES,YES,YES};
for (NSInteger i=0; i<128; i++) {
BOOL isMember[7];
isMember[0] = [[NSCharacterSet URLFragmentAllowedCharacterSet] characterIsMember:(unichar)i];
isMember[1] = [[NSCharacterSet URLHostAllowedCharacterSet] characterIsMember:(unichar)i];
isMember[2] = [[NSCharacterSet URLPasswordAllowedCharacterSet] characterIsMember:(unichar)i];
isMember[3] = [[NSCharacterSet URLPathAllowedCharacterSet] characterIsMember:(unichar)i];
isMember[4] = [[NSCharacterSet URLQueryAllowedCharacterSet] characterIsMember:(unichar)i];
isMember[5] = [[NSCharacterSet URLUserAllowedCharacterSet] characterIsMember:(unichar)i];
for (NSInteger j=0; j<6; j++) {
if (isMember[j] == NO) {
disallowedCounts[j] = disallowedCounts[j] + 1;
}
}
BOOL diff = NO;
if (isMember[1] != isMember[0]) {
diff = YES;
} else if (isMember[2] != isMember[0]) {
diff = YES;
} else if (isMember[3] != isMember[0]) {
diff = YES;
} else if (isMember[4] != isMember[0]) {
diff = YES;
} else if (isMember[5] != isMember[0]) {
diff = YES;
}
BOOL okToPrint = ![[NSCharacterSet newlineCharacterSet] characterIsMember:(unichar)i];
unichar printChar = okToPrint ? i : 0;
NSString* cfEncoded = (NSString*)CFURLCreateStringByAddingPercentEscapes(
kCFAllocatorDefault,
(CFStringRef)[NSString stringWithCharacters:(unichar*)&i length:1],
NULL,
NULL,
kCFStringEncodingUTF8
) ;
isMember[6] = (cfEncoded.length > 1) ? NO : YES;
for (NSInteger i=0; i<6; i++) {
if (isMember[6] != isMember[i]) {
matches[i] = NO;
}
}
printf(" %hhd %hhd %hhd %hhd %hhd %hhd %hhd %02lx %c %s\n",
isMember[0], isMember[1], isMember[2], isMember[3], isMember[4], isMember[5], isMember[6], (long)i, printChar, diff ? "Difference among the sets" : "");
}
printf("%s\n\n", header);
for (NSInteger i=0; i<6; i++) {
if (matches[i]) {
printf("%s has %02ld disallowed ASCII chars and MATCHES character set used by CFURLCreateStringByAddingPercentEscapes\n", labels[i], (long)disallowedCounts[i]);
} else {
printf("%s has %02ld disallowed ASCII chars and DIFFERS FROM character set used by CFURLCreateStringByAddingPercentEscapes\n", labels[i], (long)disallowedCounts[i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment