Skip to content

feat: add Dmod_StrMerge function for merging multiple strings - #339

Merged
JohnAmadis merged 3 commits into
developfrom
copilot/add-str-merge-function
Oct 24, 2026
Merged

JohnAmadis merged 3 commits into
developfrom
copilot/add-str-merge-function

Conversation

@JaxCalder

Copy link
Copy Markdown
Contributor

Summary

Added function that allows merging multiple strings into a single newly allocated string.

Changes

  • ****: Added declarations for and APIs
  • ****: Implemented and weak symbols
  • ****: Added comprehensive unit tests

API

Dmod_StrMerge (for modules)

char* result = Dmod_StrMerge(3, "Hello", " ", "World");  // "Hello World"
Dmod_Free(result);

Dmod_StrMergeEx (with explicit allocator)

char* result = Dmod_StrMergeEx(2, "my_allocator", "Hello", "World");  // "HelloWorld"
Dmod_Free(result);

Implementation details

  • First pass: calculates total length of all strings (NULL strings are skipped)
  • Second pass: allocates memory for the combined string + null terminator
  • Third pass: copies each string sequentially into the result buffer
  • Handles edge cases: NULL strings, empty strings, count <= 0

- Add Dmod_StrMerge(count, ...) macro for module code (uses DMOD_CURRENT_ALLOCATOR)
- Add Dmod_StrMergeEx(count, moduleName, ...) for explicit allocator naming
- Implement _StrMerge and _StrMergeEx weak symbols in dmod_if_str.c
- Add comprehensive unit tests for the new function
- Include tests in CMakeLists.txt
@JaxCalder
JaxCalder force-pushed the copilot/add-str-merge-function branch from 2c4e522 to df1adb1 Compare October 24, 2026 16:24
OpenCode added 2 commits October 24, 2026 16:31
Since DMOD_CURRENT_ALLOCATOR is always defined (for both modules and
system code), Dmod_StrMerge can be a pure macro that always calls
Dmod_StrMergeEx with the correct allocator. The weak symbol _StrMerge
was incorrect as it would allocate under the wrong module name.
Keep the original #if defined(DMOD_MODULE_NAME) pattern for Dmod_StrDup
to maintain backward compatibility with older module code.
@JohnAmadis
JohnAmadis merged commit 277c660 into develop Oct 24, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants