Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Contributions are what make the open-source community such an amazing place to l
- [ ] size_t stringCountSubstring(TString s, TString pattern); - Count occurrences of a substring.
- [ ] void stringCapitalize(TString *s); - Capitalize the first letter of each word.
- [x] void stringFilter(TString *s, bool (*predicate)(char)); - Remove characters not satisfying a predicate.
- [ ] void stringInsert(TString *s, size_t pos, TString toInsert); - Insert a substring at a specified position.
- [x] void stringInsert(TString *s, size_t pos, TString toInsert); - Insert a substring at a specified position.
- [ ] void stringInsertCharArr(TString *s, size_t pos, const char *toInsert); - Insert a substring from a char array.
- [x] void stringMap(TString *s, char (*func)(char)); - Apply a function to every character of the string.
- [x] void stringMapIndex(TString *s, char (*func)(size_t, char)); - Apply a function to every character of the string with access to its index.
Expand Down
32 changes: 32 additions & 0 deletions cstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -941,4 +941,36 @@ double stringToDouble(TString s) {
return number + decimal;
}

void stringInsert(TString *s, size_t pos, TString toInsert) {
size_t len_sub = stringLen(toInsert);
size_t len_str = stringLen(*s);

if (pos > len_str) {
setError(ERR_NAN);
}

char* new_data = malloc(len_str + len_sub + 1);
if(!new_data){
setError(ERR_ALLOCATE_SPACE);
}

clearError();

for(size_t i = 0; i < pos; i++){
new_data[i] = s->data[i];
}

for(size_t i = 0; i < len_sub; i++){
new_data[i + pos] = toInsert.data[i];
}

for (size_t i = pos; i < len_str; i++){
new_data[i + len_sub] = s->data[i];
}

free(s->data);
s->data = new_data;
s->size = len_str + len_sub;
Comment on lines +972 to +973
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And what about s->capacity???

}

#endif
13 changes: 13 additions & 0 deletions tests/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,18 @@ void test_stringToDouble() {
printGreen("stringToDouble\n");
}

void test_stringInsert(){
TString s1 = stringInitWithCharArr("abcdef");
TString s2 = stringInitWithCharArr("123");
Comment on lines +487 to +488
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add more test cases

stringInsert(&s1, 2, s2);

TString s1expected = stringInitWithCharArr("ab123cdef");

assertEq(strncmp(s1.data, s1expected.data, stringLen(s1expected)), 0);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use stringCompare from this library

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally good job)


printGreen("stringInsert\n");
}

int main() {
test_stringStartWith();
test_stringEndWith();
Expand Down Expand Up @@ -511,5 +523,6 @@ int main() {
test_stringIsPalindrome();
test_stringPad();
test_stringRemove();
test_stringInsert();
return 0;
}