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