src/string.c

changeset 643
5700ba9154ab
parent 628
1e2be40f0cb5
child 645
ec50abb285ad
equal deleted inserted replaced
642:98c90759f69e 643:5700ba9154ab
224 ) { 224 ) {
225 cxstring result = cx_strrchr(cx_strcast(string), chr); 225 cxstring result = cx_strrchr(cx_strcast(string), chr);
226 return (cxmutstr) {(char *) result.ptr, result.length}; 226 return (cxmutstr) {(char *) result.ptr, result.length};
227 } 227 }
228 228
229 #define STRSTR_SBO_BUFLEN 512 229 #ifndef CX_STRSTR_SBO_SIZE
230 #define CX_STRSTR_SBO_SIZE 512
231 #endif
230 232
231 cxstring cx_strstr( 233 cxstring cx_strstr(
232 cxstring haystack, 234 cxstring haystack,
233 cxstring needle 235 cxstring needle
234 ) { 236 ) {
248 * The original algorithm needs a (-1) at one single place, 250 * The original algorithm needs a (-1) at one single place,
249 * and we want to avoid that. 251 * and we want to avoid that.
250 */ 252 */
251 253
252 // local prefix table 254 // local prefix table
253 size_t s_prefix_table[STRSTR_SBO_BUFLEN]; 255 size_t s_prefix_table[CX_STRSTR_SBO_SIZE];
254 256
255 // check needle length and use appropriate prefix table 257 // check needle length and use appropriate prefix table
256 // if the pattern exceeds static prefix table, allocate on the heap 258 // if the pattern exceeds static prefix table, allocate on the heap
257 bool useheap = needle.length >= STRSTR_SBO_BUFLEN; 259 bool useheap = needle.length >= CX_STRSTR_SBO_SIZE;
258 register size_t *ptable = useheap ? calloc(needle.length + 1, 260 register size_t *ptable = useheap ? calloc(needle.length + 1,
259 sizeof(size_t)) : s_prefix_table; 261 sizeof(size_t)) : s_prefix_table;
260 262
261 // keep counter in registers 263 // keep counter in registers
262 register size_t i, j; 264 register size_t i, j;
537 cx_for_n(i, string.length) { 539 cx_for_n(i, string.length) {
538 string.ptr[i] = (char) toupper(string.ptr[i]); 540 string.ptr[i] = (char) toupper(string.ptr[i]);
539 } 541 }
540 } 542 }
541 543
542 #define REPLACE_INDEX_BUFFER_MAX 100 544 #ifndef CX_STRREPLACE_INDEX_BUFFER_SIZE
545 #define CX_STRREPLACE_INDEX_BUFFER_SIZE 64
546 #endif
543 547
544 struct cx_strreplace_ibuf { 548 struct cx_strreplace_ibuf {
545 size_t *buf; 549 size_t *buf;
546 struct cx_strreplace_ibuf *next; 550 struct cx_strreplace_ibuf *next;
547 unsigned int len; 551 unsigned int len;
568 return cx_strdup_a(allocator, str); 572 return cx_strdup_a(allocator, str);
569 573
570 // Compute expected buffer length 574 // Compute expected buffer length
571 size_t ibufmax = str.length / pattern.length; 575 size_t ibufmax = str.length / pattern.length;
572 size_t ibuflen = replmax < ibufmax ? replmax : ibufmax; 576 size_t ibuflen = replmax < ibufmax ? replmax : ibufmax;
573 if (ibuflen > REPLACE_INDEX_BUFFER_MAX) { 577 if (ibuflen > CX_STRREPLACE_INDEX_BUFFER_SIZE) {
574 ibuflen = REPLACE_INDEX_BUFFER_MAX; 578 ibuflen = CX_STRREPLACE_INDEX_BUFFER_SIZE;
575 } 579 }
576 580
577 // Allocate first index buffer 581 // Allocate first index buffer
578 struct cx_strreplace_ibuf *firstbuf, *curbuf; 582 struct cx_strreplace_ibuf *firstbuf, *curbuf;
579 firstbuf = curbuf = calloc(1, sizeof(struct cx_strreplace_ibuf)); 583 firstbuf = curbuf = calloc(1, sizeof(struct cx_strreplace_ibuf));

mercurial