+ /* In overflows, this keeps track of how much to multiply the overflowed NV
+ * by as we continue to parse the remaining digits */
+ NV factor = 0;
+
+ /* This function unifies the core of grok_bin, grok_oct, and grok_hex. It
+ * is optimized for hex conversion. For example, it uses XDIGIT_VALUE to
+ * find the numeric value of a digit. That requires more instructions than
+ * OCTAL_VALUE would, but gives the same result for the narrowed range of
+ * octal digits; same for binary. If it were ever critical to squeeze more
+ * performance from this, the function could become grok_hex, and a regen
+ * perl script could scan it and write out two edited copies for the other
+ * two functions. That would improve the performance of all three
+ * somewhat. Besides eliminating XDIGIT_VALUE for the other two, extra
+ * parameters are now passed to this to avoid conditionals. Those could
+ * become declared consts, like:
+ * const U8 base = 16;
+ * const U8 base = 8;
+ * ...
+ */
+
+ PERL_ARGS_ASSERT_GROK_BIN_OCT_HEX;
+
+ ASSUME(inRANGE(shift, 1, 4) && shift != 2);
+
+ /* Clear output flags; unlikely to find a problem that sets them */
+ *flags = 0;
+
+ if (!(input_flags & PERL_SCAN_DISALLOW_PREFIX)) {
+
+ /* strip off leading b or 0b; x or 0x.
+ for compatibility silently suffer "b" and "0b" as valid binary; "x"
+ and "0x" as valid hex numbers. */
+ if (len >= 1) {
+ if (isALPHA_FOLD_EQ(s0[0], prefix)) {
+ s0++;
+ len--;
+ }
+ else if (len >= 2 && s0[0] == '0' && (isALPHA_FOLD_EQ(s0[1], prefix))) {
+ s0+=2;
+ len-=2;
+ }
+ }
+ }
+
+ s = s0; /* s0 potentially advanced from 'start' */
+
+ /* Unroll the loop so that the first 8 digits are branchless except for the
+ * switch. A ninth hex one overflows a 32 bit word. */
+ switch (len) {
+ case 0:
+ return 0;
+ default:
+ if (UNLIKELY(! _generic_isCC(*s, class_bit))) break;
+ value = (value << shift) | XDIGIT_VALUE(*s);
+ s++;
+ /* FALLTHROUGH */
+ case 7:
+ if (UNLIKELY(! _generic_isCC(*s, class_bit))) break;
+ value = (value << shift) | XDIGIT_VALUE(*s);
+ s++;
+ /* FALLTHROUGH */
+ case 6:
+ if (UNLIKELY(! _generic_isCC(*s, class_bit))) break;
+ value = (value << shift) | XDIGIT_VALUE(*s);
+ s++;
+ /* FALLTHROUGH */
+ case 5:
+ if (UNLIKELY(! _generic_isCC(*s, class_bit))) break;
+ value = (value << shift) | XDIGIT_VALUE(*s);
+ s++;
+ /* FALLTHROUGH */
+ case 4:
+ if (UNLIKELY(! _generic_isCC(*s, class_bit))) break;
+ value = (value << shift) | XDIGIT_VALUE(*s);
+ s++;
+ /* FALLTHROUGH */
+ case 3:
+ if (UNLIKELY(! _generic_isCC(*s, class_bit))) break;
+ value = (value << shift) | XDIGIT_VALUE(*s);
+ s++;
+ /* FALLTHROUGH */
+ case 2:
+ if (UNLIKELY(! _generic_isCC(*s, class_bit))) break;
+ value = (value << shift) | XDIGIT_VALUE(*s);
+ s++;
+ /* FALLTHROUGH */
+ case 1:
+ if (UNLIKELY(! _generic_isCC(*s, class_bit))) break;
+ value = (value << shift) | XDIGIT_VALUE(*s);
+
+ if (LIKELY(len <= 8)) {
+ return value;
+ }
+
+ s++;
+ break;
+ }
+
+ bytes_so_far = s - s0;
+ factor = shift << bytes_so_far;
+ len -= bytes_so_far;