feat: initial version
This commit is contained in:
parent
5cad2db883
commit
6428ed06b0
16 changed files with 263 additions and 0 deletions
21
hdrs/libft.h
Normal file
21
hdrs/libft.h
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
#ifndef LIBFT_H
|
||||
# define LIBFT_H
|
||||
|
||||
# include <stdlib.h>
|
||||
|
||||
int ft_islower(int c);
|
||||
int ft_isupper(int c);
|
||||
int ft_tolower(int c);
|
||||
int ft_toupper(int c);
|
||||
int ft_isdigit(int c);
|
||||
int ft_isalpha(int c);
|
||||
void ft_bzero(void *s, size_t n);
|
||||
void *ft_memset(void *s, int c, size_t n);
|
||||
void *ft_memset32(void *s, int c, size_t n);
|
||||
void *ft_memset64(void *s, int c, size_t n);
|
||||
void *ft_memcpy(void *dst, const void *src, size_t n);
|
||||
void *ft_memcpy32(void *dst, const void *src, size_t n);
|
||||
void *ft_memcpy64(void *dst, const void *src, size_t n);
|
||||
int ft_atoi(const char *str);
|
||||
|
||||
#endif
|
||||
29
srcs/ft_atoi.c
Normal file
29
srcs/ft_atoi.c
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#include "libft.h"
|
||||
|
||||
int ft_atoi(const char *str)
|
||||
{
|
||||
int i = 0;
|
||||
int r = 0;
|
||||
int s = 1;
|
||||
|
||||
while (str[i] == ' ' || (str[i] >= '\t' && str[i] <= '\r'))
|
||||
i++;
|
||||
|
||||
if (str[i] == '-')
|
||||
{
|
||||
s = -1;
|
||||
i++;
|
||||
}
|
||||
else if (str[i] == '+')
|
||||
i++;
|
||||
|
||||
while(str[i] != '\0')
|
||||
{
|
||||
if (str[i] >= '0' && str[i] <= '9')
|
||||
r = r * 10 + str[i] - '0';
|
||||
else
|
||||
break;
|
||||
i++;
|
||||
}
|
||||
return (s * r);
|
||||
}
|
||||
6
srcs/ft_bzero.c
Normal file
6
srcs/ft_bzero.c
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
#include "libft.h"
|
||||
|
||||
void ft_bzero(void *s, size_t n)
|
||||
{
|
||||
ft_memset(s, '\0', n);
|
||||
}
|
||||
7
srcs/ft_isalpha.c
Normal file
7
srcs/ft_isalpha.c
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
int ft_isalpha(int c)
|
||||
{
|
||||
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
|
||||
return (1);
|
||||
else
|
||||
return (0);
|
||||
}
|
||||
8
srcs/ft_isdigit.c
Normal file
8
srcs/ft_isdigit.c
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#include "libft.h"
|
||||
|
||||
int ft_isdigit(int c)
|
||||
{
|
||||
if (c >= '0' && c <= '9')
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
8
srcs/ft_islower.c
Normal file
8
srcs/ft_islower.c
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#include "libft.h"
|
||||
|
||||
int ft_islower(int c)
|
||||
{
|
||||
if (c >= 'a' && c <= 'z')
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
8
srcs/ft_isupper.c
Normal file
8
srcs/ft_isupper.c
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#include "libft.h"
|
||||
|
||||
int ft_isupper(int c)
|
||||
{
|
||||
if (c >= 'A' && c <= 'Z')
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
12
srcs/ft_memcpy.c
Normal file
12
srcs/ft_memcpy.c
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
#include <stddef.h>
|
||||
|
||||
void *ft_memcpy(void *dst, const void *src, size_t n) {
|
||||
unsigned char *d = (unsigned char *)dst;
|
||||
const unsigned char *s = (const unsigned char *)src;
|
||||
|
||||
while (n--) {
|
||||
*d++ = *s++;
|
||||
}
|
||||
|
||||
return dst;
|
||||
}
|
||||
25
srcs/ft_memcpy32.c
Normal file
25
srcs/ft_memcpy32.c
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
void *ft_memcpy32(void *dst, const void *src, size_t n) {
|
||||
size_t i;
|
||||
uint32_t *lp_dst;
|
||||
const uint32_t *lp_src;
|
||||
unsigned char *bp_dst;
|
||||
const unsigned char *bp_src;
|
||||
|
||||
lp_dst = (uint32_t *)dst;
|
||||
lp_src = (const uint32_t *)src;
|
||||
|
||||
for (i = 0; i < n / 4; i++) {
|
||||
lp_dst[i] = lp_src[i];
|
||||
}
|
||||
|
||||
bp_dst = (unsigned char *)(lp_dst + i);
|
||||
bp_src = (const unsigned char *)(lp_src + i);
|
||||
for (i = 0; i < n % 4; i++) {
|
||||
bp_dst[i] = bp_src[i];
|
||||
}
|
||||
|
||||
return dst;
|
||||
}
|
||||
25
srcs/ft_memcpy64.c
Normal file
25
srcs/ft_memcpy64.c
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
void *ft_memcpy64(void *dst, const void *src, size_t n) {
|
||||
size_t i;
|
||||
uint64_t *lp_dst;
|
||||
const uint64_t *lp_src;
|
||||
unsigned char *bp_dst;
|
||||
const unsigned char *bp_src;
|
||||
|
||||
lp_dst = (uint64_t *)dst;
|
||||
lp_src = (const uint64_t *)src;
|
||||
|
||||
for (i = 0; i < n / 8; i++) {
|
||||
lp_dst[i] = lp_src[i];
|
||||
}
|
||||
|
||||
bp_dst = (unsigned char *)(lp_dst + i);
|
||||
bp_src = (const unsigned char *)(lp_src + i);
|
||||
for (i = 0; i < n % 8; i++) {
|
||||
bp_dst[i] = bp_src[i];
|
||||
}
|
||||
|
||||
return dst;
|
||||
}
|
||||
9
srcs/ft_memset.c
Normal file
9
srcs/ft_memset.c
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
#include "libft.h"
|
||||
|
||||
void *ft_memset(void *s, int c, size_t n)
|
||||
{
|
||||
unsigned char *p = (unsigned char *)s;
|
||||
while (n--)
|
||||
*p++ = (unsigned char)c;
|
||||
return s;
|
||||
}
|
||||
37
srcs/ft_memset32.c
Normal file
37
srcs/ft_memset32.c
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
void *ft_memset32(void *s, int c, size_t n)
|
||||
{
|
||||
unsigned char *ptr = (unsigned char *)s;
|
||||
uint32_t pattern;
|
||||
uint32_t *long_ptr;
|
||||
|
||||
unsigned char byte = (unsigned char)c;
|
||||
|
||||
while (n > 0 && ((uintptr_t)ptr % 4 != 0))
|
||||
{
|
||||
*ptr++ = byte;
|
||||
n--;
|
||||
}
|
||||
|
||||
if (n >= 4)
|
||||
{
|
||||
pattern = byte;
|
||||
pattern |= pattern << 8;
|
||||
pattern |= pattern << 16;
|
||||
|
||||
long_ptr = (uint32_t *)ptr;
|
||||
while (n >= 4)
|
||||
{
|
||||
*long_ptr++ = pattern;
|
||||
n -= 4;
|
||||
}
|
||||
ptr = (unsigned char *)long_ptr;
|
||||
}
|
||||
|
||||
while (n--)
|
||||
*ptr++ = byte;
|
||||
|
||||
return s;
|
||||
}
|
||||
38
srcs/ft_memset64.c
Normal file
38
srcs/ft_memset64.c
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
void *ft_memset64(void *s, int c, size_t n)
|
||||
{
|
||||
unsigned char *ptr = (unsigned char *)s;
|
||||
uint64_t pattern;
|
||||
uint64_t *long_ptr;
|
||||
|
||||
unsigned char byte = (unsigned char)c;
|
||||
|
||||
while (n > 0 && ((uintptr_t)ptr % 8 != 0))
|
||||
{
|
||||
*ptr++ = byte;
|
||||
n--;
|
||||
}
|
||||
|
||||
if (n >= 8)
|
||||
{
|
||||
pattern = byte;
|
||||
pattern |= pattern << 8;
|
||||
pattern |= pattern << 16;
|
||||
pattern |= pattern << 32;
|
||||
|
||||
long_ptr = (uint64_t *)ptr;
|
||||
while (n >= 8)
|
||||
{
|
||||
*long_ptr++ = pattern;
|
||||
n -= 8;
|
||||
}
|
||||
ptr = (unsigned char *)long_ptr;
|
||||
}
|
||||
|
||||
while (n--)
|
||||
*ptr++ = byte;
|
||||
|
||||
return s;
|
||||
}
|
||||
14
srcs/ft_strlen.c
Normal file
14
srcs/ft_strlen.c
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#include <stddef.h>
|
||||
|
||||
size_t ft_strlen(const char *s)
|
||||
{
|
||||
const char *p = s;
|
||||
|
||||
if (!s)
|
||||
return 0;
|
||||
|
||||
while (*p)
|
||||
++p;
|
||||
|
||||
return (size_t)(p - s);
|
||||
}
|
||||
8
srcs/ft_tolower.c
Normal file
8
srcs/ft_tolower.c
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#include "libft.h"
|
||||
|
||||
int ft_tolower(int c)
|
||||
{
|
||||
if (ft_isupper(c))
|
||||
c = c + 32;
|
||||
return (c);
|
||||
}
|
||||
8
srcs/ft_toupper.c
Normal file
8
srcs/ft_toupper.c
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#include "libft.h"
|
||||
|
||||
int ft_toupper(int c)
|
||||
{
|
||||
if (ft_islower(c))
|
||||
c = c - 32;
|
||||
return (c);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue