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

Commit

Permalink
refactor: Split string.h
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainTHD committed Nov 7, 2020
1 parent 8099577 commit ee7ca49
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 141 deletions.
113 changes: 3 additions & 110 deletions libc/include/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,115 +4,8 @@
#ifndef ROMAINOS_STRING_HPP
#define ROMAINOS_STRING_HPP

#include <stdint.h>
#include <stack>

/**
* Récupère la taille d'une string
*
* @param s String
* @return Taille
*/
u32 getLength(const char* s);

/**
* String
*/
class string {
public:
string() = delete;

/**
* Constructeur
*
* @param s Chaine
*/
explicit string(const char* s) {
this->_s = s;
this->_length = getLength(s);
}

/**
* Destructeur
*/
~string() = default;

/**
* Taille
*
* @return Taille
*/
[[nodiscard]] u32 length() const {
return this->_length;
}

/**
* To char*
*
* @return Pointeur
*/
[[nodiscard]] const char* toChar() const {
return this->_s;
}

private:
/**
* Chaine
*/
const char* _s;

/**
* Taille
*/
u32 _length;
};

/**
* Hex to String
*
* @tparam T Type
* @param value Valeur
*
* @return Hex
*/
template<typename T>
const char* hexToString(T value);

/**
* Int vers string
*
* @tparam T Type
* @param value Int
*
* @return String de l'int
*/
template<typename T>
const char* intToString(T value);

/**
* Hack pour utiliser intToString sur des pointeurs.
* Le <code>value *= -1;</code> ne passe en effet pas pour des pointeurs.
*
* @tparam T Type
* @param value Int*
*
* @return String de l'int
*/
template<typename T>
const char* intToString(T* value);

/**
* Float to string
*
* @tparam T Type
* @param value Value
* @param decimalPlaces Nb de décimales
*
* @return String de float
*/
template <typename T>
const char* floatToString(T value, u8 decimalPlaces = 3);

#include "../string/string.tpp"
#include "../string/stringClass.hpp"
#include "../string/stringC.hpp"
#include "../string/stringRomainOS.hpp"

#endif //ROMAINOS_STRING_HPP
20 changes: 0 additions & 20 deletions libc/string/string.cpp

This file was deleted.

18 changes: 18 additions & 0 deletions libc/string/stringClass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// Created by Romain on 10/09/2020.

#include "stringClass.hpp"
#include "stringC.hpp"

string::string(const char* s) {
this->_s = s;
this->_length = stdstring::strlen(s);
}

u32 string::length() const {
return this->_length;
}

const char* string::toChar() const {
return this->_s;
}
54 changes: 54 additions & 0 deletions libc/string/stringClass.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// Created by Romain on 07/11/2020.

#ifndef ROMAINOS_STRINGCLASS_HPP
#define ROMAINOS_STRINGCLASS_HPP

#include <stdint.h>

/**
* String
*/
class string {
public:
string() = delete;

/**
* Constructeur
*
* @param s Chaine
*/
explicit string(const char* s);

/**
* Destructeur
*/
~string() = default;

/**
* Taille
*
* @return Taille
*/
[[nodiscard]] u32 length() const;

/**
* To char*
*
* @return Pointeur
*/
[[nodiscard]] const char* toChar() const;

private:
/**
* Chaine
*/
const char* _s;

/**
* Taille
*/
u32 _length;
};

#endif //ROMAINOS_STRINGCLASS_HPP
55 changes: 55 additions & 0 deletions libc/string/stringRomainOS.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// Created by Romain on 07/11/2020.

#ifndef ROMAINOS_STRINGROMAINOS_HPP
#define ROMAINOS_STRINGROMAINOS_HPP

/**
* Hex to String
*
* @tparam T Type
* @param value Valeur
*
* @return Hex
*/
template<typename T>
const char* hexToString(T value);

/**
* Int vers string
*
* @tparam T Type
* @param value Int
*
* @return String de l'int
*/
template<typename T>
const char* intToString(T value);

/**
* Hack pour utiliser intToString sur des pointeurs.
* Le <code>value *= -1;</code> ne passe en effet pas pour des pointeurs.
*
* @tparam T Type
* @param value Int*
*
* @return String de l'int
*/
template<typename T>
const char* intToString(T* value);

/**
* Float to string
*
* @tparam T Type
* @param value Value
* @param decimalPlaces Nb de décimales
*
* @return String de float
*/
template <typename T>
const char* floatToString(T value, u8 decimalPlaces = 3);

#include "../string/stringRomainOS.tpp"

#endif //ROMAINOS_STRINGROMAINOS_HPP
22 changes: 11 additions & 11 deletions libc/string/string.tpp → libc/string/stringRomainOS.tpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// Created by Romain on 06/10/2020.

#ifndef ROMAINOS_STRING_TPP
#define ROMAINOS_STRING_TPP
#ifndef ROMAINOS_STRINGROMAINOS_TPP
#define ROMAINOS_STRINGROMAINOS_TPP

#include <cstdint>

Expand Down Expand Up @@ -47,24 +47,24 @@ const char* intToString(T value) {

while (sizeTester / 10 > 0) {
sizeTester /= 10;
size++;
++size;
}

if (isNegative) {
size++;
++size;
}

u64 newValue = (u64) value;
while (newValue / 10 > 0) {
u8 remainder = newValue % 10;
newValue /= 10;
integerToStringOutput[size - index] = (char) (remainder + '0');
index++;
++index;
}

u8 remainder = newValue % 10;
integerToStringOutput[size - index] = (char) (remainder + '0');
index++;
++index;

if (isNegative == 1) {
integerToStringOutput[size - index] = '-';
Expand Down Expand Up @@ -92,19 +92,19 @@ const char* floatToString(T value, u8 decimalPlaces) {
while (*intPtr != '\0') {
*floatPtr = *intPtr;

floatPtr++;
intPtr++;
++floatPtr;
++intPtr;
}

*floatPtr = '.';
floatPtr++;
++floatPtr;

T newValue = value - (int) value;

for (u8 i = 0; i < decimalPlaces; i++) {
newValue *= 10;
*floatPtr = (char) newValue + '0';
floatPtr++;
++floatPtr;
newValue = newValue - (int) newValue;
}

Expand All @@ -113,4 +113,4 @@ const char* floatToString(T value, u8 decimalPlaces) {
return floatToStringOutput;
}

#endif //ROMAINOS_STRING_TPP
#endif //ROMAINOS_STRINGROMAINOS_TPP

0 comments on commit ee7ca49

Please sign in to comment.