ObjFW  Check-in [039f60a95d]

Overview
Comment:Change the Unicode table generator so it generates way smaller tables.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 039f60a95d333a823c69242b311b16f1e725d7ffb12625bfd342e76b0bc84f2c
User & Date: js on 2009-10-18 14:20:28
Other Links: manifest | tags
Context
2009-10-18
14:22
Use the new, smaller Unicode tables. check-in: 257fc27253 user: js tags: trunk
14:20
Change the Unicode table generator so it generates way smaller tables. check-in: 039f60a95d user: js tags: trunk
13:01
Remove titlecase table. check-in: 9553ab1d8f user: js tags: trunk
Changes

Modified generators/gen_tables.m from [bfcb1fc885] to [ddead50c5d].

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
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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+








-
-
-
-
-
+
+
+
+
+
+
+
+







#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#import "OFString.h"
#import "OFFile.h"
#import "OFAutoreleasePool.h"

#define COPYRIGHT \
    @"/*\n" \
    @" * Copyright (c) 2008 - 2009\n" \
    @" *   Jonathan Schleifer <js@webkeks.org>\n" \
    @" *\n" \
    @" * All rights reserved.\n" \
    @" *\n" \
    @" * This file is part of libobjfw. It may be distributed under the " \
    @"terms of the\n" \
    @" * Q Public License 1.0, which can be found in the file LICENSE " \
    @"included in\n" \
    @" * the packaging of this file.\n" \
    @" */\n" \
    @"\n"

@interface TableGenerator: OFObject
{
	of_unichar_t upper[0x110000];
	of_unichar_t lower[0x110000];
}

- (void)fillTablesFromFile: (OFString*)file;
- (void)writeTable: (of_unichar_t*)table
	  withName: (OFString*)name
	    toFile: (OFString*)file;
- (void)writeUpperTableToFile: (OFString*)file;
- (void)writeLowerTableToFile: (OFString*)file;
- (size_t)writeTable: (of_unichar_t*)table
	    withName: (OFString*)name
	      toFile: (OFString*)file;
- (size_t)writeUpperTableToFile: (OFString*)file;
- (size_t)writeLowerTableToFile: (OFString*)file;
- (void)writeHeaderToFile: (OFString*)file
       withUpperTableSize: (size_t)upper_size
	   lowerTableSize: (size_t)lower_size;
@end

@implementation TableGenerator
- (void)fillTablesFromFile: (OFString*)file;
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init], *pool2;
	OFFile *src = [OFFile fileWithPath: file
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
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







-
-
-
+
+
+







-
+




-
+
-
-
-
-
-
-
-
-
-
-
-
-







+
+





+
-
+








		[pool2 releaseObjects];
	}

	[pool release];
}

- (void)writeTable: (of_unichar_t*)table
	  withName: (OFString*)name
	    toFile: (OFString*)file
