This repository has been archived by the owner on Nov 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 106
/
typestring.xm
190 lines (150 loc) · 4.87 KB
/
typestring.xm
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// https://gist.github.com/markd2/5961219
#import "typestring.h"
static NSString *StructEncoding (char **typeScan);
// Remove all numbers from the string. Some type encoding strings include
// offsets and/or sizes, and they're often wrong. yay?
static NSString *ScrubNumbers (NSString *string) {
NSCharacterSet *numbers = [NSCharacterSet decimalDigitCharacterSet];
NSString *numberFree = [[string componentsSeparatedByCharactersInSet: numbers] componentsJoinedByString: @""];
return numberFree;
} // ScrubNumbers
// Convert simple types.
// |typeScan| is scooted over to account for any consumed characters
static NSString *SimpleEncoding (char **typeScan) {
typedef struct TypeMap {
unichar discriminator;
const char *name;
} TypeMap;
static TypeMap s_typeMap[] = {
{ 'c', (const char *)"char" },
{ 'i', (const char *)"int" },
{ 's', (const char *)"short" },
{ 'l', (const char *)"long" },
{ 'q', (const char *)"long long" },
{ 'C', (const char *)"unsigned char" },
{ 'I', (const char *)"unsigned int" },
{ 'S', (const char *)"unsigned short" },
{ 'L', (const char *)"unsiged long" },
{ 'Q', (const char *)"unsigned long long" },
{ 'f', (const char *)"float" },
{ 'd', (const char *)"double" },
{ 'B', (const char *)"BOOL" },
{ 'v', (const char *)"void" },
{ '*', (const char *)"char *" },
{ '#', (const char *)"class" },
{ ':', (const char *)"selector" },
{ '?', (const char *)"unknown" },
};
NSString *result = nil;
TypeMap *scan = s_typeMap;
TypeMap *stop = scan + sizeof(s_typeMap) / sizeof(*s_typeMap);
while (scan < stop) {
if (scan->discriminator == **typeScan) {
result = @( scan->name );
(*typeScan)++;
break;
}
scan++;
}
return result;
} // SimpleEncoding
// Process object/id/block types. Some type strings include the class name in "quotes"
// |typeScan| is scooted over to account for any consumed characters.
static NSString *ObjectEncoding (char **typeScan) {
assert (**typeScan == '@');
(*typeScan)++; // eat the '@'
NSString *result = @"id";
if (**typeScan == '\"') {
(*typeScan)++; // eat the double-quote
char *closeQuote = *typeScan;
while (*closeQuote && *closeQuote != '\"')
closeQuote++;
*closeQuote = '\000';
result = [NSString stringWithUTF8String: *typeScan];
*closeQuote = '\"';
*typeScan = closeQuote;
return [NSString stringWithFormat:@"%@ *", result]; // heheh
}
if (**typeScan == '?') {
result = @"(^block)";
(*typeScan)++;
}
return result;
} // ObjectEncoding
// Process pointer types. Recursive since pointers are people too.
// |typeScan| is scooted over to account for any consumed characters
static NSString *PointerEncoding (char **typeScan) {
assert (**typeScan == '^');
(*typeScan)++; // eat the '^'
NSString *result = @"";
if (**typeScan == '^') {
result = PointerEncoding (typeScan);
} else if (**typeScan == '{') {
result = StructEncoding (typeScan);
} else if (**typeScan == '@') {
result = ObjectEncoding (typeScan);
} else {
result = SimpleEncoding (typeScan);
}
result = [result stringByAppendingString: @"*"];
return result;
} // PointerEncoding
// Process structure types. Pull out the name of the first structure encountered
// and not worry about any embedded structures.
// |typeScan| is scooted over to account for any consumed characters
static NSString *StructEncoding (char **typeScan) {
assert (**typeScan == '{');
(*typeScan)++; // eat the '{'
NSString *result = @"";
// find the equal sign after the struct name
char *equalSign = *typeScan;
while (*equalSign && *equalSign != '=') {
equalSign++;
}
*equalSign = '\000';
result = [NSString stringWithUTF8String: *typeScan];
*equalSign = '=';
// Eat the rest of the potentially nested structures.
int openCount = 1;
while (**typeScan && openCount) {
if (**typeScan == '{') openCount++;
if (**typeScan == '}') openCount--;
(*typeScan)++;
}
return result;
} // StructEncoding
// Given an Objective-C type encoding string, return an array of human-readable
// strings that describe each of the types.
NSArray *ParseTypeString (NSString *rawTypeString) {
NSString *typeString = ScrubNumbers (rawTypeString);
char *base = strdup ([typeString UTF8String]);
char *scan = base;
NSMutableArray *chunks = [NSMutableArray array];
while (*scan) {
NSString *stuff = SimpleEncoding (&scan);
if (stuff) {
[chunks addObject: stuff];
continue;
}
if (*scan == '@') {
stuff = ObjectEncoding (&scan);
[chunks addObject: stuff];
continue;
}
if (*scan == '^') {
stuff = PointerEncoding (&scan);
[chunks addObject: stuff];
continue;
}
if (*scan == '{') {
stuff = StructEncoding (&scan);
[chunks addObject: stuff];
continue;
}
// If we hit this, more work needs to be done.
stuff = [NSString stringWithFormat: @"(that was unexpected: %c)", *scan];
scan++;
}
free (base);
return chunks;
} // ParseTypeString