๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ 42seoul ํ”„๋กœ์ ํŠธ

[42seoul] libft : ๋‚˜๋งŒ์˜ C ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋งŒ๋“ค๊ธฐ

by kukim 2021. 10. 28.

๐Ÿ“š libft

@42seoul : (2020.04.15 ~ 2020.05.01)

๐Ÿ“– About

  • ์ผ๋ฐ˜์ ์œผ๋กœ C์–ธ์—์„œ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜๋“ค์„ ์ง์ ‘ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. (The aim of this project is to code a C library regrouping usual functions that you’ll be allowed to use in all your other projects.)
  • ๊ตฌํ˜„ ๋ชฉ๋ก

๐Ÿ“ Review

  • C์—์„œ includeํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” <ctype.h>, <string.h> <stdlib.h>์˜ ํ•จ์ˆ˜ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” split, join ํ•จ์ˆ˜ ๋“ฑ ์ง์ ‘ ๊ตฌํ˜„ํ•˜๋ฉด์„œ C์˜ ๊ธฐ๋ณธ ๋ฌธ๋ฒ•๊ณผ ํฌ์ธํ„ฐ ์—ฐ์‚ฐ, ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ(Heap), Makefile ๋“ฑ ๋ฐฐ์šธ ์ˆ˜ ์žˆ์—ˆ๋‹ค.
  • Libc์— ์ด๋ฏธ ๊ตฌํ˜„๋˜์–ด ์žˆ๋Š” ํ•จ์ˆ˜๋“ค์„ ๊ฐ€์ ธ๋‹ค ์“ฐ๋ฉด ๋˜๋Š”๋ฐ ๊ณผ์ œํ•˜๋ฉด์„œ ์™œ ๋‚ด๊ฐ€ ๊ตฌํ˜„ํ•ด์•ผํ• ๊นŒ ๊ณ ๋ฏผ๋„ ๋งŽ์ด ํ–ˆ์ง€๋งŒ ์ง์ ‘ ๋ฐ‘๋‹จ์—์„œ ํ•จ์ˆ˜๊ฐ€ ์ด๋ ‡๊ฒŒ ๋™์ž‘ํ•˜๋Š”๊ตฌ๋‚˜ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์–ด์„œ ๋ฌด์ฒ™ ์œ ์ตํ–ˆ๋‹ค. GNU์˜ Libc์™€ ๋‚ด๊ฐ€ ์ง  ์ฝ”๋“œ ๋ฒค์น˜๋งˆํ‚น ํ–ˆ๋Š”๋ฐ ์ฒ˜์ฐธํ•˜๊ฒŒ ๋ฌด๋„ˆ์กŒ๋‹ค. Libc๋Š” ์–ด์…ˆ๋ธ”๋ฆฌ๋กœ ์™„๋ฒฝํžˆ ์ตœ์ ํ™” ๋˜์–ด์žˆ์–ด ๋‹น์—ฐํ•œ ๊ฒฐ๊ณผ์˜€์ง€๋งŒ ๋ˆˆ์œผ๋กœ ๋ณด๋‹ˆ ๋น„์ฐธํ•˜๋ฉด์„œ ์•ฝ๊ฐ„ ์š•์‹ฌ์ด ์ƒ๊ฒผ๋‹ค. ํ•˜์ง€๋งŒ ์š•์‹ฌ๋งŒ ๊ฐ–๊ณ  ๋‹ค๋ฅธ ํ”„๋กœ์ ํŠธ๋ฅผ ํ•˜์ž.

๐Ÿ Run

Unit Test : All Success

  • alelievr๋‹˜์ด ๋งŒ๋“  ์ฝ”๋“œ๋กœ ์ „์ฒด์ ์ธ ์œ ๋‹›ํ…Œ์ŠคํŠธ๋ฅผ ํ™•์ธํ•˜์˜€๋‹ค. ๋ชจ๋“  ์ฝ”๋“œ ์ด์ƒ ์—†์ด libc์™€ ๋™์ผํ•˜๊ฒŒ ๋™์ž‘ํ•œ๋‹ค.
libft_unit_test

