Skip to Content
Sunbeen's Blog
42 SEOULC 언어Libft

Libft

🔹 Introduction

이 프로젝트의 목적은 흔히 쓰이는 함수들을 재구성하여 이후의 모든 프로젝트에서 사용될 C 라이브러리를 제작하는 것입니다.

ft_bzero

ft_bzero

영어 문서 : https://man7.org/linux/man-pages/man3/bzero.3.html

  • DESCRIPTION
bzero() 함수는 s가 가리키는 위치에서 시작하는 n 바이트 메모리에 0을 ('\0'을 담은 바이트들을) 기록해서 데이터를 지운다.
  • RETURN VALUE
없음.
  • CODE
/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_bzero.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: sunbchoi <sunbchoi@student.42seoul.kr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/23 19:37:46 by sunbchoi #+# #+# */ /* Updated: 2020/12/23 21:26:59 by sunbchoi ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" void *ft_bzero(void *dest, size_t size) { size_t loop; loop = 0; while (loop < size) { ((unsigned char*)dest)[loop] = 0; loop++; } return (dest); }

ft_memccpy

ft_memccpy

영어 문서 : https://man7.org/linux/man-pages/man3/memccpy.3.html

  • DESCRIPTION
memcpy() 함수는 메모리 영역 src에서 메모리 영역 dest로 n개 바이트를 복사한다. 두 메모리 영역이 겹쳐선 안 된다. 메모리 영역이 겹친다면 memmove(3)를 쓰면 된다.
  • RETURN VALUE
memcpy() 함수는 dest 포인터를 반환한다.
  • CODE
/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_memccpy.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: sunbchoi <sunbchoi@student.42seoul.kr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/23 21:28:35 by sunbchoi #+# #+# */ /* Updated: 2020/12/23 21:29:34 by sunbchoi ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" void *ft_memccpy(void *dest, const void *src, int check, size_t size) { size_t loop; loop = 0; while (loop < size) { ((unsigned char*)dest)[loop] = ((unsigned char*)src)[loop]; if (((unsigned char*)src)[loop] == (unsigned char)check) return (&((unsigned char*)dest)[loop + 1]); loop++; } return (0); }

ft_memchr

ft_memchr

영어 문서 : https://man7.org/linux/man-pages/man3/memchr.3.html

  • DESCRIPTION
memchr() 함수는 s가 가리키는 메모리 영역의 처음 n 개 바이트에서 c의 첫 번째 인스턴스를 탐색한다. s가 가리키는 메모리 영역의 바이트들과 c 모두 unsigned char로 해석한다. char *p = rawmemchr(s, '\0');
  • RETURN VALUE
성공 : memchr() 일치하는 바이트에 대한 포인터를 반환한다. 실패 : 주어진 메모리 영역 내에 그 문자가 없으면 NULL을 반환한다.
  • CODE
/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_memchr.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: sunbchoi <sunbchoi@student.42seoul.kr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/23 21:30:01 by sunbchoi #+# #+# */ /* Updated: 2020/12/31 00:20:36 by sunbchoi ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" void *ft_memchr(void *dest, int find_char, size_t size) { size_t loop; loop = 0; while (loop < size) { if (((unsigned char*)dest)[loop] == (unsigned char)find_char) return (&((unsigned char*)dest)[loop]); loop++; } return (0); }

ft_memcmp

ft_memcmp

영어 문서 : https://man7.org/linux/man-pages/man3/memcmp.3.html

  • DESCRIPTION
memcmp() 문자열 s1, s2를 비교하여, 성공시 0을 아닐 경우 정수값을 반환한다.
  • RETURN VALUE
성공 : return (0) 성공 : n이 0이면 반환 값이 0이다. 실패 :0 아닌 반환 값의 부호는 s1과 s2에서 첫 번째로 다른 (unsigned char 타입으로 해석한) 바이트 쌍의 값 차이의 부호에 의해 정해진다.
  • CODE
/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_memcmp.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: sunbchoi <sunbchoi@student.42seoul.kr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/23 21:31:29 by sunbchoi #+# #+# */ /* Updated: 2020/12/23 22:04:28 by sunbchoi ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" int ft_memcmp(const void *dest, const void *src, size_t size) { unsigned char *dest_char; unsigned char *src_char; size_t loop; loop = 0; dest_char = (unsigned char*)dest; src_char = (unsigned char*)src; while (loop < size) { if (dest_char[loop] != src_char[loop]) return (dest_char[loop] - src_char[loop]); loop++; } return (0); }

ft_memcpy

ft_memcpy

