Join Function with Custom Separator Support
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
- Function Signature: Modify
ast_evaluate_function_callto handle 2 arguments for join - Argument Parsing: Support both 1-arg (default separator) and 2-arg (custom separator) forms
- Separator Handling: Use custom separator when provided, fall back to โ, โ when not
- 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 joinarguments[1]: Separator string (optional)
Test Cases
join arr ", "-> comma-space separatedjoin arr "|"-> pipe separatedjoin arr ""-> no separatorjoin 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.