Debian Codegraph @

src/combine.c

raw symbols SCIP
1 /* Conversion of files between different charsets and surfaces.
2 Copyright © 1990-2018 Free Software Foundation, Inc.
3 Contributed by François Pinard <[email protected]>, 1990.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public License
7 as published by the Free Software Foundation; either version 3 of the
8 License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be
11 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the Recode Library; see the file `COPYING.LIB'.
17 If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 #include "config.h"
21 #include "common.h"
22 #include "hash.h"
23
24 /* FIXME: Softly report errors. */
25
26 /* The satisfactory aspects are that Recode is now able to combine a set of
27 sequence of UCS-2 characters into single codes, or explode those single
28 codes into the original sequence. It may happen that many sequences reduce
29 to the same code, one of them is arbitrarily taken as canonical. Any
30 combining sequence of an equivalent set produces the equivalent code, but
31 at explode time, codes are always turned into their canonical sequence. I
32 did not limit the length of a combining sequence, yet it is usually small.
33 Also, I did not put any limit on the number of possibly equivalent
34 sequences. For combining, for each possible partial match in any sequence,
35 there is a state. States corresponding to the scan of a single code are
36 found by hash coding, while other states are linearly searched on a linked
37 list of possible shifts, rooted on the state corresponding to the partial
38 sequence, but without its last code. I expect this combination of methods
39 to execute reasonably fast. Exploding is much simpler, and I merely use
40 hash coding.
41
42 The less satisfactory aspects are that the user interface is still very
43 crude. For the time being, I merely added a "combined" charset: combining
44 is done by `u2..co', exploding is done by `co..u2'. We surely could do
45 much better, and build on the concept of options (introduced by `+' in the
46 request syntax), Options are probably going to be used for other
47 transliteration matters. Even if built on UCS-2, combining matters would
48 have natural extensions in other _smaller_ charsets, and it would be nice
49 being able to derive the functionality automatically. One difficulty to
50 foresee is that, contrarily to surfaces, options like transliteration or
51 combining _require_ specific charsets (like UCS-2) as a vehicle, and if
52 UCS-2 is not otherwise mandated in the request, we would have to create
53 such an UCS-2 intermediate. On the other hand, if UCS-2 is sandwiched
54 somewhere in a complex series of steps, we should implicitly propagate
55 options towards the UCS-2 step. Even if nothing looks utterly difficult, I
56 guess that for doing it right, a lot of attention and care is needed.
57 Also, I'm pretty sure that scrutinizing the documents (from w3.org or
58 Unicode) that were suggested to us will raise new specific concerns. */
59
60 /* Exploding. */
61
62 /*---------------------------.
63 | Initialize for exploding. |
64 `---------------------------*/
65
66 static size_t
67 combined_hash (const void *void_data, size_t table_size)
68 {
69 const unsigned short *data = (const unsigned short *) void_data;
70
71 return *data % table_size;
72 }
73
74 static bool
75 combined_compare (const void *void_first, const void *void_second)
76 {
77 const unsigned short *first = (const unsigned short *) void_first;
78 const unsigned short *second = (const unsigned short *) void_second;
79
80 return *first == *second;
81 }
82
83 bool
84 recode_init_explode (RECODE_STEP step,
85 RECODE_CONST_REQUEST request _GL_UNUSED,
86 RECODE_CONST_OPTION_LIST before_options,
87 RECODE_CONST_OPTION_LIST after_options)
88 {
89 const unsigned short *data = (const unsigned short *) step->step_table;
90 Hash_table *table;
91
92 if (before_options || after_options)
93 return false;
94
95 table = hash_initialize (0, NULL, combined_hash, combined_compare, NULL);
96
97 if (!table)
98 return false;
99 step->step_type = RECODE_EXPLODE_STEP;
100 step->step_table = table;
101 step->step_table_term_routine = (void (*) (void *)) hash_free;
102
103 if (!data)
104 return true;
105
106 while (*data != DONE)
107 {
108 if (!hash_insert (table, data))
109 return false;
110
111 while (*data != DONE)
112 data++;
113 data++;
114 }
115
116 return true;
117 }
118
119 /*------------------------------------.
120 | Execute exploding transformations. |
121 `------------------------------------*/
122
123 bool
124 recode_explode_byte_byte (RECODE_SUBTASK subtask)
125 {
126 Hash_table *table = (Hash_table *) subtask->step->step_table;
127 unsigned value;
128
129 while (value = recode_get_byte (subtask), value != (unsigned)EOF)
130 {
131 unsigned short lookup = value;
132 unsigned short *result = (unsigned short *) hash_lookup (table, &lookup);
133
134 if (result)
135 {
136 result++;
137 while (*result != DONE && *result != ELSE)
138 {
139 recode_put_byte (*result, subtask);
140 result++;
141 }
142 }
143 else
144 recode_put_byte (value, subtask);
145 }
146
147 SUBTASK_RETURN (subtask);
148 }
149
150 bool
151 recode_explode_ucs2_byte (RECODE_SUBTASK subtask)
152 {
153 Hash_table *table = (Hash_table *) subtask->step->step_table;
154 unsigned value;
155
156 while (recode_get_ucs2 (&value, subtask))
157 {
158 unsigned short lookup = value;
159 unsigned short *result = (unsigned short *) hash_lookup (table, &lookup);
160
161 if (result)
162 {
163 result++;
164 while (*result != DONE && *result != ELSE)
165 {
166 recode_put_byte (*result, subtask);
167 result++;
168 }
169 }
170 else
171 recode_put_byte (value, subtask);
172 }
173
174 SUBTASK_RETURN (subtask);
175 }
176
177 bool
178 recode_explode_byte_ucs2 (RECODE_SUBTASK subtask)
179 {
180 Hash_table *table = (Hash_table *) subtask->step->step_table;
181 unsigned value;
182
183 if (value = recode_get_byte (subtask), value != (unsigned)EOF)
184 {
185 if (subtask->task->byte_order_mark)
186 recode_put_ucs2 (BYTE_ORDER_MARK, subtask);
187
188 while (true)
189 {
190 unsigned short lookup = value;
191 unsigned short *result
192 = (unsigned short *) hash_lookup (table, &lookup);
193
194 if (result)
195 {
196 result++;
197 while (*result != DONE && *result != ELSE)
198 recode_put_ucs2 (*result++, subtask);
199 }
200 else
201 recode_put_ucs2 (value, subtask);
202
203 if (value = recode_get_byte (subtask), value == (unsigned)EOF)
204 break;
205 }
206 }
207
208 SUBTASK_RETURN (subtask);
209 }
210
211 bool
212 recode_explode_ucs2_ucs2 (RECODE_SUBTASK subtask)
213 {
214 Hash_table *table = (Hash_table *) subtask->step->step_table;
215 unsigned value;
216
217 if (recode_get_ucs2 (&value, subtask))
218 {
219 if (subtask->task->byte_order_mark)
220 recode_put_ucs2 (BYTE_ORDER_MARK, subtask);
221
222 while (true)
223 {
224 unsigned short lookup = value;
225 unsigned short *result
226 = (unsigned short *) hash_lookup (table, &lookup);
227
228 if (result)
229 {
230 result++;
231 while (*result != DONE && *result != ELSE)
232 recode_put_ucs2 (*result++, subtask);
233 }
234 else
235 recode_put_ucs2 (value, subtask);
236
237 if (!recode_get_ucs2 (&value, subtask))
238 break;
239 }
240 }
241
242 SUBTASK_RETURN (subtask);
243 }
244
245 /* Combining. */
246
247 /* A combining state represents the history of reading one or more characters
248 while forming a combining sequence.
249
250 Hash coding is used to find an initial state from the first character in a
251 sequence. If that state is not found, the character surely does not start
252 any sequence. For subsequent characters, possible shifted states from the
253 current state are kept on that state, as a linked chain. This combination
254 of methods is expected to behave fast enough in practice. */
255
256 struct state
257 {
258 unsigned short character; /* last character seen to trigger this state */
259 unsigned short result; /* character equivalent to the combination */
260 struct state *shift; /* list of states for one more character */
261 struct state *unshift; /* state for one less character (back link) */
262 struct state *next; /* next state in a linked chain of states */
263 };
264
265 /*---------------------------.
266 | Initialize for combining. |
267 `---------------------------*/
268
269 static size_t
270 state_hash (const void *void_data, size_t table_size)
271 {
272 const struct state *data = (const struct state *) void_data;
273
274 return data->character % table_size;
275 }
276
277 static bool
278 state_compare (const void *void_first, const void *void_second)
279 {
280 const struct state *first = (const struct state *) void_first;
281 const struct state *second = (const struct state *) void_second;
282
283 return first->character == second->character;
284 }
285
286 static void
287 state_free (void *void_state)
288 {
289 struct state *state = (struct state *) void_state;
290 struct state *shift = state->shift;
291
292 while (shift != NULL)
293 {
294 struct state *next_shift = shift->next;
295 state_free (shift);
296 shift = next_shift;
297 }
298 free (state);
299 }
300
301 static struct state *
302 prepare_shifted_state (struct state *state, unsigned character,
303 RECODE_CONST_STEP step)
304 {
305 if (state)
306 {
307 struct state *shift = state->shift;
308
309 while (shift)
310 if (shift->character == character)
311 return shift;
312 else
313 shift = shift->next;
314
315 if (shift = (struct state *) malloc (sizeof (struct state)), !shift)
316 return NULL;
317
318 shift->character = character;
319 shift->result = NOT_A_CHARACTER;
320 shift->shift = NULL;
321 shift->unshift = state;
322 shift->next = state->shift;
323 state->shift = shift;
324 return shift;
325 }
326 else
327 {
328 Hash_table *table = (Hash_table *) step->step_table;
329 struct state lookup;
330
331 lookup.character = character;
332 state = (struct state *) hash_lookup (table, &lookup);
333 if (!state)
334 {
335 if (state = (struct state *) malloc (sizeof (struct state)), !state)
336 return NULL;
337
338 state->character = character;
339 state->result = character;
340 state->shift = NULL;
341 state->unshift = NULL;
342 state->next = NULL;
343
344 if (!hash_insert (table, state))
345 return NULL;
346 }
347 return state;
348 }
349 }
350
351 static struct state *
352 find_shifted_state (struct state *state, unsigned character,
353 RECODE_CONST_STEP step)
354 {
355 if (state)
356 {
357 struct state *shift = state->shift;
358
359 while (shift)
360 if (shift->character == character)
361 return shift;
362 else
363 shift = shift->next;
364
365 return NULL;
366 }
367 else
368 {
369 Hash_table *table = (Hash_table *) step->step_table;
370 struct state lookup;
371
372 lookup.character = character;
373 return (struct state *) hash_lookup (table, &lookup);
374 }
375 }
376
377 bool
378 recode_init_combine (RECODE_STEP step,
379 RECODE_CONST_REQUEST request _GL_UNUSED,
380 RECODE_CONST_OPTION_LIST before_options,
381 RECODE_CONST_OPTION_LIST after_options)
382 {
383 const unsigned short *data = (const unsigned short *) step->step_table;
384 Hash_table *table;
385
386 if (before_options || after_options)
387 return false;
388
389 table = hash_initialize (0, NULL, state_hash, state_compare, state_free);
390
391 if (!table)
392 return false;
393 step->step_type = RECODE_COMBINE_STEP;
394 step->step_table = table;
395 step->step_table_term_routine = (void (*) (void *)) hash_free;
396
397 if (!data)
398 return true;
399
400 while (*data != DONE)
401 {
402 unsigned short result = *data++;
403 struct state *state = NULL;
404
405 while (*data != DONE)
406 if (*data == ELSE)
407 {
408 if (state)
409 {
410 if (state->result != NOT_A_CHARACTER)
411 abort ();
412 state->result = result;
413 state = NULL;
414 }
415 data++;
416 }
417 else
418 {
419 state = prepare_shifted_state (state, *data++, step);
420 if (!state)
421 return false;
422 }
423
424 if (state)
425 {
426 if (state->result != NOT_A_CHARACTER
427 && state->result != state->character)
428 abort ();
429 state->result = result;
430 }
431 data++;
432 }
433
434 return true;
435 }
436
437 /*------------------.
438 | Handle bactrack. |
439 `------------------*/
440
441 /* When we cannot shift into a state allowing a terminal resulting character,
442 we ought to backtrack until such a terminal character if found, then output
443 this resulting character for representing the partial sequence which ends
444 with that state. Then, we merely copy characters seen after that state.
445
446 This approach does not properly scan for combinings which might exist in
447 the copied characters, presuming that this case does not occur in practice.
448 If we later find that it does, backtracing will have to be revisited. */
449
450 static void
451 backtrack_byte (struct state *state, RECODE_SUBTASK subtask)
452 {
453 if (state->result == NOT_A_CHARACTER)
454 {
455 backtrack_byte (state->unshift, subtask);
456 recode_put_byte (state->character, subtask);
457 }
458 else
459 recode_put_byte (state->result, subtask);
460 }
461
462 static void
463 backtrack_ucs2 (struct state *state, RECODE_SUBTASK subtask)
464 {
465 if (state->result == NOT_A_CHARACTER)
466 {
467 backtrack_ucs2 (state->unshift, subtask);
468 recode_put_ucs2 (state->character, subtask);
469 }
470 else
471 recode_put_ucs2 (state->result, subtask);
472 }
473
474 /*------------------------------------.
475 | Execute combining transformations. |
476 `------------------------------------*/
477
478 bool
479 recode_combine_byte_byte (RECODE_SUBTASK subtask)
480 {
481 struct state *state = NULL;
482 unsigned value;
483
484 if (value = recode_get_byte (subtask), value != (unsigned)EOF)
485 {
486 while (true)
487 {
488 struct state *shift
489 = find_shifted_state (state, value, subtask->step);
490
491 if (shift)
492 {
493 state = shift;
494 if (value = recode_get_byte (subtask), value == (unsigned)EOF)
495 break;
496 }
497 else if (state)
498 {
499 if (state->result == NOT_A_CHARACTER)
500 backtrack_byte (state, subtask);
501 else
502 recode_put_byte (state->result, subtask);
503 state = NULL;
504 }
505 else
506 {
507 recode_put_byte (value, subtask);
508 if (value = recode_get_byte (subtask), value == (unsigned)EOF)
509 break;
510 }
511 }
512
513 if (state)
514 {
515 if (state->result == NOT_A_CHARACTER)
516 backtrack_byte (state, subtask);
517 else
518 recode_put_byte (state->result, subtask);
519 }
520 }
521
522 SUBTASK_RETURN (subtask);
523 }
524
525 bool
526 recode_combine_ucs2_byte (RECODE_SUBTASK subtask)
527 {
528 struct state *state = NULL;
529 unsigned value;
530
531 if (recode_get_ucs2 (&value, subtask))
532 {
533 while (true)
534 {
535 struct state *shift
536 = find_shifted_state (state, value, subtask->step);
537
538 if (shift)
539 {
540 state = shift;
541 if (!recode_get_ucs2 (&value, subtask))
542 break;
543 }
544 else if (state)
545 {
546 if (state->result == NOT_A_CHARACTER)
547 backtrack_byte (state, subtask);
548 else
549 recode_put_byte (state->result, subtask);
550 state = NULL;
551 }
552 else
553 {
554 recode_put_byte (value, subtask);
555 if (!recode_get_ucs2 (&value, subtask))
556 break;
557 }
558 }
559
560 if (state)
561 {
562 if (state->result == NOT_A_CHARACTER)
563 backtrack_byte (state, subtask);
564 else
565 recode_put_byte (state->result, subtask);
566 }
567 }
568
569 SUBTASK_RETURN (subtask);
570 }
571
572 bool
573 recode_combine_byte_ucs2 (RECODE_SUBTASK subtask)
574 {
575 unsigned value;
576
577 if (value = recode_get_byte (subtask), value != (unsigned)EOF)
578 {
579 struct state *state = NULL;
580
581 if (subtask->task->byte_order_mark)
582 recode_put_ucs2 (BYTE_ORDER_MARK, subtask);
583
584 while (true)
585 {
586 struct state *shift
587 = find_shifted_state (state, value, subtask->step);
588
589 if (shift)
590 {
591 state = shift;
592 if (value = recode_get_byte (subtask), value == (unsigned)EOF)
593 break;
594 }
595 else if (state)
596 {
597 if (state->result == NOT_A_CHARACTER)
598 backtrack_ucs2 (state, subtask);
599 else
600 recode_put_ucs2 (state->result, subtask);
601 state = NULL;
602 }
603 else
604 {
605 recode_put_ucs2 (value, subtask);
606 if (value = recode_get_byte (subtask), value == (unsigned)EOF)
607 break;
608 }
609 }
610
611 if (state)
612 {
613 if (state->result == NOT_A_CHARACTER)
614 backtrack_ucs2 (state, subtask);
615 else
616 recode_put_ucs2 (state->result, subtask);
617 }
618 }
619
620 SUBTASK_RETURN (subtask);
621 }
622
623 bool
624 recode_combine_ucs2_ucs2 (RECODE_SUBTASK subtask)
625 {
626 unsigned value;
627
628 if (recode_get_ucs2 (&value, subtask))
629 {
630 struct state *state = NULL;
631
632 if (subtask->task->byte_order_mark)
633 recode_put_ucs2 (BYTE_ORDER_MARK, subtask);
634
635 while (true)
636 {
637 struct state *shift
638 = find_shifted_state (state, value, subtask->step);
639
640 if (shift)
641 {
642 state = shift;
643 if (!recode_get_ucs2 (&value, subtask))
644 break;
645 }
646 else if (state)
647 {
648 if (state->result == NOT_A_CHARACTER)
649 backtrack_ucs2 (state, subtask);
650 else
651 recode_put_ucs2 (state->result, subtask);
652 state = NULL;
653 }
654 else
655 {
656 recode_put_ucs2 (value, subtask);
657 if (!recode_get_ucs2 (&value, subtask))
658 break;
659 }
660 }
661
662 if (state)
663 {
664 if (state->result == NOT_A_CHARACTER)
665 backtrack_ucs2 (state, subtask);
666 else
667 recode_put_ucs2 (state->result, subtask);
668 }
669 }
670
671 SUBTASK_RETURN (subtask);
672 }