# Emulating \@ifnextchar in expl3

I would like to write an expl3 version of the following.

\documentclass{article}
\makeatletter
\def\ie{i.e\@ifnextchar.{}{.\@{} }}
\makeatother

\pagestyle{empty}
\begin{document}
\noindent
\ie Hello\newline
\ie. Hello\newline
\end{document}


I'm aware of the \peek_after:Nw command, but I don't understand how to implement it. In fact, all the \peek_... commands seem to require you to explicitly feed it a token. I seem to have to write a command which takes arguments.

I could try something like:

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\eg}{ m }
{
e.g
\str_if_eq_x:nnF { . } { #1 }{.\@{} }
}
\ExplSyntaxOff
\pagestyle{empty}
\begin{document}
\noindent
\eg Hello\newline
\eg. Hello\newline
{\eg} Hello
\end{document}


But there are a couple of sticking points.

First, because \eg is defined to take an argument, it gobbles up the following H in the first instance. \ie doesn't have this problem. (I could get around this by writing something like \str_if_eq_x:nnF { . } { #1 }{.\@{} #1}, but in my real document for which all of this is a greatly reduced MWE, I run into more issues when #1 contains something that's not a character.)

Second, {\eg} creates an error because it's expecting an argument but encounters what it believes are unbalanced parentheses. Again, {\ie} very nicely does not have this issue.

So this approach is really not what I'm looking for.

Here's my attempt at using \peek_after:Nw

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn

\cs_new:Nn \ae_eg:
{
\peek_after:Nw \ae_test_period:
}

\cs_new:Nn \ae_test_period:
{
\str_if_eq_x:nnF  { \l_peek_token }  { . }  { FALSE }
}

\NewDocumentCommand{\eg}{}
{
e.g \ae_eg:
}

\ExplSyntaxOff
\pagestyle{empty}
\begin{document}
\noindent
\eg. Hello\newline
\eg  Hello\newline
\end{document}


But \l_peek_token is never seen to be the same as .

You are looking in the right place with \peek_after:wN, but that is the most low-level function (a wrapper around \futurelet). What you want is \peek_meaning:NF:

\documentclass{article}
\usepackage{expl3,xparse}
\ExplSyntaxOn
\NewDocumentCommand { \ie } { }
{
i.e
\peek_meaning:NF . % Just a 'false' branch needed
{ . \@ ~ }
}
\ExplSyntaxOff

\pagestyle{empty}
\begin{document}
\noindent
\ie Hello\newline
\ie. Hello\newline
\end{document}


\@ifnextchar does its comparison using \ifx, which is meaning in expl3 term. \@ifnextchar also skips spaces, which is not relevant here but might mean you want \peek_meaning_ignore_spaces:N(TF), depending on the full context.

It seems that I'm using the wrong equality test: I should have written:

\cs_generate_variant:Nn \token_if_eq_meaning:NNTF {NnTF}
\cs_new:Nn \ae_test_period:
{
\token_if_eq_meaning:NnTF  \l_peek_token   { . } {  }  { .\@{~} }
}