Returns the quoted string (Includes escape character for any single quotes) E.g. DONT -> 'DONT' DON'T -> 'DON\'T'
| 2045 | // E.g. DONT -> 'DONT' |
| 2046 | // DON'T -> 'DON\'T' |
| 2047 | FORCE_INLINE |
| 2048 | const char* quote_utf8(gdv_int64 context, const char* in, gdv_int32 in_len, |
| 2049 | gdv_int32* out_len) { |
| 2050 | if (in_len <= 0) { |
| 2051 | *out_len = 0; |
| 2052 | return ""; |
| 2053 | } |
| 2054 | |
| 2055 | int32_t double_len = 0; |
| 2056 | // Test multiply overflow for in_len |
| 2057 | if (ARROW_PREDICT_FALSE( |
| 2058 | arrow::internal::MultiplyWithOverflow(2, in_len, &double_len))) { |
| 2059 | gdv_fn_context_set_error_msg(context, "Memory allocation size too large."); |
| 2060 | *out_len = 0; |
| 2061 | return ""; |
| 2062 | } |
| 2063 | |
| 2064 | int32_t alloc_length = 0; |
| 2065 | // Test add overflow for in_len |
| 2066 | if (ARROW_PREDICT_FALSE( |
| 2067 | arrow::internal::AddWithOverflow(2, double_len, &alloc_length))) { |
| 2068 | gdv_fn_context_set_error_msg(context, "Memory allocation size too large."); |
| 2069 | *out_len = 0; |
| 2070 | return ""; |
| 2071 | } |
| 2072 | |
| 2073 | // try to allocate double size output string (worst case) |
| 2074 | auto out = reinterpret_cast<char*>(gdv_fn_context_arena_malloc(context, alloc_length)); |
| 2075 | if (out == nullptr) { |
| 2076 | gdv_fn_context_set_error_msg(context, "Could not allocate memory for output string."); |
| 2077 | *out_len = 0; |
| 2078 | return ""; |
| 2079 | } |
| 2080 | // The output string should start with a single quote |
| 2081 | out[0] = '\''; |
| 2082 | gdv_int32 counter = 1; |
| 2083 | for (int i = 0; i < in_len; i++) { |
| 2084 | if (memcmp(in + i, "'", 1) == 0) { |
| 2085 | out[counter] = '\\'; |
| 2086 | counter++; |
| 2087 | out[counter] = '\''; |
| 2088 | } else { |
| 2089 | out[counter] = in[i]; |
| 2090 | } |
| 2091 | counter++; |
| 2092 | } |
| 2093 | out[counter] = '\''; |
| 2094 | *out_len = counter + 1; |
| 2095 | return out; |
| 2096 | } |
| 2097 | |
| 2098 | FORCE_INLINE |
| 2099 | gdv_int32 evaluate_return_char_length(gdv_int32 text_len, gdv_int32 actual_text_len, |