Skip to content

Commit

Permalink
Improve str_replace and add tests for it (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
manugarg authored May 24, 2023
1 parent 701a914 commit e7c99e3
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 16 deletions.
6 changes: 5 additions & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ jsapi_buildstamp: spidermonkey/js/src
spidermonkey/libjs.a: spidermonkey/js/src
cd spidermonkey && SMCFLAGS="$(SHFLAGS) $(SMCFLAGS)" $(MAKE) jslib

pacparser.o: pacparser.c pac_utils.h pacparser.h jsapi_buildstamp
pac_utils_test: pac_utils_test.c pac_utils.h
$(CC) $(MAINT_CFLAGS) $(CFLAGS) $(SHFLAGS) pac_utils_test.c -o pac_utils_test -lm -L. -I.

pacparser.o: pacparser.c pac_utils.h pacparser.h pac_utils_test jsapi_buildstamp
./pac_utils_test
$(CC) $(MAINT_CFLAGS) $(CFLAGS) $(SHFLAGS) -c pacparser.c -o pacparser.o
touch pymod/pacparser_o_buildstamp

Expand Down
50 changes: 35 additions & 15 deletions src/pac_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA

#include <string.h>
#include <stdlib.h>

static const char *pacUtils =
"function dnsDomainIs(host, domain) {\n"
" return (host.length >= domain.length &&\n"
Expand Down Expand Up @@ -335,31 +338,48 @@ static const char *pacUtils =


// You must free the result if result is non-NULL.
char *str_replace(const char *orig, char *rep, char *with) {
char *result; // the return string
char *ins; // the next insert point
char *tmp; // varies
int count; // number of replacements
int len_front; // distance between rep and end of last rep
int len_rep = strlen(rep);
int len_with = strlen(with);
char *str_replace(const char *orig, const char *rep, const char *with) {
if (orig == NULL || rep == NULL || with == NULL) {
return NULL;
}

size_t len_orig = strnlen(orig, 1024);
size_t len_rep = strnlen(rep, 1024);
size_t len_with = strnlen(with, 1024);

if (len_orig == 0 || len_rep == 0) {
char *result = malloc(len_orig + 1);
strcpy(result, orig);
return result;
}

// Get the count of replacements
ins = orig;
for (count = 0; (tmp = strstr(ins, rep)); ++count) {
ins = tmp + len_rep;
// Count replacements needed
int count; // number of replacements
char const *start = orig;
// Cursor moves through the string, looking for rep.
char const *cursor;
for (count = 0;; ++count) {
cursor = strstr(start, rep);
if (cursor == NULL) {
break;
}
start = cursor + len_rep;
}

tmp = result = malloc(strlen(orig) + (len_with - len_rep) * count + 1);
char *tmp;
char *result;
tmp = result = malloc(len_orig + (len_with - len_rep) * count + 1);

// first time through the loop, all the variable are set correctly
// from here on,
// tmp points to the end of the result string
// ins points to the next occurrence of rep in orig
// orig points to the remainder of orig after "end of rep"
while (count--) {
ins = strstr(orig, rep);
len_front = ins - orig;
const char *ins = strstr(orig, rep);
int len_front = (int)(ins - orig); // How far have we moved
// Into the tmp, copy everything until we reach the rep.
// and move tmp forward.
tmp = strncpy(tmp, orig, len_front) + len_front;
tmp = strcpy(tmp, with) + len_with;
orig += len_front + len_rep; // move to next "end of rep"
Expand Down
40 changes: 40 additions & 0 deletions src/pac_utils_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "pac_utils.h"

#define STREQ(s1, s2) (strcmp((s1), (s2)) == 0)

// Unit tests
int main() {
// Test case 1: Single replacement
assert(STREQ("Hello, universe!", str_replace("Hello, world!", "world", "universe")));

// Test case 2: Multiple replacements
assert(STREQ("one cat, two cat, red cat, blue cat",
str_replace("one fish, two fish, red fish, blue fish", "fish", "cat")));
// Expected output: "one cat, two cat, red cat, blue cat"

// Test case 3: No replacements
assert(STREQ("AI is amazing", str_replace("AI is amazing", "robot", "AI")));

// Test case 4: Empty original string
assert(STREQ("", str_replace("", "hello", "world")));

// Test case 5: Empty replacement string
assert(STREQ("Hello, world!", str_replace("Hello, world!", "", "universe")));

// Test case 6: Empty "with" string
assert(STREQ("Hello, !", str_replace("Hello, world!", "world", "")));

// Test case 7: Complex replacements
assert(STREQ("abcdeXYZcba", str_replace("abcdeedcba", "ed", "XYZ")));

// Test case 8: Null inputs
assert(str_replace(NULL, "hello", "world") == NULL);
assert(str_replace("Hello hello", NULL, "world") == NULL);
assert(str_replace("Hello hello", "hello", NULL) == NULL);

return 0;
}

0 comments on commit e7c99e3

Please sign in to comment.