Skip to content
This repository has been archived by the owner on Apr 7, 2022. It is now read-only.

Commit

Permalink
new: Strings C
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainTHD committed Nov 7, 2020
1 parent ee7ca49 commit e763e0f
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
41 changes: 41 additions & 0 deletions libc/string/stringC.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// Created by Romain on 10/09/2020.

#include "stringC.hpp"

namespace stdstring {
int strcmp(const char* a, const char* b) {
u32 i = 0;
while (true) {
if (a[i] < b[i]) {
return -1;
}
else if (a[i] > b[i]) {
return 1;
}
else {
if (a[i] == '\0') {
return 0;
}
++i;
}
}
}

u32 strlen(const char* str) {
u32 i = 0;
while (str[i] != '\0') {
++i;
}
return i;
}

char* strdup(const char* str) {
int len = strlen(str);
return (char*) stdlib::memcpy(malloc(sizeof(char) * (len + 1)), str, len + 1);;
}

char* strcpy(char* dest, const char* src) {
return (char*) stdlib::memcpy(dest, src, strlen(src) + 1);
}
}
49 changes: 49 additions & 0 deletions libc/string/stringC.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// Created by Romain on 07/11/2020.

#ifndef ROMAINOS_STRINGC_HPP
#define ROMAINOS_STRINGC_HPP

#include <memory.h>

namespace stdstring {
/**
* Compare deux string
*
* @param a String 1
* @param b String 2
*
* @return 0 si a == b, -1 si a < b, 1 si a > b
*/
int strcmp(const char* a, const char* b);

/**
* Calcule la taille d'une chaîne
*
* @param str String
*
* @return Taille
*/
u32 strlen(const char* str);

/**
* Duplique une chaine en effectuant un malloc
*
* @param str Chaine
*
* @return Taille
*/
char* strdup(const char* str);

/**
* Copie une chaine
*
* @param dest Destination
* @param src Source
*
* @return Chaine copiée
*/
char* strcpy(char* dest, const char* src);
}

#endif //ROMAINOS_STRINGC_HPP

0 comments on commit e763e0f

Please sign in to comment.