Skip to content
Open
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
22 changes: 22 additions & 0 deletions cstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ void stringPrint(TString s);
void stringDebug(TString s);

void stringRemoveChar(TString *s, char c);
void stringInsert(TString *s, size_t pos, TString toInsert);
void stringSwap(TString *s1, TString *s2);
void stringPushBack(TString *s, char c);
void stringPushFront(TString *s, char c);
Expand Down Expand Up @@ -664,6 +665,27 @@ void stringRemoveChar(TString *s, char c) {
s->size = newSize;
}

void stringInsert(TString *s, size_t pos, TString toInsert) {
Copy link
Owner

Choose a reason for hiding this comment

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

Добавьте тесты в файл по образцу, чтобы протестировать новую функцию

if (pos >= s->size) return;
Copy link
Owner

Choose a reason for hiding this comment

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

В таком случае стоит поставить ошибку перед завершением. Используйте clearError и setError


size_t substringLength = toInsert.size;
size_t newSize = s->size + substringLength;

TString secondPartCopy = stringInit(s->size - pos);
secondPartCopy.size = s->size - pos;
for (size_t i = pos; i < s->size; ++i) {
secondPartCopy.data[i - pos] = s->data[i];
}

for (size_t i = pos; i < pos + substringLength; ++i) {
s->data[i] = toInsert.data[i - pos];
}
for (size_t i = pos + substringLength; i < newSize; ++i) {
s->data[i] = secondPartCopy.data[i - substringLength - pos];
Copy link
Owner

Choose a reason for hiding this comment

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

s->data не вместит в себя обе строки, если capacity < newSize. Возможно стоит использовать stringSubstring, stringConcat или же прописать логику расширения буффера строки самостоятельно

}
s->size = newSize;
}

void stringSwap(TString *s1, TString *s2) {
if (s1 == NULL || s2 == NULL) {
setError(ERR_NULL_POINTER);
Expand Down