summaryrefslogtreecommitdiffstats
path: root/unicode/unicode_wordbreak.c
blob: 7fab8f5762c182a71d41a92b98cafff471cb6dad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
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
59
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
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
295
296
297
298
299
300
301
302
303
304
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
/*
** Copyright 2011-2013 Double Precision, Inc.
** See COPYING for distribution information.
**
*/

#include	"unicode_config.h"
#include	"courier-unicode.h"

#include <unistd.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "wordbreaktab_internal.h"
#include "wordbreaktab.h"

/*
** We need to keep track of the original character, in addition
** to the wordbreaking class, to check WB3.
*/

typedef struct {
	uint8_t cl;
	char32_t ch;
} wb_info_t;

/*
** Internal object.
*/
struct unicode_wb_info {
	int (*cb_func)(int, void *);
	void *cb_arg;

	/* Previous character seen. */
	wb_info_t prevclass;

	/*
	** For some rules we peek an extra character, and so need to
	** stash away the 2nd previous character seen, when we're looking at
	** it.
	*/
	wb_info_t prev2class;

	/*
	** How many (Extend | Format | ZWJ) were processed, so far,
	** for WB4's sake.
	*/
	size_t wb4_cnt;

	/*
	** Most recently processed WB4 character.
	*/
	wb_info_t wb4_last;

	/*
	** Each character received by unicode_wb_next is forwarded to
	** this handler.
	*/
	int (*next_handler)(unicode_wb_info_t, wb_info_t);

	/*
	** unicode_wb_end() calls this. If we were in a middle of a
	** multi-char rule, this wraps things up.
	*/
	int (*end_handler)(unicode_wb_info_t);
};

/* Forward declarations */

static int sot(unicode_wb_info_t i, wb_info_t cl);
static int wb1and2_done(unicode_wb_info_t i, wb_info_t cl);

static int seen_wb67_handler(unicode_wb_info_t i, wb_info_t cl);
static int seen_wb67_end_handler(unicode_wb_info_t i);
static int wb67_done(unicode_wb_info_t i, wb_info_t prevclass, wb_info_t cl);

static int seen_wb7bc_handler(unicode_wb_info_t i, wb_info_t cl);
static int seen_wb7bc_end_handler(unicode_wb_info_t i);
static int wb7bc_done(unicode_wb_info_t i, wb_info_t prevclass, wb_info_t cl);

static int seen_wb1112_handler(unicode_wb_info_t i, wb_info_t cl);
static int seen_wb1112_end_handler(unicode_wb_info_t i);
static int wb1112_done(unicode_wb_info_t i, wb_info_t prevclass, wb_info_t cl);

static int seen_wb1516_handler(unicode_wb_info_t i, wb_info_t cl);
static int wb1516_done(unicode_wb_info_t i, wb_info_t prevclass, wb_info_t cl);

unicode_wb_info_t unicode_wb_init(int (*cb_func)(int, void *),
				  void *cb_arg)
{
	unicode_wb_info_t i=calloc(1, sizeof(struct unicode_wb_info));

	if (!i)
		return NULL;

	i->next_handler=sot;
	i->cb_func=cb_func;
	i->cb_arg=cb_arg;
	i->wb4_cnt=0;
	return i;
}

int unicode_wb_end(unicode_wb_info_t i)
{
	int rc=0;

	if (i->end_handler)
		rc=(*i->end_handler)(i);

	free(i);
	return rc;
}

int unicode_wb_next_cnt(unicode_wb_info_t i,
			const char32_t *chars,
			size_t cnt)
{
	int rc;

	while (cnt)
	{
		rc=unicode_wb_next(i, *chars++);
		--cnt;
		if (rc)
			return rc;
	}
	return 0;
}

int unicode_wb_next(unicode_wb_info_t i, char32_t ch)
{
	wb_info_t info;

	info.ch=ch;

	info.cl=unicode_tab_lookup(ch,
				   unicode_starting_indextab,
				   unicode_starting_pagetab,
				   sizeof(unicode_starting_indextab)/
				   sizeof(unicode_starting_indextab[0]),
				   unicode_rangetab,
				   sizeof(unicode_rangetab)/
				   sizeof(unicode_rangetab[0]),
				   unicode_classtab,
				   UNICODE_WB_OTHER);

	return (*i->next_handler)(i, info);
}

#if 0

static int result(unicode_wb_info_t i, int flag)
{
	return (*i->cb_func)(flag, i->cb_arg);
}
#else
#define result(i,flag) ( (*(i)->cb_func)( (flag), (i)->cb_arg))
#endif

/*
** Check for WB3C
*/

