@@ -123,44 +123,68 @@ class json_structural_scanner {
123123 really_inline void scan (const uint8_t *buf, const size_t len, utf8_checker &utf8_checker);
124124};
125125
126- // return a bitvector indicating where we have characters that end an odd-length
127- // sequence of backslashes (and thus change the behavior of the next character
128- // to follow). A even-length sequence of backslashes, and, for that matter, the
129- // largest even-length prefix of our odd-length sequence of backslashes, simply
130- // modify the behavior of the backslashes themselves.
131- // We also update the prev_iter_ends_odd_backslash reference parameter to
132- // indicate whether we end an iteration on an odd-length sequence of
133- // backslashes, which modifies our subsequent search for odd-length
134- // sequences of backslashes in an obvious way.
135- really_inline uint64_t follows_odd_sequence_of (const uint64_t match, uint64_t &overflow) {
126+ // Routines to print masks and text for debugging bitmask operations
127+ UNUSED static char * format_input_text (const simd8x64<uint8_t > in) {
128+ static char *buf = (char *)malloc (sizeof (simd8x64<uint8_t >) + 1 );
129+ in.store ((uint8_t *)buf);
130+ for (size_t i=0 ; i<sizeof (simd8x64<uint8_t >); i++) {
131+ if (buf[i] < ' ' ) { buf[i] = ' _' ; }
132+ }
133+ buf[sizeof (simd8x64<uint8_t >)] = ' \0 ' ;
134+ return buf;
135+ }
136+
137+ UNUSED static char * format_mask (uint64_t mask) {
138+ static char *buf = (char *)malloc (64 + 1 );
139+ for (size_t i=0 ; i<64 ; i++) {
140+ buf[i] = (mask & (size_t (1 ) << i)) ? ' X' : ' ' ;
141+ }
142+ buf[64 ] = ' \0 ' ;
143+ return buf;
144+ }
145+
146+ //
147+ // Finds escaped characters (characters following \).
148+ //
149+ // Handles runs of backslashes like \\\" and \\\\" correctly (yielding 0101 and 01010, respectively).
150+ //
151+ // Does this by:
152+ // - Shift the escape mask to get potentially escaped characters (characters after backslashes).
153+ // - Mask escaped sequences that start on *even* bits with 1010101010 (odd bits are escaped, even bits are not)
154+ // - Mask escaped sequences that start on *odd* bits with 0101010101 (even bits are escaped, odd bits are not)
155+ //
156+ // To distinguish between escaped sequences starting on even/odd bits, it finds the start of all
157+ // escape sequences, filters out the ones that start on even bits, and adds that to the mask of
158+ // escape sequences. This causes the addition to clear out the sequences starting on odd bits (since
159+ // the start bit causes a carry), and leaves even-bit sequences alone.
160+ //
161+ // Example:
162+ //
163+ // text | \\\ | \\\"\\\" \\\" \\"\\" |
164+ // escape | xxx | xx xxx xxx xx xx | Removed overflow backslash; will | it into follows_escape
165+ // odd_starts | x | x x x | escape & ~even_bits & ~follows_escape
166+ // even_seq | c| cxxx c xx c | c = carry bit -- will be masked out later
167+ // invert_mask | | cxxx c xx c| even_seq << 1
168+ // follows_escape | xx | x xx xxx xxx xx xx | Includes overflow bit
169+ // escaped | x | x x x x x x x x |
170+ // desired | x | x x x x x x x x |
171+ // text | \\\ | \\\"\\\" \\\" \\"\\" |
172+ //
173+ really_inline uint64_t find_escaped (uint64_t escape, uint64_t &escaped_overflow) {
174+ // If there was overflow, pretend the first character isn't a backslash
175+ escape &= ~escaped_overflow;
176+ uint64_t follows_escape = escape << 1 | escaped_overflow;
177+
178+ // Get sequences starting on even bits by clearing out the odd series using +
136179 const uint64_t even_bits = 0x5555555555555555ULL ;
137- const uint64_t odd_bits = ~even_bits;
138- uint64_t start_edges = match & ~(match << 1 );
139- /* flip lowest if we have an odd-length run at the end of the prior
140- * iteration */
141- uint64_t even_start_mask = even_bits ^ overflow;
142- uint64_t even_starts = start_edges & even_start_mask;
143- uint64_t odd_starts = start_edges & ~even_start_mask;
144- uint64_t even_carries = match + even_starts;
145-
146- uint64_t odd_carries;
147- /* must record the carry-out of our odd-carries out of bit 63; this
148- * indicates whether the sense of any edge going to the next iteration
149- * should be flipped */
150- bool new_overflow = add_overflow (match, odd_starts, &odd_carries);
151-
152- odd_carries |= overflow; /* push in bit zero as a
153- * potential end if we had an
154- * odd-numbered run at the
155- * end of the previous
156- * iteration */
157- overflow = new_overflow ? 0x1ULL : 0x0ULL ;
158- uint64_t even_carry_ends = even_carries & ~match;
159- uint64_t odd_carry_ends = odd_carries & ~match;
160- uint64_t even_start_odd_end = even_carry_ends & odd_bits;
161- uint64_t odd_start_even_end = odd_carry_ends & even_bits;
162- uint64_t odd_ends = even_start_odd_end | odd_start_even_end;
163- return odd_ends;
180+ uint64_t odd_sequence_starts = escape & ~even_bits & ~follows_escape;
181+ uint64_t sequences_starting_on_even_bits;
182+ escaped_overflow = add_overflow (odd_sequence_starts, escape, &sequences_starting_on_even_bits);
183+ uint64_t invert_mask = sequences_starting_on_even_bits << 1 ; // The mask we want to return is the *escaped* bits, not escapes.
184+
185+ // Mask every other backslashed character as an escaped character
186+ // Flip the mask for sequences that start on even bits, to correct them
187+ return (even_bits ^ invert_mask) & follows_escape;
164188}
165189
166190//
@@ -209,7 +233,7 @@ really_inline ErrorValues json_structural_scanner::detect_errors_on_eof(bool str
209233//
210234really_inline uint64_t json_structural_scanner::find_strings (const simd::simd8x64<uint8_t > in) {
211235 const uint64_t backslash = in.eq (' \\ ' );
212- const uint64_t escaped = follows_odd_sequence_of (backslash, prev_escaped);
236+ const uint64_t escaped = find_escaped (backslash, prev_escaped);
213237 const uint64_t quote = in.eq (' "' ) & ~escaped;
214238 // prefix_xor flips on bits inside the string (and flips off the end quote).
215239 const uint64_t in_string = prefix_xor (quote) ^ prev_in_string;
0 commit comments