]> git.sur5r.net Git - openldap/blob - libraries/liblunicode/ucdata/ucpgba.c
Cyrus SASL uses screwy terms.
[openldap] / libraries / liblunicode / ucdata / ucpgba.c
1 /*
2  * Copyright 1999 Computing Research Labs, New Mexico State University
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COMPUTING RESEARCH LAB OR NEW MEXICO STATE UNIVERSITY BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
19  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
20  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 #ifndef lint
23 #ifdef __GNUC__
24 static char rcsid[] __attribute__ ((unused)) = "$Id: ucpgba.c,v 1.4 1999/11/29 16:41:06 mleisher Exp $";
25 #else
26 static char rcsid[] = "$Id: ucpgba.c,v 1.4 1999/11/29 16:41:06 mleisher Exp $";
27 #endif
28 #endif
29
30 #include "portable.h"
31
32 #include <stdio.h>
33 #include <stdlib.h>
34
35 #include "ucdata.h"
36 #include "ucpgba.h"
37
38 /*
39  * These macros are used while reordering of RTL runs of text for the
40  * special case of non-spacing characters being in runs of weakly
41  * directional text.  They check for weak and non-spacing, and digits and
42  * non-spacing.
43  */
44 #define ISWEAKSPECIAL(cc)  ucisprop(cc, UC_EN|UC_ES|UC_MN, UC_ET|UC_AN|UC_CS)
45 #define ISDIGITSPECIAL(cc) ucisprop(cc, UC_ND|UC_MN, 0)
46
47 /*
48  * These macros are used while breaking a string into runs of text in
49  * different directions.  Descriptions:
50  *
51  * ISLTR_LTR - Test for members of an LTR run in an LTR context.  This looks
52  *             for characters with ltr, non-spacing, weak, and neutral
53  *             properties.
54  *
55  * ISRTL_RTL - Test for members of an RTL run in an RTL context.  This looks
56  *             for characters with rtl, non-spacing, weak, and neutral
57  *             properties.
58  *
59  * ISRTL_NEUTRAL  - Test for RTL or neutral characters.
60  *
61  * ISWEAK_NEUTRAL - Test for weak or neutral characters.
62  */
63 #define ISLTR_LTR(cc) ucisprop(cc, UC_L|UC_MN|UC_EN|UC_ES,\
64                                UC_ET|UC_AN|UC_CS|UC_B|UC_S|UC_WS|UC_ON)
65
66 #define ISRTL_RTL(cc) ucisprop(cc, UC_R|UC_MN|UC_EN|UC_ES,\
67                                UC_ET|UC_AN|UC_CS|UC_B|UC_S|UC_WS|UC_ON)
68
69 #define ISRTL_NEUTRAL(cc) ucisprop(cc, UC_R, UC_B|UC_S|UC_WS|UC_ON)
70 #define ISWEAK_NEUTRAL(cc) ucisprop(cc, UC_EN|UC_ES, \
71                                     UC_B|UC_S|UC_WS|UC_ON|UC_ET|UC_AN|UC_CS)
72
73 /*
74  * This table is temporarily hard-coded here until it can be constructed
75  * automatically somehow.
76  */
77 static unsigned long _symmetric_pairs[] = {
78     0x0028, 0x0029, 0x0029, 0x0028, 0x003C, 0x003E, 0x003E, 0x003C,
79     0x005B, 0x005D, 0x005D, 0x005B, 0x007B, 0x007D, 0x007D, 0x007B,
80     0x2045, 0x2046, 0x2046, 0x2045, 0x207D, 0x207E, 0x207E, 0x207D,
81     0x208D, 0x208E, 0x208E, 0x208D, 0x3008, 0x3009, 0x3009, 0x3008,
82     0x300A, 0x300B, 0x300B, 0x300A, 0x300C, 0x300D, 0x300D, 0x300C,
83     0x300E, 0x300F, 0x300F, 0x300E, 0x3010, 0x3011, 0x3011, 0x3010,
84     0x3014, 0x3015, 0x3015, 0x3014, 0x3016, 0x3017, 0x3017, 0x3016,
85     0x3018, 0x3019, 0x3019, 0x3018, 0x301A, 0x301B, 0x301B, 0x301A,
86     0xFD3E, 0xFD3F, 0xFD3F, 0xFD3E, 0xFE59, 0xFE5A, 0xFE5A, 0xFE59,
87     0xFE5B, 0xFE5C, 0xFE5C, 0xFE5B, 0xFE5D, 0xFE5E, 0xFE5E, 0xFE5D,
88     0xFF08, 0xFF09, 0xFF09, 0xFF08, 0xFF3B, 0xFF3D, 0xFF3D, 0xFF3B,
89     0xFF5B, 0xFF5D, 0xFF5D, 0xFF5B, 0xFF62, 0xFF63, 0xFF63, 0xFF62,
90 };
91
92 static int _symmetric_pairs_size =
93 sizeof(_symmetric_pairs)/sizeof(_symmetric_pairs[0]);
94
95 /*
96  * This routine looks up the other form of a symmetric pair.
97  */
98 static unsigned long
99 _ucsymmetric_pair(unsigned long c)
100 {
101     int i;
102
103     for (i = 0; i < _symmetric_pairs_size; i += 2) {
104         if (_symmetric_pairs[i] == c)
105           return _symmetric_pairs[i+1];
106     }
107     return c;
108 }
109
110 /*
111  * This routine creates a new run, copies the text into it, links it into the
112  * logical text order chain and returns it to the caller to be linked into
113  * the visual text order chain.
114  */
115 static ucrun_t *
116 _add_run(ucstring_t *str, unsigned long *src,
117          unsigned long start, unsigned long end, int direction)
118 {
119     long i, t;
120     ucrun_t *run;
121
122     run = (ucrun_t *) malloc(sizeof(ucrun_t));
123     run->visual_next = run->visual_prev = 0;
124     run->direction = direction;
125
126     run->cursor = ~0;
127
128     run->chars = (unsigned long *)
129         malloc(sizeof(unsigned long) * ((end - start) << 1));
130     run->positions = run->chars + (end - start);
131
132     run->source = src;
133     run->start = start;
134     run->end = end;
135
136     if (direction == UCPGBA_RTL) {
137         /*
138          * Copy the source text into the run in reverse order and select
139          * replacements for the pairwise punctuation and the <> characters.
140          */
141         for (i = 0, t = end - 1; start < end; start++, t--, i++) {
142             run->positions[i] = t;
143             if (ucissymmetric(src[t]) || src[t] == '<' || src[t] == '>')
144               run->chars[i] = _ucsymmetric_pair(src[t]);
145             else
146               run->chars[i] = src[t];
147         }
148     } else {
149         /*
150          * Copy the source text into the run directly.
151          */
152         for (i = start; i < end; i++) {
153             run->positions[i - start] = i;
154             run->chars[i - start] = src[i];
155         }
156     }
157
158     /*
159      * Add the run to the logical list for cursor traversal.
160      */
161     if (str->logical_first == 0)
162       str->logical_first = str->logical_last = run;
163     else {
164         run->logical_prev = str->logical_last;
165         str->logical_last->logical_next = run;
166         str->logical_last = run;
167     }
168
169     return run;
170 }
171
172 static void
173 _ucadd_rtl_segment(ucstring_t *str, unsigned long *source, unsigned long start,
174                    unsigned long end)
175 {
176     unsigned long s, e;
177     ucrun_t *run, *lrun;
178
179     /*
180      * This is used to splice runs into strings with overall LTR direction.
181      * The `lrun' variable will never be NULL because at least one LTR run was
182      * added before this RTL run.
183      */
184     lrun = str->visual_last;
185
186     for (e = s = start; s < end;) {
187         for (; e < end && ISRTL_NEUTRAL(source[e]); e++) ;
188
189         if (e > s) {
190             run = _add_run(str, source, s, e, UCPGBA_RTL);
191
192             /*
193              * Add the run to the visual list for cursor traversal.
194              */
195             if (str->visual_first != 0) {
196                 if (str->direction == UCPGBA_LTR) {
197                     run->visual_prev = lrun;
198                     run->visual_next = lrun->visual_next;
199                     if (lrun->visual_next != 0)
200                       lrun->visual_next->visual_prev = run;
201                     lrun->visual_next = run;
202                     if (lrun == str->visual_last)
203                       str->visual_last = run;
204                 } else {
205                     run->visual_next = str->visual_first;
206                     str->visual_first->visual_prev = run;
207                     str->visual_first = run;
208                 }
209             } else
210               str->visual_first = str->visual_last = run;
211         }
212
213         /*
214          * Now handle the weak sequences such that multiple non-digit groups
215          * are kept together appropriately and added as RTL sequences.
216          */
217         for (s = e; e < end && ISWEAKSPECIAL(source[e]); e++) {
218             if (!ISDIGITSPECIAL(source[e]) &&
219                 (e + 1 == end || !ISDIGITSPECIAL(source[e + 1])))
220               break;
221         }
222
223         if (e > s) {
224             run = _add_run(str, source, s, e, UCPGBA_LTR);
225
226             /*
227              * Add the run to the visual list for cursor traversal.
228              */
229             if (str->visual_first != 0) {
230                 if (str->direction == UCPGBA_LTR) {
231                     run->visual_prev = lrun;
232                     run->visual_next = lrun->visual_next;
233                     if (lrun->visual_next != 0)
234                       lrun->visual_next->visual_prev = run;
235                     lrun->visual_next = run;
236                     if (lrun == str->visual_last)
237                       str->visual_last = run;
238                 } else {
239                     run->visual_next = str->visual_first;
240                     str->visual_first->visual_prev = run;
241                     str->visual_first = run;
242                 }
243             } else
244               str->visual_first = str->visual_last = run;
245         }
246
247         /*
248          * Collect all weak non-digit sequences for an RTL segment.  These
249          * will appear as part of the next RTL segment or will be added as
250          * an RTL segment by themselves.
251          */
252         for (s = e; e < end && ucisweak(source[e]) && !ucisdigit(source[e]);
253              e++) ;
254     }
255
256     /*
257      * Capture any weak non-digit sequences that occur at the end of the RTL
258      * run.
259      */
260     if (e > s) {
261         run = _add_run(str, source, s, e, UCPGBA_RTL);
262
263         /*
264          * Add the run to the visual list for cursor traversal.
265          */
266         if (str->visual_first != 0) {
267             if (str->direction == UCPGBA_LTR) {
268                 run->visual_prev = lrun;
269                 run->visual_next = lrun->visual_next;
270                 if (lrun->visual_next != 0)
271                   lrun->visual_next->visual_prev = run;
272                 lrun->visual_next = run;
273                 if (lrun == str->visual_last)
274                   str->visual_last = run;
275             } else {
276                 run->visual_next = str->visual_first;
277                 str->visual_first->visual_prev = run;
278                 str->visual_first = run;
279             }
280         } else
281           str->visual_first = str->visual_last = run;
282     }
283 }
284
285 static void
286 _ucadd_ltr_segment(ucstring_t *str, unsigned long *source, unsigned long start,
287                    unsigned long end)
288 {
289     ucrun_t *run;
290
291     run = _add_run(str, source, start, end, UCPGBA_LTR);
292
293     /*
294      * Add the run to the visual list for cursor traversal.
295      */
296     if (str->visual_first != 0) {
297         if (str->direction == UCPGBA_LTR) {
298             run->visual_prev = str->visual_last;
299             str->visual_last->visual_next = run;
300             str->visual_last = run;
301         } else {
302             run->visual_next = str->visual_first;
303             str->visual_first->visual_prev = run;
304             str->visual_first = run;
305         }
306     } else
307       str->visual_first = str->visual_last = run;
308 }
309
310 ucstring_t *
311 ucstring_create(unsigned long *source, unsigned long start, unsigned long end,
312                 int default_direction, int cursor_motion)
313 {
314     int rtl_first;
315     unsigned long s, e;
316     ucstring_t *str;
317
318     str = (ucstring_t *) malloc(sizeof(ucstring_t));
319
320     /*
321      * Set the initial values.
322      */
323     str->cursor_motion = cursor_motion;
324     str->logical_first = str->logical_last = 0;
325     str->visual_first = str->visual_last = str->cursor = 0;
326     str->source = source;
327     str->start = start;
328     str->end = end;
329
330     /*
331      * If the length of the string is 0, then just return it at this point.
332      */
333     if (start == end)
334       return str;
335
336     /*
337      * This flag indicates whether the collection loop for RTL is called
338      * before the LTR loop the first time.
339      */
340     rtl_first = 0;
341
342     /*
343      * Look for the first character in the string that has strong
344      * directionality.
345      */
346     for (s = start; s < end && !ucisstrong(source[s]); s++) ;
347
348     if (s == end)
349       /*
350        * If the string contains no characters with strong directionality, use
351        * the default direction.
352        */
353       str->direction = default_direction;
354     else
355       str->direction = ucisrtl(source[s]) ? UCPGBA_RTL : UCPGBA_LTR;
356
357     if (str->direction == UCPGBA_RTL)
358       /*
359        * Set the flag that causes the RTL collection loop to run first.
360        */
361       rtl_first = 1;
362
363     /*
364      * This loop now separates the string into runs based on directionality.
365      */
366     for (s = e = 0; s < end; s = e) {
367         if (!rtl_first) {
368             /*
369              * Determine the next run of LTR text.
370              */
371
372             while (e < end && ISLTR_LTR(source[e]))
373               e++;
374             if (str->direction != UCPGBA_LTR) {
375                 while (e > s && ISWEAK_NEUTRAL(source[e - 1]))
376                   e--;
377             }
378
379             /*
380              * Add the LTR segment to the string.
381              */
382             if (e > s)
383               _ucadd_ltr_segment(str, source, s, e);
384         }
385
386         /*
387          * Determine the next run of RTL text.
388          */
389         s = e;
390         while (e < end && ISRTL_RTL(source[e]))
391           e++;
392         if (str->direction != UCPGBA_RTL) {
393             while (e > s && ISWEAK_NEUTRAL(source[e - 1]))
394               e--;
395         }
396
397         /*
398          * Add the RTL segment to the string.
399          */
400         if (e > s)
401           _ucadd_rtl_segment(str, source, s, e);
402
403         /*
404          * Clear the flag that allowed the RTL collection loop to run first
405          * for strings with overall RTL directionality.
406          */
407         rtl_first = 0;
408     }
409
410     /*
411      * Set up the initial cursor run.
412      */
413     str->cursor = str->logical_first;
414     if (str != 0)
415       str->cursor->cursor = (str->cursor->direction == UCPGBA_RTL) ?
416           str->cursor->end - str->cursor->start : 0;
417
418     return str;
419 }
420
421 void
422 ucstring_free(ucstring_t *s)
423 {
424     ucrun_t *l, *r;
425
426     if (s == 0)
427       return;
428
429     for (l = 0, r = s->visual_first; r != 0; r = r->visual_next) {
430         if (r->end > r->start)
431           free((char *) r->chars);
432         if (l)
433           free((char *) l);
434         l = r;
435     }
436     if (l)
437       free((char *) l);
438
439     free((char *) s);
440 }
441
442 int
443 ucstring_set_cursor_motion(ucstring_t *str, int cursor_motion)
444 {
445     int n;
446
447     if (str == 0)
448       return -1;
449
450     n = str->cursor_motion;
451     str->cursor_motion = cursor_motion;
452     return n;
453 }
454
455 static int
456 _ucstring_visual_cursor_right(ucstring_t *str, int count)
457 {
458     int cnt = count;
459     unsigned long size;
460     ucrun_t *cursor;
461
462     if (str == 0)
463       return 0;
464
465     cursor = str->cursor;
466     while (cnt > 0) {
467         size = cursor->end - cursor->start;
468         if ((cursor->direction == UCPGBA_RTL && cursor->cursor + 1 == size) ||
469             cursor->cursor + 1 > size) {
470             /*
471              * If the next run is NULL, then the cursor is already on the
472              * far right end already.
473              */
474             if (cursor->visual_next == 0)
475               /*
476                * If movement occured, then report it.
477                */
478               return (cnt != count);
479
480             /*
481              * Move to the next run.
482              */
483             str->cursor = cursor = cursor->visual_next;
484             cursor->cursor = (cursor->direction == UCPGBA_RTL) ? -1 : 0;
485             size = cursor->end - cursor->start;
486         } else
487           cursor->cursor++;
488         cnt--;
489     }
490     return 1;
491 }
492
493 static int
494 _ucstring_logical_cursor_right(ucstring_t *str, int count)
495 {
496     int cnt = count;
497     unsigned long size;
498     ucrun_t *cursor;
499
500     if (str == 0)
501       return 0;
502
503     cursor = str->cursor;
504     while (cnt > 0) {
505         size = cursor->end - cursor->start;
506         if (str->direction == UCPGBA_RTL) {
507             if (cursor->direction == UCPGBA_RTL) {
508                 if (cursor->cursor + 1 == size) {
509                     if (cursor == str->logical_first)
510                       /*
511                        * Already at the beginning of the string.
512                        */
513                       return (cnt != count);
514
515                     str->cursor = cursor = cursor->logical_prev;
516                     size = cursor->end - cursor->start;
517                     cursor->cursor = (cursor->direction == UCPGBA_LTR) ?
518                         size : 0;
519                 } else
520                   cursor->cursor++;
521             } else {
522                 if (cursor->cursor == 0) {
523                     if (cursor == str->logical_first)
524                       /*
525                        * At the beginning of the string already.
526                        */
527                       return (cnt != count);
528
529                     str->cursor = cursor = cursor->logical_prev;
530                     size = cursor->end - cursor->start;
531                     cursor->cursor = (cursor->direction == UCPGBA_LTR) ?
532                         size : 0;
533                 } else
534                   cursor->cursor--;
535             }
536         } else {
537             if (cursor->direction == UCPGBA_RTL) {
538                 if (cursor->cursor == 0) {
539                     if (cursor == str->logical_last)
540                       /*
541                        * Already at the end of the string.
542                        */
543                       return (cnt != count);
544
545                     str->cursor = cursor = cursor->logical_next;
546                     size = cursor->end - cursor->start;
547                     cursor->cursor = (cursor->direction == UCPGBA_LTR) ?
548                         0 : size - 1;
549                 } else
550                   cursor->cursor--;
551             } else {
552                 if (cursor->cursor + 1 > size) {
553                     if (cursor == str->logical_last)
554                       /*
555                        * Already at the end of the string.
556                        */
557                       return (cnt != count);
558
559                     str->cursor = cursor = cursor->logical_next;
560                     cursor->cursor = (cursor->direction == UCPGBA_LTR) ?
561                         0 : size - 1;
562                 } else
563                   cursor->cursor++;
564             }
565         }
566         cnt--;
567     }
568     return 1;
569 }
570
571 int
572 ucstring_cursor_right(ucstring_t *str, int count)
573 {
574     if (str == 0)
575       return 0;
576     return (str->cursor_motion == UCPGBA_CURSOR_VISUAL) ?
577         _ucstring_visual_cursor_right(str, count) :
578         _ucstring_logical_cursor_right(str, count);
579 }
580
581 static int
582 _ucstring_visual_cursor_left(ucstring_t *str, int count)
583 {
584     int cnt = count;
585     unsigned long size;
586     ucrun_t *cursor;
587
588     if (str == 0)
589       return 0;
590
591     cursor = str->cursor;
592     while (cnt > 0) {
593         size = cursor->end - cursor->start;
594         if ((cursor->direction == UCPGBA_LTR && cursor->cursor == 0) ||
595             cursor->cursor - 1 < -1) {
596             /*
597              * If the preceding run is NULL, then the cursor is already on the
598              * far left end already.
599              */
600             if (cursor->visual_prev == 0)
601               /*
602                * If movement occured, then report it.
603                */
604               return (cnt != count);
605
606             /*
607              * Move to the previous run.
608              */
609             str->cursor = cursor = cursor->visual_prev;
610             size = cursor->end - cursor->start;
611             cursor->cursor = (cursor->direction == UCPGBA_RTL) ?
612                 size : size - 1;
613         } else
614           cursor->cursor--;
615         cnt--;
616     }
617     return 1;
618 }
619
620 static int
621 _ucstring_logical_cursor_left(ucstring_t *str, int count)
622 {
623     int cnt = count;
624     unsigned long size;
625     ucrun_t *cursor;
626
627     if (str == 0)
628       return 0;
629
630     cursor = str->cursor;
631     while (cnt > 0) {
632         size = cursor->end - cursor->start;
633         if (str->direction == UCPGBA_RTL) {
634             if (cursor->direction == UCPGBA_RTL) {
635                 if (cursor->cursor == -1) {
636                     if (cursor == str->logical_last)
637                       /*
638                        * Already at the end of the string.
639                        */
640                       return (cnt != count);
641
642                     str->cursor = cursor = cursor->logical_next;
643                     size = cursor->end - cursor->start;
644                     cursor->cursor = (cursor->direction == UCPGBA_LTR) ?
645                         0 : size - 1;
646                 } else
647                   cursor->cursor--;
648             } else {
649                 if (cursor->cursor + 1 > size) {
650                     if (cursor == str->logical_last)
651                       /*
652                        * At the end of the string already.
653                        */
654                       return (cnt != count);
655
656                     str->cursor = cursor = cursor->logical_next;
657                     size = cursor->end - cursor->start;
658                     cursor->cursor = (cursor->direction == UCPGBA_LTR) ?
659                         0 : size - 1;
660                 } else
661                   cursor->cursor++;
662             }
663         } else {
664             if (cursor->direction == UCPGBA_RTL) {
665                 if (cursor->cursor + 1 == size) {
666                     if (cursor == str->logical_first)
667                       /*
668                        * Already at the beginning of the string.
669                        */
670                       return (cnt != count);
671
672                     str->cursor = cursor = cursor->logical_prev;
673                     size = cursor->end - cursor->start;
674                     cursor->cursor = (cursor->direction == UCPGBA_LTR) ?
675                         size : 0;
676                 } else
677                   cursor->cursor++;
678             } else {
679                 if (cursor->cursor == 0) {
680                     if (cursor == str->logical_first)
681                       /*
682                        * Already at the beginning of the string.
683                        */
684                       return (cnt != count);
685
686                     str->cursor = cursor = cursor->logical_prev;
687                     cursor->cursor = (cursor->direction == UCPGBA_LTR) ?
688                         size : 0;
689                 } else
690                   cursor->cursor--;
691             }
692         }
693         cnt--;
694     }
695     return 1;
696 }
697
698 int
699 ucstring_cursor_left(ucstring_t *str, int count)
700 {
701     if (str == 0)
702       return 0;
703     return (str->cursor_motion == UCPGBA_CURSOR_VISUAL) ?
704         _ucstring_visual_cursor_left(str, count) :
705         _ucstring_logical_cursor_left(str, count);
706 }
707
708 void
709 ucstring_cursor_info(ucstring_t *str, int *direction, unsigned long *position)
710 {
711     long c;
712     unsigned long size;
713     ucrun_t *cursor;
714
715     if (str == 0 || direction == 0 || position == 0)
716       return;
717
718     cursor = str->cursor;
719
720     *direction = cursor->direction;
721
722     c = cursor->cursor;
723     size = cursor->end - cursor->start;
724
725     if (c == size)
726       *position = (cursor->direction == UCPGBA_RTL) ?
727           cursor->start : cursor->positions[c - 1];
728     else if (c == -1)
729       *position = (cursor->direction == UCPGBA_RTL) ?
730           cursor->end : cursor->start;
731     else
732       *position = cursor->positions[c];
733 }