MCPcopy Create free account
hub / github.com/apache/arrow / byte_substr_binary_int32_int32

Function byte_substr_binary_int32_int32

cpp/src/gandiva/precompiled/string_ops.cc:2552–2599  ·  view source on GitHub ↗

Produces the binary representation of a string y characters long derived by starting at offset 'x' and considering the defined length 'y'. Notice that the offset index may be a negative number (starting from the end of the string), or a positive number starting on index 1. Cases: BYTE_SUBSTR("TestString", 1, 10) => "TestString" BYTE_SUBSTR("TestString", 5, 10) => "String" BYTE_SUBSTR("TestString",

Source from the content-addressed store, hash-verified

2550// BYTE_SUBSTR("TestString", -6, 10) => "String"
2551// BYTE_SUBSTR("TestString", -600, 10) => "TestString"
2552FORCE_INLINE
2553const char* byte_substr_binary_int32_int32(gdv_int64 context, const char* text,
2554 gdv_int32 text_len, gdv_int32 offset,
2555 gdv_int32 length, gdv_int32* out_len) {
2556 // the first offset position for a string is 1, so not consider offset == 0
2557 // also, the length should be always a positive number
2558 if (text_len == 0 || offset == 0 || length <= 0) {
2559 *out_len = 0;
2560 return "";
2561 }
2562
2563 int32_t startPos = 0;
2564 if (offset >= 0) {
2565 startPos = offset - 1;
2566 } else if (text_len + offset >= 0) {
2567 startPos = text_len + offset;
2568 }
2569
2570 // an offset past the end of the text leaves nothing to copy; without this the
2571 // truncation below yields a negative *out_len that memcpy reads as a huge size.
2572 // check before allocating so a past-end offset needs no output buffer at all
2573 if (startPos >= text_len) {
2574 *out_len = 0;
2575 return "";
2576 }
2577
2578 char* ret =
2579 reinterpret_cast<gdv_binary>(gdv_fn_context_arena_malloc(context, text_len));
2580
2581 if (ret == nullptr) {
2582 gdv_fn_context_set_error_msg(context, "Could not allocate memory for output string");
2583 *out_len = 0;
2584 return "";
2585 }
2586
2587 // calculate end position from length and truncate to upper value bounds.
2588 // startPos < text_len is guaranteed above, so text_len - startPos is positive;
2589 // comparing against it avoids the startPos + length overflow when length is
2590 // near INT32_MAX, which would otherwise leave *out_len huge for the memcpy.
2591 if (length > text_len - startPos) {
2592 *out_len = text_len - startPos;
2593 } else {
2594 *out_len = length;
2595 }
2596
2597 memcpy(ret, text + startPos, *out_len);
2598 return ret;
2599}
2600
2601FORCE_INLINE
2602void concat_word(char* out_buf, int* out_idx, const char* in_buf, int in_len,

Callers 1

TESTFunction · 0.85

Calls 2

Tested by 1

TESTFunction · 0.68