libutf

UTF-8 library
git clone git://git.suckless.org/libutf
Log | Files | Refs | README | LICENSE

mkrunetype.awk (1948B)


      1 # See LICENSE file for copyright and license details.
      2 
      3 BEGIN {
      4 	FS = ";"
      5 	# set up hexadecimal lookup table
      6 	for(i = 0; i < 16; i++)
      7 		hex[sprintf("%X",i)] = i;
      8 }
      9 
     10 $3  ~ /^L/ { alphav[alphac++] = $1; }
     11 $3  ~ /^Z/ { spacev[spacec++] = $1; }
     12 $3 == "Lu" { upperv[upperc++] = $1; }
     13 $3 == "Ll" { lowerv[lowerc++] = $1; }
     14 $3 == "Lt" { titlev[titlec++] = $1; }
     15 $3 == "Nd" { digitv[digitc++] = $1; }
     16 
     17 END {
     18 	print "/* Automatically generated from "FILENAME" by mkrunetype.awk */\n"
     19 
     20 	mkis("alpha", alphav, alphac);
     21 	mkis("space", spacev, spacec);
     22 	mkis("upper", upperv, upperc);
     23 	mkis("lower", lowerv, lowerc);
     24 	mkis("title", titlev, titlec);
     25 	mkis("digit", digitv, digitc);
     26 }
     27 
     28 # parse hexadecimal rune index to int
     29 function code(s) {
     30 	x = 0;
     31 	for(i = 1; i <= length(s); i++) {
     32 		c = substr(s, i, 1);
     33 		x = (x*16) + hex[c];
     34 	}
     35 	return x;
     36 }
     37 
     38 # generate 'is<name>rune' unicode lookup function
     39 function mkis(name, runev, runec) {
     40 	rune1c = 0;
     41 	rune2c = 0;
     42 
     43 	# sort rune groups into singletons and ranges
     44 	for(j = k = 0; j < runec; j++) {
     45 		if(j+1 == runec || code(runev[j+1]) != code(runev[j])+1) {
     46 			if(j == k) {
     47 				rune1v[rune1c] = runev[j];
     48 				rune1c++;
     49 			}
     50 			else {
     51 				rune2v0[rune2c] = runev[k];
     52 				rune2v1[rune2c] = runev[j];
     53 				rune2c++;
     54 			}
     55 			k = j+1;
     56 		}
     57 	}
     58 	# generate list of ranges
     59 	if(rune2c > 0) {
     60 		print "static Rune "name"2[][2] = {";
     61 		for(j = 0; j < rune2c; j++) {
     62 			print "\t{ 0x"rune2v0[j]", 0x"rune2v1[j]" },";
     63 		}
     64 		print "};\n";
     65 	}
     66 	# generate list of singletons
     67 	if(rune1c > 0) {
     68 		print "static Rune "name"1[] = {";
     69 		for(j = 0; j < rune1c; j++) {
     70 			print "\t0x"rune1v[j]",";
     71 		}
     72 		print "};\n";
     73 	}
     74 	# generate lookup function
     75 	print "int\nis"name"rune(Rune r)\n{";
     76 	if(rune2c > 0)
     77 		print "\tif(bsearch(&r, "name"2, nelem("name"2), sizeof *"name"2, &rune2cmp))\n\t\treturn 1;";
     78 	if(rune1c > 0)
     79 		print "\tif(bsearch(&r, "name"1, nelem("name"1), sizeof *"name"1, &rune1cmp))\n\t\treturn 1;";
     80 	print "\treturn 0;\n}\n";
     81 }