コード例 #1
0
ファイル: ft_itoa_base.c プロジェクト: Jino42/ft_ls
char		*ft_itoa_base(long long n, int base)
{
	long long int	nb;
	int				i;
	int				over;
	char			*str;

	over = 0;
	nb = n;
	i = count_number(nb, base) - 1;
	if ((str = ft_strnew(count_number(nb, base))) == NULL)
		return (NULL);
	if (nb < 0)
	{
		str[0] = '-';
		nb = nb * -1;
	}
	if (nb == 0)
		str[i] = '0';
	while (nb != 0)
	{
		over = nb % base > 9 ? 'a' - 10 : '0';
		str[i] = nb % base + over;
		i--;
		nb = nb / base;
	}
	return (str);
}
コード例 #2
0
ファイル: ft_itoa.c プロジェクト: kasou/lem-in
static int	count_number(int n)
{
	if (n == 2147483647 || n == -2147483648)
		return (10);
	if (n < 10)
		return (1);
	else
		return (1 + count_number(n / 10));
}
コード例 #3
0
ファイル: ft_multitoa_base.c プロジェクト: Jino42/ft_ls
char		*ft_itoa_ll_limit(long long int nb)
{
	int				i;
	char			*str;
	long long int	nb2;

	i = count_number(nb, 10) - 1;
	str = ft_strnew(count_number(nb, 10));
	if (str == NULL)
		return (NULL);
	if (nb < 0)
		str[0] = '-';
	if (nb == 0)
		str[i] = '0';
	while (nb != 0)
	{
		nb2 = (nb % 10);
		nb2 *= nb2 < 0 ? -1 : 1;
		str[i] = nb2 + '0';
		i--;
		nb = nb / 10;
	}
	return (str);
}
コード例 #4
0
ファイル: ft_itoa.c プロジェクト: kasou/lem-in
char		*ft_itoa(int n)
{
	char	*p;
	int		neg;
	int		nb_number;
	int		i;

	neg = is_neg(n);
	if (n == -2147483648)
		return (get_max_int_neg());
	if (neg)
		n = n * -1;
	nb_number = count_number(n);
	i = nb_number + neg;
	p = (char *) malloc(sizeof(char) * (nb_number + 1 + neg));
	p[i] = '\0';
	while (--i)
	{
		p[i] = n % 10 + 48;
		n = n / 10;
	}
	p[i] = get_first_char(neg, n);
	return (p);
}
コード例 #5
0
ファイル: lexer.c プロジェクト: nholland94/tdb
lexeme read_token(buffer_reader *reader) {
  lexeme lex;
  rune c;

  lex.lexeme_type = LEXEME_UNKNOWN;

  do {
    lex.location = buffer_reader_get_current_pointer(reader);
    buffer_reader_skip(reader);
    c = *lex.location;
    // c = buffer_reader_read_rune(reader);
  } while(is_whitespace(c));

  if(is_special_symbol(c)) {
    lex.lexeme_type = LEXEME_ATOM;
    lex.size = 1;
  } else if(is_alpha(c)) {
    lex.lexeme_type = LEXEME_ATOM;
    lex.size = count_atom(reader);
  } else if(is_numeric(c)) {
    lex.lexeme_type = LEXEME_NUMBER;
    lex.size = count_number(reader);
  } else {
    switch(c) {
    case '\'':
      lex.lexeme_type = LEXEME_CHAR;
      lex.location++;
      lex.size = 1;
      check_char(reader);
      break;

    case '"':
      lex.lexeme_type = LEXEME_STRING;
      lex.location++;
      lex.size = count_string(reader);
      break;

    case '{':
      lex.lexeme_type = LEXEME_LEFT_BRACKET;
      lex.size = 1;
      break;

    case '}':
      lex.lexeme_type = LEXEME_RIGHT_BRACKET;
      lex.size = 1;
      break;

    case 0x2190: // left arrow
      lex.lexeme_type = LEXEME_LEFT_ARROW;
      lex.size = 1;
      break;

    case 0x235D: // up shoe jot
      lex.lexeme_type = LEXEME_COMMENT;
      lex.size = count_until(reader, '\n') + 1;
      break;

    default:
      printf("Unhandled character in lexer: %4x\n", c);
      lex.size = 1;
    }
  }

  return lex;
}