영어 문서 : https://man7.org/linux/man-pages/man3/memcpy.3.html

  • DESCRIPTION
memcpy() 함수는 메모리 영역 src에서 메모리 영역 dest로 n개 바이트를 복사한다. 두 메모리 영역이 겹쳐선 안 된다. 메모리 영역이 겹친다면 memmove(3)를 쓰면 된다.
  • RETURN VALUE
memcpy() 함수는 dest 포인터를 반환한다.
  • CODE
/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_memcpy.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: sunbchoi <sunbchoi@student.42seoul.kr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/23 21:32:08 by sunbchoi #+# #+# */ /* Updated: 2021/01/03 20:21:12 by sunbchoi ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" void *ft_memcpy(void *dest, const void *src, size_t size) { size_t loop; if (dest == 0 && src == 0) return (0); loop = 0; while (loop < size) { ((unsigned char*)dest)[loop] = ((unsigned char*)src)[loop]; loop++; } return (dest); }

ft_memmove

ft_memmove

영어 문서 : https://man7.org/linux/man-pages/man3/memmove.3.html

  • DESCRIPTION
memmove() 함수는 메모리 영역 src에서 메모리 영역 dest로 n개 바이트를 복사한다. 메모리 영역이 겹칠 수 있다. 마치 src의 바이트들을 src나 dest와 겹치지 않는 임시 배열로 복사한 다음 그 임시 배열을 dest로 복사하는 것처럼 복사가 이뤄진다.
  • RETURN VALUE
memmove() 함수는 dest 포인터를 반환한다.
  • CODE
/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_memmove.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: sunbchoi <sunbchoi@student.42seoul.kr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/23 21:33:08 by sunbchoi #+# #+# */ /* Updated: 2021/01/03 20:21:09 by sunbchoi ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" void *ft_memmove(void *dest, const void *src, size_t size) { unsigned char *temp_d; const unsigned char *temp_s; if (dest == 0 && src == 0) return (0); if (dest <= src) { temp_d = (unsigned char*)dest; temp_s = (unsigned char*)src; while (size--) *temp_d++ = *temp_s++; } else { temp_d = (unsigned char*)dest + size; temp_s = (unsigned char*)src + size; while (size--) *--temp_d = *--temp_s; } return (dest); }

ft_memset

ft_memset

영어 문서 : https://man7.org/linux/man-pages/man3/memset.3.html

  • DESCRIPTION
memset() 함수는 s가 가리키는 메모리 구역의 처음 n 바이트를 바이트 c로 똑같이 채운다.
  • RETURN VALUE
memset() 함수는 메모리 구역 s의 포인터를 반환한다.
  • CODE
/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_memset.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: sunbchoi <sunbchoi@student.42seoul.kr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/23 21:36:08 by sunbchoi #+# #+# */ /* Updated: 2020/12/23 21:36:14 by sunbchoi ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" void *ft_memset(void *dest, int fill_char, size_t size) { size_t loop; loop = 0; while (loop < size) { ((unsigned char*)dest)[loop] = (unsigned char)fill_char; loop++; } return (dest); }

ft_strlen

ft_strlen

영어 문서 : https://man7.org/linux/man-pages/man3/strlen.3.html

  • DESCRIPTION
strlen() 함수는 s가 가리키는 문자열의 길이를 계산한다. 종료 널 바이트('\0')는 제외한다.
  • RETURN VALUE
strlen() 함수는 s가 가리키는 문자열의 바이트 수를 반환한다.
  • CODE
/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_strlen.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: sunbchoi <sunbchoi@student.42seoul.kr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/23 22:04:37 by sunbchoi #+# #+# */ /* Updated: 2020/12/23 22:18:27 by sunbchoi ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" size_t ft_strlen(const char *dest) { size_t loop; loop = 0; while (dest[loop] != 0) { loop++; } return (loop); }

ft_strlcpy

ft_strlcpy

영어 문서 : https://man7.org/linux/man-pages/man7/string_copying.7.html

  • DESCRIPTION
strlcpy() 함수는 문자열 src를 dest로 복사하되, dst 버퍼 크기(size)를 넘지 않도록 제한하여 복사한다. 두 메모리 영역이 겹치는 경우 결과는 규정되어 있지 않다.
  • RETURN VALUE
원본 문자열 src의 전체 길이를 반환한다 (null 제외)
  • CODE
