[42seoul] libft (chapter 2)

ppparkta·2022년 7월 11일
1

42Seoul

목록 보기
2/10

chapter2

substr

  • malloc으로 메모리를 할당받고 s로부터 부분 문자열을 생성하여 반환하는 함수
  • start 부터 최대 len까지의 길이를 갖지만 len이 s의 길이를 초과한 경우를 고려하여 함수를 만들었음
char	*ft_substr(char const *s, unsigned int start, size_t len)
{
	size_t		i;
	size_t		j;
	char		*ans;

	i = 0;
	j = ft_strlen(s);
	if (s == 0)
		return (0);
	if (j <= start)
		return (ft_strdup(""));
	j -= start;
	if (j < len)
		len = j;
	ans = (char *)malloc(sizeof(char) * (len + 1));
	if (ans == 0)
		return (0);
	while (i < len && s[start])
		ans[i++] = s[start++];
	ans[i] = 0;
	return (ans);
}

strjoin

  • 메모리를 할당받은 뒤s1과 s2를 이어붙인 문자열을 생성하여 반환하는 함수
char	*ft_strjoin(char const *s1, char const *s2)
{
	char	*ans;
	size_t	i;
	size_t	j;

	if (s1 == 0 || s2 == 0)
		return (0);
	ans = (char *)malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 1));
	if (ans == 0)
		return (0);
	i = 0;
	while (i < ft_strlen(s1))
	{
		ans[i] = s1[i];
		i++;
	}
	j = 0;
	while (j < ft_strlen(s2))
	{
		ans[i + j] = s2[j];
		j++;
	}
	ans[i + j] = 0;
	return (ans);
}

strtrim

  • 메모리를 할당받은 뒤 s1의 양 끝에서 지정된 문자들이 제거된 사본을 반환하는 함수
  • 양 끝의 기준? 단순히 좌우? ㅇㅇ
  • strchr은 문자열 내에서 c를 찾았을 때 그 시작주소를 리턴하는 함수
  • substr은 s로부터 부분 문자열을 생성하여 반환하는 함수
char	*ft_strtrim(char const *s1, char const *set)
{
	size_t	start;
	size_t	end;

	start = 0;
	end = ft_strlen(s1) - 1;
	if (s1 == 0 || set == 0)
		return (0);
	while (s1[start] && ft_strchr(set, s1[start]))
		start++;
	if (start >= end)
		return (ft_strdup(""));
	while (end > start && ft_strchr(set, s1[end]))
		end--;
	return (ft_substr(s1, start, (end - start + 1)));
}

ft_split

  • 메모리를 할당받은 뒤 구분자c를 기준으로 s를 분할하여 그 결과를 담은 새로운 문자열 배열을 반환하는 함수
  • 배열의 끝는 0으로 끝나며 중간에 할당에 실패하면 이전까지 할당받은 메모리를 free해야 함
static size_t	word_cnt(char const *s, char c)
{
	size_t	i;
	size_t	cnt;

	cnt = 0;
	i = 0;
	while (s[i])
	{
		if (s[i] != c)
		{
			cnt++;
			while (s[i] != c && s[i])
				i++;
		}
		else
			i++;
	}
	return (cnt);
}

static char	*ft_strndup(const char *s, size_t n)
{
	size_t	i;
	char	*s1;

	i = 0;
	s1 = 0;
	if (n == 0)
		return (0);
	s1 = (char *)malloc(sizeof(char) * n + 1);
	if (s1 == 0)
		return (0);
	while (i < n)
	{
		s1[i] = s[i];
		i++;
	}
	s1[i] = 0;
	return (s1);
}

static char	**free_thing(char	**ans)
{
	size_t	i;

	i = 0;
	while (ans[i])
	{
		free(ans[i]);
		i++;
	}
	free(ans);
	return (0);
}

char	**ft_split(char const *s, char c)
{
	size_t	i;
	size_t	j;
	size_t	temp;
	char	**ans;

	if (s == 0)
		return (0);
	ans = (char **)malloc(sizeof(char *) * (word_cnt(s, c) + 1));
	if (ans == 0)
		return (0);
	i = 0;
	j = 0;
	while (i < word_cnt(s, c) && s[j] != 0)
	{
		while (s[j] == c)
			j++;
		temp = j;
		while (s[j] != c && s[j] != 0)
			j++;
		ans[i] = ft_strndup(&s[temp], j - temp);
		if (ans[i++] == 0)
			return (free_thing(ans));
	}
	ans[i] = 0;
	return (ans);
}

ft_itoa

  • 메모리를 할당받은 후 n을 문자열로 변환하여 반환하는 함수
  • 음수도 처리되지만 부호가 두개 이상인 경우는 오류로 간주함
  • int_cnt는 문자열의 길이를 계산하는 함수임.
static size_t	int_cnt(int n, size_t *sym)
{
	size_t	i;

	*sym = 0;
	i = 0;
	if (n == 0)
		return (1);
	if (n < 0)
	{
		if (n == -2147483648)
			return (11);
		else
		{
			n = -n;
			*sym = 1;
		}
	}
	while (n)
	{
		n /= 10;
		i++;
	}
	return (i + *sym);
}

static void	str_fill(char *ans, size_t len, int n)
{
	if (n == 0)
		ans[0] = '0';
	while (n)
	{
		ans[len - 1] = n % 10 + '0';
		n /= 10;
		len --;
	}
}

char	*ft_itoa(int n)
{
	size_t		len;
	size_t		sym;
	char		*ans;

	len = int_cnt(n, &sym);
	ans = malloc(sizeof(unsigned char) * len + 1);
	if (ans == 0)
		return (0);
	ans[len] = 0;
	if (n == -2147483648)
	{
		ans[10] = '8';
		ans[0] = '-';
		n = 214748364;
		len--;
	}
	else if (sym == 1)
	{
		ans[0] = '-';
		n = -n;
	}
	str_fill(ans, len, n);
	return (ans);
}

ft_strmapi

  • 문자열 s의 각 문자에 f함수를 적용하고 해당 문자 인덱스를 f의 첫번째 인자로 사용하는 함수
  • f함수를 적용한 새로운 문자열을 메모리 할당받아 생성해야 함
char	*ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
	unsigned int	len;
	unsigned int	i;
	char			*ans;

	if (s == 0 || f == 0)
		return (0);
	len = (unsigned int)ft_strlen(s);
	ans = (char *)malloc(sizeof(char) * len + 1);
	if (ans == 0)
		return (0);
	i = 0;
	while (i < len)
	{
		ans[i] = f(i, s[i]);
		i++;
	}
	ans[len] = 0;
	return (ans);
}

ft_striteri

  • s를 순회하며 f함수를 적용시키는 함수
void	ft_striteri(char *s, void (*f)(unsigned int, char *))
{
	unsigned int	len;
	unsigned int	i;

	len = (unsigned int)ft_strlen(s);
	i = 0;
	while (i < len)
	{
		f(i, &s[i]);
		i++;
	}
}

ft_putchar_fd(char c, int fd)

  • 제공받은 파일 식별자에 문자 c를 출력하는 함수

파일식별자(file descriptor)란?
파일을 열었을 때 그 파일에 번호를 부여하는 것

012
stdinputstdoutputstderr
profile
겉촉속촉

0개의 댓글

관련 채용 정보