github.com/liquid-dev/text@v0.3.3-liquid/width/common_test.go (about)

     1  // Code generated by running "go generate" in github.com/liquid-dev/text. DO NOT EDIT.
     2  
     3  package width
     4  
     5  // This code is shared between the main code generator and the test code.
     6  
     7  import (
     8  	"flag"
     9  	"log"
    10  	"strconv"
    11  	"strings"
    12  
    13  	"github.com/liquid-dev/text/internal/gen"
    14  	"github.com/liquid-dev/text/internal/ucd"
    15  )
    16  
    17  var (
    18  	outputFile = flag.String("out", "tables.go", "output file")
    19  )
    20  
    21  var typeMap = map[string]elem{
    22  	"A":  tagAmbiguous,
    23  	"N":  tagNeutral,
    24  	"Na": tagNarrow,
    25  	"W":  tagWide,
    26  	"F":  tagFullwidth,
    27  	"H":  tagHalfwidth,
    28  }
    29  
    30  // getWidthData calls f for every entry for which it is defined.
    31  //
    32  // f may be called multiple times for the same rune. The last call to f is the
    33  // correct value. f is not called for all runes. The default tag type is
    34  // Neutral.
    35  func getWidthData(f func(r rune, tag elem, alt rune)) {
    36  	// Set the default values for Unified Ideographs. In line with Annex 11,
    37  	// we encode full ranges instead of the defined runes in Unified_Ideograph.
    38  	for _, b := range []struct{ lo, hi rune }{
    39  		{0x4E00, 0x9FFF},   // the CJK Unified Ideographs block,
    40  		{0x3400, 0x4DBF},   // the CJK Unified Ideographs Externsion A block,
    41  		{0xF900, 0xFAFF},   // the CJK Compatibility Ideographs block,
    42  		{0x20000, 0x2FFFF}, // the Supplementary Ideographic Plane,
    43  		{0x30000, 0x3FFFF}, // the Tertiary Ideographic Plane,
    44  	} {
    45  		for r := b.lo; r <= b.hi; r++ {
    46  			f(r, tagWide, 0)
    47  		}
    48  	}
    49  
    50  	inverse := map[rune]rune{}
    51  	maps := map[string]bool{
    52  		"<wide>":   true,
    53  		"<narrow>": true,
    54  	}
    55  
    56  	// We cannot reuse package norm's decomposition, as we need an unexpanded
    57  	// decomposition. We make use of the opportunity to verify that the
    58  	// decomposition type is as expected.
    59  	ucd.Parse(gen.OpenUCDFile("UnicodeData.txt"), func(p *ucd.Parser) {
    60  		r := p.Rune(0)
    61  		s := strings.SplitN(p.String(ucd.DecompMapping), " ", 2)
    62  		if !maps[s[0]] {
    63  			return
    64  		}
    65  		x, err := strconv.ParseUint(s[1], 16, 32)
    66  		if err != nil {
    67  			log.Fatalf("Error parsing rune %q", s[1])
    68  		}
    69  		if inverse[r] != 0 || inverse[rune(x)] != 0 {
    70  			log.Fatalf("Circular dependency in mapping between %U and %U", r, x)
    71  		}
    72  		inverse[r] = rune(x)
    73  		inverse[rune(x)] = r
    74  	})
    75  
    76  	// <rune range>;<type>
    77  	ucd.Parse(gen.OpenUCDFile("EastAsianWidth.txt"), func(p *ucd.Parser) {
    78  		tag, ok := typeMap[p.String(1)]
    79  		if !ok {
    80  			log.Fatalf("Unknown width type %q", p.String(1))
    81  		}
    82  		r := p.Rune(0)
    83  		alt, ok := inverse[r]
    84  		if tag == tagFullwidth || tag == tagHalfwidth && r != wonSign {
    85  			tag |= tagNeedsFold
    86  			if !ok {
    87  				log.Fatalf("Narrow or wide rune %U has no decomposition", r)
    88  			}
    89  		}
    90  		f(r, tag, alt)
    91  	})
    92  }