/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_strlcpy.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: sunbchoi <sunbchoi@student.42seoul.kr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/23 22:08:58 by sunbchoi #+# #+# */ /* Updated: 2021/01/04 11:53:06 by sunbchoi ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" size_t ft_strlcpy(char *dest, const char *src, size_t dest_size) { size_t src_size; if (dest == 0 || src == 0) return (0); src_size = ft_strlen(src); if (dest_size == 0) return (src_size); else if (src_size < dest_size) { ft_memcpy(dest, src, src_size); dest[src_size] = 0; } else { ft_memcpy(dest, src, dest_size - 1); dest[dest_size - 1] = 0; } return (src_size); }

ft_strlcat

ft_strlcat

  • DESCRIPTION
strlcat() 함수는 문자열 dst의 끝에 src를 이어붙이되, 버퍼 전체 크기(size)를 넘지 않도록 제한한다.
  • RETURN VALUE
초기 dest 길이 + src 길이 영어 문서 : https://man7.org/linux/man-pages/man7/string_copying.7.html
  • CODE
/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_strlcat.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: sunbchoi <sunbchoi@student.42seoul.kr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/23 22:27:05 by sunbchoi #+# #+# */ /* Updated: 2020/12/31 10:34:41 by sunbchoi ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" size_t ft_strlcat(char *dest, const char *src, size_t size) { size_t loop; size_t dest_len; size_t src_len; dest_len = ft_strlen(dest); src_len = ft_strlen(src); loop = 0; if (dest_len > size - 1 || size == 0) return (size + src_len); if (dest_len < size - 1) { while (src[loop] != 0 && dest_len + loop < size - 1) { dest[dest_len + loop] = src[loop]; loop++; } } dest[dest_len + loop] = 0; return (dest_len + src_len); }

ft_strchr

ft_strchr

영어 문서 : https://man7.org/linux/man-pages/man3/strchr.3.html

  • DESCRIPTION
strchr() 함수는 문자열 s에서 문자 c의 첫 번째 위치에 대한 포인터를 반환한다. 여기서 "문자"는 "바이트"를 뜻한다. 확장 문자나 다중 바이트 문자에는 이 함수들이 동작하지 않는다.
  • RETURN VALUE
문자열 s에서 문자 c의 첫 번째 위치에 대한 포인터
  • CODE
/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_strchr.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: sunbchoi <sunbchoi@student.42seoul.kr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/23 22:41:15 by sunbchoi #+# #+# */ /* Updated: 2020/12/28 21:04:11 by sunbchoi ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" char *ft_strchr(const char *dest, int check) { while (1) { if (*dest == (char)check) return ((char *)dest); if (*dest == 0) return (0); dest++; } }

ft_strrchr

ft_strrchr

영어 문서 : https://man7.org/linux/man-pages/man3/strrchr.3.html

  • DESCRIPTION
strrchr() 함수는 문자열 s에서 문자 c의 마지막 위치에 대한 포인터를 반환한다.
  • RETURN VALUE
문자열 s에서 문자 c의 마지막 위치에 대한 포인터
  • CODE
/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_strrchr.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: sunbchoi <sunbchoi@student.42seoul.kr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/23 22:54:32 by sunbchoi #+# #+# */ /* Updated: 2021/01/02 17:46:17 by sunbchoi ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" char *ft_strrchr(const char *dest, int check) { char *save_point; unsigned char check_char; save_point = 0; check_char = (unsigned char)check; while (1) { if (*dest == (char)check) save_point = (char*)dest; if (*dest == 0) return (save_point); dest++; } return (0); }

ft_strnstr

ft_strnstr

영어 문서 : https://man.freebsd.org/cgi/man.cgi?query=strnstr&sektion=3

  • DESCRIPTION
strnstr() 함수는 문자열 dest 안에서 문자열 src이 처음으로 등장하는 위치를 찾는다. 단, 최대 len 바이트까지만 검색한다 '\0' 이후는 검색하지 않는다.
  • RETURN VALUE
- 1. src 빈 문자열일 경우 - return dest; - 2. src을 찾지 못한 경우 - return NULL; - 3. src 문자열을 찾은 경우 (src가 dest에서 시작되는 위치의 포인터 반환) - return (dest + index);
  • CODE
/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_strnstr.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: sunbchoi <sunbchoi@student.42seoul.kr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/23 23:06:07 by sunbchoi #+# #+# */ /* Updated: 2021/01/04 01:08:27 by sunbchoi ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" char *ft_strnstr(const char *dest, const char *src, size_t len) { size_t src_len; src_len = ft_strlen(src); if (*src) { if (len == 0) return (0); while (len > 0 && ft_strncmp(dest, src, src_len) != 0) { if (len <= src_len) return (0); if (*dest == 0) return (0); dest++; len--; } } return ((char*)dest); }

ft_strncmp

ft_strncmp

영어 문서 : https://man7.org/linux/man-pages/man3/strncmp.3.html

  • DESCRIPTION
strncmp() 함수는 두 문자열 s1, s2를 최대 n 바이트까지 비교한다. 다음과 같은 경우에서 함수가 종료된다. - 서로 다른 문자를 만남 - '\0' (문자열 끝) 도달 - 비교 완료
  • RETURN VALUE
- 성공 : return 0; - 실패 : (두 문자열의 첫 번째 다른 문자 차이값을 반환) return (unsigned char)s1[i] - (unsigned char)s2[i] < 0
  • CODE
/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_strncmp.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: sunbchoi <sunbchoi@student.42seoul.kr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/25 21:16:30 by sunbchoi #+# #+# */ /* Updated: 2020/12/25 21:23:05 by sunbchoi ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" int ft_strncmp(const char *dest, const char *src, size_t size) { size_t loop; unsigned char *dest_copy; unsigned char *src_copy; dest_copy = (unsigned char*)dest; src_copy = (unsigned char*)src; loop = 0; while (loop < size) { if (dest_copy[loop] != src_copy[loop] || dest_copy[loop] == 0 || src_copy[loop] == 0) return ((int)(dest_copy[loop] - src_copy[loop])); loop++; } return (0); }

ft_atoi

ft_atoi

영어 문서 : https://man7.org/linux/man-pages/man3/atoi.3.html

  • DESCRIPTION
atoi() 함수는 nptr이 가리키는 문자열의 처음 부분을 int로 변환한다. atoi()에서는 오류를 감지하지 않는다.
  • RETURN VALUE
성공 : 변환한 값 실패 : 오류 시 0
  • CODE
/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_atoi.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: sunbchoi <sunbchoi@student.42seoul.kr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/25 21:24:23 by sunbchoi #+# #+# */ /* Updated: 2021/01/04 02:32:08 by sunbchoi ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" int is_space(char c) { if (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v') return (1); else return (0); } int ft_atoi(char *str) { long long value; long long sign; value = 0; sign = 1; while (is_space(*str) == 1) str++; if (*str == '+' || *str == '-') { if (*str == '-') sign *= -1; str++; } while (*str >= '0' && *str <= '9') { value *= 10; value += *str - '0'; str++; } if (value > 2147483647 && sign == 1) return (-1); if (value > 2147483648 && sign == -1) return (0); value *= sign; return (value); }

ft_isalpha

ft_isalpha

영어 문서 : https://man7.org/linux/man-pages/man3/isalpha.3.html

  • DESCRIPTION
isalpha() 영문자인지 확인. 표준 "C" 로캘에선 (isupper(c) || islower(c))와 동등하다. 일부 로캘에선 대문자도 아니고 소문자도 아니면서 isalpha()가 참인 문자가 추가로 있을 수 있다.
  • RETURN VALUE
문자 c가 검사 대상 유형에 속하면 0 아닌 값을 반환한다. 아니면 0을 반환한다.
  • CODE
/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_isalpha.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: sunbchoi <sunbchoi@student.42seoul.kr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/25 21:37:48 by sunbchoi #+# #+# */ /* Updated: 2020/12/31 00:19:37 by sunbchoi ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" int ft_isalpha(int c) { unsigned char check; if (c >= 256 || c < 0) return (0); check = (unsigned char)c; if ((check >= 'a' && check <= 'z') || (check >= 'A' && check <= 'Z')) return (1); else return (0); }

ft_isdigit

ft_isdigit

영어 문서 : https://man7.org/linux/man-pages/man3/isdigit.3.html

  • DESCRIPTION
(0에서 9까지) 숫자인지 확인.
  • RETURN VALUE
isdigit() (0에서 9까지) 숫자인지 확인. 문자 c가 검사 대상 유형에 속하면 0 아닌 값을 반환한다. 아니면 0을 반환한다.
  • CODE
/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_isdigit.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: sunbchoi <sunbchoi@student.42seoul.kr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/25 21:43:06 by sunbchoi #+# #+# */ /* Updated: 2020/12/28 18:25:22 by sunbchoi ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" int ft_isdigit(int c) { unsigned char check; if (c >= 256 || c < 0) return (0); check = (unsigned char)c; if (check >= '0' && check <= '9') return (1); else return (0); }

ft_isalnum

ft_isalnum

영어 문서 : https://man7.org/linux/man-pages/man3/isalnum.3.html

  • DESCRIPTION
영문자 또는 숫자 문자인지 확인. (isalpha(c) || isdigit(c))와 동등하다.
  • RETURN VALUE
문자 c가 검사 대상 유형에 속하면 0 아닌 값을 반환한다. 아니면 0을 반환한다.
  • CODE
/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_isalnum.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: sunbchoi <sunbchoi@student.42seoul.kr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/28 16:40:57 by sunbchoi #+# #+# */ /* Updated: 2020/12/28 17:08:02 by sunbchoi ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" int ft_isalnum(int c) { if (c >= 256 || c < 0) return (0); if (ft_isalpha(c) == 1 || ft_isdigit(c) == 1) { return (1); } else { return (0); } }

ft_isprint

ft_isprint

영어 문서 : https://man7.org/linux/man-pages/man3/isprint.3.html

  • DESCRIPTION
공백을 포함한 출력 가능 문자인지 확인.
  • RETURN VALUE
문자 c가 검사 대상 유형에 속하면 0 아닌 값을 반환한다. 아니면 0을 반환한다.
  • CODE
/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_isprint.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: sunbchoi <sunbchoi@student.42seoul.kr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/28 16:47:21 by sunbchoi #+# #+# */ /* Updated: 2020/12/28 17:08:00 by sunbchoi ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" int ft_isprint(int c) { unsigned char check; if (c >= 256 || c < 0) return (0); check = (unsigned char)c; if (check >= ' ' && check <= '~') { return (1); } else { return (0); } }

ft_isascii

ft_isascii

영어 문서 : https://man7.org/linux/man-pages/man3/isascii.3.html

  • DESCRIPTION
c가 ASCII 문자 집합에 들어가는 7비트 unsigned char 값인지 확인.
  • RETURN VALUE
문자 c가 검사 대상 유형에 속하면 0 아닌 값을 반환한다. 아니면 0을 반환한다.
  • CODE
/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_isascii.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: sunbchoi <sunbchoi@student.42seoul.kr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/28 16:37:34 by sunbchoi #+# #+# */ /* Updated: 2021/01/02 17:43:04 by sunbchoi ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" int ft_isascii(int c) { if (c >= 0 && c <= 127) return (1); else return (0); }

ft_tolower

ft_tolower

영어 문서 : https://man7.org/linux/man-pages/man3/tolower.3.html

  • DESCRIPTION
이 함수들은 소문자를 대문자로, 그리고 그 반대로 변환한다.
  • RETURN VALUE
변환된 글자의 값을 반환한다. 변환이 불가능하면 c를 반환한다.
  • CODE
/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_tolower.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: sunbchoi <sunbchoi@student.42seoul.kr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/28 17:01:42 by sunbchoi #+# #+# */ /* Updated: 2020/12/28 18:26:09 by sunbchoi ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" int ft_tolower(int c) { unsigned char check; if (c >= 256 || c < 0) return (c); check = (unsigned char)c; if (check >= 'A' && check <= 'Z') c += 32; return (c); }

ft_toupper

ft_toupper

영어 문서 : https://man7.org/linux/man-pages/man3/toupper.3.html

  • DESCRIPTION
이 함수들은 소문자를 대문자로, 그리고 그 반대로 변환한다.
  • RETURN VALUE
변환된 글자의 값을 반환한다. 변환이 불가능하면 c를 반환한다.
  • CODE
/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_toupper.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: sunbchoi <sunbchoi@student.42seoul.kr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/28 16:58:22 by sunbchoi #+# #+# */ /* Updated: 2020/12/28 17:07:12 by sunbchoi ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" int ft_toupper(int c) { unsigned char check; if (c >= 256 || c < 0) return (c); check = (unsigned char)c; if (check >= 'a' && check <= 'z') c -= 32; return (c); }

ft_calloc

ft_calloc

영어 문서 : https://man7.org/linux/man-pages/man3/calloc.3.html

  • DESCRIPTION
calloc() 함수는 size 바이트를 할당하여 할당한 메모리에 대한 포인터를 반환한다. 초기화된 메모리를 반환한다.
  • RETURN VALUE
할당한 메모리에 대한 포인터를 반환한다.
  • CODE
/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_calloc.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: sunbchoi <sunbchoi@student.42seoul.kr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/28 17:43:33 by sunbchoi #+# #+# */ /* Updated: 2020/12/30 13:38:27 by sunbchoi ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" void *ft_calloc(size_t nelem, size_t elsize) { size_t len; void *memory; len = nelem * elsize; memory = malloc(len); if (memory == 0) return (0); ft_bzero(memory, len); return (memory); }

