diff --git a/Makefile b/Makefile index cdc2265..7b9b348 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -NAME = cbytes +NAME = bytekit CC = clang CINCLUDES = -Isrc @@ -107,7 +107,7 @@ TARGETS = \ .PHONY: $(TARGETS) test: $(BIN_DIRECTORY) library - @$(CC) $(CINCLUDES) $(TESTS_DIRECTORY)/main.c -o $(BIN_DIRECTORY)/test -L$(LIB_DIRECTORY) -lcbytes + @$(CC) $(CINCLUDES) $(TESTS_DIRECTORY)/main.c -o $(BIN_DIRECTORY)/test -L$(LIB_DIRECTORY) -l$(NAME) @echo "[ OK ] Test file created: $(BIN_DIRECTORY)/test" test_copy: $(BIN_DIRECTORY) library @@ -115,6 +115,11 @@ test_copy: $(BIN_DIRECTORY) library @echo "[ OK ] Test copy file created: $(BIN_DIRECTORY)/test_copy" @./$(BIN_DIRECTORY)/test_copy +test_equal: $(BIN_DIRECTORY) library + @$(CC) $(CINCLUDES) $(TESTS_DIRECTORY)/test_equal.c -o $(BIN_DIRECTORY)/test_equal -L$(LIB_DIRECTORY) -l$(NAME) -DTEST_EQUAL + @echo "[ OK ] Test equal file created: $(BIN_DIRECTORY)/test_equal" + @./$(BIN_DIRECTORY)/test_equal + library: $(LIB_DIRECTORY) $(BUILDERS) ifdef CTARGET @ar -rc $(LIB_DIRECTORY)/lib$(NAME)_$(CTARGET).a $(BUILD_DIRECTORY)/$(NAME).o diff --git a/README.md b/README.md index 413b780..6985c80 100644 --- a/README.md +++ b/README.md @@ -1,39 +1,45 @@ -# cBytes +# bytekit -Char bytes is based on a series of uniformly creasing addresses, to which each address has a binary value and that value corresponds to character in the **ASCII table**. +**bytekit** is a C library for manipulating character arrays (strings) as sequences of bytes, where each address holds a binary value corresponding to a character in the ASCII table. +--- -## Character array and character pointer +## Character array vs. character pointer -What is the difference between character array and character pointer ?
+What is the difference between a character array and a character pointer? Consider the following example: ```c char arr[] = "Hello World"; // array version -char ptr* = "Hello World"; // pointer version +char *ptr = "Hello World"; // pointer version ``` -The type of both the variables is a pointer to `char` or `(char\*)`, so you can pass either of them to a function whose formal argument accepts an array of characters or character pointer. In C programming, the name of an array always points to the base address, roughly speaking, an array is a pointer. -Here are differences: -- 1 `arr` is an array of `12` characters. When compiler sees the statement: +- `arr` is an array of 12 characters. When the compiler sees: ```c char arr[] = "Hello World"; ``` - It allocates `12` consecutive bytes of memory and associates the address of the first allocated byte with `arr`. + it allocates 12 consecutive bytes of memory and associates the address of the first byte with `arr`. ![allocations for char array in c](https://overiq.com/media/uploads/character-array-in-memory-1504599203175.png) - On the other hand when the compiler sees the statement. + +- `ptr` is a pointer to a string literal. When the compiler sees: ```c - char ptr* = "Hello World"; + char *ptr = "Hello World"; ``` - It allocates `12` consecutive bytes for string literal `"Hello World"` and `4` extra bytes for pointer variable `ptr`. And assigns the address of the string literal to `ptr`. So, in this case, a total of `16` bytes are allocated. + it allocates 12 bytes for the string literal `"Hello World"` (in read-only memory) and extra bytes for the pointer variable `ptr`. The pointer is assigned the address of the string literal. ![allocations for char pointer in c](https://overiq.com/media/uploads/2020/07/26/character-pointer-and-string-literal-1504599248003.png) -## This library +In C, the name of an array points to the base address, but an array and a pointer are not the same: +- An array allocates storage for the data. +- A pointer just stores an address, which may point to a string literal (read-only) or to a buffer (modifiable). + +--- + +## Library functions -In this library we have the following functions for manipulating strings: +This library provides the following functions for manipulating strings: - `length`: get the length of a string. - `equal`: check if two strings are the same. -- `copy`: copy characters from one string to another string. -- `subcopy`: copy a specific portion of a string, using the starting index and an ending index, to another string. +- `copy`: copy characters from one string to another. +- `subcopy`: copy a specific portion of a string, using the starting and ending index, to another string. - `subcopy_len`: copy a specific portion of a string, using the length, to another string. - `subcopy_index_len`: copy a specific portion of a string, using the starting index and the length, to another string. \ No newline at end of file diff --git a/src/cbytes.c b/src/bytekit.c similarity index 98% rename from src/cbytes.c rename to src/bytekit.c index c671517..295a8fd 100644 --- a/src/cbytes.c +++ b/src/bytekit.c @@ -2,7 +2,7 @@ * My library for string * Author: David Gaspar */ -#include "cbytes.h" +#include "bytekit.h" // Get argument length unsigned int length(cbytes_t arg) { @@ -18,7 +18,7 @@ unsigned int length(cbytes_t arg) { } // Check that the values of two string (bytes_t ) are equal. -ubyte_t equal(cbytes_t bytes_1, cbytes_t bytes_2) { +byte_t equal(cbytes_t bytes_1, cbytes_t bytes_2) { unsigned int bytes_1_length = length(bytes_1); unsigned int bytes_2_length = length(bytes_2); diff --git a/src/cbytes.h b/src/bytekit.h similarity index 68% rename from src/cbytes.h rename to src/bytekit.h index 7a9d200..5c73a3f 100644 --- a/src/cbytes.h +++ b/src/bytekit.h @@ -1,31 +1,26 @@ -#ifndef DEBUG -#include -#endif - /* - * cBytes library + * Bytekit library * Author: David Gaspar */ -#ifndef CBYTES_H_ -#define CBYTES_H_ +#ifndef BYTEKIT_H +#define BYTEKIT_H typedef char byte_t; -typedef unsigned char ubyte_t; -typedef char *bytes_t; -typedef const char *cbytes_t; +typedef byte_t *bytes_t; +typedef const byte_t *cbytes_t; #ifndef bool -#define true (ubyte_t)1 -#define false (ubyte_t)0 +#define true (byte_t)1 +#define false (byte_t)0 #endif unsigned int length(cbytes_t arg); int index_of(cbytes_t target, cbytes_t fragment); -ubyte_t equal(cbytes_t bytes_1, cbytes_t bytes_2); +byte_t equal(cbytes_t bytes_1, cbytes_t bytes_2); byte_t copy(cbytes_t src, bytes_t dst, unsigned long sizeof_dst); byte_t subcopy(cbytes_t src, unsigned int start_index, unsigned int end_index, bytes_t dst, unsigned long sizeof_dst); byte_t subcopy_len(cbytes_t src, unsigned int dst_length, bytes_t dst, unsigned long sizeof_dst); byte_t subcopy_index_len(cbytes_t src, unsigned int start_index, unsigned int dst_length, bytes_t dst, unsigned long sizeof_dst); -#endif \ No newline at end of file +#endif diff --git a/tests/copy_test.c b/tests/copy_test.c index 0eab7a4..591a2a5 100644 --- a/tests/copy_test.c +++ b/tests/copy_test.c @@ -1,4 +1,4 @@ -#include +#include #include "utils/log.c" void copy_test(const char *data) diff --git a/tests/index_of_test.c b/tests/index_of_test.c index 8b0c015..5e002da 100644 --- a/tests/index_of_test.c +++ b/tests/index_of_test.c @@ -1,5 +1,5 @@ #include -#include +#include #include "utils/log.c" void insert_lookup_string(char *chars); diff --git a/tests/length_test.c b/tests/length_test.c index a3acafd..3d2717b 100644 --- a/tests/length_test.c +++ b/tests/length_test.c @@ -1,5 +1,5 @@ #include -#include +#include #include "utils/log.c" void length_test(const char *data) diff --git a/tests/main.c b/tests/main.c index 0449b3a..793bbb5 100644 --- a/tests/main.c +++ b/tests/main.c @@ -1,5 +1,5 @@ #include -#include +#include #include "copy_test.c" #include "length_test.c" #include "index_of_test.c" diff --git a/tests/test_copy.c b/tests/test_copy.c index 2c9a5b1..8cd605b 100644 --- a/tests/test_copy.c +++ b/tests/test_copy.c @@ -1,4 +1,4 @@ -#include +#include #include "utils/log.c" int test_copy() { diff --git a/tests/test_equal.c b/tests/test_equal.c index 1c25e34..7479922 100644 --- a/tests/test_equal.c +++ b/tests/test_equal.c @@ -1,4 +1,4 @@ -#include +#include #include "utils/log.c" int test_equal() { @@ -41,4 +41,10 @@ int test_equal() { } return 0; // All tests passed -} \ No newline at end of file +} + +#ifdef TEST_EQUAL +int main() { + return test_equal(); +} +#endif \ No newline at end of file