1
1
mirror of https://github.com/theoludwig/libcproject.git synced 2025-05-21 23:21:15 +02:00

15 Commits

Author SHA1 Message Date
2fd8d102e9 feat: add mathematics_max, mathematics_min
Also add `mathematics_max_values` and
`mathematics_min_values` to check in array.
2024-09-13 15:35:19 +02:00
85ce5228ef feat: add mathematics_opposite 2024-09-13 14:44:26 +02:00
c49d5f5421 feat: add string_zero_pad 2024-09-12 12:31:58 +02:00
35b868d0c1 feat: add string_pad_start 2024-09-12 12:22:53 +02:00
7683aa1db7 docs(license): add email address 2024-01-30 01:25:17 +01:00
6eee39fffb chore(release): 4.2.1 [skip ci] 2023-12-26 20:20:38 +00:00
ab9860e969 fix: markdownlint in LICENSE 2023-12-26 21:19:20 +01:00
a50773e058 chore(release): 4.2.0 [skip ci] 2023-12-26 19:47:09 +00:00
1e0bf99ef6 feat: add string_last_position_of 2023-12-26 20:40:46 +01:00
ec6e748d24 feat: add string_position_of 2023-12-26 20:30:54 +01:00
9bb21e070f build(deps): update latest 2023-12-26 19:42:12 +01:00
bb9c7a1668 Merge branch 'master' of github.com:theoludwig/libcproject 2023-10-23 23:02:35 +02:00
211648d29f chore: better Prettier config for easier reviews 2023-10-23 23:02:29 +02:00
574aeb414e chore(release): 4.1.1 [skip ci] 2023-10-13 09:07:49 +00:00
e0115dd7d9 fix: error in array_list_remove (always removed the last index and not the index given) (#6)
Co-authored-by: Maxime Rumpler <mrumpler68@gmail.com>
2023-10-13 11:04:38 +02:00
23 changed files with 408 additions and 75 deletions

View File

@ -1,2 +1,2 @@
BasedOnStyle: 'Google'
BasedOnStyle: "Google"
ColumnLimit: 0

View File

@ -1,8 +1,8 @@
---
name: '🐛 Bug Report'
about: 'Report an unexpected problem or unintended behavior.'
title: '[Bug]'
labels: 'bug'
name: "🐛 Bug Report"
about: "Report an unexpected problem or unintended behavior."
title: "[Bug]"
labels: "bug"
---
<!--

View File

@ -1,8 +1,8 @@
---
name: '📜 Documentation'
about: 'Correct spelling errors, improvements or additions to documentation files (README, CONTRIBUTING...).'
title: '[Documentation]'
labels: 'documentation'
name: "📜 Documentation"
about: "Correct spelling errors, improvements or additions to documentation files (README, CONTRIBUTING...)."
title: "[Documentation]"
labels: "documentation"
---
<!-- Please make sure your issue has not already been fixed. -->

View File

@ -1,8 +1,8 @@
---
name: '✨ Feature Request'
about: 'Suggest a new feature idea.'
title: '[Feature]'
labels: 'feature request'
name: "✨ Feature Request"
about: "Suggest a new feature idea."
title: "[Feature]"
labels: "feature request"
---
<!-- Please make sure your issue has not already been fixed. -->

View File

@ -1,8 +1,8 @@
---
name: '🔧 Improvement'
about: 'Improve structure/format/performance/refactor/tests of the code.'
title: '[Improvement]'
labels: 'improvement'
name: "🔧 Improvement"
about: "Improve structure/format/performance/refactor/tests of the code."
title: "[Improvement]"
labels: "improvement"
---
<!-- Please make sure your issue has not already been fixed. -->

View File

@ -1,8 +1,8 @@
---
name: '🙋 Question'
about: 'Further information is requested.'
title: '[Question]'
labels: 'question'
name: "🙋 Question"
about: "Further information is requested."
title: "[Question]"
labels: "question"
---
### Question

View File

@ -1,4 +1,4 @@
name: 'CI'
name: "CI"
on:
push:
@ -8,33 +8,33 @@ on:
jobs:
ci:
runs-on: 'ubuntu-latest'
runs-on: "ubuntu-latest"
steps:
- uses: 'actions/checkout@v3.5.3'
- uses: "actions/checkout@v4.1.1"
- run: 'sudo apt update'
- run: "sudo apt update"
- name: 'Install Build Tools'
run: 'sudo apt install --yes build-essential gcc make clang-format'
- name: "Install Build Tools"
run: "sudo apt install --yes build-essential gcc make clang-format"
- name: 'Install Documentation Tools'
run: 'sudo apt install --yes doxygen doxygen-gui doxygen-doc graphviz'
- name: "Install Documentation Tools"
run: "sudo apt install --yes doxygen doxygen-gui doxygen-doc graphviz"
- run: 'gcc --version'
- run: "gcc --version"
- run: 'make'
- run: 'make run'
- run: 'make test'
- run: 'make lint'
- run: 'make documentation'
- run: 'make set_version'
- run: 'make clean'
- run: "make"
- run: "make run"
- run: "make test"
- run: "make lint"
- run: "make documentation"
- run: "make set_version"
- run: "make clean"
lint-commit:
runs-on: 'ubuntu-latest'
steps:
- uses: 'actions/checkout@v3.5.3'
with:
fetch-depth: 0
runs-on: "ubuntu-latest"
steps:
- uses: "actions/checkout@v4.1.1"
with:
fetch-depth: 0
- uses: 'wagoid/commitlint-github-action@v5.4.1'
- uses: "wagoid/commitlint-github-action@v5.4.4"

View File

@ -1,4 +1,4 @@
name: 'Release'
name: "Release"
on:
push:
@ -6,52 +6,52 @@ on:
jobs:
release:
runs-on: 'ubuntu-latest'
runs-on: "ubuntu-latest"
steps:
- uses: 'actions/checkout@v3.5.3'
- uses: "actions/checkout@v4.1.1"
with:
fetch-depth: 0
persist-credentials: false
submodules: recursive
submodules: "recursive"
- name: 'Import GPG key'
uses: 'crazy-max/ghaction-import-gpg@v5.3.0'
- name: "Import GPG key"
uses: "crazy-max/ghaction-import-gpg@v6.1.0"
with:
gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }}
git_user_signingkey: true
git_commit_gpgsign: true
- run: 'sudo apt update'
- run: "sudo apt update"
- name: 'Install Build Tools'
run: 'sudo apt install --yes build-essential gcc make clang-format'
- name: "Install Build Tools"
run: "sudo apt install --yes build-essential gcc make clang-format"
- name: 'Install Documentation Tools'
run: 'sudo apt install --yes doxygen doxygen-gui doxygen-doc graphviz'
- name: "Install Documentation Tools"
run: "sudo apt install --yes doxygen doxygen-gui doxygen-doc graphviz"
- run: 'make set_version'
- run: "make set_version"
- name: 'Use Node.js'
uses: 'actions/setup-node@v3.7.0'
- name: "Use Node.js"
uses: "actions/setup-node@v4.0.1"
with:
node-version: '18.17.0'
node-version: "20.10.0"
- name: 'Install Release Tools'
run: 'npm install --save-dev semantic-release@21.0.7 @commitlint/cli@17.6.7 @commitlint/config-conventional@17.6.7 @semantic-release/git@10.0.1 @semantic-release/exec@6.0.3 @saithodev/semantic-release-backmerge@3.2.0 vercel@31.2.2'
- name: "Install Release Tools"
run: "npm install --save-dev semantic-release@22.0.12 @commitlint/cli@18.4.3 @commitlint/config-conventional@18.4.3 @semantic-release/git@10.0.1 @semantic-release/exec@6.0.3 @saithodev/semantic-release-backmerge@4.0.1 vercel@33.0.1"
- run: 'rm --force package.json package-lock.json'
- run: "rm --force package.json package-lock.json"
- name: 'Release'
run: 'npx semantic-release'
- name: "Release"
run: "npx semantic-release"
env:
GH_TOKEN: ${{ secrets.GH_TOKEN }}
GIT_COMMITTER_NAME: ${{ secrets.GIT_NAME }}
GIT_COMMITTER_EMAIL: ${{ secrets.GIT_EMAIL }}
- name: 'Generate Documentation'
run: 'make documentation'
- name: "Generate Documentation"
run: "make documentation"
- name: 'Deploy to Vercel'
- name: "Deploy to Vercel"
run: 'npx vercel ./documentation/html --token="${VERCEL_TOKEN}" --prod'
env:
VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }}