ft_strdup

ft_strdup

영어 문서 : https://man7.org/linux/man-pages/man3/strdup.3.html

  • DESCRIPTION
strdup() 함수는 문자열 s의 복제본인 새 문자열에 대한 포인터를 반환한다.
  • RETURN VALUE
성공 시 strdup() 함수는 복제 문자열의 포인터를 반환한다. 가용 메모리가 충분치 않은 경우 NULL을 반환하며 오류를 나타내도록 errno를 설정한다.
  • CODE
/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_strdup.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: sunbchoi <sunbchoi@student.42seoul.kr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/28 17:53:00 by sunbchoi #+# #+# */ /* Updated: 2021/01/02 17:48:45 by sunbchoi ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" char *ft_strdup(const char *s) { size_t len; char *memory; len = ft_strlen(s); memory = (char*)malloc(len + 1); if (memory == 0) return (0); ft_bzero((void*)memory, len + 1); ft_memcpy(memory, s, len); memory[len] = 0; return (memory); }

ft_substr

ft_substr

영어 문서 : https://man7.org/linux/man-pages/man3/function::substr.3stap.html

  • DESCRIPTION
ft_substr() 함수는 문자열 s에서 특정 위치(start)부터 최대 길이(len)만큼 잘라서 새로운 문자열을 생성한다.
  • RETURN VALUE
성공 → 새로 할당된 substring 문자열 반환 실패 → NULL (malloc 실패)
  • CODE
/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_substr.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: sunbchoi <sunbchoi@student.42seoul.kr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/28 18:38:33 by sunbchoi #+# #+# */ /* Updated: 2021/01/04 13:56:39 by sunbchoi ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" char *ft_substr(char const *s, unsigned int start, size_t len) { char sub_str[len + 1]; size_t str_len; if (s == 0) return (0); str_len = ft_strlen(s); if (str_len <= start) return (ft_strdup("")); ft_strlcpy(sub_str, s + start, len + 1); return (ft_strdup(sub_str)); }

ft_strjoin

ft_strjoin

영어 문서 : https://man7.org/linux/man-pages/man3/string.3.html

  • DESCRIPTION
strjoin() 함수는 두 문자열 s1, s2를 이어붙여서 하나의 새로운 문자열로 반환한다.
  • RETURN VALUE
성공 → 새로 생성된 문자열 반환 실패 → NULL (malloc 실패)
  • CODE
/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_strjoin.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: sunbchoi <sunbchoi@student.42seoul.kr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/28 20:27:00 by sunbchoi #+# #+# */ /* Updated: 2020/12/31 10:36:02 by sunbchoi ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" char *ft_strjoin(char const *s1, char const *s2) { size_t s1_len; size_t s2_len; char *str; if (s1 == 0 && s2 == 0) return (0); s1_len = s1 ? ft_strlen(s1) : 0; s2_len = s2 ? ft_strlen(s2) : 0; str = (char*)ft_calloc(sizeof(char), s1_len + s2_len + 1); if (str == 0) return (0); ft_memcpy(str, s1, s1_len); ft_strlcat(str, s2, s1_len + s2_len + 1); return (str); }

ft_strtrim

ft_strtrim

항목내용
Function Nameft_strtrim
Prototypechar *ft_strtrim(char const *s1, char const *set);
Files to Submit-
Parameterss1: 잘라낼 대상 문자열
set: 제거할 문자 집합
Return Value잘린 문자열 반환 할당 실패 시 NULL
External Functionmalloc
DESCRIPTIONmalloc을 사용하여 메모리를 할당하고, 문자열 s1의 앞과 뒤에서 set에 포함된 문자들을 제거한 새로운 문자열을 반환
  • CODE
/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_strtrim.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: sunbchoi <sunbchoi@student.42seoul.kr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/03/04 20:52:40 by daelee #+# #+# */ /* Updated: 2021/01/02 17:47:16 by sunbchoi ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" int find_front(const char *s1, const char *set) { size_t len; size_t loop; len = ft_strlen(s1); loop = 0; while (loop < len) { if (ft_strchr(set, s1[loop]) == 0) break ; loop++; } return (loop); } int find_end(const char *s1, const char *set) { size_t len; size_t loop; len = ft_strlen(s1); loop = 0; while (loop < len) { if (ft_strchr(set, s1[len - loop - 1]) == 0) break ; loop++; } return (len - loop); } char *ft_strtrim(char const *s1, char const *set) { int front; int end; char *str; if (s1 == 0) return (0); if (set == 0) return (ft_strdup(s1)); front = find_front(s1, set); end = find_end(s1, set); if (front >= end) return (ft_strdup("")); str = (char *)ft_calloc(sizeof(char), (end - front + 1)); if (str == 0) return (0); ft_strlcpy(str, s1 + front, end - front + 1); return (str); }

