Join Function with Custom Separator Support

Date: 2025-07-29 03:30
Author: XMD Team
Topic: Enhanced join function implementation

Overview

The current join function only supports the default โ€œ, โ€œ separator. Level 5 requires support for custom separators to enable flexible array joining.

Current Implementation

// join(array) -> "elem1, elem2, elem3"
join ["a", "b", "c"]  // Returns "a, b, c"

Required Enhancement

// join(array, separator) -> "elem1<sep>elem2<sep>elem3"
join arr ", "   // Returns "elem1, elem2, elem3"
join arr "|"    // Returns "elem1|elem2|elem3"  
join arr " "    // Returns "elem1 elem2 elem3"

Implementation Strategy

  1. Function Signature: Modify ast_evaluate_function_call to handle 2 arguments for join
  2. Argument Parsing: Support both 1-arg (default separator) and 2-arg (custom separator) forms
  3. Separator Handling: Use custom separator when provided, fall back to โ€œ, โ€œ when not
  4. Backward Compatibility: Ensure existing join calls still work

Technical Details

  • Location: src/ast_evaluate_function_call/ast_evaluate_function_call.c
  • Function: ast_evaluate_function_call() in join handler section
  • Arguments:
    • arguments[0]: Array to join
    • arguments[1]: Separator string (optional)

Test Cases

  1. join arr ", " -> comma-space separated
  2. join arr "|" -> pipe separated
  3. join arr "" -> no separator
  4. join arr -> default โ€œ, โ€œ separator (backward compatibility)

Memory Management

  • Properly calculate total length including custom separator
  • Handle empty separator case
  • Free all allocated memory

This enhancement enables the level 5 functionality while maintaining backward compatibility.