src/string.c

changeset 962
cd418898af5c
parent 926
8fdd8d78c14b
equal deleted inserted replaced
961:bc8b7c5f55fb 962:cd418898af5c
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29 #include "cx/string.h" 29 #include "cx/string.h"
30 #include "cx/utils.h"
31 30
32 #include <string.h> 31 #include <string.h>
33 #include <stdarg.h> 32 #include <stdarg.h>
34 #include <ctype.h> 33 #include <ctype.h>
35 34
87 if (count == 0) return 0; 86 if (count == 0) return 0;
88 87
89 va_list ap; 88 va_list ap;
90 va_start(ap, count); 89 va_start(ap, count);
91 size_t size = 0; 90 size_t size = 0;
92 cx_for_n(i, count) { 91 for (size_t i = 0; i < count; i++) {
93 cxstring str = va_arg(ap, cxstring); 92 cxstring str = va_arg(ap, cxstring);
94 size += str.length; 93 size += str.length;
95 } 94 }
96 va_end(ap); 95 va_end(ap);
97 96
112 va_list ap; 111 va_list ap;
113 va_start(ap, count); 112 va_start(ap, count);
114 113
115 // get all args and overall length 114 // get all args and overall length
116 size_t slen = str.length; 115 size_t slen = str.length;
117 cx_for_n(i, count) { 116 for (size_t i = 0; i < count; i++) {
118 cxstring s = va_arg (ap, cxstring); 117 cxstring s = va_arg (ap, cxstring);
119 strings[i] = s; 118 strings[i] = s;
120 slen += s.length; 119 slen += s.length;
121 } 120 }
122 va_end(ap); 121 va_end(ap);
130 if (str.ptr == NULL) abort(); 129 if (str.ptr == NULL) abort();
131 130
132 // concatenate strings 131 // concatenate strings
133 size_t pos = str.length; 132 size_t pos = str.length;
134 str.length = slen; 133 str.length = slen;
135 cx_for_n(i, count) { 134 for (size_t i = 0; i < count; i++) {
136 cxstring s = strings[i]; 135 cxstring s = strings[i];
137 memcpy(str.ptr + pos, s.ptr, s.length); 136 memcpy(str.ptr + pos, s.ptr, s.length);
138 pos += s.length; 137 pos += s.length;
139 } 138 }
140 139
191 cxstring string, 190 cxstring string,
192 int chr 191 int chr
193 ) { 192 ) {
194 chr = 0xFF & chr; 193 chr = 0xFF & chr;
195 // TODO: improve by comparing multiple bytes at once 194 // TODO: improve by comparing multiple bytes at once
196 cx_for_n(i, string.length) { 195 for (size_t i = 0; i < string.length; i++) {
197 if (string.ptr[i] == chr) { 196 if (string.ptr[i] == chr) {
198 return cx_strsubs(string, i); 197 return cx_strsubs(string, i);
199 } 198 }
200 } 199 }
201 return (cxstring) {NULL, 0}; 200 return (cxstring) {NULL, 0};
554 suffix.ptr, suffix.length) == 0; 553 suffix.ptr, suffix.length) == 0;
555 #endif 554 #endif
556 } 555 }
557 556
558 void cx_strlower(cxmutstr string) { 557 void cx_strlower(cxmutstr string) {
559 cx_for_n(i, string.length) { 558 for (size_t i = 0; i < string.length; i++) {
560 string.ptr[i] = (char) tolower(string.ptr[i]); 559 string.ptr[i] = (char) tolower(string.ptr[i]);
561 } 560 }
562 } 561 }
563 562
564 void cx_strupper(cxmutstr string) { 563 void cx_strupper(cxmutstr string) {
565 cx_for_n(i, string.length) { 564 for (size_t i = 0; i < string.length; i++) {
566 string.ptr[i] = (char) toupper(string.ptr[i]); 565 string.ptr[i] = (char) toupper(string.ptr[i]);
567 } 566 }
568 } 567 }
569 568
570 #ifndef CX_STRREPLACE_INDEX_BUFFER_SIZE 569 #ifndef CX_STRREPLACE_INDEX_BUFFER_SIZE
746 delim.length = ctx->delim.length; 745 delim.length = ctx->delim.length;
747 } 746 }
748 747
749 // if more delimiters are specified, check them now 748 // if more delimiters are specified, check them now
750 if (ctx->delim_more_count > 0) { 749 if (ctx->delim_more_count > 0) {
751 cx_for_n(i, ctx->delim_more_count) { 750 for (size_t i = 0; i < ctx->delim_more_count; i++) {
752 cxstring d = cx_strstr(haystack, ctx->delim_more[i]); 751 cxstring d = cx_strstr(haystack, ctx->delim_more[i]);
753 if (d.length > 0 && (delim.length == 0 || d.ptr < delim.ptr)) { 752 if (d.length > 0 && (delim.length == 0 || d.ptr < delim.ptr)) {
754 delim.ptr = d.ptr; 753 delim.ptr = d.ptr;
755 delim.length = ctx->delim_more[i].length; 754 delim.length = ctx->delim_more[i].length;
756 } 755 }

mercurial