ft_split

ft_split

항목내용
Function Nameft_split
Prototypechar **ft_split(char const *s, char c);
Files to Submit-
Parameters s: 분할할 문자열
c: 구분자 문자
Return Value 분할 결과로 생성된 문자열 배열 반환
메모리 할당 실패 시 NULL
External Functionmalloc, free
DESCRIPTION malloc(3)을 사용하여 메모리를 할당하고, 문자열 s를 구분자 c를 기준으로 나누어 생성된 문자열 배열을 반환한다.
배열의 마지막은 NULL 포인터로 끝나야 한다.
  • CODE
/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_split.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: sunbchoi <sunbchoi@student.42seoul.kr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/31 17:34:13 by sunbchoi #+# #+# */ /* Updated: 2021/01/04 11:50:51 by sunbchoi ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" char **str_malloc_count(char *str, char check) { char **words; int state; int count; int loop; state = 0; count = 0; loop = 0; while (str[loop] != 0) { if (str[loop] == check) state = 0; else { if (state == 0) count++; state = 1; } loop++; } words = (char**)ft_calloc(sizeof(char *), count + 1); return (words); } char *str_rangecopy(char *str, char check, size_t start) { char *dest; size_t len; size_t loop; len = start; while (str[len] != check) len++; dest = (char *)ft_calloc(sizeof(char), (len - start + 1)); if (dest == 0) return (0); loop = 0; while (loop + start < len) { dest[loop] = str[start + loop]; loop++; } dest[loop] = 0; return (dest); } void fill_words(char **words, char *str, char check) { int len; int state; int loop; len = -1; state = 0; loop = -1; while (str[++len] != 0) { if (str[len] == check) state = 0; else if (state == 0) { words[++loop] = str_rangecopy(str, check, len); state = 1; } } words[++loop] = 0; } char **ft_split(char const *s, char c) { char **words; if (s == 0) return (0); words = str_malloc_count((char*)s, c); if (words == 0) return (0); fill_words(words, (char*)s, c); return (words); }

ft_itoa

ft_itoa

항목내용
Function Nameft_itoa
Prototypechar *ft_itoa(int n);
Files to Submit-
Parameters n: 변환할 정수 값
Return Value 정수를 문자열로 변환한 결과 반환
메모리 할당 실패 시 NULL
External Functionmalloc
DESCRIPTION malloc(3)을 사용하여 메모리를 할당하고, 인자로 전달된 정수를 문자열로 변환하여 반환한다.
음수도 반드시 처리해야 한다.
  • CODE
/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_itoa.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: sunbchoi <sunbchoi@student.42seoul.kr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/29 15:17:30 by sunbchoi #+# #+# */ /* Updated: 2021/01/02 17:44:31 by sunbchoi ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" void set_value(char *temp_str, char *str, int len, int sign) { int loop; loop = 0; if (sign == 1) str[loop++] = '-'; while (len > 0) str[loop++] = temp_str[len-- - 1]; } char *ft_itoa(int n) { char temp_str[20]; char *str; int sign; int len; sign = 0; len = 0; if (n == -2147483648) return (ft_strdup("-2147483648")); if (n < 0) { sign = 1; n *= -1; } while (n >= 10) { temp_str[len++] = '0' + (n % 10); n = n / 10; } temp_str[len++] = '0' + n % 10; str = ft_calloc(sizeof(char), len + sign + 1); if (str == 0) return (0); set_value(temp_str, str, len, sign); return (str); }

ft_strmapi

ft_strmapi

항목내용
Function Nameft_strmapi
Prototypechar *ft_strmapi(char const *s, char (*f)(unsigned int, char));
Files to Submit-
Parameters s: 순회할 문자열
f: 각 문자에 적용할 함수
Return Value 함수 f를 순차적으로 적용하여 생성된 새로운 문자열 반환
메모리 할당 실패 시 NULL
External Functionmalloc
DESCRIPTION 문자열 s의 각 문자에 함수 f를 적용한다. 이때 인덱스를 첫 번째 인자로, 문자를 두 번째 인자로 전달한다.
결과를 저장하기 위해 malloc(3)으로 새로운 문자열을 생성하여 반환한다.
  • CODE
