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..14 unused
34//       13     mayNeedNorm
35//       12..11 attributes
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	// Attributes
57	attributesMask = 0x1800
58	viramaModifier = 0x1800
59	modifier       = 0x1000
60	rtl            = 0x0800
61
62	mayNeedNorm = 0x2000
63)
64
65// A category corresponds to a category defined in the IDNA mapping table.
66type category uint16
67
68const (
69	unknown              category = 0 // not currently defined in unicode.
70	mapped               category = 1
71	disallowedSTD3Mapped category = 2
72	deviation            category = 3
73)
74
75const (
76	valid               category = 0x08
77	validNV8            category = 0x18
78	validXV8            category = 0x28
79	disallowed          category = 0x40
80	disallowedSTD3Valid category = 0x80
81	ignored             category = 0xC0
82)
83
84// join types and additional rune information
85const (
86	joiningL = (iota + 1)
87	joiningD
88	joiningT
89	joiningR
90
91	//the following types are derived during processing
92	joinZWJ
93	joinZWNJ
94	joinVirama
95	numJoinTypes
96)
97
98func (c info) isMapped() bool {
99	return c&0x3 != 0
100}
101
102func (c info) category() category {
103	small := c & catSmallMask
104	if small != 0 {
105		return category(small)
106	}
107	return category(c & catBigMask)
108}
109
110func (c info) joinType() info {
111	if c.isMapped() {
112		return 0
113	}
114	return (c >> joinShift) & joinMask
115}
116
117func (c info) isModifier() bool {
118	return c&(modifier|catSmallMask) == modifier
119}
120
121func (c info) isViramaModifier() bool {
122	return c&(attributesMask|catSmallMask) == viramaModifier
123}
124