๐Ÿ“š

[Libft] Part 2

Category
Libft
Tags
Day learned
2020/02/27
Property
Empty
์ด๊ณณ์€ libft ํ”„๋กœ์ ํŠธ์˜ part2 ํ•จ์ˆ˜๋“ค์„ ๊ตฌํ˜„ํ•˜๋ฉด์„œ ๋ฉ”๋ชจํ–ˆ๋˜ ๋‚ด์šฉ๋“ค์„ ์ •๋ฆฌํ•ด๋‘” ํŽ˜์ด์ง€ ์ž…๋‹ˆ๋‹ค.
์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ํ•จ์ˆ˜๋“ค์€ ๊พธ์ค€ํžˆ ์—…๋ฐ์ดํŠธ ๋˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€์žฅ ์ตœ์‹ ์˜ ์ฝ”๋“œ๋Š” ์•„๋ž˜ ๊นƒํ—™ ๋ ˆํฌ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”.
๋ชฉ์ฐจ
1. ft_substr
Prototype
char *ft_substr(char const *s, unsigned int start, size_t len);
Parameters
#1. The string from which to create the substring.
#2. The start index of the substring in the string โ€™sโ€™.
#3. The maximum length of the substring.
Return value
The substring. NULL if the allocation fails.
External functs.
malloc
Description
Allocates (with malloc(3)) and returns a substring from the string โ€™sโ€™. The substring begins at index โ€™startโ€™ and is of maximum size โ€™lenโ€™.
Source
char *ft_substr(char const *s, unsigned int start, size_t len) { char *substr; if (!s) return (NULL); if (ft_strlen(s) < start) return (ft_strdup("")); if (!(substr = (char *)malloc(sizeof(char) * (len + 1)))) return (NULL); ft_strlcpy(substr, s+start, len+1); return (substr); }
C
โ€ข
char const *s
s๋Š” ๋ฌธ์ž์—ด์ด ์ €์žฅ๋œ ๋ฉ”๋ชจ๋ฆฌ์˜ ์ฒซ ์ฃผ์†Œ๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ํฌ์ธํ„ฐ ๋ณ€์ˆ˜
โ€ข
๊ธˆ์ง€๋˜๋Š” ๊ฒƒ : ์ดˆ๊ธฐํ™” ํ•  ๋•Œ ํ•œ๋ฒˆ ์ง€์ •๋œ ๋ฌธ์ž์—ด ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.
์˜ˆ๋ฅผ ๋“ค๋ฉด, char const *s = "๋ฌธ์ž์—ด ์ƒ์ˆ˜1" ๋กœ ์ดˆ๊ธฐํ™”๋ฅผ ํ–ˆ๋Š”๋ฐ,
s = "๋ฌธ์ž์—ด ์ƒ์ˆ˜2" ํ—ˆ์šฉ ๋˜์ง€ ์•Š์Œ.
โ€ข
์˜ˆ์™ธ1 : s[0] ์ผ ๋•Œ๋Š” return 0;
โ€ข
์˜ˆ์™ธ2 : s์˜ ๊ธธ์ด๊ฐ€ start ์ฃผ์†Œ๋ณด๋‹ค ์ž‘์„ ๋•Œ๋Š” substr์„ ๋งŒ๋“ค ์ˆ˜ ์—†์œผ๋ฏ€๋กœ
-> ๋นˆ ๋ฌธ์ž์—ด ๋ฐ˜ํ™˜.
โ€ข
start ๋ถ€ํ„ฐ len๊ฐœ ๋งŒํผ์„ ๋ณต์‚ฌํ•˜๋ฉด ๋˜๋‹ˆ๊นŒ, ft_strncpy ํ˜น์€ ft_memmove๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค.
2. ft_strjoin
Prototype
char *ft_strjoin(char const *s1, char const *s2);
Parameters
#1. The prefix string.
#2. The suffix string.
Return value
The new string. NULL if the allocation fails.
External functs.
malloc
Description
Allocates (with malloc(3)) and returns a new string, which is the result of the concatenation of โ€™s1โ€™ and โ€™s2โ€™.
#include "libft.h" char *ft_strjoin(char const *s1, char const *s2) { char *newstr; int s1_len; int s2_len; if (!(s1) && !(s2)) return (NULL); else if (!(s1) || !(s2)) return (!(s1) ? ft_strdup(s2) : ft_strdup(s1)); s1_len = ft_strlen(s1); s2_len = ft_strlen(s2); if (!(newstr = (char *)malloc(sizeof(char) * (s1_len + s2_len + 1)))) return (NULL); ft_strlcpy(newstr, s1, s1_len + 1); ft_strlcat(newstr + (s1_len), s2, s2_len + 1); return (newstr); }
C
โ€ข
s == 0; ๊ณผ s[0] = '\0';์˜ ์ฐจ์ด.
โ€ข
char * s = "" <- ์ด๊ฑด ๋นˆ๋ฌธ์ž์—ด
โ€ข
char *s = NULL <- ์ด๊ฑด ๊ทธ๋ƒฅ sํฌ์ธํ„ฐ๊ฐ€ ๊ฐ€๋ฅดํ‚ค๋Š”๊ฒŒ ์—†๋Š” ๊ฒƒ(NULL ์ธ๊ฑฐ)
โ€ข
s๋ฅผ ๋ฆฌํ„ด ๋ฐ›์•˜์„ ๋•Œ,
์ „์ž๋Š” s[0] == '\0' ์ด๋ผ์„œ ๋ฌธ์ž์—ด์˜ ๋์ด ๋ฐ”๋กœ ๊ฒ€์ƒ‰๋˜๊ณ 
ํ›„์ž๋Š” s[0] ๊ฐ’ ์ž์ฒด๋ฅผ ๋ณด๋ ค๊ณ ํ•˜๋ฉด ์„ธ๊ทธ๋จผํŠธ ํดํŠธ๊ฐ€ ๋œฌ๋‹ค.
3. ft_strtrim
Prototype char
*ft_strtrim(char const *s1, char const *set);
Parameters
#1. The string to be trimmed.
#2. The reference set of characters to trim.
Return value
The trimmed string. NULL if the allocation fails.
External functs.
malloc
Description
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
#include "libft.h" int ft_getstart(const char *s1, const char *set) { size_t len; size_t i; len = ft_strlen(s1); i = 0; while (i < len) { if (ft_strchr(set, s1[i]) == 0) break ; i++; } return (i); } int ft_getend(const char *s1, const char *set) { size_t len; size_t i; len = ft_strlen(s1); i = 0; while (i < len) { if (ft_strchr(set, s1[len - i - 1]) == 0) break ; i++; } return (len - i); } char *ft_strtrim(char const *s1, char const *set) { int start; int end; char *newstr; if (s1 == NULL) return (NULL); if (set == NULL) return (ft_strdup(s1)); start = ft_getstart(s1, set); end = ft_getend(s1, set); if (start >= end) return (ft_strdup("")); newstr = (char *)malloc(sizeof(char) * (end - start + 1) + 1); if (newstr == NULL) return (NULL); ft_strlcpy(newstr, s1 + start, end - start + 1); return(newstr); }
C
exemple : 1) s1 = "ABCCBA" set = "AB" rรฉsultat : "CC" 2) s1 = "ACCBACBA" set = "AB" rรฉsultat : "CCBAC" 3) s1 = "Hello World!" set = "Hlde" rรฉsultat : "o World!"
C
โ€ข
s1์˜ ์•ž๋’ค๋กœ set์— ๋“ค์–ด๊ฐ„ ๋ฌธ์ž๋Š” ๋ชจ๋‘ ์‚ญ์ œํ•œ ๋’ค ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
โ€ข
์•ž ๋’ค ๊ณต๋ฐฑ์„ ์ž๋ฅผ ๋•Œ ์œ ์šฉํ•  ๊ฒƒ ๊ฐ™๋‹ค.
โ€ข
๋‹ค ๊ตฌํ˜„ํ•˜๊ณ  ๋ณด๋‹ˆ ft_strsub ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ–ˆ์œผ๋ฉด 4์ค„ ์ด์ƒ์„ ์ค„์ผ ์ˆ˜ ์žˆ์—ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค. ๋‚˜์ค‘์— ๋ฐ”๊ฟ”๋ด์•ผ์ง€.
โ€ข
if (start >= end) โ†’ย return (ft_strdup(""));
4. ft_split
Prototype
char **ft_split(char const *s, char c);
Parameters
#1. The string to be split.
#2. The delimiter character.
Return value
The array of new strings resulting from the split. NULL if the allocation fails.
External functs.
malloc, free
Description
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
#include "libft.h" static char **ft_malloc_error(char **tab) { unsigned int i; i = 0; while (tab[i]) { free(tab[i]); i++; } free(tab); return (NULL); } static unsigned int ft_get_nb_strs(char const *s, char c) { unsigned int i; unsigned int nb_strs; if (!s[0]) return (0); i = 0; nb_strs = 0; while (s[i] && s[i] == c) //c๊ฐ€ ์ œ์ผ ์•ž์—, ์—ฌ๋Ÿฌ ๊ฐœ ์—ฐ์†์œผ๋กœ ์žˆ์„ ๋•Œ ํŒจ์Šค i++; while (s[i]) { if (s[i] == c) { nb_strs++; while (s[i] && s[i] == c) i++; continue ; } i++; } if (s[i - 1] != c) nb_strs++; //๋งˆ์ง€๋ง‰์ด c๋กœ ์•ˆ ๋๋‚ฌ์œผ๋ฉด +1 return (nb_strs); } static void ft_get_next_str(char **next_str, unsigned int *next_str_len, char c) { unsigned int i; *next_str += *next_str_len; //์‹ค์ œ next_str ์ฃผ์†Œ ++ *next_str_len = 0; i = 0; while (**next_str && **next_str == c) (*next_str)++; while ((*next_str)[i]) //๊ฐฏ์ˆ˜ ์ƒ ๋•Œ๋Š” ์ง€์—ญ๋ณ€์ˆ˜ i ์ด์šฉ { if ((*next_str)[i] == c) return ; (*next_str_len)++; i++; } } char **ft_split(char const *s, char c) { char **tab; char *next_str; unsigned int next_str_len; unsigned int nb_strs; unsigned int i; if (!s) return (NULL); nb_strs = ft_get_nb_strs(s, c); if (!(tab = (char **)malloc(sizeof(char *) * (nb_strs + 1)))) //๋งˆ์ง€๋ง‰ tab[i]์—๋„ NULL๋ณด์žฅ return (NULL); i = 0; next_str = (char *)s; next_str_len = 0; while (i < nb_strs) { ft_get_next_str(&next_str, &next_str_len, c); if (!(tab[i] = (char *)malloc(sizeof(char) * (next_str_len + 1)))) return (ft_malloc_error(tab)); ft_strlcpy(tab[i], next_str, next_str_len + 1); i++; } tab[i] = NULL; return (tab); } int main(void) { char **tab; unsigned int i; i = 0; tab = ft_split("sdsdsdasd", ' '); while (tab[i] != NULL) { printf("%s\n", tab[i]); i++; } }
C
โ€ข
๋„ˆ๋ฌด ์–ด๋ ค์›Œ์„œ ๋‹ค๋ฅธ ๋ถ„์˜ ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ–ˆ๋‹ค.
โ€ข
์ •์  ํ•จ์ˆ˜
์ •์  ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์…”์„œ ์กฐ๊ธˆ ์ฐพ์•„๋ดค๋Š”๋ฐ, ์ •์  ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ์ด ํŒŒ์ผ ์•ˆ์—์„œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜๊ฐ€ ๋œ๋‹ค. ๋‹ค๋ฅธ ํŒŒ์ผ์— ๊ฐ™์€ ์ด๋ฆ„์˜ ํ•จ์ˆ˜๊ฐ€ ์žˆ์–ด๋„ ์ถฉ๋Œํ•˜์ง€ ์•Š๋Š”๋‹ค.
5. ft_itoa
Prototype
char *ft_itoa(int n);
Parameters
#1. the integer to convert.
Return value
The string representing the integer. NULL if the allocation fails.
External functs.
malloc
Description
Allocates (with malloc(3)) and returns a string representing the integer received as an argument. Negative numbers must be handled.
#include "libft.h" long int ft_abs(long int nbr) { return ((nbr < 0) ? -nbr : nbr); } int ft_len(long int nbr) { int len; len = (nbr <= 0) ? 1 : 0; while (nbr != 0) { nbr = nbr / 10; len++; } return (len); } char *ft_itoa(int n) { int len; int sign; char *c; sign = (n < 0) ? -1 : 1; len = ft_len(n); c = (char *)malloc(sizeof(char) * len + 1); c[len] = '\0'; len--; while (len >= 0) { c[len] = '0' + ft_abs(n % 10); n = ft_abs(n / 10); len--; } if (sign == -1) c[0] = '-'; return (c); }
C
โ€ข
'0' + (n % 10) ์‹์œผ๋กœ ๋‚˜๋จธ์ง€๋ฅผ ๊ตฌํ•œ ๋’ค ๋ฌธ์ž ์•„์Šคํ‚ค์ฝ”๋“œ๋กœ ๋ณ€ํ™˜ํ•ด ๋ฐฐ์—ด์˜ ๋’ท ๋ถ€๋ถ„ ๋ถ€ํ„ฐ ์ฑ„์›Œ์ค€๋‹ค. ์ด๋•Œ ์•„์Šคํ‚ค์ฝ”๋“œ ์ƒ์œผ๋กœ ์Œ์ˆ˜๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜๋Š” ์—†์œผ๋‹ˆ ์ ˆ๋Œ€๊ฐ’ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์ค€๋‹ค.
โ€ข
์ •์ˆ˜์˜ ๊ธธ์ด๋ฅผ ์žด ๋•Œ๋Š” ๋ชซ ๋‚˜๋จธ์ง€ ์—ฐ์‚ฐ์„ ์‚ฌ์šฉํ•œ๋‹ค. ๋ชซ์ด 0์ด ๋  ๋•Œ ๊นŒ์ง€. ๊ทธ๋ฆฌ๊ณ  ์Œ์ˆ˜์ผ ๊ฒฝ์šฐ์—๋Š” '-' ๋ถ€ํ˜ธ๊ฐ€ ๋“ค์–ด๊ฐ€์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— len์— +1 ์„ ํ•ด์ค€๋‹ค.
6. ft_strmapi
Prototype
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
Parameters
#1. The string on which to iterate.
#2. The function to apply to each character.
Return value
The string created from the successive applications of โ€™fโ€™. Returns NULL if the allocation fails.
External functs.
malloc
Description
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โ€™.
#include "libft.h" char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) { char *newstr; unsigned int len; unsigned int i; if (s == 0 || f == 0) return (NULL); len = ft_strlen(s); if (!(newstr = (char *)malloc(sizeof(char)*(len + 1)))) return (NULL); i = 0; while (s[i]) { newstr[i] = f(i, s[i]); i++; } newstr[i] = '\0'; return (newstr); } char f(unsigned int i, char c) { if (i+1) { if (ft_isalpha(c)) c = c - 32; } return (c); } int main(void) { char *s = "hi daehyun lee"; printf("%s\n", ft_strmapi(s, f)); return 0; }
C
โ€ข
ํ•จ์ˆ˜ ํฌ์ธํ„ฐ
โ€ข
๋ฐ˜ํ™˜๊ฐ’์ž๋ฃŒํ˜• (*ํ•จ์ˆ˜ํฌ์ธํ„ฐ์ด๋ฆ„)();
โ€ข
ํ•จ์ˆ˜ ํฌ์ธํ„ฐ๋ฅผ ์„ ์–ธํ•  ๋•Œ๋Š” ํ•จ์ˆ˜ ํฌ์ธํ„ฐ์™€ ์ €์žฅ๋  ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’ ์ž๋ฃŒํ˜•, ๋งค๊ฐœ๋ณ€์ˆ˜ ์ž๋ฃŒํ˜•๊ณผ ๊ฐœ์ˆ˜๊ฐ€ ์ผ์น˜ํ•ด์•ผ ํ•œ๋‹ค.
#include <stdio.h> void hello() { printf("Hello, world!\n"); } void bonjour() { printf("bonjour le monde!\n"); } int main() { void (*fp)(); // ๋ฐ˜ํ™˜๊ฐ’๊ณผ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์—†๋Š” ํ•จ์ˆ˜ ํฌ์ธํ„ฐ fp ์„ ์–ธ fp = hello; // hello ํ•จ์ˆ˜์˜ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋ฅผ ํ•จ์ˆ˜ ํฌ์ธํ„ฐ fp์— ์ €์žฅ fp(); // Hello, world! fp = bonjour; // bonjour ํ•จ์ˆ˜์˜ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋ฅผ ํ•จ์ˆ˜ ํฌ์ธํ„ฐ fp์— ์ €์žฅ fp(); // bonjour le monde! return 0; }
C
โ€ข
fp์—๋Š” hello ํ•จ์ˆ˜ ๋Œ€์‹  bonjour ํ•จ์ˆ˜๋ฅผ ๋„ฃ์–ด์„œ ํ˜ธ์ถœํ•ด๋„ ๋œ๋‹ค.
โ€ข
์ฆ‰, ํ•จ์ˆ˜ ํฌ์ธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ•จ์ˆ˜๋ฅผ ์ „๊ตฌ ์†Œ์ผ“์ฒ˜๋Ÿผ ๊ฐˆ์•„ ๋ผ์šธ ์ˆ˜ ์žˆ๋‹ค....
โ€ข
7. ft_putchar_fd
Prototype
void ft_putchar_fd(char c, int fd);
Parameters
#1. The character to output.
#2. The file descriptor on which to write.
Return value
None
External functs.
write
Description
Outputs the character โ€™cโ€™ to the given file descriptor.
#include "libft.h" void ft_putchar_fd(char c, int fd) { if (fd < 0) return ; write(fd, &c, 1); }
C
โ€ข
void ํ•จ์ˆ˜์— return ?
open() ํ•จ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์—, fd=open("data.txt", O_RDONLY) ํŒŒ์ผ ์˜คํ”ˆ์— ์‹คํŒจํ•˜๋ฉด file descriptor๋Š” ์Œ์ˆ˜๊ฐ’์„ ๊ฐ–๊ฒŒ ๋œ๋‹ค.
์ด ๋•Œ 'ํ•จ์ˆ˜์˜ ์ข…๊ฒฐ'์„ ์˜๋ฏธํ•˜๋Š” return ; ์‚ฌ์šฉํ•œ๋‹ค.
โ€ข
#include <linux/fs.h>
write() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์ด ํ—ค๋”ํŒŒ์ผ์„ ์ธํด๋ฃจ๋“œ ํ•ด์•ผํ•œ๋‹ค.
8. ft_putstr_fd
Prototype
void ft_putstr_fd(char *s, int fd);
Parameters
#1. The string to output.
#2. The file descriptor on which to write.
External functs.
write
Description
Outputs the string โ€™sโ€™ to the given file descriptor.
#include "libft.h" void ft_putstr_fd(char *s, int fd) { if (!(s) || fd < 0) return ; write(fd, s, ft_strlen(s)); }
Plain Text
9. ft_putendl_fd
Prototype
void ft_putendl_fd(char *s, int fd);
Parameters
#1. The string to output.
#2. The file descriptor on which to write.
External functs.
write
Description
Outputs the string โ€™sโ€™ to the given file descriptor, followed by a newline.
#include "libft.h" void ft_putendl_fd(char *s, int fd) { if (!(s) || fd < 0) return ; write(fd, s, ft_strlen(s)); write(fd, "\n", 1); }
C
10. ft_putnbr_fd
Prototype
void ft_putnbr_fd(int n, int fd);
Parameters
#1. The integer to output.
#2. The file descriptor on which to write.
External functs.
write
Description
Outputs the integer โ€™nโ€™ to the given file descriptor.
#include "libft.h" void display_nbr(int n, int fd) { if (n >= 10) display_nbr(n / 10, fd); write(fd, &"0123456789"[n % 10], 1); } void ft_putnbr_fd(int n, int fd) { if (fd < 0) return ; if (n == -2147483648) { write(fd, "-2147483648", 11); return ; } if (n < 0) { write(fd, "-", 1); n *= -1; } display_nbr(n, fd); }
C
โ€ข
๋‚œ ์ง€๊ธˆ๊นŒ์ง€ display_nbr ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค ๋•Œ ์•„๋ž˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ ๋ฐฐ์—ด์„ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด ์ถœ๋ ฅํ–ˆ์—ˆ๋Š”๋ฐ, writeํ•จ์ˆ˜ ์ธ์ž์— ๋ฐ”๋กœ ๋ฌธ์ž์—ด์˜ ์ฃผ์†Œ๋ฅผ ๋„ฃ์–ด๋ฒ„๋ฆฌ๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์—ˆ๋‹ค. 3์ค„ ์ ˆ์•ฝ ๊ฐ€๋Šฅ.
void display_nbr(int n, int fd) { char *decimal; decimal = "0123456789"; if (n >= 10) display_nbr(n / 10, fd); write(fd, decimal + (n % 10), 1); }
C