/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_strmapi.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: sunbchoi <sunbchoi@student.42seoul.kr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/30 10:47:55 by sunbchoi #+# #+# */ /* Updated: 2021/01/02 17:45:14 by sunbchoi ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) { char *str; unsigned int len; unsigned int i; if (s == 0 || f == 0) return (0); len = ft_strlen(s); str = (char *)ft_calloc(sizeof(char), (len + 1)); if (str == 0) return (0); i = 0; while (s[i] != 0) { str[i] = f(i, s[i]); i++; } str[i] = '\0'; return (str); }

ft_putchar_fd

ft_putchar_fd

항목내용
Function Nameft_putchar_fd
Prototypevoid ft_putchar_fd(char c, int fd);
Files to Submit-
Parameters c: 출력할 문자
fd: 출력할 파일 디스크립터
Return Value없음
External Functionwrite
DESCRIPTION 지정된 파일 디스크립터(fd)에 문자 c를 출력한다.
  • CODE
/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_putchar_fd.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: sunbchoi <sunbchoi@student.42seoul.kr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/30 11:08:36 by sunbchoi #+# #+# */ /* Updated: 2021/01/02 17:48:24 by sunbchoi ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" void ft_putchar_fd(char c, int fd) { if (fd < 0) return ; else write(fd, &c, 1); }

ft_putstr_fd

ft_putstr_fd

항목내용
Function Nameft_putstr_fd
Prototypevoid ft_putstr_fd(char *s, int fd);
Files to Submit-
Parameters s: 출력할 문자열
fd: 출력할 파일 디스크립터
Return Value없음
External Functionwrite
DESCRIPTION 지정된 파일 디스크립터(fd)에 문자열 s를 출력한다.
  • CODE
/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_putstr_fd.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: sunbchoi <sunbchoi@student.42seoul.kr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/30 13:16:01 by sunbchoi #+# #+# */ /* Updated: 2021/01/02 17:45:00 by sunbchoi ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" void ft_putstr_fd(char *s, int fd) { if (fd < 0 || s == 0) return ; else write(fd, s, ft_strlen(s)); }

ft_putendl_fd

ft_putendl_fd

항목내용
Function Nameft_putendl_fd
Prototypevoid ft_putendl_fd(char *s, int fd);
Files to Submit-
Parameters s: 출력할 문자열
fd: 출력할 파일 디스크립터
Return Value없음
External Functionwrite
DESCRIPTION 지정된 파일 디스크립터(fd)에 문자열 s를 출력한 후,
개행 문자('\n')를 추가로 출력한다.
  • CODE
/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_putendl_fd.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: sunbchoi <sunbchoi@student.42seoul.kr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/30 13:18:01 by sunbchoi #+# #+# */ /* Updated: 2021/01/02 17:48:35 by sunbchoi ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" void ft_putendl_fd(char *s, int fd) { if (fd < 0 || s == 0) return ; else { write(fd, s, ft_strlen(s)); write(fd, "\n", 1); } }

ft_putnbr_fd

ft_putnbr_fd

항목내용
Function Nameft_putnbr_fd
Prototypevoid ft_putnbr_fd(int n, int fd);
Files to Submit-
Parameters n: 출력할 정수
fd: 출력할 파일 디스크립터
Return Value없음
External Functionwrite
DESCRIPTION 정수 n을 문자열 형태로 변환하여,
지정된 파일 디스크립터(fd)에 출력한다.
  • CODE
/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_putnbr_fd.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: sunbchoi <sunbchoi@student.42seoul.kr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/30 13:32:13 by sunbchoi #+# #+# */ /* Updated: 2021/01/04 13:28:01 by sunbchoi ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" void set_value_fd(char *temp_str, char *str, int len, int sign) { int loop; loop = 0; if (sign == 1) str[loop++] = '-'; while (len > 0) str[loop++] = temp_str[len-- - 1]; } void ft_itoa_fd(int n, int fd) { char temp_str[20]; char new_str[20]; int sign; int len; sign = 0; len = 0; ft_bzero(temp_str, 20); ft_bzero(new_str, 20); if (n < 0) { sign = 1; n *= -1; } while (n >= 10) { temp_str[len++] = '0' + (n % 10); n = n / 10; } temp_str[len++] = '0' + n % 10; set_value_fd(temp_str, new_str, len, sign); write(fd, new_str, ft_strlen(new_str)); } void ft_putnbr_fd(int n, int fd) { if (fd < 0) return ; if (n == -2147483648) { write(fd, "-2147483648", 11); return ; } ft_itoa_fd(n, fd); }
Last updated on