ObjFW  Check-in [411b99fea8]

Overview
Comment:OFLocale: Add a stack machine for plurals
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 411b99fea8a421851aa1019fbf2f8256b3b1da1dfe2b7a647f9c9c01fa6f2ffb
User & Date: js on 2020-04-12 15:39:05
Other Links: manifest | tags
Context
2020-04-12
15:57
OFLocale: Add . operator to check for real numbers check-in: f5dd9b7f73 user: js tags: trunk
15:39
OFLocale: Add a stack machine for plurals check-in: 411b99fea8 user: js tags: trunk
12:55
ofarc: Fix wrong indentation check-in: b186e992ae user: js tags: trunk
Changes

Modified src/OFLocale.m from [af7c460737] to [c6430c7378].

19
20
21
22
23
24
25

26
27
28
29
30
31
32
..
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
...
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
...
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441

#include <locale.h>

#import "OFLocale.h"
#import "OFString.h"
#import "OFArray.h"
#import "OFDictionary.h"


#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFInvalidEncodingException.h"
#import "OFInvalidFormatException.h"
#import "OFOpenItemFailedException.h"

................................................................................
			*language = [OFString stringWithCString: locale
						       encoding: enc];
	} @finally {
		free(locale);
	}
}
#endif

static OFString *
evaluateDictionary(OFDictionary *dictionary, OFDictionary *variables)
{
	OFEnumerator *keyEnumerator = [dictionary keyEnumerator];
	OFEnumerator *objectEnumerator = [dictionary objectEnumerator];

	OFString *key;
	id object;

	while ((key = [keyEnumerator nextObject]) != nil &&
	    (object = [objectEnumerator nextObject]) != nil) {
		OFString *var, *expected;

















		size_t pos;

		if (![key isKindOfClass: [OFString class]] ||









































		    ![object isKindOfClass: [OFString class]])
















































































			@throw [OFInvalidFormatException exception];

		if (key.length == 0)
			continue;


		pos = [key rangeOfString: @"="].location;
		if (pos == OF_NOT_FOUND)










			@throw [OFInvalidFormatException exception];

		var = [key substringWithRange: of_range(0, pos)];
		expected = [key substringWithRange:
		    of_range(pos + 1, key.length - pos - 1)];



		if ([[variables objectForKey: var] isEqual: expected])










			return object;
	}

	return [dictionary objectForKey: @""];


}

static OFString *
evaluateArray(OFArray *array, OFDictionary *variables)
{
	OFMutableString *string = [OFMutableString string];

	for (id object in array) {
		if ([object isKindOfClass: [OFString class]])
			[string appendString: object];
		else if ([object isKindOfClass: [OFDictionary class]])
			[string appendString:
			    evaluateDictionary(object, variables)];
		else
			@throw [OFInvalidFormatException exception];
	}

	[string makeImmutable];

	return string;
................................................................................
	OFConstantString *name;
	const char *UTF8String = NULL;
	size_t last, UTF8StringLength;
	int state = 0;

	variables = [OFMutableDictionary dictionary];
	while ((name = va_arg(arguments, OFConstantString *)) != nil)
		[variables setObject: [va_arg(arguments, id) description]
			      forKey: name];

	for (OFDictionary *strings in _localizedStrings) {
		id string = [strings objectForKey: ID];

		if (string == nil)
			continue;
................................................................................
			if (UTF8String[i] == ']') {
				OFString *var = [OFString
				    stringWithUTF8String: UTF8String + last
						  length: i - last];
				OFString *value = [variables objectForKey: var];

				if (value != nil)
					[ret appendString: value];

				last = i + 1;
				state = 0;
			}
			break;
		}
	}







>







 








|
|

|
<
>
|
|

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

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

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

<
<
<
>
>

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


<
>
>










|

|







 







|







 







|







19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
..
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
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
263
264
265
266
267
268
269
270
271
272

273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
...
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
...
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595

#include <locale.h>

#import "OFLocale.h"
#import "OFString.h"
#import "OFArray.h"
#import "OFDictionary.h"
#import "OFNumber.h"

#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFInvalidEncodingException.h"
#import "OFInvalidFormatException.h"
#import "OFOpenItemFailedException.h"

................................................................................
			*language = [OFString stringWithCString: locale
						       encoding: enc];
	} @finally {
		free(locale);
	}
}
#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;
		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 {
			of_unichar_t firstCharacter =
			    [atom characterAtIndex: 0];

			if (firstCharacter >= '0' && firstCharacter <= '9')
				type = TYPE_LITERAL;
			else
				type = TYPE_VARIABLE;
		}

		switch (type) {
		case TYPE_LITERAL:
			[stack addObject:
			    [OFNumber numberWithIntMax: atom.decimalValue]];
			break;
		case TYPE_VARIABLE:
			if ((var = [variables objectForKey: atom]) == nil)
				@throw [OFInvalidFormatException exception];

			if ([var isKindOfClass: [OFString class]])
				var = [OFNumber numberWithIntMax:
				    [var decimalValue]];

			[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 numberWithIntMax:
				    [first intMaxValue] + [second intMaxValue]];
				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:
			stackSize = stack.count;
			first = [OFNumber numberWithBool:
			    ![stack.lastObject boolValue]];
			[stack replaceObjectAtIndex: stackSize - 1
					 withObject: first];
			break;
		}
	}

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



	return [stack.firstObject boolValue];
}



static OFString *
evaluateConditionals(OFArray *conditions, OFDictionary *variables)
{
	for (OFDictionary *dictionary in conditions) {
		OFString *condition, *value;
		bool found = false;

		for (OFString *key in dictionary) {
			if (found)
				@throw [OFInvalidFormatException exception];




			condition = key;
			value = [dictionary objectForKey: key];


			if (![condition isKindOfClass: [OFString class]] ||
			    ![value isKindOfClass: [OFString class]])
				@throw [OFInvalidFormatException exception];

			found = true;
		}
		if (!found)
			@throw [OFInvalidFormatException exception];

		if (evaluateCondition(condition, variables))
			return value;
	}


	/* Need to have a fallback as the last one. */
	@throw [OFInvalidFormatException exception];
}

static OFString *
evaluateArray(OFArray *array, OFDictionary *variables)
{
	OFMutableString *string = [OFMutableString string];

	for (id object in array) {
		if ([object isKindOfClass: [OFString class]])
			[string appendString: object];
		else if ([object isKindOfClass: [OFArray class]])
			[string appendString:
			    evaluateConditionals(object, variables)];
		else
			@throw [OFInvalidFormatException exception];
	}

	[string makeImmutable];

	return string;
................................................................................
	OFConstantString *name;
	const char *UTF8String = NULL;
	size_t last, UTF8StringLength;
	int state = 0;

	variables = [OFMutableDictionary dictionary];
	while ((name = va_arg(arguments, OFConstantString *)) != nil)
		[variables setObject: va_arg(arguments, id)
			      forKey: name];

	for (OFDictionary *strings in _localizedStrings) {
		id string = [strings objectForKey: ID];

		if (string == nil)
			continue;
................................................................................
			if (UTF8String[i] == ']') {
				OFString *var = [OFString
				    stringWithUTF8String: UTF8String + last
						  length: i - last];
				OFString *value = [variables objectForKey: var];

				if (value != nil)
					[ret appendString: value.description];

				last = i + 1;
				state = 0;
			}
			break;
		}
	}