\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#410: Three Features of scan Adapted for binary scan/format}
\date{August 26, 2012}
\author{Andreas Leitgeb}
\urlstyle{sf}
\setlength{\parskip}{1ex}
\setlength{\parindent}{0pt}
\def\tipversion$#1${\texttt{\$#1\$}}
\def\tiplangle#1{\ensuremath{<}}
\def\tiprangle#1{\ensuremath{>}}
\def\tipbar#1{\ensuremath{|}}
\def\tipmail#1#2{\(\langle\){\small\expandafter\url{#1@#2}}\(\rangle\)}
\ifx\pdfoutput\undefined
\newcommand{\tipimage}[2]{\typeout{Make sure you download #1.eps}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.eps}}{\includegraphics[width=0.8\textwidth]{#1.eps}}}
\newcommand{\tipxref}[1]{}
\newcommand{\tipxrefend}{}
\else
\newcommand{\tipimage}[2]{\typeout{Make sure you create #1.pdf}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.pdf}}{\includegraphics[width=0.8\textwidth]{#1.pdf}}}
\pdfcatalog{/PageMode /UseOutlines}
\newcommand{\tipxref}[1]{\pdfannotlink attr {/C [0.5 0.5 1.0] /Border [0 0 1]} goto name {#1}}
\newcommand{\tipxrefend}{\pdfendlink}
\fi
\newenvironment{tipabstract}{\begin{abstract}}{\end{abstract}}
\begin{document}\maketitle
\begin{center}\begin{tabularx}{\linewidth}{|r@{: }X|}\hline
\textbf{TIP \#410}&\textbf{Three Features of scan Adapted for binary scan/format}\\\hline
Author&

Andreas Leitgeb \tipmail{avl}{logic.at}
\\
Created&Sunday, \(\text{26}^{\text{th}}\) August 2012\\
Type&Project\\
Tcl Version&\(8.7\)\\
State&Draft\\
Vote&Pending\\
Version&\tipversion$Revision: 1.2 $\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This proposal specifies three new features for \textbf{binary scan} and \textbf{binary format} that already exist similarly for \textbf{scan}, namely: \textbf{\#} for consuming a count-value from the parameter list (like ``\textbf{scan \%*}''), \textbf{p} for writing current position to a consumed parameter variable (like ``\textbf{scan \%n}'') and returning a single parsed value if no parameter is left.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Rationale}

Experience with \textbf{binary format} and \textbf{binary scan} indicates that there are some features of \textbf{scan} which it would be highly desirable to have. In particular, the ability to take an item length as a separate parameter, to store the current location, and to return a \textbf{single matched value} when last variable is not supplied would all be highly desirable.

Different symbols for some of the operations have had to be chosen, as both ``*'' and ``n'' already exist and have a different meaning for \textbf{binary scan} and \textbf{binary format}. Also, unlike with \textit{scan}. no list of values shall be returned (except for a single counted conversion), but instead only one extra conversion character allowed. Experience with scan shows that people tend to forget about the list-layer and use \textit{[scan ``08'' \%d]} directly as a number, which, while safe for integers, is just the wrong thing to do.

The TIP-Author believes that these are all rather ``low hanging fruit''. If this turns out not to be the case, then any controversial one of these features shall be moved to its own TIP.

\section{Proposal}

A ``\textbf{\#}'' (number sign) at a place in the format-string where a number or a ``\textbf{*}'' is currently allowed, shall consume one item from the parameter list and interpret it as a number. It shall only occur after a conversion specifier that accepts trailing numbers. The parameter consumed for ``\textbf{\#}'' is the one \textit{after} the parameter used for the conversion specifier itself, as the ``\textbf{\#}'' follows that specifier.

A new conversion specifier ``\textbf{p}'' shall not accept a trailing count and consume one item from parameter list and interpret it as the name of a local variable into which to store the current cursor-position. No data is consumed for \textbf{binary scan} and no data produced for \textbf{binary format}.

A \textbf{binary scan} with a format-string that contains \textbf{one} data conversion specifier \textbf{more} than variable parameters shall return the remaining converted value (or an empty string if the last conversion wasn't successful).

\subsection{Details}

A ``\textbf{p}''-conversion is not counted. In classic usage with variable parameters, the return value of \textbf{binary scan} gives only the number of real data conversions, thus not counting ``\textbf{@}'', ``\textbf{x}'', ``\textbf{X}'' or ``\textbf{p}''.

A ``\textbf{\#}'' given as count will always imply a list of values written to the variable, even if the value is ``1'' and the list is of length 1. A negative value could change the direction for relative movements ``\textbf{x}'' and ``\textbf{X}'', and is treated as 0 in all other cases. A non-numeric value (including the empty string!) given for a ``\textbf{\#}'' causes the \textbf{binary} command to return an error, just like garbage in the format string would. It is explicitly not intended to get single-value behaviour with ``\textbf{\#}'' and empty string, nor have the separate count-value contain an asterisk or further conversion characters.

\section{Further Ideas}

Eventually, as a special case for \textbf{binary scan}, the following idiom shall be allowed outside of the basic specification:
\begingroup\small\begin{verbatim}
  binary scan "\0\0\0\x2A" "I p" pos
\end{verbatim}
\endgroup

returns 42 and then writes 4 to variable pos.

While the idiom would be quite practical, there is a risk of reader's confusion about which value would be written and which returned, despite unambiguous definition. Also, this one might turn out to be less than trivial to implement, as it would require some lookahead to reserve the remaining parameter for ``p'', not for ``I'' that is currently at hand.

\section{Examples}
\begingroup\small\begin{verbatim}
   # pad out 12 nuls, then set cursor to 0, write an 
   #   int, record position, then write another int.
   set data [binary format "x# @0 I p I" 12 1 pos 42]
   --> data: "\0\0\0\1\0\0\0\x2A\0\0\0\0"  pos: 4

   # set cursor position from value of first param, scan 
   # three items from the data and write them to the
   # next three parameter variables, then write new cursor
   # position to next parameter variable.
   binary scan $data "@# Iss p" $pos beI leS1 leS2 pos
   --> beI: 42  leS1:0 leS2:0 pos:12

   # 4 is value for "#", no further param, thus return 
   # result for "I"
   set val [binary scan $data "@# I" 4]
   --> val: 42

   # error case: more than one conversion to return
   set val [binary scan $data "@# II" 4]
   --> error: "not enough arguments for all format specifiers"

   # extra "further ideas" feature:
   set val [binary scan $data "@# I p" 4 pos]
   --> val: 42 pos: 8
\end{verbatim}
\endgroup

\section{Rejected Alternatives}

For a format string ``\textbf{a\#}'', one could have argued to use first parameter for the count and second for the conversion target, as that would be the order of relevance (count is needed before the resulting value is even generated). I think, this is a bikeshedding issue, and any choice is a good choice, so I went with order of occurrence, thus ``\textbf{a\#}'' expects the target variable first, and the count second.

It would be possible to use ``\textbf{@}'' without a count instead of ``\textbf{p}'', but I consider it dangerous, when a previous error turns into overwriting of variables. I consider a typo ``\textbf{@ 42 ...}'' to be common enough to not want to give it a new unexpected meaning and side-effect.

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\end{document}