View File

@ -12,7 +12,7 @@ FILE_PATTERNS = *.h \
CODE_OF_CONDUCT.md \
CONTRIBUTING.md \
LICENSE
HTML_EXTRA_STYLESHEET = doxygen-awesome-css/doxygen-awesome.css
HTML_EXTRA_STYLESHEET = doxygen-awesome-css/doxygen-awesome.css
RECURSIVE = YES
EXCLUDE = test doxygen-awesome-css node_modules

View File

@ -1,6 +1,6 @@
MIT License
# MIT License
Copyright (c) Théo LUDWIG
Copyright (c) Théo LUDWIG <contact@theoludwig.fr>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@ -5,11 +5,11 @@
</p>
<p align="center">
<a href="./CONTRIBUTING.md"><img src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat" /></a>
<a href="./CONTRIBUTING.md"><img src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat" alt="Contributing" /></a>
<a href="./LICENSE"><img src="https://img.shields.io/badge/licence-MIT-blue.svg" alt="Licence MIT"/></a>
<a href="./CODE_OF_CONDUCT.md"><img src="https://img.shields.io/badge/Contributor%20Covenant-v2.0%20adopted-ff69b4.svg" alt="Contributor Covenant" /></a>
<br />
<a href="https://github.com/theoludwig/libcproject/actions/workflows/ci.yml"><img src="https://github.com/theoludwig/libcproject/actions/workflows/ci.yml/badge.svg?branch=develop" /></a>
<a href="https://github.com/theoludwig/libcproject/actions/workflows/ci.yml"><img src="https://github.com/theoludwig/libcproject/actions/workflows/ci.yml/badge.svg?branch=develop" alt="CI" /></a>
<a href="https://conventionalcommits.org"><img src="https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg" alt="Conventional Commits" /></a>
<a href="https://github.com/semantic-release/semantic-release"><img src="https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg" alt="semantic-release" /></a>
</p>

