Skip to content

Commit 7a4eb93

Browse files
committed
small string optimizations
1 parent 4f8c002 commit 7a4eb93

13 files changed

Lines changed: 506 additions & 234 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ a.out
77
obj
88
lib
99
tasks.txt
10+
cstr_api_tests

‎Main.c‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
#include "cstring.h"
22

3+
34
int main(void)
45
{
5-
cstring str = cstr_init("");
6-
cstring other = cstr_init("");
7-
6+
cstring s = cstr_init();
7+
cstring str1 = cstr_init("k-hahkj89217");
8+
cstr_literal(str2, "YOu Beyocchhh");
9+
cstring tmp = (cstring)str2;
10+
11+
cstr_push_back(&s, 'A');
12+
cstr_push_back(&str1, 'B');
813

9-
cstr_swap(&str, &other);
14+
cstr_swap(&str1, &s);
1015

11-
printf("str = %s ==&== Size = %lu\n", str, cstr_size(str));
12-
printf("other = %s ==&== Size = %lu\n", other, cstr_size(other));
13-
}
16+
cstr_println(s);
17+
cstr_println(str1);
18+
cstr_println(tmp);
19+
}

‎Makefile‎

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,17 @@ endif
1414
CC := gcc
1515
CFLAGS := -Wall -Wextra -Wpedantic -Wshadow -Wformat=2 -Wcast-align -Wconversion -Wsign-conversion -Wnull-dereference
1616

17-
# Ensure lib directory exists
1817
LIB_DIR := lib
1918

20-
# Detect src folder
21-
HAS_SRC := $(wildcard src)
22-
2319
# -------- LIBRARY --------
2420
LIB_NAME := libcstring.a
2521
LIB_SRC := cstring.c
2622
LIB_OBJ := $(LIB_DIR)/cstring.o
2723

