github.com/bytedance/sonic@v1.11.7-0.20240517092252-d2edb31b167b/native/quote.c (about) 1 #include "parsing.h" 2 3 ssize_t quote(const char *sp, ssize_t nb, char *dp, ssize_t *dn, uint64_t flags) { 4 ssize_t nd = *dn; 5 const char * ds = dp; 6 const char * ss = sp; 7 const quoted_t * tab; 8 9 /* select quoting table */ 10 if (!(flags & F_DBLUNQ)) { 11 tab = _SingleQuoteTab; 12 } else { 13 tab = _DoubleQuoteTab; 14 } 15 16 if (*dn >= nb * MAX_ESCAPED_BYTES) { 17 *dn = memcchr_quote_unsafe(sp, nb, dp, tab); 18 return nb; 19 } 20 21 /* find the special characters, copy on the fly */ 22 while (nb != 0) { 23 int nc; 24 uint8_t ch; 25 ssize_t rb = memcchr_quote(sp, nb, dp, nd); 26 27 /* not enough buffer space */ 28 if (rb < 0) { 29 *dn = dp - ds - rb - 1; 30 return -(sp - ss - rb - 1) - 1; 31 } 32 33 /* skip already copied bytes */ 34 sp += rb; 35 dp += rb; 36 nb -= rb; 37 nd -= rb; 38 39 /* get the escape entry, handle consecutive quotes */ 40 while (nb != 0) { 41 ch = *(uint8_t *)sp; 42 nc = tab[ch].n; 43 44 /* check for escape character */ 45 if (nc == 0) { 46 break; 47 } 48 49 /* check for buffer space */ 50 if (nc > nd) { 51 *dn = dp - ds; 52 return -(sp - ss) - 1; 53 } 54 55 /* copy the quoted value */ 56 memcpy_p8(dp, tab[ch].s, nc); 57 sp++; 58 nb--; 59 dp += nc; 60 nd -= nc; 61 } 62 } 63 64 /* all done */ 65 *dn = dp - ds; 66 return sp - ss; 67 }