View File

@ -25,8 +25,8 @@ void array_list_remove(struct array_list* list, size_t index) {
if (index >= list->size) {
return;
}
for (size_t i = index + 1; i < list->size - 1; i++) {
list->data[i - 1] = list->data[i];
for (size_t i = index; i < list->size - 1; i++) {
list->data[i] = list->data[i + 1];
}
list->size--;
}

View File

@ -32,3 +32,39 @@ float mathematics_square_root(float number) {
unsigned long long mathematics_factorial(unsigned long long number) {
return number == 0 ? 1 : number * mathematics_factorial(number - 1);
}
int64_t mathematics_opposite(int64_t number) {
return number * -1;
}
int64_t mathematics_max(int64_t number1, int64_t number2) {
return number1 > number2 ? number1 : number2;
}
int64_t mathematics_max_values(int64_t *values, size_t values_length) {
int64_t max = 0;
if (values_length <= 0) {
return max;
}
max = values[0];
for (size_t index = 1; index < values_length; index++) {
max = mathematics_max(max, values[index]);
}
return max;
}
int64_t mathematics_min(int64_t number1, int64_t number2) {
return number1 > number2 ? number2 : number1;
}
int64_t mathematics_min_values(int64_t *values, size_t values_length) {
int64_t min = 0;
if (values_length <= 0) {
return min;
}
min = values[0];
for (size_t index = 1; index < values_length; index++) {
min = mathematics_min(min, values[index]);
}
return min;
}

View File

@ -5,6 +5,7 @@
#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
#include "types.h"
@ -66,4 +67,59 @@ float mathematics_square_root(float number);
*/
unsigned long long mathematics_factorial(unsigned long long number);
/**
* @brief Calulcates the opposite number (additive inverse).
*
* @param number
* @return int64_t
*
* @code
* mathematics_opposite(7) // -7
*
* mathematics_opposite(-7) // 7
* @endcode
* @since vTODO
*/
int64_t mathematics_opposite(int64_t number);
/**
* @brief Returns the largest number between 2 numbers.
*
* @param number1
* @param number2
* @return int64_t
* @since vTODO
*/
int64_t mathematics_max(int64_t number1, int64_t number2);
/**
* @brief Returns the largest number between multiple numbers. If the array is empty, returns 0.
*
* @param values
* @param values_length
* @return int64_t
* @since vTODO
*/
int64_t mathematics_max_values(int64_t *values, size_t values_length);
/**
* @brief Returns the smallest number between 2 numbers.
*
* @param number1
* @param number2
* @return int64_t
* @since vTODO
*/
int64_t mathematics_min(int64_t number1, int64_t number2);
/**
* @brief Returns the smallest number between multiple numbers. If the array is empty, returns 0.
*
* @param values
* @param values_length
* @return int64_t
* @since vTODO
*/
int64_t mathematics_min_values(int64_t *values, size_t values_length);
#endif

View File

@ -384,3 +384,59 @@ bool string_ends_with(const string_t string, const string_t prefix) {
}
return ends_with;
}
size_t string_position_of(const string_t string, const char character) {
size_t position_found = 0;
size_t string_length = string_get_length(string);
for (size_t index = 0; index < string_length && position_found == 0; index++) {
if (string[index] == character) {
position_found = index + 1;
}
}
return position_found;
}
size_t string_last_position_of(const string_t string, const char character) {
size_t position_found = 0;
size_t string_length = string_get_length(string);
while (string_length > 0 && position_found == 0) {
if (string[string_length - 1] == character) {
position_found = string_length;
}
string_length--;
}
return position_found;
}
string_t string_pad_start(const string_t string, const string_t pad_string, size_t target_length) {
string_t result = malloc(sizeof(char) * (target_length + 1));
size_t initial_length = string_get_length(string);
size_t left_length = target_length - initial_length;
if (target_length <= initial_length) {
left_length = 0;
}
size_t pad_length = string_get_length(pad_string);
size_t count_pad_string = 0;
size_t index_initial_string = 0;
for (size_t index = 0; index < target_length; index++) {
if (index < left_length) {
size_t index_pad_string = count_pad_string % pad_length;
result[index] = pad_string[index_pad_string];
count_pad_string += 1;
} else {
result[index] = string[index_initial_string];
index_initial_string += 1;
}
}
result[target_length] = '\0';
return result;
}
string_t string_zero_pad(uint64_t number, size_t places) {
string_t number_string = convert_number_to_string((long long)number);
string_t pad_string = string_copy("0");
string_t result = string_pad_start(number_string, pad_string, places);
free(pad_string);
free(number_string);
return result;
}

View File

@ -224,6 +224,7 @@ bool string_get_is_substring(const string_t string, const string_t substring);
* @param number
* @param separator
* @return string_t
*
* @code
* string_get_formatted_number(1000, " ") // "1 000"
* string_get_formatted_number(1000, ",") // "1,000"
@ -264,4 +265,63 @@ bool string_starts_with(const string_t string, const string_t prefix);
*/
bool string_ends_with(const string_t string, const string_t prefix);
/**
* @brief Returns the position (index + 1) within the string of the first occurrence of the specified substring (0 if not found).
*
* @param string
* @param substring
* @return size_t
*
* @code
* string_position_of("hello world", 'e') // 2
* @endcode
* @since v4.2.0
*/
size_t string_position_of(const string_t string, const char character);
/**
* @brief Returns the position (index + 1) within the string of the last occurrence of the specified substring (0 if not found).
*
* @param string
* @param character
* @return size_t
*
* @code
* string_last_position_of("hello world", 'o') // 8
* @endcode
* @since v4.2.0
*/
size_t string_last_position_of(const string_t string, const char character);
/**
* @brief Pads a `string` with another `pad_string` (multiple times, if needed) until the resulting string reaches the `target_length`. The padding is applied from the start (left) of the string.
*
* @param string The string to pad.
* @param pad_string The string to pad the current string with, to the left.
* @param target_length
* @return string_t
*
* @code
* string_pad_start("hello", " ", 10) // " hello"
* @endcode
* @since vTODO
*/
string_t string_pad_start(const string_t string, const string_t pad_string, size_t target_length);
/**
* @brief Pad a number with zeros.
*
* @param number
* @param places
* @return string_t
*
* @code
* string_zero_pad(1, 2) // "01"
*
* string_zero_pad(10, 2) // "10"
* @endcode
* @since vTODO
*/
string_t string_zero_pad(uint64_t number, size_t places);
#endif

View File

@ -34,4 +34,15 @@ void array_list_test() {
assert(array_list_get(list, 100) == (void *)95);
array_list_free(list);
list = array_list_initialization();
array_list_add(list, (void *)'a');
array_list_add(list, (void *)'b');
array_list_add(list, (void *)'c');
array_list_remove(list, 1);
assert(array_list_get(list, 0) == (void *)'a');
assert(array_list_get(list, 1) == (void *)'c');
assert(list->size == 2);
array_list_free(list);
}

View File

@ -6,6 +6,11 @@ void mathematics_test() {
mathematics_root_test();
mathematics_square_root_test();
mathematics_factorial_test();
mathematics_opposite_test();
mathematics_max_test();
mathematics_max_values_test();
mathematics_min_test();
mathematics_min_values_test();
}
void mathematics_absolute_value_test() {
@ -56,3 +61,38 @@ void mathematics_factorial_test() {
assert(mathematics_factorial(9) == 362880);
assert(mathematics_factorial(10) == 3628800);
}
void mathematics_opposite_test() {
assert(mathematics_opposite(-7) == 7);
assert(mathematics_opposite(7) == -7);
}
void mathematics_max_test() {
assert(mathematics_max(0, 0) == 0);
assert(mathematics_max(0, 1) == 1);
assert(mathematics_max(2, 0) == 2);
assert(mathematics_max(54, 37) == 54);
}
void mathematics_max_values_test() {
int64_t values[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
assert(mathematics_max_values(values, 10) == 9);
int64_t values2[] = {8, 6, 4, 7};
assert(mathematics_max_values(values2, 4) == 8);
}
void mathematics_min_test() {
assert(mathematics_min(0, 0) == 0);
assert(mathematics_min(3, 5) == 3);
assert(mathematics_min(2, 1) == 1);
assert(mathematics_min(54, 37) == 37);
}
void mathematics_min_values_test() {
int64_t values[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
assert(mathematics_min_values(values, 10) == 0);
int64_t values2[] = {9, 6, 8, 7};
assert(mathematics_min_values(values2, 4) == 6);
}

View File

@ -17,4 +17,14 @@ void mathematics_square_root_test();
void mathematics_factorial_test();
void mathematics_opposite_test();
void mathematics_max_test();
void mathematics_max_values_test();
void mathematics_min_test();
void mathematics_min_values_test();
#endif

View File

@ -25,6 +25,10 @@ void string_test() {
string_get_last_occurence_of_character_test();
string_starts_with_test();
string_ends_with_test();
string_position_of_test();
string_last_position_of_test();
string_pad_start_test();
string_zero_pad_test();
}
void string_get_length_test() {
@ -277,3 +281,55 @@ void string_ends_with_test() {
assert(!string_ends_with("abcdef", "bcd"));
assert(!string_ends_with("abcdef", "abcdefg"));
}
void string_position_of_test() {
assert(string_position_of("hello world", 'e') == 2);
assert(string_position_of("hello world", 'o') == 5);
assert(string_position_of("abcdef", 'a') == 1);
assert(string_position_of("abcdef", 'b') == 2);
assert(string_position_of("abcdef", 'c') == 3);
assert(string_position_of("abcdef", 'd') == 4);
assert(string_position_of("abcdef", 'e') == 5);
assert(string_position_of("abcdef", 'f') == 6);
assert(string_position_of("abcdef", 'g') == 0);
}
void string_last_position_of_test() {
assert(string_last_position_of("hello world", 'e') == 2);
assert(string_last_position_of("hello world", 'o') == 8);
assert(string_last_position_of("abcdef", 'a') == 1);
assert(string_last_position_of("abcdef", 'b') == 2);
assert(string_last_position_of("abcdef", 'c') == 3);
assert(string_last_position_of("abcdef", 'd') == 4);
assert(string_last_position_of("abcdef", 'e') == 5);
assert(string_last_position_of("abcdef", 'f') == 6);
assert(string_last_position_of("abcdef", 'g') == 0);
}
void string_pad_start_test() {
string_t result = string_pad_start("hello", "ab", 10);
assert(assert_string_equal(result, "ababahello"));
free(result);
result = string_pad_start("hello", "ab", 4);
assert(assert_string_equal(result, "hell"));
free(result);
result = string_pad_start("hello", "ab", 5);
assert(assert_string_equal(result, "hello"));
free(result);
result = string_pad_start("hello", "ab", 6);
assert(assert_string_equal(result, "ahello"));
free(result);
}
void string_zero_pad_test() {
string_t result = string_zero_pad(1, 2);
assert(assert_string_equal(result, "01"));
free(result);
result = string_zero_pad(10, 2);
assert(assert_string_equal(result, "10"));
free(result);
}

View File

@ -58,4 +58,12 @@ void string_starts_with_test();
void string_ends_with_test();
void string_position_of_test();
void string_last_position_of_test();
void string_pad_start_test();
void string_zero_pad_test();
#endif

View File

@ -1,4 +1,4 @@
#ifndef __LIBCPROJECT_VERSION__
#define __LIBCPROJECT_VERSION__ "4.1.0"
#define __LIBCPROJECT_VERSION__ "4.2.1"
#endif