| 2694 | }; |
| 2695 | |
| 2696 | static inline const char* concat_ws_impl(int64_t context, const char* separator, |
| 2697 | int32_t separator_len, bool separator_validity, |
| 2698 | bool* out_valid, int32_t* out_len, |
| 2699 | std::initializer_list<WordArg> words) { |
| 2700 | *out_len = 0; |
| 2701 | |
| 2702 | // Separator validity check |
| 2703 | if (!separator_validity) { |
| 2704 | *out_valid = false; |
| 2705 | return ""; |
| 2706 | } |
| 2707 | if (separator_len < 0) { |
| 2708 | gdv_fn_context_set_error_msg(context, "Separator length cannot be negative"); |
| 2709 | *out_valid = false; |
| 2710 | return ""; |
| 2711 | } |
| 2712 | |
| 2713 | SafeLengthState state; |
| 2714 | |
| 2715 | // Accumulate all word lengths safely |
| 2716 | for (const WordArg& w : words) { |
| 2717 | if (!safe_accumulate_word(context, state, w.len, w.valid)) { |
| 2718 | *out_len = 0; |
| 2719 | *out_valid = false; |
| 2720 | return ""; |
| 2721 | } |
| 2722 | } |
| 2723 | |
| 2724 | // Add separator lengths |
| 2725 | if (!safe_add_separators(context, &state, separator_len)) { |
| 2726 | return handle_overflow_failure(out_valid, out_len); |
| 2727 | } |
| 2728 | |
| 2729 | // Empty result |
| 2730 | if (state.total_len == 0) { |
| 2731 | return handle_empty_result(out_valid, out_len); |
| 2732 | } |
| 2733 | |
| 2734 | // Allocate memory |
| 2735 | char* out = |
| 2736 | reinterpret_cast<char*>(gdv_fn_context_arena_malloc(context, state.total_len)); |
| 2737 | if (out == nullptr) { |
| 2738 | gdv_fn_context_set_error_msg(context, "Could not allocate memory for output string"); |
| 2739 | return handle_overflow_failure(out_valid, out_len); |
| 2740 | } |
| 2741 | |
| 2742 | // Concatenate all words |
| 2743 | char* tmp = out; |
| 2744 | int out_idx = 0; |
| 2745 | bool seenAnyValidInput = false; |
| 2746 | |
| 2747 | for (const WordArg& w : words) { |
| 2748 | concat_word(tmp, &out_idx, w.data, w.len, w.valid, separator, separator_len, |
| 2749 | &seenAnyValidInput); |
| 2750 | } |
| 2751 | |
| 2752 | *out_valid = true; |
| 2753 | *out_len = out_idx; |
no test coverage detected