2824
# -------- APP --------
29-
SRC := $(wildcard src/*.c)
30-
OBJ := $(patsubst src/%.c,$(LIB_DIR)/%.o,$(SRC))
31-
32-
TARGET := app$(EXE_EXT)
25+
SRC := Main.c
26+
OBJ := $(LIB_DIR)/Main.o
27+
TARGET := obj$(EXE_EXT)
3328

3429
# Default
3530
all: compile
@@ -38,40 +33,28 @@ all: compile
3833
$(LIB_DIR):
3934
@$(MKDIR) $(LIB_DIR)
4035

41-
# -------- COMPILE LIB OBJECT --------
42-
$(LIB_DIR)/%.o: %.c | $(LIB_DIR)
36+
# Compile library object
37+
$(LIB_DIR)/cstring.o: cstring.c | $(LIB_DIR)
4338
$(CC) $(CFLAGS) -c $< -o $@
4439

45-
# -------- COMPILE SRC OBJECTS --------
46-
$(LIB_DIR)/%.o: src/%.c | $(LIB_DIR)
40+
# Compile main object
41+
$(LIB_DIR)/Main.o: Main.c | $(LIB_DIR)
4742
$(CC) $(CFLAGS) -c $< -o $@
4843

49-
# -------- BUILD LIBRARY --------
44+
# Build library
5045
$(LIB_NAME): $(LIB_OBJ)
5146
ar rcs $@ $^
5247

53-
# -------- BUILD LOGIC --------
54-
compile: $(LIB_NAME)
55-
ifdef HAS_SRC
56-
$(MAKE) build_app
57-
endif
58-
59-
# -------- BUILD APP --------
60-
build_app: $(OBJ)
48+
# Build everything
49+
compile: $(LIB_NAME) $(OBJ)
6150
$(CC) $(OBJ) -L. -lcstring -o $(TARGET)
6251

63-
# -------- RUN --------
52+
# Run
6453
run: compile
65-
ifdef HAS_SRC
6654
./$(TARGET)
67-
else
68-
@echo "No executable (no src folder)"
69-
endif
7055

71-
# -------- CLEAN --------
56+
# Clean
7257
clean:
7358
$(RM) $(LIB_DIR)$(SEP)*.o
7459
$(RM) $(LIB_NAME)
75-
ifdef HAS_SRC
76-
$(RM) $(TARGET)
77-
endif
60+
$(RM) $(TARGET)

‎cstring.c‎

Lines changed: 93 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ cstr_getstr(cstring *str, size_t cnt, FILE* stream)
4444
pvt_cstr_assert(cnt < cstr_capacity(str_02_),
4545
"count is larger than capacity to read.");
4646

47-
char l_str_array[cnt];
48-
fgets(l_str_array, (int)cnt, stream);
49-
pvt_copy_(str_02_, &l_str_array[0], cnt);
47+
size_t read_01_ = fread(str_02_, sizeof(*str_02_), cnt, stream);
48+
str_02_[read_01_] = (char)0;
49+
pvt_set_total_size_(str_02_, read_01_);
5050
}
5151

5252

5353
void __unused
54-
cstr_println(cstring str)
54+
cstr_println(const_cstring str)
5555
{
5656
fputs(str, stdout);
5757
putc('\n', stdout);
@@ -65,37 +65,27 @@ cstr_println(cstring str)
6565
void __unused
6666
cstr_resize(cstring *str, const size_t count, const char ch)
6767
{
68-
cstring str_ = *str;
69-
size_t idx_ = CSTRMIN(count, cstr_size(str_)),
70-
size_ = CSTRMAX(count, cstr_size(str_));
68+
cstring str_ = NULL;
69+
size_t size_ = cstr_size(*str);
70+
71+
cstr_reserve(str, count + 1);
72+
str_ = *str;
7173

72-
char ch_ = (count == size_) ? ch : (char)0;
73-
cstr_reserve(str, count);
74-
pvt_fill_(str_, idx_, size_ - idx_, ch_);
74+
if(count > size_) {
75+
pvt_fill_(str_, size_, count - size_, ch);
76+
}
77+
78+
str_[count] = (char)0;
79+
pvt_set_total_size_(str_, count);
7580
}
7681

7782

7883
void __unused
7984
cstr_swap(cstring *str, cstring *other)
8085
{
81-
cstring str1_ = *str, str2_ = *other;
82-
const size_t size1_ = cstr_size(str1_),
83-
size2_ = cstr_size(str2_);
84-
cstr_reserve(&str1_, size2_);
85-
cstr_reserve(&str2_, size1_);
86-
87-
size_t max_ = CSTRMAX(size1_, size2_);
88-
for(size_t i = 0; i < max_; ++i) {
89-
char tmp = str1_[i];
90-
str1_[i] = str2_[i];
91-
str2_[i] = tmp;
92-
}
93-
94-
pvt_set_total_size_(str1_, size2_);
95-
pvt_set_total_size_(str2_, size1_);
96-
97-
*str = str1_;
98-
*other = str2_;
86+
cstring tmp = *str;
87+
*str = *other;
88+
*other = tmp;
9989
}
10090

10191
/**
@@ -110,7 +100,7 @@ cstr_substr(const_cstring str, const size_t pos, const size_t count)
110100
cstring substr_ = NULL;
111101
size_t count_ = CSTRMIN(count, cstr_size(str) - pos);
112102

113-
pvt_buf_grow_(substr_, count_);
103+
pvt_buf_grow_(substr_, count_ + 1);
114104
pvt_copy_(substr_, str + pos, count_);
115105
return substr_;
116106
}
@@ -129,12 +119,12 @@ cstring __unused
129119
intl_assign_str_range(cstring *str, const_cstring other,
130120
const size_t pos, const size_t count)
131121
{
132-
cstring str_ = *str;
133-
size_t size_ = pvt_cstr_strlen(other),
134-
count_ = CSTRMIN(count, size_ - pos);
135-
if(pos >= size_) return str_;
122+
size_t size_ = pvt_cstr_strlen(other);
123+
if(pos >= size_) return *str;
124+
size_t count_ = CSTRMIN(count, size_ - pos);
136125

137-
cstr_reserve(str, count_);
126+
cstr_reserve(str, count_ + 1);
127+
cstring str_ = *str;
138128
pvt_cstr_memmove(str_, other + pos, count_);
139129
str_[count_] = (char)0;
140130
pvt_set_total_size_(str_, count_);
@@ -144,9 +134,8 @@ intl_assign_str_range(cstring *str, const_cstring other,
144134
cstring __unused
145135
intl_assign_cnt_ch(cstring *str, const size_t count, const char ch)
146136
{
137+
cstr_reserve(str, count + 1);
147138
cstring str_ = *str;
148-
149-
cstr_reserve(str, count);
150139
pvt_fill_(str_, 0, count, ch);
151140
str_[count] = (char)0;
152141
pvt_set_total_size_(str_, count);
@@ -196,7 +185,7 @@ intl_init_cpy_str_w_iter(cstr_iterator begin,
196185
cstring str_ = NULL;
197186
size_t size_ = cstr_distance(begin, end);
198187

199-
pvt_buf_grow_(str_, size_);
188+
pvt_buf_grow_(str_, size_ + 1);
200189
pvt_copy_(str_, begin.it, size_);
201190
return str_;
202191
}
@@ -207,12 +196,12 @@ intl_init_cpy_str_w_off(const_cstring other,
207196
const size_t start, const size_t offset)
208197
{
209198
cstring str_ = NULL;
210-
size_t size_ = pvt_cstr_strlen(other) + 1,
199+
size_t size_ = pvt_cstr_strlen(other),
211200
count_ = CSTRMIN(offset, size_ - start);
212201
pvt_cstr_assert(other != NULL && (start < size_),
213202
"Unsupported arguments passed for Constructor");
214203

215-
pvt_buf_grow_(str_, count_);
204+
pvt_buf_grow_(str_, count_ + 1);
216205
pvt_copy_(str_, other + start, count_);
217206
return str_;
218207
}
@@ -261,7 +250,7 @@ intl_insert_cnt_ch(cstring *str, const size_t index,
261250
pvt_cstr_assert(count_ <= size_,
262251
"index for insert is out of bounds");
263252

264-
cstr_reserve(str, size_);
253+
cstr_reserve(str, size_ + 1);
265254
pvt_cstr_memmove(&str_[count_], &str_[index], size_ - index);
266255
pvt_fill_(str_, index, count, ch);
267256

@@ -280,7 +269,7 @@ intl_insert_str_range(cstring *str, const size_t index,
280269
pvt_cstr_assert(index <= size_,
281270
"index for insert is out of bounds");
282271

283-
cstr_reserve(str, size_ + count_);
272+
cstr_reserve(str, size_ + count_ + 1);
284273
pvt_cstr_memmove(&str_[index + count_], &str_[index],
285274
size_ - index + 1);
286275

@@ -296,21 +285,22 @@ intl_insert_iter_cnt_ch(cstring *str, cstr_iterator pos,
296285
const size_t count, const char ch)
297286
{
298287
cstring str_ = *str;
299-
cstr_iterator end = cstr_end(str_);
300-
pvt_cstr_assert(pos.it <= end.it,
301-
"Iterators are out of range [first, last)");
288+
pvt_cstr_assert(pos.it <= cstr_end(str_).it,
289+
"[cstr_insert]::Iterators are out of range");
302290

303-
size_t size_ = cstr_size(str_),
304-
count_ = cstr_distance(pos, end);
291+
size_t count_ = cstr_distance(pos, cstr_end(str_)),
292+
size_ = cstr_size(str_) + count_,
293+
pos_ = cstr_size(str_) - count_;
294+
305295

306-
cstr_reserve(&str_, size_ + count);
296+
cstr_reserve(&str_, size_ + 1);
307297
pvt_cstr_memmove(pos.it + count, pos.it, count_);
308-
pvt_fill_(str_, size_ - count_, count, ch);
298+
pvt_fill_(str_, pos_, count, ch);
309299

310-
pvt_set_total_size_(str_, size_ + count);
311-
str_[size_ + count] = (char)0;
300+
pvt_set_total_size_(str_, size_);
301+
str_[size_] = (char)0;
312302
*str = str_;
313-
return cstr_begin(str_ + (size_ - count_));
303+
return cstr_begin(str_ + (pos_));
314304
}
315305

316306

@@ -325,17 +315,17 @@ intl_replace_it_fill_ch(cstring *str, cstr_const_iterator first,
325315
const char ch)
326316
{
327317
cstring str_ = *str;
328-
if((first.it < cstr_cbegin(str_).it) ||
329-
(last.it > cstr_cend(str_).it) ||
330-
(first.it > last.it)) {
331-
return str_;
332-
}
318+
cstring first__ = (cstring)first.it,
319+
last__ = (cstring)last.it;
320+
size_t pos_ = (size_t)(first__ - str_);
333321

334-
size_t count_ = CSTRMIN(count2, cstr_distance(first, last));
335-
336-
cstring ptr = (cstring)first.it;
337-
for(size_t i = 0; i < count_; ++i) ptr[i] = ch;
338-
return str_;
322+
pvt_cstr_assert((first__ >= cstr_cbegin(str_).it) &&
323+
(last__ <= cstr_cend(str_).it) &&
324+
(first__ <= last__),
325+
"[cstr_replace]::Iterators are out of range");
326+
327+
intl_erase_range_citer(str, first, last);
328+
return intl_insert_cnt_ch(str, pos_, count2, ch);
339329
}
340330

341331
cstring __unused
@@ -344,17 +334,47 @@ intl_replace_it_str_off(cstring *str, cstr_const_iterator first,
344334
const size_t count2)
345335
{
346336
cstring str_ = *str;
347-
if((first.it < cstr_cbegin(str_).it) ||
348-
(last.it > cstr_cend(str_).it) ||
349-
(first.it > last.it)) {
350-
return str_;
337+
cstring first__ = (cstring)first.it,
338+
last__ = (cstring)last.it;
339+
size_t count_ = CSTRMIN(count2, pvt_cstr_strlen(other)),
340+
pos_ = (size_t)(first__ - str_);
341+
342+
pvt_cstr_assert((first__ >= cstr_cbegin(str_).it) &&
343+
(last__ <= cstr_cend(str_).it) &&
344+
(first__ <= last__),
345+
"[cstr_replace]::Iterators are out of range");
346+
347+
intl_erase_range_citer(str, first, last);
348+
return intl_insert_str_range(str, pos_, count_, other);
349+
}
350+
351+
/**
352+
* Functions to find
353+
* -----------------
354+
*/
355+
356+
size_t __unused
357+
intl_find_str_range(const_cstring str, const_cstring other,
358+
const size_t pos, const size_t count)
359+
{
360+
size_t size = cstr_size(str);
361+
if (pos >= size || count == 0) return CSTR_NPOS;
362+
if (count > size - pos) return CSTR_NPOS;
363+
364+
for (size_t i = pos; i <= size - count; ++i) {
365+
if (memcmp(str + i, other, count) == 0) {
366+
return i;
367+
}
351368
}
369+
return CSTR_NPOS;
370+
}
352371

353-
size_t count_ = CSTRMIN(cstr_distance(first, last),
354-
CSTRMIN(count2, pvt_cstr_strlen(other)));
355-
356-
cstring ptr = (cstring)first.it;
357-
for(size_t i = 0; i < count_; ++i) ptr[i] = other[i];
358-
359-
return str_;
360-
}
372+
size_t __unused
373+
intl_find_ch_offset(const_cstring str, const char ch, const size_t pos)
374+
{
375+
size_t size = cstr_size(str);
376+
if (pos >= size) return CSTR_NPOS;
377+
378+
char *found = memchr(str + pos, ch, size - pos);
379+
return found ? (size_t)(found - str) : CSTR_NPOS;
380+
}

0 commit comments

Comments
 (0)