Created
February 1, 2009 23:14
-
-
Save jeremyBanks/56692 to your computer and use it in GitHub Desktop.
[2010-01] goofing around with string substitution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //&>/dev/null;x="${0%.*}";[ ! "$x" -ot "$0" ]||(rm -f "$x";cc -o "$x" "$0")&&"$x" $*;exit | |
| #import <stdio.h> | |
| #import <stdlib.h> | |
| #import <string.h> | |
| typedef struct strpart { | |
| char* start; | |
| int len; | |
| struct strpart* next; | |
| } strpart; | |
| strpart* new_strpart(char* start, int len) { | |
| strpart* new = malloc(sizeof(strpart)); | |
| new->start = start; | |
| new->len = len; | |
| new->next = NULL; | |
| return(new); | |
| } | |
| // Given the tail of a strpart list, adds an element and returns the | |
| // new tail address. | |
| strpart* strpart_append(strpart* tail, char* start, int len) { | |
| strpart* new_tail = new_strpart(start, len); | |
| tail->next = new_tail; | |
| return new_tail; | |
| } | |
| char* strpart_compile(strpart* head) { | |
| int len_result = 0; | |
| char* result; | |
| strpart* current; | |
| for(current = head; current; current = current->next) { | |
| len_result += current->len; | |
| current = current->next; | |
| } | |
| result = malloc(sizeof(char) * len_result + 1); | |
| int result_index = 0; | |
| int current_index = 0; | |
| for(current = head; current; current = current->next) { | |
| for(current_index = 0; current_index < current->len; current_index++) { | |
| result[result_index++] = current->start[current_index]; | |
| } | |
| } | |
| result[result_index] = '\x00'; | |
| return(result); | |
| } | |
| void strpart_free(strpart* head) { | |
| strpart* current; | |
| strpart* next; | |
| for(current = head; current; current = current->next) { | |
| next = current->next; | |
| free(current); | |
| current = next; | |
| } | |
| } | |
| char* strsub(char* subject, char* old, char* new) { | |
| int len_subject = strlen(subject); | |
| int len_old = strlen(old); | |
| int len_new = strlen(new); | |
| strpart* head = new_strpart(NULL, 0); | |
| strpart* tail = head; | |
| int index_subject; | |
| int index_old; | |
| int index_added = 0; | |
| for(index_subject = 0; index_subject < len_subject;) { | |
| for(index_old = 0; index_old < len_old && | |
| index_old + index_subject < len_subject && | |
| old[index_old]; index_old++); | |
| printf("subject/added = %i/%i\n", index_subject, index_added); | |
| printf("head->next = %p\n", head->next); | |
| printf("%s\n", strpart_compile(head)); | |
| if(index_old == len_old) { | |
| if(index_added < index_subject - 1) { | |
| tail = strpart_append(tail, &subject[index_added + 1], index_subject - index_added); | |
| } | |
| tail = strpart_append(tail, new, len_new); | |
| index_added = index_subject = index_subject + len_old; | |
| } else { | |
| index_subject++; | |
| } | |
| } | |
| if(index_added < index_subject - 1) { | |
| tail = strpart_append(tail, &subject[index_added + 1], index_subject - index_added); | |
| } | |
| char* result = strpart_compile(head); | |
| strpart_free(head); | |
| return(result); | |
| } | |
| int main(int argc, char* argv[]) { | |
| printf("%s\n", strsub("Ha ha ham ha!", "ham", "pork")); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment