# expl3 - Counting the number of leading spaces of an argument

How can I count the number of leading spaces and not all spaces as with the use of \str_count_spaces ?

Concretly I would like to read a content line by line and for each line of input I would use the indentation to build the corresponding output.

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn
\seq_new:N \l__pmbc_words_seq
\tl_new:N \l__pmbc_one_temp_tl

}

% ...
}
\ExplSyntaxOff

\begin{document}

\end{document}

• TeX ignores consecutive spaces, so unless you force them in some other way, typing \nbleadingsp{ a} or \nbleadingsp{ a} or \nbleadingsp{ a} will all have one single space token. – Phelype Oleinik Aug 13 '20 at 16:33
• You can do it only if you don't allow \nbleadingsp to be in the argument of another command. Or globally changing the category code of the space, which would break so many things… – egreg Aug 13 '20 at 16:43

As Phelype Oleinik and egreg has said, it's possible only by reading the argument verbatim, which forbides the use in another command.

In the following code, the argument is taken verbatim by use of the v specifier of xparse.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand { \nbleadingsp } { v }
{ \__pmbc_nbleadingsp:n { #1 } }

{
\int_zero:N \l_tmpa_int
\str_map_inline:nn { #1 }
{
\str_if_eq:nnTF { ##1 } { ~ }
{ \int_incr:N \l_tmpa_int }
{ \str_map_break: }
}
\int_use:N \l_tmpa_int
}
\ExplSyntaxOff

\begin{document}