src/string.c

changeset 1500
d20037235c9c
parent 1426
3a89b31f0724
equal deleted inserted replaced
1499:d0a0a41405bb 1500:d20037235c9c
89 const CxAllocator *alloc, 89 const CxAllocator *alloc,
90 cxmutstr *dest, 90 cxmutstr *dest,
91 cxstring src 91 cxstring src
92 ) { 92 ) {
93 if (cxReallocate(alloc, &dest->ptr, src.length + 1)) { 93 if (cxReallocate(alloc, &dest->ptr, src.length + 1)) {
94 return 1; 94 return 1; // LCOV_EXCL_LINE
95 } 95 }
96 96
97 memcpy(dest->ptr, src.ptr, src.length); 97 memcpy(dest->ptr, src.ptr, src.length);
98 dest->length = src.length; 98 dest->length = src.length;
99 dest->ptr[dest->length] = '\0'; 99 dest->ptr[dest->length] = '\0';
135 // compute overall length 135 // compute overall length
136 bool overflow = false; 136 bool overflow = false;
137 size_t slen = str.length; 137 size_t slen = str.length;
138 for (size_t i = 0; i < count; i++) { 138 for (size_t i = 0; i < count; i++) {
139 cxstring s = va_arg(ap, cxstring); 139 cxstring s = va_arg(ap, cxstring);
140 if (slen > SIZE_MAX - str.length) overflow = true; 140 if (slen > SIZE_MAX - s.length) overflow = true;
141 slen += s.length; 141 slen += s.length;
142 } 142 }
143 va_end(ap); 143 va_end(ap);
144 144
145 // abort in case of overflow 145 // abort in case of overflow
154 if (str.ptr == NULL) { 154 if (str.ptr == NULL) {
155 newstr = cxMalloc(alloc, slen + 1); 155 newstr = cxMalloc(alloc, slen + 1);
156 } else { 156 } else {
157 newstr = cxRealloc(alloc, str.ptr, slen + 1); 157 newstr = cxRealloc(alloc, str.ptr, slen + 1);
158 } 158 }
159 if (newstr == NULL) { 159 if (newstr == NULL) { // LCOV_EXCL_START
160 va_end(ap2); 160 va_end(ap2);
161 return (cxmutstr) {NULL, 0}; 161 return (cxmutstr) {NULL, 0};
162 } 162 } // LCOV_EXCL_STOP
163 str.ptr = newstr; 163 str.ptr = newstr;
164 164
165 // concatenate strings 165 // concatenate strings
166 size_t pos = str.length; 166 size_t pos = str.length;
167 str.length = slen; 167 str.length = slen;
519 ) { 519 ) {
520 cxmutstr result = { 520 cxmutstr result = {
521 cxMalloc(allocator, string.length + 1), 521 cxMalloc(allocator, string.length + 1),
522 string.length 522 string.length
523 }; 523 };
524 // LCOV_EXCL_START
524 if (result.ptr == NULL) { 525 if (result.ptr == NULL) {
525 result.length = 0; 526 result.length = 0;
526 return result; 527 return result;
527 } 528 }
529 // LCOV_EXCL_STOP
528 memcpy(result.ptr, string.ptr, string.length); 530 memcpy(result.ptr, string.ptr, string.length);
529 result.ptr[string.length] = '\0'; 531 result.ptr[string.length] = '\0';
530 return result; 532 return result;
531 } 533 }
532 534

mercurial