#define WB3C_APPLIES(prevclass,uclass)			\
	((prevclass).cl == UNICODE_WB_ZWJ &&		\
	 unicode_emoji_extended_pictographic((uclass).ch))

/*
** Finished WB4 processing. Emit the equivalent number of non-break
** indications.
*/
static int wb4(unicode_wb_info_t i)
{
	int rc=0;

	while (i->wb4_cnt > 0)
	{
		--i->wb4_cnt;

		if (rc == 0)
			rc=result(i, 0);
	}
	return rc;
}

#define SET_HANDLER(next,end) (i->next_handler=next, i->end_handler=end)

static int sot(unicode_wb_info_t i, wb_info_t cl)
{
	i->prevclass=cl;
	SET_HANDLER(wb1and2_done, NULL);

	return result(i, 1);	/* WB1 */
}

static int wb4_handled(unicode_wb_info_t i, wb_info_t prevclass, wb_info_t cl);

static int wb1and2_done(unicode_wb_info_t i, wb_info_t cl)
{
	wb_info_t prevclass=i->prevclass;

	i->prevclass=cl;

	if (prevclass.cl == UNICODE_WB_CR && cl.cl == UNICODE_WB_LF)
		return result(i, 0); /* WB3 */

	switch (prevclass.cl) {
	case UNICODE_WB_CR:
	case UNICODE_WB_LF:
	case UNICODE_WB_Newline:
		return result(i, 1); /* WB3a */
	}

	switch (cl.cl) {
	case UNICODE_WB_CR:
	case UNICODE_WB_LF:
	case UNICODE_WB_Newline:
		return result(i, 1); /* WB3b */
	}

	if (WB3C_APPLIES(prevclass, cl))
		return result(i, 0); /* WB3c */

	if (prevclass.cl == UNICODE_WB_WSegSpace &&
	    cl.cl == UNICODE_WB_WSegSpace)
		return result(i, 0); /* WB3d */

	return wb4_handled(i, prevclass, cl);
}

/*
** Macros, as defined in the TR
*/
#define AHLetter(c) (c.cl == UNICODE_WB_ALetter || \
		     c.cl == UNICODE_WB_Hebrew_Letter)
#define MidNumLetQ(c) (c.cl == UNICODE_WB_MidNumLet || \
		       c.cl == UNICODE_WB_Single_Quote)

/*
** Whether the character is applicable to the WB4 rule.
*/

#define WB4(C)	((C).cl == UNICODE_WB_Extend || (C).cl == UNICODE_WB_Format ||\
		 (C).cl == UNICODE_WB_ZWJ)

/*
** Check if the current character invokes the WB4 rule, if so return s0,
** doing nothing, here, after performing some record keeping.
*/

#define WB4_APPLY(i,cl)				\
	do {					\
		if (WB4(cl))			\
		{				\
			++(i)->wb4_cnt;		\
			(i)->wb4_last=(cl);	\
			return 0;		\
		}				\
	} while (0)

/*
** After processing WB4, check if the last WB4-processed character
** will invoke WB3C for the next character.
**
** This is invoked after WB4_APPLY. The return value must be stored in an
** int.
**
** This must be followed by WB4_END. Then, after WB4_END, if this returned
** non 0, WB3C applies, returning a non-break indication.
*/

#define WB3C_APPLIES_AFTER_WB4(i,cl)			\
	( (i)->wb4_cnt > 0 &&				\
	  WB3C_APPLIES( (i)->wb4_last, (cl)))

/*
** Wrapper for invoking wb4() after detecting that it no longer applies. This
** gets invoked:
**
** - After WB4_APPLY
**
** - After WB3C_APPLIES_AFTER_WB4
*/

#define WB4_END(i)				\
	do {					\
						\
		int rc=wb4(i);			\
						\
		if (rc)				\
			return rc;		\
	} while (0)


static int resume_wb4(unicode_wb_info_t i, wb_info_t cl)
{
	if (!WB4(cl))
	{
		SET_HANDLER(wb1and2_done, NULL);

		if (WB3C_APPLIES(i->wb4_last, cl))
		{
			i->prevclass=cl;
			return result(i, 0);
		}

		wb_info_t prevclass=i->prevclass;

		i->prevclass=cl;

		return wb4_handled(i, prevclass, cl);
	}
	i->wb4_last=cl;
	return result(i, 0);
}


