libgrapheme

grapheme cluster utility library
git clone git://git.suckless.org/libgrapheme
Log | Files | Refs | LICENSE

grapheme_boundary.c (942B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <stddef.h>
      3 #include <stdint.h>
      4 #include <stdio.h>
      5 #include <string.h>
      6 
      7 #include "../grapheme.h"
      8 #include "../data/grapheme_boundary_test.h"
      9 
     10 #define LEN(x) (sizeof(x) / sizeof(*x))
     11 
     12 int
     13 main(void)
     14 {
     15 	int state;
     16 	size_t i, j, k, len, failed;
     17 
     18 	/* grapheme break test */
     19 	for (i = 0, failed = 0; i < LEN(t); i++) {
     20 		for (j = 0, k = 0, state = 0, len = 1; j < t[i].cplen; j++) {
     21 			if ((j + 1) == t[i].cplen ||
     22 			    grapheme_boundary(t[i].cp[j], t[i].cp[j + 1],
     23 			                      &state)) {
     24 				/* check if our resulting length matches */
     25 				if (k == t[i].lenlen || len != t[i].len[k++]) {
     26 					fprintf(stderr, "Failed \"%s\"\n",
     27 					        t[i].descr);
     28 					failed++;
     29 					break;
     30 				}
     31 				len = 1;
     32 			} else {
     33 				len++;
     34 			}
     35 		}
     36 	}
     37 	printf("Grapheme break test: Passed %zu out of %zu tests.\n",
     38 	       LEN(t) - failed, LEN(t));
     39 
     40 	return (failed > 0) ? 1 : 0;
     41 }