github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/native/scanning.h (about) 1 /** 2 * Copyright 2023 CloudWeGo Authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "native.h" 18 19 #ifndef SCANNING_H 20 #define SCANNING_H 21 22 #define FSM_VAL 0 23 #define FSM_ARR 1 24 #define FSM_OBJ 2 25 #define FSM_KEY 3 26 #define FSM_ELEM 4 27 #define FSM_ARR_0 5 28 #define FSM_OBJ_0 6 29 30 #define FSM_DROP(v) (v)->sp-- 31 #define FSM_REPL(v, t) (v)->vt[(v)->sp - 1] = (t) 32 33 #define FSM_CHAR(c) \ 34 do \ 35 { \ 36 if (ch != (c)) \ 37 return -ERR_INVAL; \ 38 } while (0) 39 #define FSM_XERR(v) \ 40 do \ 41 { \ 42 long r = (v); \ 43 if (r < 0) \ 44 return r; \ 45 } while (0) 46 47 #define VALID_DEFAULT 0 // basic validate, except JSON string. 48 #define VALID_FULL 1 // also validate JSON string, including control chars or invalid UTF-8. 49 50 #define check_bits(mv) \ 51 if (unlikely((v = mv & (mv - 1)) != 0)) \ 52 { \ 53 return -(sp - ss + __builtin_ctz(v) + 1); \ 54 } 55 56 #define check_sidx(iv) \ 57 if (likely(iv == -1)) \ 58 { \ 59 iv = sp - ss - 1; \ 60 } \ 61 else \ 62 { \ 63 return -(sp - ss); \ 64 } 65 66 #define check_vidx(iv, mv) \ 67 if (mv != 0) \ 68 { \ 69 if (likely(iv == -1)) \ 70 { \ 71 iv = sp - ss + __builtin_ctz(mv); \ 72 } \ 73 else \ 74 { \ 75 return -(sp - ss + __builtin_ctz(mv) + 1); \ 76 } \ 77 } 78 79 #define FSM_INIT(self, v) \ 80 self->sp = 1; \ 81 self->vt[0] = v; 82 83 #define FSM_PUSH(self, v) \ 84 if (self->sp >= MAX_RECURSE) \ 85 { \ 86 return -ERR_RECURSE_MAX; \ 87 } \ 88 else \ 89 { \ 90 self->vt[self->sp++] = v; \ 91 long r = 0; \ 92 } 93 94 #define FSM_ZERO(e) \ 95 do \ 96 { \ 97 int re = e; \ 98 if (re != 0) \ 99 { \ 100 return re; \ 101 } \ 102 } while (0) 103 104 char advance_ns(const GoString *src, long *p); 105 106 int64_t advance_dword(const GoString *src, long *p, long dec, int64_t ret, uint32_t val); 107 108 #endif // SCANNING_H