Skip to content

Instantly share code, notes, and snippets.

@42joonpark
Created June 29, 2021 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 42joonpark/edbd927103e4c56ec76515c3e78edc81 to your computer and use it in GitHub Desktop.
Save 42joonpark/edbd927103e4c56ec76515c3e78edc81 to your computer and use it in GitHub Desktop.
int get_token(char input)
{
if (input == '%')
return (TK_PERCENTAGE);
else if (input >= '1' && input <= '9')
return (TK_ONE_NINE);
else if (input == '\0')
return (TK_NULL);
else if (input == '.')
return (TK_DOT);
else if (input == '*')
return (TK_ASTERISK);
else if (input == '-')
return (TK_HYPEN);
else if (input == '0')
return (TK_ZERO);
else if (input == 'c' || input == 'd' || input == 's' ||
input == 'x' || input == 'X' || input == 'u' ||
input == 'i' || input == 'p')
return (TK_SPECIFIER);
else
return (TK_ELSE);
return (-1);
}
int get_state(int state, int input)
{
int arr[81] = { 2, 1, 10, 1, 1, 1, 1, 1, 1,
2, 1, 10, 1, 1, 1, 1, 1, 1,
8, 3, -1, 5, 4, 2, 2, 8, -1,
8, 3, -1, 5, -1, -1, 3, 8, -1,
8, -1, -1, 5, -1, -1, -1, 8, -1,
8, 6, -1, -1, 7, -1, 6, 8, -1,
8, 6, -1, -1, -1, -1, 6, 8, -1,
8, -1, -1, -1, -1, -1, -1, 8, -1
};
return (arr[(state * 9) + input]);
}
while (*str)
{
token = get_token(*str);
state = get_state(state, token);
if (state == 1)
write(1, str, 1);
if (state == 2)
{
if (*str == '-')
{
opt->hypen = 1;
opt->padc = ' ';
}
else if (*str == '0' && opt->hypen != 1)
opt->padc = '0';
}
if (state == 3)
{
opt->width *= 10;
opt->width += (*str - '0');
}
if (state == 4)
{
opt->width = va_arg(ap, int);
if (opt->width < 0 && !opt->hypen)
{
opt->hypen = 1;
opt->padc = ' ';
}
if (opt->width < 0)
opt->width = -opt->width;
}
if (state == 5)
{
opt->dot = 1;
opt->length = 0;
}
if (state == 6)
{
opt->length *= 10;
opt->length += (*str - '0');
}
if (state == 7)
opt->length = va_arg(ap, int);
if (state == 8 || state == 10) // 현재 % 에 대한 것들은 다 읽은 거니까 종료해야지
break ;
if (state == -1)
return (-1);
str++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment