1// Copyright 2016 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// +build ignore
6
7package main
8
9// This file contains definitions for interpreting the trie value of the idna
10// trie generated by "go run gen*.go". It is shared by both the generator
11// program and the resultant package. Sharing is achieved by the generator
12// copying gen_trieval.go to trieval.go and changing what's above this comment.
13
14// info holds information from the IDNA mapping table for a single rune. It is
15// the value returned by a trie lookup. In most cases, all information fits in
16// a 16-bit value. For mappings, this value may contain an index into a slice
17// with the mapped string. Such mappings can consist of the actual mapped value
18// or an XOR pattern to be applied to the bytes of the UTF8 encoding of the
19// input rune. This technique is used by the cases packages and reduces the
20// table size significantly.
21//
22// The per-rune values have the following format:
23//
24//   if mapped {
25//     if inlinedXOR {
26//       15..13 inline XOR marker
27//       12..11 unused
28//       10..3  inline XOR mask
29//     } else {
30//       15..3  index into xor or mapping table
31//     }
32//   } else {
33//       15..13 unused
34//           12 modifier (including virama)
35//           11 virama modifier
36//       10..8  joining type
37//        7..3  category type
38//   }
39//      2  use xor pattern
40//   1..0  mapped category
41//
42// See the definitions below for a more detailed description of the various
43// bits.
44type info uint16
45
46const (
47	catSmallMask = 0x3
48	catBigMask   = 0xF8
49	indexShift   = 3
50	xorBit       = 0x4    // interpret the index as an xor pattern
51	inlineXOR    = 0xE000 // These bits are set if the XOR pattern is inlined.
52
53	joinShift = 8
54	joinMask  = 0x07
55
56	viramaModifier = 0x0800
57	modifier       = 0x1000
58)
59
60// A category corresponds to a category defined in the IDNA mapping table.
61type category uint16
62
63const (
64	unknown              category = 0 // not defined currently in unicode.
65	mapped               category = 1
66	disallowedSTD3Mapped category = 2
67	deviation            category = 3
68)
69
70const (
71	valid               category = 0x08
72	validNV8            category = 0x18
73	validXV8            category = 0x28
74	disallowed          category = 0x40
75	disallowedSTD3Valid category = 0x80
76	ignored             category = 0xC0
77)
78
79// join types and additional rune information
80const (
81	joiningL = (iota + 1)
82	joiningD
83	joiningT
84	joiningR
85
86	//the following types are derived during processing
87	joinZWJ
88	joinZWNJ
89	joinVirama
90	numJoinTypes
91)
92
93func (c info) isMapped() bool {
94	return c&0x3 != 0
95}
96
97func (c info) category() category {
98	small := c & catSmallMask
99	if small != 0 {
100		return category(small)
101	}
102	return category(c & catBigMask)
103}
104
105func (c info) joinType() info {
106	if c.isMapped() {
107		return 0
108	}
109	return (c >> joinShift) & joinMask
110}
111
112func (c info) isModifier() bool {
113	return c&(modifier|catSmallMask) == modifier
114}
115
116func (c info) isViramaModifier() bool {
117	return c&(viramaModifier|catSmallMask) == viramaModifier
118}
119