Merge pull request #1812 from journaltraces/feat/libc-stpcpy

feat(libc): implement stpcpy
This commit is contained in:
Adrià 2026-10-10 00:08:33 +02:00 committed by GitHub
commit e51d4e1f21
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 0 deletions

View File

@ -55,6 +55,12 @@ std::size_t APS5_VABI strlcat_nid_postfix(char* destination, const char* source,
return destinationLength + sourceLength;
}
char* APS5_VABI stpcpy_nid_postfix(char* destination, const char* source) {
const auto length = std::strlen(source);
std::memcpy(destination, source, length + 1);
return destination + length;
}
char* APS5_VABI strtok_r_nid_postfix(char* text, const char* delimiters, char** state) {
if (text == nullptr) text = *state;
if (text == nullptr) return nullptr;

View File

@ -13,6 +13,7 @@ char* APS5_VABI strncat_nid_postfix(char*, const char*, std::size_t);
char* APS5_VABI strpbrk_nid_postfix(const char*, const char*);
std::size_t APS5_VABI strcspn_nid_postfix(const char*, const char*);
std::size_t APS5_VABI strlcat_nid_postfix(char*, const char*, std::size_t);
char* APS5_VABI stpcpy_nid_postfix(char*, const char*);
char* APS5_VABI strtok_r_nid_postfix(char*, const char*, char**);
char* APS5_VABI strtok_nid_postfix(char*, const char*);
char* APS5_VABI strcasestr_nid_postfix(const char*, const char*);
@ -133,6 +134,12 @@ int main() {
Require(strlcat_nid_postfix(buffer, "xyz", 0) == 3);
buffer[0] = '\0';
Require(strlcat_nid_postfix(buffer, "x", 1) == 1 && buffer[0] == '\0');
char chained[8] = "zzzzzzz";
char* end = stpcpy_nid_postfix(chained, "ab");
Require(end == chained + 2 && *end == '\0' && chained[3] == 'z');
end = stpcpy_nid_postfix(end, "cd");
Require(end == chained + 4 && std::strcmp(chained, "abcd") == 0);
Require(stpcpy_nid_postfix(end, "") == end && chained[5] == 'z');
Require(strncat_nid_postfix(buffer, "xyz", 2) == buffer);
Require(std::strcmp(buffer, "xy") == 0);
Require(strpbrk_nid_postfix(buffer, "ay") == buffer + 1);