ObjFW  Check-in [fe77fef5ec]

Overview
Comment:OFLocale: Switch to infix notation
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: fe77fef5ecda5c6761d4a82a06211335c90a36d2e205dcfa785797cf4fe2c365
User & Date: js on 2020-04-12 22:09:42
Other Links: manifest | tags
Context
2020-04-12
22:28
utils: Properly translate plurals check-in: 9ee403e4fc user: js tags: trunk
22:09
OFLocale: Switch to infix notation check-in: fe77fef5ec user: js tags: trunk
15:57
OFLocale: Add . operator to check for real numbers check-in: f5dd9b7f73 user: js tags: trunk
Changes

Modified src/OFLocale.m from [279230af16] to [12259d9f0f].

34
35
36
37
38
39
40

41
42
43
44
45
46
47
..
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
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
228
229
230
231
232
233
234
235

236
237
238
239
240
241
242
243
244

245
246
247
248

249
250
251
252
253
254
255
256
257
258
259
260
261
262
...
311
312
313
314
315
316
317





























318
319
320
321
322
323
324
#ifdef OF_AMIGAOS
# include <proto/dos.h>
# include <proto/exec.h>
# include <proto/locale.h>
#endif

static OFLocale *currentLocale = nil;


#ifndef OF_AMIGAOS
static void
parseLocale(char *locale, of_string_encoding_t *encoding,
    OFString **language, OFString **territory)
{
	if ((locale = of_strdup(locale)) == NULL)
................................................................................
	}
}
#endif

static bool
evaluateCondition(OFString *condition, OFDictionary *variables)
{
	OFMutableArray *stack;


	if (condition.length == 0)
		return true;














	stack = [OFMutableArray array];

	for (OFString *atom in [condition
	    componentsSeparatedByString: @" "
				options: OF_STRING_SKIP_EMPTY]) {
		enum {
			TYPE_LITERAL,
			TYPE_VARIABLE,
			TYPE_EQUAL,
			TYPE_NOT_EQUAL,
			TYPE_LESS,
			TYPE_LESS_EQUAL,
			TYPE_GREATER,
			TYPE_GREATER_EQUAL,
			TYPE_ADD,
			TYPE_MODULO,
			TYPE_AND,
			TYPE_OR,
			TYPE_NOT,
			TYPE_IS_REAL
		} type;


		id var, first, second;
		size_t stackSize;


		if ([atom isEqual: @"=="]) {
			type = TYPE_EQUAL;

		} else if ([atom isEqual: @"!="]) {
			type = TYPE_NOT_EQUAL;
		} else if ([atom isEqual: @"<"]) {
			type = TYPE_LESS;
		} else if ([atom isEqual: @"<="]) {
			type = TYPE_LESS_EQUAL;
		} else if ([atom isEqual: @">"]) {

			type = TYPE_GREATER;
		} else if ([atom isEqual: @">="]) {
			type = TYPE_GREATER_EQUAL;
		} else if ([atom isEqual: @"+"]) {
			type = TYPE_ADD;


		} else if ([atom isEqual: @"%"]) {
			type = TYPE_MODULO;
		} else if ([atom isEqual: @"&&"]) {

			type = TYPE_AND;
		} else if ([atom isEqual: @"||"]) {
			type = TYPE_OR;


		} else if ([atom isEqual: @"!"]) {
			type = TYPE_NOT;
		} else if ([atom isEqual: @"."]) {
			type = TYPE_IS_REAL;
		} else {
			of_unichar_t firstCharacter =
			    [atom characterAtIndex: 0];


			if ((firstCharacter >= '0' && firstCharacter <= '9') ||
			    firstCharacter == '-')

				type = TYPE_LITERAL;
			else
				type = TYPE_VARIABLE;

		}



		switch (type) {
		case TYPE_LITERAL:

			[stack addObject:
			    [OFNumber numberWithDouble: atom.doubleValue]];
			break;
		case TYPE_VARIABLE:





			if ((var = [variables objectForKey: atom]) == nil)
				@throw [OFInvalidFormatException exception];

			if ([var isKindOfClass: [OFString class]])

				var = [OFNumber numberWithDouble:
				    [var doubleValue]];







			[stack addObject: var];
			break;
		case TYPE_EQUAL:
		case TYPE_NOT_EQUAL:
		case TYPE_LESS:
		case TYPE_LESS_EQUAL:
		case TYPE_GREATER:
		case TYPE_GREATER_EQUAL:
		case TYPE_ADD:
		case TYPE_MODULO:
		case TYPE_AND:
		case TYPE_OR:












			stackSize = stack.count;
			first = [stack objectAtIndex: stackSize - 2];
			second = [stack objectAtIndex: stackSize - 1];

			switch (type) {
			case TYPE_EQUAL:

				var = [OFNumber numberWithBool:
				    [first isEqual: second]];
				break;
			case TYPE_NOT_EQUAL:

				var = [OFNumber numberWithBool:
				    ![first isEqual: second]];
				break;
			case TYPE_LESS:

				var = [OFNumber numberWithBool: [first
				    compare: second] == OF_ORDERED_ASCENDING];
				break;
			case TYPE_LESS_EQUAL:

				var = [OFNumber numberWithBool: [first
				    compare: second] != OF_ORDERED_DESCENDING];
				break;
			case TYPE_GREATER:

				var = [OFNumber numberWithBool: [first
				    compare: second] == OF_ORDERED_DESCENDING];
				break;
			case TYPE_GREATER_EQUAL:

				var = [OFNumber numberWithBool: [first
				    compare: second] != OF_ORDERED_ASCENDING];
				break;
			case TYPE_ADD:

				var = [OFNumber numberWithDouble:
				    [first doubleValue] + [second doubleValue]];
				break;
			case TYPE_MODULO:

				var = [OFNumber numberWithIntMax:
				    [first intMaxValue] % [second intMaxValue]];
				break;
			case TYPE_AND:

				var = [OFNumber numberWithBool:
				    [first boolValue] && [second boolValue]];
				break;
			case TYPE_OR:

				var = [OFNumber numberWithBool:
				    [first boolValue] || [second boolValue]];
				break;
			default:
				OF_ENSURE(0);
			}

			[stack replaceObjectAtIndex: stackSize - 2
					 withObject: var];
			[stack removeLastObject];

			break;
		case TYPE_NOT:
		case TYPE_IS_REAL:

			stackSize = stack.count;
			first = stack.lastObject;

			switch (type) {
			case TYPE_NOT:
				var = [OFNumber numberWithBool:
				    ![first boolValue]];
				break;
			case TYPE_IS_REAL:

				var = [OFNumber numberWithBool:
				    [first doubleValue] != [first intMaxValue]];
				break;
			default:

				OF_ENSURE(0);
			}

			[stack replaceObjectAtIndex: stackSize - 1
					 withObject: var];
			break;
		}
	}

	if (stack.count != 1)
		@throw [OFInvalidFormatException exception];

	return [stack.firstObject boolValue];
}
................................................................................

	return string;
}

@implementation OFLocale
@synthesize language = _language, territory = _territory, encoding = _encoding;
@synthesize decimalPoint = _decimalPoint;






























+ (OFLocale *)currentLocale
{
	return currentLocale;
}

+ (OFString *)language







>







 







|

>



>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
|


<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
>
>
|
|
>
|
<
<
>
|
|
|
|
|
|
<
>
|
|
|
<
<
>
>
|
|
<
>
|
<
<
>
>
|
|
|
|
|
|
<

>
|
<
>
|
|
|
>
|
>
>
|
<
<
>
|
<
<
|
>
>
>
>
>
|


<
>
|
<
|
>
>
>
>
>
>
|
<
<
<
<
<
<
<
<
<
<
<
>
>
>
>
>
>
>
>
>
>
>
>




<
<
>


<
<
>


<
<
>


<
<
>


<
<
>


<
<
>


<
<
>


<
<
>


<
<
>


<
<
>


<
|

<




<
<
<
<
>



|
<


<
<
>


<
<
>

<



|
|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
..
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
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
228

229
230
231
232




233
234
235
236
237

238
239


240
241
242


243
244

245
246
247
248
249
250
251
252
253
254
255
256
...
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#ifdef OF_AMIGAOS
# include <proto/dos.h>
# include <proto/exec.h>
# include <proto/locale.h>
#endif

static OFLocale *currentLocale = nil;
static OFDictionary *operatorPrecedences = nil;

#ifndef OF_AMIGAOS
static void
parseLocale(char *locale, of_string_encoding_t *encoding,
    OFString **language, OFString **territory)
{
	if ((locale = of_strdup(locale)) == NULL)
................................................................................
	}
}
#endif