- (size_t)writeTable: (of_unichar_t*)table
	    withName: (OFString*)name
	      toFile: (OFString*)file
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFAutoreleasePool *pool2;
	OFFile *f = [OFFile fileWithPath: file
				    mode: @"wb"];

	of_unichar_t i, j;
	BOOL empty;
	size_t last_used = SIZE_MAX;
	BOOL table_used[0x1100];

	memset(table_used, NO, 0x1100);

	[f writeString: @"/*\n"
	[f writeString: COPYRIGHT
	    @" * Copyright (c) 2008 - 2009\n"
	    @" *   Jonathan Schleifer <js@webkeks.org>\n"
	    @" *\n"
	    @" * All rights reserved.\n"
	    @" *\n"
	    @" * This file is part of libobjfw. It may be distributed under "
	    @"the terms of the\n"
	    @" * Q Public License 1.0, which can be found in the file LICENSE "
	    @"included in\n"
	    @" * the packaging of this file.\n"
	    @" */\n"
	    @"\n"
	    @"#include \"config.h\"\n"
	    @"\n"
	    @"#import \"OFString.h\"\n\n"];

	[f writeString: @"static const of_unichar_t nop_page[0x100] = {};\n\n"];

	for (i = 0; i < 0x110000; i += 0x100) {
		BOOL empty;

		empty = YES;

		for (j = i; j < i + 0x100; j++) {
			if (table[j] != 0) {
				empty = NO;
				last_used = i >> 8;
				table_used[i >> 8] = YES;
				table_used[last_used] = YES;
			}
		}

		if (!empty) {
		       	pool2 = [[OFAutoreleasePool alloc] init];

			[f writeString: [OFString stringWithFormat:
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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217


218
219
220
221
222
223
224
225
226
227








+
+

-
-
+
+


-
+

-
+


-
+

-
-
-
-
+
+
+
+







+


-
+






-
+





+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







+


-
-
+
+
+
+
+





			}

			[f writeString: @"};\n\n"];

			[pool2 release];
		}
	}

	last_used++;

	[f writeString: [OFString stringWithFormat:
	    @"const of_unichar_t* const of_unicode_%s_table[0x1100] = {\n\t",
	    [name cString]]];
	    @"const of_unichar_t* const of_unicode_%s_table[0x%X] = {\n\t",
	    [name cString], last_used]];

	pool2 = [[OFAutoreleasePool alloc] init];
	for (i = 0; i < 0x1100; i++) {
	for (i = 0; i < last_used; i++) {
		if (table_used[i])
			[f writeString: [OFString stringWithFormat: @"page_%d,",
			[f writeString: [OFString stringWithFormat: @"page_%d",
								    i]];
		else
			[f writeString: @"nop_page,"];
			[f writeString: @"nop_page"];

		if ((i + 1) % 4)
			[f writeString: @" "];
		else if (i < 0x1100 - 4)
			[f writeString: @"\n\t"];
		if ((i + 1) % 4 == 0)
			[f writeString: @",\n\t"];
		else if (i + 1 < last_used)
			[f writeString: @", "];

		[pool2 releaseObjects];
	}

	[f writeString: @"\n};\n"];

	[pool release];
	return last_used;
}

- (void)writeUpperTableToFile: (OFString*)file
- (size_t)writeUpperTableToFile: (OFString*)file
{
	return [self writeTable: upper
		       withName: @"upper"
			 toFile: file];
}

- (void)writeLowerTableToFile: (OFString*)file
- (size_t)writeLowerTableToFile: (OFString*)file
{
	return [self writeTable: lower
		       withName: @"lower"
			 toFile: file];
}

- (void)writeHeaderToFile: (OFString*)file
       withUpperTableSize: (size_t)upper_size
	   lowerTableSize: (size_t)lower_size
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFFile *f = [OFFile fileWithPath: file
				    mode: @"wb"];

	[f writeString: COPYRIGHT
	    @"#import \"OFString.h\"\n\n"];

	[f writeString: [OFString stringWithFormat:
	    @"#define OF_UNICODE_UPPER_TABLE_SIZE 0x%X\n"
	    @"#define OF_UNICODE_LOWER_TABLE_SIZE 0x%X\n\n",
	    upper_size, lower_size]];

	[f writeString:
	    @"extern const of_unichar_t* const\n"
	    @"    of_unicode_upper_table[OF_UNICODE_UPPER_TABLE_SIZE];\n"
	    @"extern const of_unichar_t* const\n"
	    @"    of_unicode_lower_table[OF_UNICODE_LOWER_TABLE_SIZE];"];

	[pool release];
}
@end

int
main()
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	TableGenerator *tgen = [[[TableGenerator alloc] init] autorelease];
	size_t upper_size, lower_size;

	[tgen fillTablesFromFile: @"UnicodeData.txt"];
	[tgen writeUpperTableToFile: @"../src/unicode_upper.m"];
	[tgen writeLowerTableToFile: @"../src/unicode_lower.m"];
	upper_size = [tgen writeUpperTableToFile: @"../src/unicode_upper.m"];
	lower_size = [tgen writeLowerTableToFile: @"../src/unicode_lower.m"];
	[tgen writeHeaderToFile: @"../src/unicode.h"
	     withUpperTableSize: upper_size
		 lowerTableSize: lower_size];

	[pool release];

	return 0;
}