MCPcopy Index your code
hub / github.com/MapServer/MapServer / msReplaceSubstring

Function msReplaceSubstring

mapstring.c:521–577  ·  view source on GitHub ↗

------------------------------------------------------------------------------- */ Replace all occurances of old with new in str. */ It is assumed that str was dynamically created using malloc. */ ------------------------------------------------------------------------------- */

Source from the content-addressed store, hash-verified

519/* It is assumed that str was dynamically created using malloc. */
520/* ------------------------------------------------------------------------------- */
521char *msReplaceSubstring(char *str, const char *old, const char *new)
522{
523 size_t str_len, old_len, new_len, tmp_offset;
524 char *tmp_ptr;
525
526 if(new == NULL)
527 new = "";
528
529 /*
530 ** If old is not found then leave str alone
531 */
532 if( (tmp_ptr = strstr(str, old)) == NULL)
533 return(str);
534
535 /*
536 ** Grab some info about incoming strings
537 */
538 str_len = strlen(str);
539 old_len = strlen(old);
540 new_len = strlen(new);
541
542 /*
543 ** Now loop until old is NOT found in new
544 */
545 while( tmp_ptr != NULL ) {
546
547 /*
548 ** re-allocate memory for buf assuming 1 replacement of old with new
549 ** don't bother reallocating if old is larger than new)
550 */
551 if (old_len < new_len) {
552 tmp_offset = tmp_ptr - str;
553 str_len = str_len - old_len + new_len;
554 str = (char *)msSmallRealloc(str, (str_len + 1)); /* make new space for a copy */
555 tmp_ptr = str + tmp_offset;
556 }
557
558 /*
559 ** Move the trailing part of str to make some room unless old_len == new_len
560 */
561 if (old_len != new_len) {
562 memmove(tmp_ptr+new_len, tmp_ptr+old_len, strlen(tmp_ptr)-old_len+1);
563 }
564
565 /*
566 ** Now copy new over old
567 */
568 memcpy(tmp_ptr, new, new_len);
569
570 /*
571 ** And look for more matches in the rest of the string
572 */
573 tmp_ptr = strstr(tmp_ptr + new_len, old);
574 }
575
576 return(str);
577}
578

Callers 15

msSLDApplySLDFunction · 0.85
processIfTagFunction · 0.85
processIncludeTagFunction · 0.85
processItemTagFunction · 0.85
processExtentTagFunction · 0.85
processShplabelTagFunction · 0.85
processDateTagFunction · 0.85
processShpxyTagFunction · 0.85
processMetadataFunction · 0.85
processIconFunction · 0.85
generateGroupTemplateFunction · 0.85

Calls 1

msSmallReallocFunction · 0.85

Tested by

no test coverage detected