static bool
evaluateCondition(OFString *condition, OFDictionary *variables)
{
	OFMutableArray *tokens, *operators, *stack;

	/* Empty condition is the fallback that's always true */
	if (condition.length == 0)
		return true;

	/*
	 * Dirty hack to allow not needing spaces after "!" or "(" and spaces
	 * before ")".
	 * TODO: Replace with a proper tokenizer.
	 */
	condition = [condition stringByReplacingOccurrencesOfString: @"!"
							 withString: @"! "];
	condition = [condition stringByReplacingOccurrencesOfString: @"("
							 withString: @"( "];
	condition = [condition stringByReplacingOccurrencesOfString: @")"
							 withString: @" )"];

	/* Substitute variables and convert to RPN first */
	tokens = [OFMutableArray array];
	operators = [OFMutableArray array];
	for (OFString *token in [condition
	    componentsSeparatedByString: @" "
				options: OF_STRING_SKIP_EMPTY]) {
















		unsigned precedence;

		if ([token isEqual: @"("]) {
			[operators addObject: @"("];
			continue;
		}



		if ([token isEqual: @")"]) {
			for (;;) {
				OFString *operator = operators.lastObject;
				if (operator == nil)
					@throw [OFInvalidFormatException
					    exception];


				if ([operator isEqual: @"("]) {
					[operators removeLastObject];
					break;


				}

				[tokens addObject: operator];
				[operators removeLastObject];

			}
			continue;


		}

		precedence = [[operatorPrecedences objectForKey: token]
		    unsignedIntValue];
		if (precedence > 0) {
			for (;;) {
				OFNumber *operator = operators.lastObject;
				unsigned otherPrecedence;


				if (operator == nil || [operator isEqual: @"("])
					break;


				otherPrecedence = [[operatorPrecedences
				    objectForKey: operator] unsignedIntValue];
				if (otherPrecedence >= precedence)
					break;

				[tokens addObject: operator];
				[operators removeLastObject];
			}



			[operators addObject: token];


			continue;
		}

		of_unichar_t c = [token characterAtIndex: 0];

		if ((c < '0' || c > '9') && c != '-')
			if ((token = [variables objectForKey: token]) == nil)
				@throw [OFInvalidFormatException exception];


		[tokens addObject:
		    [OFNumber numberWithDouble: token.doubleValue]];

	}
	for (size_t i = operators.count; i > 0; i--) {
		OFString *operator = [operators objectAtIndex: i - 1];

		if ([operator isEqual: @"("])
			@throw [OFInvalidFormatException exception];

		[tokens addObject: operator];











	}

	/* Evaluate RPN */
	stack = [OFMutableArray array];
	for (id token in tokens) {
		unsigned precedence = [[operatorPrecedences
		    objectForKey: token] unsignedIntValue];
		id var, first, second;
		size_t stackSize;

		/* Only unary operators have precedence 1 */
		if (precedence > 1) {
			stackSize = stack.count;
			first = [stack objectAtIndex: stackSize - 2];
			second = [stack objectAtIndex: stackSize - 1];



			if ([token isEqual: @"=="])
				var = [OFNumber numberWithBool:
				    [first isEqual: second]];


			else if ([token isEqual: @"!="])
				var = [OFNumber numberWithBool:
				    ![first isEqual: second]];


			else if ([token isEqual: @"<"])
				var = [OFNumber numberWithBool: [first
				    compare: second] == OF_ORDERED_ASCENDING];


			else if ([token isEqual: @"<="])
				var = [OFNumber numberWithBool: [first
				    compare: second] != OF_ORDERED_DESCENDING];


			else if ([token isEqual: @">"])
				var = [OFNumber numberWithBool: [first
				    compare: second] == OF_ORDERED_DESCENDING];


			else if ([token isEqual: @">="])
				var = [OFNumber numberWithBool: [first
				    compare: second] != OF_ORDERED_ASCENDING];


			else if ([token isEqual: @"+"])
				var = [OFNumber numberWithDouble:
				    [first doubleValue] + [second doubleValue]];


			else if ([token isEqual: @"%"])
				var = [OFNumber numberWithIntMax:
				    [first intMaxValue] % [second intMaxValue]];


			else if ([token isEqual: @"&&"])
				var = [OFNumber numberWithBool:
				    [first boolValue] && [second boolValue]];


			else if ([token isEqual: @"||"])
				var = [OFNumber numberWithBool:
				    [first boolValue] || [second boolValue]];

			else
				OF_ENSURE(0);


			[stack replaceObjectAtIndex: stackSize - 2
					 withObject: var];
			[stack removeLastObject];




		} else if (precedence == 1) {
			stackSize = stack.count;
			first = stack.lastObject;

			if ([token isEqual: @"!"])

				var = [OFNumber numberWithBool:
				    ![first boolValue]];


			else if ([token isEqual: @"is_real"])
				var = [OFNumber numberWithBool:
				    [first doubleValue] != [first intMaxValue]];


			else
				OF_ENSURE(0);


			[stack replaceObjectAtIndex: stackSize - 1
					 withObject: var];
		} else
			[stack addObject: token];
	}

	if (stack.count != 1)
		@throw [OFInvalidFormatException exception];

	return [stack.firstObject boolValue];
}
................................................................................

	return string;
}

@implementation OFLocale
@synthesize language = _language, territory = _territory, encoding = _encoding;
@synthesize decimalPoint = _decimalPoint;

+ (void)initialize
{
	OFNumber *one, *two, *three, *four;

	if (self != [OFLocale class])
		return;

	/* 1 is also used to denote a unary operator. */
	one = [OFNumber numberWithUnsignedInt: 1];
	two = [OFNumber numberWithUnsignedInt: 2];
	three = [OFNumber numberWithUnsignedInt: 3];
	four = [OFNumber numberWithUnsignedInt: 4];

	operatorPrecedences = [[OFDictionary alloc] initWithKeysAndObjects:
	    @"==", two,
	    @"!=", two,
	    @"<", two,
	    @"<=", two,
	    @">", two,
	    @">=", two,
	    @"+", two,
	    @"%", two,
	    @"&&", three,
	    @"||", four,
	    @"!", one,
	    @"is_real", one,
	    nil];
}

+ (OFLocale *)currentLocale
{
	return currentLocale;
}

+ (OFString *)language