github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/golang/text/secure/precis/gen.go (about)

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Unicode table generator.
     6  // Data read from the web.
     7  
     8  // +build ignore
     9  
    10  package main
    11  
    12  import (
    13  	"flag"
    14  	"log"
    15  	"unicode"
    16  	"unicode/utf8"
    17  
    18  	"github.com/insionng/yougam/libraries/x/text/internal/gen"
    19  	"github.com/insionng/yougam/libraries/x/text/internal/triegen"
    20  	"github.com/insionng/yougam/libraries/x/text/internal/ucd"
    21  	"github.com/insionng/yougam/libraries/x/text/unicode/norm"
    22  	"github.com/insionng/yougam/libraries/x/text/unicode/rangetable"
    23  )
    24  
    25  var assigned, disallowedRunes *unicode.RangeTable
    26  
    27  func main() {
    28  	gen.Init()
    29  
    30  	// Load data
    31  	runes := []rune{}
    32  	ucd.Parse(gen.OpenUCDFile("DerivedCoreProperties.txt"), func(p *ucd.Parser) {
    33  		if p.String(1) == "Default_Ignorable_Code_Point" {
    34  			runes = append(runes, p.Rune(0))
    35  		}
    36  	})
    37  	ucd.Parse(gen.OpenUCDFile("HangulSyllableType.txt"), func(p *ucd.Parser) {
    38  		if p.String(1) == "LVT" {
    39  			runes = append(runes, p.Rune(0))
    40  		}
    41  	})
    42  
    43  	disallowedRunes = rangetable.New(runes...)
    44  	assigned = rangetable.Assigned(unicode.Version)
    45  
    46  	writeTables()
    47  	gen.Repackage("gen_trieval.go", "trieval.go", "precis")
    48  }
    49  
    50  var outputFile = flag.String("output", "tables.go", "output file for generated tables; default tables.go")
    51  
    52  // The Exceptions class as defined in RFC 5892
    53  var exceptions = map[uint32]property{
    54  	0x00DF: pValid,
    55  	0x03C2: pValid,
    56  	0x06FD: pValid,
    57  	0x06FE: pValid,
    58  	0x0F0B: pValid,
    59  	0x3007: pValid,
    60  	0x00B7: contextO,
    61  	0x0375: contextO,
    62  	0x05F3: contextO,
    63  	0x05F4: contextO,
    64  	0x30FB: contextO,
    65  	0x0660: contextO,
    66  	0x0661: contextO,
    67  	0x0662: contextO,
    68  	0x0663: contextO,
    69  	0x0664: contextO,
    70  	0x0665: contextO,
    71  	0x0666: contextO,
    72  	0x0667: contextO,
    73  	0x0668: contextO,
    74  	0x0669: contextO,
    75  	0x06F0: contextO,
    76  	0x06F1: contextO,
    77  	0x06F2: contextO,
    78  	0x06F3: contextO,
    79  	0x06F4: contextO,
    80  	0x06F5: contextO,
    81  	0x06F6: contextO,
    82  	0x06F7: contextO,
    83  	0x06F8: contextO,
    84  	0x06F9: contextO,
    85  	0x0640: disallowed,
    86  	0x07FA: disallowed,
    87  	0x302E: disallowed,
    88  	0x302F: disallowed,
    89  	0x3031: disallowed,
    90  	0x3032: disallowed,
    91  	0x3033: disallowed,
    92  	0x3034: disallowed,
    93  	0x3035: disallowed,
    94  	0x303B: disallowed,
    95  }
    96  
    97  func isLetterDigits(r rune) bool {
    98  	return unicode.In(r,
    99  		unicode.Ll, unicode.Lu, unicode.Lm, unicode.Lo, // Letters
   100  		unicode.Mn, unicode.Mc, // Modifiers
   101  		unicode.Nd, // Digits
   102  	)
   103  }
   104  
   105  func isIdDisAndFreePVal(r rune) bool {
   106  	return unicode.In(r,
   107  		unicode.Lt, unicode.Nl, unicode.No, // Other letters / numbers
   108  		unicode.Me,                                     // Modifiers
   109  		unicode.Zs,                                     // Spaces
   110  		unicode.Sm, unicode.Sc, unicode.Sk, unicode.So, // Symbols
   111  		unicode.Pc, unicode.Pd, unicode.Ps, unicode.Pe,
   112  		unicode.Pi, unicode.Pf, unicode.Po, // Punctuation
   113  	)
   114  }
   115  
   116  func isHasCompat(r rune) bool {
   117  	return !norm.NFKC.IsNormalString(string(r))
   118  }
   119  
   120  func writeTables() {
   121  	propTrie := triegen.NewTrie("derivedProperties")
   122  	w := gen.NewCodeWriter()
   123  	defer w.WriteGoFile(*outputFile, "precis")
   124  	gen.WriteUnicodeVersion(w)
   125  
   126  	// Iterate over all the runes...
   127  	for i := uint32(0); i < unicode.MaxRune; i++ {
   128  		r := rune(i)
   129  
   130  		if !utf8.ValidRune(r) {
   131  			continue
   132  		}
   133  
   134  		p, ok := exceptions[i]
   135  		switch {
   136  		case ok:
   137  		case !unicode.In(r, assigned):
   138  			p = unassigned
   139  		case r >= 33 && r <= 126: // Is ASCII 7
   140  			p = pValid
   141  		case r == 0x200C || r == 0x200D: // Is join control
   142  			p = contextJ
   143  		case unicode.In(r, disallowedRunes, unicode.Cc):
   144  			p = disallowed
   145  		case isHasCompat(r):
   146  			p = idDis | freePVal
   147  		case isLetterDigits(r):
   148  			p = pValid
   149  		case isIdDisAndFreePVal(r):
   150  			p = idDis | freePVal
   151  		default:
   152  			p = disallowed
   153  		}
   154  		propTrie.Insert(r, uint64(p))
   155  	}
   156  	sz, err := propTrie.Gen(w)
   157  	if err != nil {
   158  		log.Fatal(err)
   159  	}
   160  	w.Size += sz
   161  }