Skip to content
Open

Dev #30

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
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
NAME = cbytes
NAME = bytekit

CC = clang
CINCLUDES = -Isrc
Expand Down Expand Up @@ -107,14 +107,19 @@ 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
@$(CC) $(CINCLUDES) $(TESTS_DIRECTORY)/test_copy.c -o $(BIN_DIRECTORY)/test_copy -L$(LIB_DIRECTORY) -l$(NAME) -DTEST_COPY
@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
Expand Down
38 changes: 22 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 ? </br>
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.
4 changes: 2 additions & 2 deletions src/cbytes.c → src/bytekit.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* My library for string
* Author: David Gaspar <davidgaspar.dev@gmail.com>
*/
#include "cbytes.h"
#include "bytekit.h"

// Get argument length
unsigned int length(cbytes_t arg) {
Expand All @@ -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);

Expand Down
23 changes: 9 additions & 14 deletions src/cbytes.h → src/bytekit.h
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
#ifndef DEBUG
#include <stdio.h>
#endif

/*
* cBytes library
* Bytekit library
* Author: David Gaspar <davidgaspar.dev@gmail.com>
*/
#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;
Comment on lines +9 to +10
Copy link

Copilot AI Oct 6, 2025

Choose a reason for hiding this comment

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

The typedef byte_t *bytes_t creates a redundant type alias. Since byte_t is already defined as char, this is equivalent to char *bytes_t, which could be confusing. Consider using char * directly or renaming to be more descriptive like byte_array_t.

Copilot uses AI. Check for mistakes.

#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
#endif
2 changes: 1 addition & 1 deletion tests/copy_test.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <cbytes.h>
#include <bytekit.h>
#include "utils/log.c"

void copy_test(const char *data)
Expand Down
2 changes: 1 addition & 1 deletion tests/index_of_test.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <stdio.h>
#include <cbytes.h>
#include <bytekit.h>
#include "utils/log.c"

void insert_lookup_string(char *chars);
Expand Down
2 changes: 1 addition & 1 deletion tests/length_test.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <stdio.h>
#include <cbytes.h>
#include <bytekit.h>
#include "utils/log.c"

void length_test(const char *data)
Expand Down
2 changes: 1 addition & 1 deletion tests/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <stdio.h>
#include <cbytes.h>
#include <bytekit.h>
#include "copy_test.c"
#include "length_test.c"
#include "index_of_test.c"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_copy.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <cbytes.h>
#include <bytekit.h>
#include "utils/log.c"

int test_copy() {
Expand Down
10 changes: 8 additions & 2 deletions tests/test_equal.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <cbytes.h>
#include <bytekit.h>
#include "utils/log.c"

int test_equal() {
Expand Down Expand Up @@ -41,4 +41,10 @@ int test_equal() {
}

return 0; // All tests passed
}
}

#ifdef TEST_EQUAL
int main() {
return test_equal();
}
#endif