\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#105: Add Prefix Matching for Switch}
\date{July 03, 2002}
\author{Donal K. Fellows}
\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 \#105}&\textbf{Add Prefix Matching for Switch}\\\hline
Author&

Donal K. Fellows \tipmail{fellowsd}{cs.man.ac.uk}
\\
Created&Wednesday, \(\text{3}^{\text{rd}}\) July 2002\\
Type&Project\\
Tcl Version&\(8.5\)\\
State&Withdrawn\\
Vote&Pending\\
Version&\tipversion$Revision: 1.4 $\\
Post History&\\
Obsoleted By&195\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This TIP adds a new option to the [switch] command to support matching of strings to unique prefixes of patterns, similar to Tcl's existing subcommand-name matching or Tk's option-name matching.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Rationale}

When code (particularly in script libraries) wants to support shortest unique prefix matching in the manner of the Tcl core (as provided by \textit{Tcl\_GetIndexFromObj}) currently either the prefixes have to be precomputed (by hand or by script) or the matching has to be done backwards. In the first case, this is either error-prone or requires an extra piece of code that has to be developed by the programmer. In the second case, the code has to be converted into a pattern which is matched against the list of supported options in some way, which is either inefficient or has hazards if the string being matched contains characters that are meaningful to the matching engine being used. Instead, it would be far nicer if we could make the core support this directly, so that script authors could just say what they mean.

\section{Proposed Change}

To support this, I propose modifying the \textit{switch} command to take an extra option \textit{-prefix} (which should be mutually exclusive with \textit{-exact}, \textit{-glob} and \textit{-regexp} of course) to enable prefix matching. When prefix matching is enabled, the arm chosen for execution will be the one such that the switch value is identical to or an unambiguous prefix of its pattern (i.e. it will not be a prefix of any other pattern listed, unless the pattern of the arm chosen is exactly equal to the switch value.) If there is no arm whose pattern is an unambiguous prefix of the switch value, the default arm will be selected for execution, or if there is no default arm, the switch command will terminate without an error and with an empty result (this is in contrast to the behaviour of \textit{Tcl\_GetIndexFromObj}.)

\section{Examples}

The command:
\begingroup\small\begin{verbatim}
switch -prefix f {
   foo {
      puts "matched foo"
   }
   bar {
      puts "matched bar"
   }
}
\end{verbatim}
\endgroup

prints ``matched foo''. The command:
\begingroup\small\begin{verbatim}
switch -prefix b {
   bar {
      puts "matched bar"
   }
   boo {
      puts "matched boo"
   }
   default {
      puts "the default action"
   }
}
\end{verbatim}
\endgroup

prints ``the default action'' (``b'' is a prefix of two patterns.) The command:
\begingroup\small\begin{verbatim}
switch -prefix tcl {
   tcl {
      puts "The Tool Command Language"
   }
   tk {
      puts "The Tk Toolkit"
   }
   tcl/tk {
      puts "A cool combination"
   }
}
\end{verbatim}
\endgroup

prints ``The Tool Command Language'' (although ``tcl'' is a prefix of two patterns, it matches one of them exactly.)

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

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