static int wb4_handled(unicode_wb_info_t i, wb_info_t prevclass, wb_info_t cl)
{
	if (WB4(cl))
	{
		i->prevclass=prevclass;
		i->wb4_last=cl;
		SET_HANDLER(resume_wb4, 0);
		return result(i, 0); /* WB4 */
	}

	if (AHLetter(prevclass) && AHLetter(cl))
	{
		return result(i, 0); /* WB5 */
	}

	if (AHLetter(prevclass) &&
	    (MidNumLetQ(cl) || cl.cl == UNICODE_WB_MidLetter))
	{
		i->prev2class=prevclass;
		SET_HANDLER(seen_wb67_handler, seen_wb67_end_handler);
		return 0;
	}

	return wb67_done(i, prevclass, cl);
}

/*
** AHLetter (MidLetter | MidNumLetQ) seen, is this followed by AHLetter?
*/

static int seen_wb67_handler(unicode_wb_info_t i, wb_info_t cl)
{
	int rc;

	WB4_APPLY(i, cl);

	SET_HANDLER(wb1and2_done, NULL);

	if (AHLetter(cl))
	{
		i->prevclass=cl;

		rc=result(i, 0);  /* WB6 */

		WB4_END(i);
		if (rc == 0)
			rc=result(i, 0); /* WB7 */

		return rc;
	}

	int wb3c_applies=WB3C_APPLIES_AFTER_WB4(i, cl);

	rc=seen_wb67_end_handler(i);

	if (wb3c_applies)
		return result(i, 0);

	if (rc == 0)
		rc=(*i->next_handler)(i, cl);

	return rc;
}

/*
** AHLetter (MidLetter | MidNumLetQ) seen, with the second
** character's status not returned yet, and now either sot, or something
** else.
*/

static int seen_wb67_end_handler(unicode_wb_info_t i)
{
	int rc=wb67_done(i, i->prev2class, i->prevclass);

	if (rc)
		return rc;
	WB4_END(i);
	return 0;
}

static int wb67_done(unicode_wb_info_t i, wb_info_t prevclass, wb_info_t cl)
{
	if (prevclass.cl == UNICODE_WB_Hebrew_Letter &&
	    cl.cl == UNICODE_WB_Single_Quote)
		return result(i, 0); /* WB7a */

	if (prevclass.cl == UNICODE_WB_Hebrew_Letter &&
	    cl.cl == UNICODE_WB_Double_Quote)
	{
		i->prev2class=prevclass;
		SET_HANDLER(seen_wb7bc_handler, seen_wb7bc_end_handler);
		return 0;
	}

	return wb7bc_done(i, prevclass, cl);
}

/*
** Hebrew_Letter Double_Quote       ?
**
**               prevclass          cl
**
** Seen Hebrew_Letter Double_Quote, with the second character's status
** not returned yet.
*/

static int seen_wb7bc_handler(unicode_wb_info_t i, wb_info_t cl)
{
	int rc;

	WB4_APPLY(i, cl);

	SET_HANDLER(wb1and2_done, NULL);

	if (cl.cl == UNICODE_WB_Hebrew_Letter)
	{
		i->prevclass=cl;

		rc=result(i, 0);  /* WB7b */

		WB4_END(i);

		if (rc == 0)
			rc=result(i, 0); /* WB7c */

		return rc;
	}

	int wb3c_applies=WB3C_APPLIES_AFTER_WB4(i, cl);

	rc=seen_wb7bc_end_handler(i);

	if (wb3c_applies)
		return result(i, 0);
	if (rc == 0)
		rc=(*i->next_handler)(i, cl);

	return rc;
}

/*
** Seen Hebrew_Letter Double_Quote, with the second
** character's status not returned yet, and now sot or something else.
*/

static int seen_wb7bc_end_handler(unicode_wb_info_t i)
{
	int rc=wb7bc_done(i, i->prev2class, i->prevclass);

	if (rc)
		return rc;

	WB4_END(i);

	return 0;
}

static int wb7bc_done(unicode_wb_info_t i, wb_info_t prevclass, wb_info_t cl)
{
	if (prevclass.cl == UNICODE_WB_Numeric && cl.cl == UNICODE_WB_Numeric)
		return result(i, 0); /* WB8 */

	if (AHLetter(prevclass) && cl.cl == UNICODE_WB_Numeric)
		return result(i, 0); /* WB9 */

	if (prevclass.cl == UNICODE_WB_Numeric && AHLetter(cl))
		return result(i, 0); /* WB10 */

	if (prevclass.cl == UNICODE_WB_Numeric &&
	    (cl.cl == UNICODE_WB_MidNum || MidNumLetQ(cl)))
	{
		i->prev2class=prevclass;
		SET_HANDLER(seen_wb1112_handler, seen_wb1112_end_handler);
		return 0;
	}

	return wb1112_done(i, prevclass, cl);
}

