-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquote.go
More file actions
173 lines (145 loc) · 4.87 KB
/
quote.go
File metadata and controls
173 lines (145 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//go:build goexperiment.simd && amd64
package simdcsv
import (
"bytes"
"math/bits"
"simd/archsimd"
)
// =============================================================================
// Whitespace Handling
// =============================================================================
// isWhitespace reports whether b is a space or tab character.
func isWhitespace(b byte) bool {
return b == ' ' || b == '\t'
}
// skipLeadingWhitespace returns the number of leading whitespace bytes.
func skipLeadingWhitespace(data []byte) int {
for i := 0; i < len(data); i++ {
if !isWhitespace(data[i]) {
return i
}
}
return len(data)
}
// =============================================================================
// Quote Detection
// =============================================================================
// isQuotedFieldStart checks if data starts with a quote, optionally after whitespace.
// Returns (isQuoted, quoteOffset) where quoteOffset is the position of the opening quote.
func isQuotedFieldStart(data []byte, trimLeadingSpace bool) (bool, int) {
if len(data) == 0 {
return false, 0
}
if data[0] == '"' {
return true, 0
}
if trimLeadingSpace {
offset := skipLeadingWhitespace(data)
if offset > 0 && offset < len(data) && data[offset] == '"' {
return true, offset
}
}
return false, 0
}
// isEscapedQuote checks if the quote at position i is escaped (followed by another quote).
func isEscapedQuote(data []byte, i int) bool {
return i+1 < len(data) && data[i+1] == '"'
}
// =============================================================================
// Closing Quote Search - Unified Dispatch
// =============================================================================
// findClosingQuote finds the closing quote in a quoted field.
// Returns the index of the closing quote, or -1 if not found.
// Handles escaped double quotes ("").
func findClosingQuote(data []byte, startAfterOpenQuote int) int {
remaining := len(data) - startAfterOpenQuote
if shouldUseSIMD(remaining) {
return findClosingQuoteSIMD(data, startAfterOpenQuote)
}
return findClosingQuoteScalar(data, startAfterOpenQuote)
}
// =============================================================================
// Closing Quote Search - Scalar Implementation
// =============================================================================
// findClosingQuoteScalar finds the closing quote using scalar operations.
func findClosingQuoteScalar(data []byte, startAfterOpenQuote int) int {
for i := startAfterOpenQuote; i < len(data); {
next := bytes.IndexByte(data[i:], '"')
if next == -1 {
return -1
}
pos := i + next
if isEscapedQuote(data, pos) {
i = pos + 2
continue
}
return pos
}
return -1
}
// =============================================================================
// Closing Quote Search - SIMD Implementation
// =============================================================================
// findClosingQuoteSIMD uses AVX-512 to find the closing quote in 64-byte chunks.
func findClosingQuoteSIMD(data []byte, startAfterOpenQuote int) int {
int8Data := bytesToInt8Slice(data)
i := startAfterOpenQuote
for i+simdChunkSize <= len(data) {
chunk := archsimd.LoadInt8x64Slice(int8Data[i : i+simdChunkSize])
mask := chunk.Equal(cachedQuoteCmp).ToBits()
if mask == 0 {
i += simdChunkSize
continue
}
result, newI, done := processQuoteMask(data, i, mask)
if result >= 0 {
return result
}
if done {
break
}
i = newI
}
return findClosingQuoteScalar(data, i)
}
// processQuoteMask processes quote positions in a SIMD chunk mask.
// Returns (closingQuoteIdx, newPosition, shouldExitLoop).
func processQuoteMask(data []byte, chunkStart int, mask uint64) (int, int, bool) {
// Fast path: no adjacent quotes in this chunk, so first quote closes.
if mask&(mask<<1) == 0 {
pos := bits.TrailingZeros64(mask)
if pos == simdChunkSize-1 {
newPos := chunkStart + simdChunkSize
if newPos < len(data) && data[newPos] == '"' {
// Boundary double quote → skip both
return -1, newPos + 1, false
}
// Closing quote at boundary
return chunkStart + pos, chunkStart, false
}
return chunkStart + pos, chunkStart, false
}
for mask != 0 {
pos := bits.TrailingZeros64(mask)
// Boundary case: quote at last position of chunk
if pos == simdChunkSize-1 {
newPos := chunkStart + simdChunkSize
if newPos < len(data) && data[newPos] == '"' {
// Boundary double quote → skip both
return -1, newPos + 1, false
}
// Closing quote at boundary
return chunkStart + pos, chunkStart, false
}
// Check if next bit is also set (double quote "")
nextBit := uint64(1) << (pos + 1)
if mask&nextBit != 0 {
// Double quote → clear both bits and continue
mask &^= (uint64(1) << pos) | nextBit
continue
}
// Single quote = closing quote
return chunkStart + pos, chunkStart, false
}
return -1, chunkStart + simdChunkSize, false
}