src/string.c

changeset 1001
5c9ec5a0a4ef
parent 985
68754c7de906
child 1040
1ecf4dbbc60c
equal deleted inserted replaced
1000:1aecddf7e209 1001:5c9ec5a0a4ef
105 size_t count, 105 size_t count,
106 ... 106 ...
107 ) { 107 ) {
108 if (count == 0) return str; 108 if (count == 0) return str;
109 109
110 cxstring *strings = calloc(count, sizeof(cxstring)); 110 cxstring strings_stack[8];
111 if (!strings) abort(); 111 cxstring *strings;
112 if (count > 8) {
113 strings = calloc(count, sizeof(cxstring));
114 if (strings == NULL) {
115 return (cxmutstr) {NULL, 0};
116 }
117 } else {
118 strings = strings_stack;
119 }
112 120
113 va_list ap; 121 va_list ap;
114 va_start(ap, count); 122 va_start(ap, count);
115 123
116 // get all args and overall length 124 // get all args and overall length
121 slen += s.length; 129 slen += s.length;
122 } 130 }
123 va_end(ap); 131 va_end(ap);
124 132
125 // reallocate or create new string 133 // reallocate or create new string
134 char *newstr;
126 if (str.ptr == NULL) { 135 if (str.ptr == NULL) {
127 str.ptr = cxMalloc(alloc, slen + 1); 136 newstr = cxMalloc(alloc, slen + 1);
128 } else { 137 } else {
129 str.ptr = cxRealloc(alloc, str.ptr, slen + 1); 138 newstr = cxRealloc(alloc, str.ptr, slen + 1);
130 } 139 }
131 if (str.ptr == NULL) abort(); 140 if (newstr == NULL) {
141 free(strings);
142 return (cxmutstr) {NULL, 0};
143 }
144 str.ptr = newstr;
132 145
133 // concatenate strings 146 // concatenate strings
134 size_t pos = str.length; 147 size_t pos = str.length;
135 str.length = slen; 148 str.length = slen;
136 for (size_t i = 0; i < count; i++) { 149 for (size_t i = 0; i < count; i++) {
141 154
142 // terminate string 155 // terminate string
143 str.ptr[str.length] = '\0'; 156 str.ptr[str.length] = '\0';
144 157
145 // free temporary array 158 // free temporary array
146 free(strings); 159 if (strings != strings_stack) {
160 free(strings);
161 }
147 162
148 return str; 163 return str;
149 } 164 }
150 165
151 cxstring cx_strsubs( 166 cxstring cx_strsubs(

mercurial