/*
**              Numeric     (MidNum | MidNumLet )     ?
**
**                               prevclass            cl
**
** Seen Numeric (MidNum | MidNumLet), with the second character's status
** not returned yet.
*/

static int seen_wb1112_handler(unicode_wb_info_t i, wb_info_t cl)
{
	int rc;

	WB4_APPLY(i, cl);

	SET_HANDLER(wb1and2_done, NULL);

	if (cl.cl == UNICODE_WB_Numeric)
	{
		i->prevclass=cl;

		rc=result(i, 0);  /* WB12 */

		WB4_END(i);

		if (rc == 0)
			rc=result(i, 0); /* WB11 */

		return rc;
	}

	int wb3c_applies=WB3C_APPLIES_AFTER_WB4(i, cl);

	rc=seen_wb1112_end_handler(i);

	if (wb3c_applies)
		return result(i, 0);

	if (rc == 0)
		rc=(*i->next_handler)(i, cl);

	return rc;
}

/*
** Seen Numeric (MidNum | MidNumLet), with the second character's status
** not returned yet, and now sot.
*/

static int seen_wb1112_end_handler(unicode_wb_info_t i)
{
	int rc=wb1112_done(i, i->prev2class, i->prevclass);

	if (rc)
		return rc;

	WB4_END(i);

	return 0;
}

static int wb1112_done(unicode_wb_info_t i, wb_info_t prevclass, wb_info_t cl)
{
	if (prevclass.cl == UNICODE_WB_Katakana &&
	    cl.cl == UNICODE_WB_Katakana)
		return result(i, 0); /* WB13 */

	switch (prevclass.cl) {
	case UNICODE_WB_ALetter:
	case UNICODE_WB_Hebrew_Letter:
	case UNICODE_WB_Numeric:
	case UNICODE_WB_Katakana:
	case UNICODE_WB_ExtendNumLet:
		if (cl.cl == UNICODE_WB_ExtendNumLet)
			return result(i, 0); /* WB13a */
	}

	if (prevclass.cl == UNICODE_WB_ExtendNumLet)
		switch (cl.cl) {
		case UNICODE_WB_ALetter:
		case UNICODE_WB_Hebrew_Letter:
		case UNICODE_WB_Numeric:
		case UNICODE_WB_Katakana:
			return result(i, 0); /* WB13b */
		}

	if (prevclass.cl == UNICODE_WB_Regional_Indicator &&
	    cl.cl == UNICODE_WB_Regional_Indicator)
	{
		SET_HANDLER(seen_wb1516_handler, 0);
		return result(i, 0);
	}

	return wb1516_done(i, prevclass, cl);
}

static int seen_wb1516_handler(unicode_wb_info_t i, wb_info_t cl)
{
	WB4_APPLY(i, cl);

	SET_HANDLER(wb1and2_done, NULL);

	int wb3c_applies=WB3C_APPLIES_AFTER_WB4(i, cl);

	WB4_END(i);

	if (wb3c_applies)
		return result(i, 0);

	if (cl.cl == UNICODE_WB_Regional_Indicator)
	{
		wb_info_t prevclass=i->prevclass;

		i->prevclass=cl;

		return wb1516_done(i, prevclass, cl);
	}

	return (*i->next_handler)(i, cl);
}

static int wb1516_done(unicode_wb_info_t i, wb_info_t prevclass, wb_info_t cl)
{
	return result(i, 1); /* WB999 */
}

/* --------------------------------------------------------------------- */

struct unicode_wbscan_info {
	unicode_wb_info_t wb_handle;

	int found;
	size_t cnt;
};

static int unicode_wbscan_callback(int, void *);

unicode_wbscan_info_t unicode_wbscan_init()
{
	unicode_wbscan_info_t i=calloc(1, sizeof(struct unicode_wbscan_info));

	if (!i)
		return NULL;

	if ((i->wb_handle=unicode_wb_init(unicode_wbscan_callback, i)) == NULL)
	{
		free(i);
		return NULL;
	}

	return i;
}

int unicode_wbscan_next(unicode_wbscan_info_t i, char32_t ch)
{
	if (!i->found)
		unicode_wb_next(i->wb_handle, ch);

	return i->found;
}

size_t unicode_wbscan_end(unicode_wbscan_info_t i)
{
	size_t n;

	unicode_wb_end(i->wb_handle);

	n=i->cnt;
	free(i);
	return n;
}

static int unicode_wbscan_callback(int flag, void *arg)
{
	unicode_wbscan_info_t i=(unicode_wbscan_info_t)arg;

	if (flag && i->cnt > 0)
		i->found=1;

	if (!i->found)
		++i->cnt;
	return 0;
}