1 // Three stage4 code generation holes that every one of them produced a WRONG
2 // ANSWER rather than an error, found while converting the Unicode tables.
3 //
4 // 1. string([]rune) and []rune(string) were a reinterpretation of the three
5 // word header, so string([]rune{'a','b','c'}) was a 3-byte string holding
6 // "a\0\0" - the first three bytes of the rune array.
7 // 2. A keyed composite literal whose key was a NAMED CONSTANT dropped the key
8 // and stored the element at the running position, so a table keyed
9 // ruleInitial, ruleRTL, ruleRTLFinal, ruleLTR, ... came out shifted by one
10 // and every LTR label failed the bidi rule.
11 // 3. A func-typed field promoted from an embedded struct was not found at all:
12 // the call reported "undefined field", which is why idna.Profile.process's
13 // `p.mapping(p, s)` could not be built.
14 //
15 // Everything is in lib/ because package main has no frame arena, so the return
16 // path these functions exercise would not run at all in main.
17 package lib
18 19 // State values double as the keys of the table below.
20 const (
21 stateStart stateId = iota
22 stateLTR
23 stateLTRFinal
24 stateRTL
25 stateInvalid
26 )
27 28 type stateId uint8
29 30 type transition struct {
31 next stateId
32 mask uint16
33 }
34 35 // table is keyed by NAMED CONSTANTS, in an order that is not the numeric order:
36 // a positional reading puts stateRTL's row at index 1 and breaks the walk.
37 func table() (t [6][2]transition) {
38 return [6][2]transition{
39 stateStart: {
40 {stateLTRFinal, 1},
41 {stateRTL, 2},
42 },
43 stateRTL: {
44 {stateInvalid, 0},
45 {stateInvalid, 0},
46 },
47 stateLTR: {
48 {stateLTRFinal, 1},
49 {stateLTR, 4},
50 },
51 stateLTRFinal: {
52 {stateLTRFinal, 1},
53 {stateLTR, 4},
54 },
55 }
56 }
57 58 // Walk reports the state the machine ends in after following masks left to
59 // right. For codes {1, 1, 1} that is stateLTRFinal (2); a table read
60 // positionally ends in stateInvalid (4).
61 func Walk(codes []uint16) (st stateId) {
62 st = stateStart
63 for _, c := range codes {
64 tr := table()[st]
65 switch {
66 case tr[0].mask&c != 0:
67 st = tr[0].next
68 case tr[1].mask&c != 0:
69 st = tr[1].next
70 default:
71 st = stateInvalid
72 }
73 }
74 return st
75 }
76 77 // keyedSlice uses a named constant key in a SLICE literal, which has the same
78 // rule and the same failure.
79 func keyedSlice() (s []int32) {
80 return []int32{
81 stateStart: 10,
82 stateLTRFinal: 40,
83 stateInvalid: 50,
84 }
85 }
86 87 func KeyedSliceAt(i int32) (v int32, n int32) {
88 s := keyedSlice()
89 return s[i], int32(len(s))
90 }
91 92 // RunesToBytes converts a rune slice to a string: UTF-8 encoding, not a
93 // reinterpretation of the header.
94 func RunesToBytes(r []rune) (s string) {
95 return string(r)
96 }
97 98 // BytesToRunes converts a string to a rune slice: UTF-8 decoding, not one
99 // "rune" per byte.
100 func BytesToRunes(s string) (r []rune) {
101 return []rune(s)
102 }
103 104 // options mirrors the embedding shape idna.Profile uses: the func field lives
105 // on the embedded struct and is called through the outer one.
106 type options struct {
107 mapFn func(p *Profile, s []byte) (b []byte, n int32)
108 }
109 110 type Profile struct {
111 options
112 }
113 114 func lower(s []byte) (b []byte) {
115 b = []byte{:len(s)}
116 for i := int32(0); i < int32(len(s)); i++ {
117 c := s[i]
118 if 'A' <= c && c <= 'Z' {
119 c = c + 32
120 }
121 b[i] = c
122 }
123 return b
124 }
125 126 func mapLower(p *Profile, s []byte) (b []byte, n int32) {
127 return lower(s), int32(len(s))
128 }
129 130 func NewProfile() (p *Profile) {
131 return &Profile{options{mapFn: mapLower}}
132 }
133 134 // ThroughField calls the PROMOTED func field, which is the construct that used
135 // to be reported as an undefined field.
136 func ThroughField(s []byte) (b []byte, n int32) {
137 p := NewProfile()
138 var err bool
139 if p.mapFn != nil {
140 b, n = p.mapFn(p, s)
141 }
142 if err {
143 return s, 0
144 }
145 return b, n
146 }
147