Benchmark (My functions vs Libc)

  • ์„ฑ๋Šฅ์ฐจ์ด : ๋งŽ๊ฒŒ๋Š” 50๋ฐฐ, ์ ๊ฒŒ๋Š” 1.2๋ฐฐ ์ •๋„ Libc ํ•จ์ˆ˜๋“ค์ด ๋” ๋น ๋ฅด๊ฒŒ ๋‚˜์™”๋‹ค.
  • atoi ํ•จ์ˆ˜๋Š” ๊ฑฐ์˜ ๋™์ผํ•˜๊ฒŒ ๋‚˜์˜จ๋‹ค.
  • ์™„ํŒจ
libft_benchmark

๐Ÿ“š Function List

๐Ÿ“• Libc functions (Standard C Library)

  • <ctype.h>, <string.h> <stdlib.h>์˜ ๋ช‡ ๊ฐ€์ง€ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.

Description : man [function]

Type Function
<string.h> : memory memset, memcpy, memccpy,memmove, memchr, memcmp
<string.h> : string strlen, strlcpy, strlcat, strchr, strrchr, strnstr, strncmp, strdup
<stdlib.h> calloc, atoi
<ctype.h> isalpha, isdigit, isalnum, isascii, isprint, toupper, tolower

๐Ÿ“— Additional functions

  • libc์— ํฌํ•จ๋˜์ง€ ์•Š์ง€๋งŒ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.
  • string : ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ ํ•จ์ˆ˜
  • put_fd : ๋ฌธ์ž์—ด ์ถœ๋ ฅ ํ•จ์ˆ˜
Type Function Description
string ft_substr Allocates (with malloc(3)) and returns a substring from the string ’s’. The substring begins at index ’start’ and is of maximum size ’len’.
string ft_strjoin Allocates (with malloc(3)) and returns a new string, which is the result of the concatenation of ’s1’ and ’s2’.
string ft_strtrim Allocates (with malloc(3)) and returns a copy of ’s1’ with the characters specified in ’set’ removed from the beginning and the end of the string.
string ft_split Allocates (with malloc(3)) and returns an array of strings obtained by splitting ’s’ using the character ’c’ as a delimiter. The array must be ended by a NULL pointer.
string ft_itoa Allocates (with malloc(3)) and returns a string representing the integer received as an argument. Negative numbers must be handled.
string ft_strmapi Applies the function ’f’ to each character of the string ’s’ to create a new string (with malloc(3)) resulting from successive applications of ’f’.
put_fd ft_putchar_fd Outputs the character ’c’ to the given file descriptor.
put_fd ft_putstr_fd Outputs the string ’s’ to the given file descriptor.
put_fd ft_putendl_fd Outputs the string ’s’ to the given file descriptor, followed by a newline.
put_fd ft_putnbr_fd Outputs the integer ’n’ to the given file descriptor.

๐Ÿ“˜ Linked list

  • ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.
Type Function Description
linked-list ft_lstnew Allocates (with malloc(3)) and returns a new element. The variable ’content’ is initialized with the value of the parameter ’content’. The variable ’next’ is initialized to NULL.
linked-list ft_lstadd_front Adds the element ’new’ at the beginning of the list.
linked-list ft_lstsize Counts the number of elements in a list.
linked-list ft_lstlast Returns the last element of the list.
linked-list ft_lstadd_back Adds the element ’new’ at the end of the list.
linked-list ft_lstdelone Takes as a parameter an element and frees the memory of the element’s content using the function ’del’ given as a parameter and free the element. The memory of ’next’ must not be freed.
linked-list ft_lstclear Deletes and frees the given element and every successor of that element, using the function ’del’ and free(3). Finally, the pointer to the list must be set to NULL.
linked-list ft_lstiter Iterates the list ’lst’ and applies the function ’f’ to the content of each element.
linked-list ft_lstmap Iterates the list ’lst’ and applies the function ’f’ to the content of each element. Creates a new list resulting of the successive applications of the function ’f’. The ’del’ function is used to delete the content of an element if needed.

๐Ÿ”— Reference

๐Ÿง‘๐Ÿป‍๐Ÿ’ป Author

kukim

 

ํƒœ๊ทธ

,

๋Œ“๊ธ€0