Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/user/user.tex

    re71b09a r5ecaeca  
    1111%% Created On       : Wed Apr  6 14:53:29 2016
    1212%% Last Modified By : Peter A. Buhr
    13 %% Last Modified On : Tue Jan 30 09:02:41 2024
    14 %% Update Count     : 6046
     13%% Last Modified On : Sun Jan 14 17:27:41 2024
     14%% Update Count     : 5764
    1515%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    1616
     
    4646%\input{common}                                                                                 % common CFA document macros
    4747\usepackage[dvips,plainpages=false,pdfpagelabels,pdfpagemode=UseNone,colorlinks=true,pagebackref=true,linkcolor=blue,citecolor=blue,urlcolor=blue,pagebackref=true,breaklinks=true]{hyperref}
    48 \urlstyle{sf}
    4948\usepackage{breakurl}
    5049
     
    164163
    165164\Index*[C++]{\CC{}}~\cite{c++:v1} had a similar goal 30 years ago, allowing object-oriented programming to be incrementally added to C.
    166 However, \CC currently has the disadvantages of a strong object-oriented bias, multiple legacy design-choices that are difficult to update, and active divergence of the language model from C, requiring significant effort and training to incrementally add \CC to a C code-base.
     165However, \CC currently has the disadvantages of a strong object-oriented bias, multiple legacy design-choices that are difficult to update, and active divergence of the language model from C, requiring significant effort and training to incrementally add \CC to a C-based project.
    167166In contrast, \CFA has 30 years of hindsight and a clean starting point.
    168167
     
    201200\end{center}
    202201While \CFA I/O \see{\VRef{s:StreamIOLibrary}} looks similar to \Index*[C++]{\CC{}}, there are important differences, such as automatic spacing between variables and an implicit newline at the end of the expression list, similar to \Index*{Python}~\cite{Python}.
    203 In general, \CFA programs are 10\% to 30\% shorter than their equivalent C/\CC counterparts.
    204202
    205203
     
    220218Even with all its problems, C continues to be popular because it allows writing software at virtually any level in a computer system without restriction.
    221219For system programming, where direct access to hardware, storage management, and real-time issues are a requirement, C is the only language of choice.
    222 The TIOBE index~\cite{TIOBE} for February 2023 ranks the top six most \emph{popular} programming languages as C 17.4\%, \Index*{Java} 12\%, Python 12\%, \Index*[C++]{\CC{}} 7.6\%, \Csharp 4\%, Visual Basic 3.8\% = 56.8\%, where the next 50 languages are less than 2\% each, with a long tail.
     220The TIOBE index~\cite{TIOBE} for February 2021 ranks the top six most \emph{popular} programming languages as C 17.4\%, \Index*{Java} 12\%, Python 12\%, \Index*[C++]{\CC{}} 7.6\%, \Csharp 4\%, Visual Basic 3.8\% = 56.8\%, where the next 50 languages are less than 2\% each, with a long tail.
    223221The top 4 rankings over the past 35 years are:
    224222\begin{center}
     
    240238however, it largely extended the C language, and did not address many of C's existing problems.\footnote{%
    241239Two important existing problems addressed were changing the type of character literals from ©int© to ©char© and enumerator from ©int© to the type of its enumerators.}
    242 \Index*{Fortran}~\cite{Fortran08}, \Index*{Cobol}~\cite{Cobol14}, and \Index*{Ada}~\cite{Ada16} are examples of programming languages that took an evolutionary approach, where modern language-features (\eg objects, concurrency) are added and problems fixed within the framework of the existing language.
     240\Index*{Fortran}~\cite{Fortran08}, \Index*{Cobol}~\cite{Cobol14}, and \Index*{Ada}~\cite{Ada12} are examples of programming languages that took an evolutionary approach, where modern language-features (\eg objects, concurrency) are added and problems fixed within the framework of the existing language.
    243241\Index*{Java}~\cite{Java8}, \Index*{Go}~\cite{Go}, \Index*{Rust}~\cite{Rust} and \Index*{D}~\cite{D} are examples of the revolutionary approach for modernizing C/\CC, resulting in a new language rather than an extension of the descendent.
    244242These languages have different syntax and semantics from C, do not interoperate directly with C, and are not systems languages because of restrictive memory-management or garbage collection.
     
    265263% extending the C type system with parametric polymorphism and overloading, as opposed to the \Index*[C++]{\CC{}} approach of object-oriented extensions.
    266264\CFA{}\hspace{1pt}'s polymorphism was originally formalized by \Index*{Glen Ditchfield}\index{Ditchfield, Glen}~\cite{Ditchfield92}, and first implemented by \Index*{Richard Bilson}\index{Bilson, Richard}~\cite{Bilson03}.
    267 However, at that time, there was little interest in extending C, so work did not continue.
     265However, at that time, there was little interesting in extending C, so work did not continue.
    268266As the saying goes, ``\Index*{What goes around, comes around.}'', and there is now renewed interest in the C programming language because of the legacy code-base, so the \CFA project was restarted in 2015.
    269267
     
    274272\CFA is designed to integrate directly with existing C programs and libraries.
    275273The most important feature of \Index{interoperability} is using the same \Index{calling convention}s, so there is no complex interface or overhead to call existing C routines.
    276 This feature allows \CFA programmers to take advantage of the existing panoply of C libraries to access thousands of software features.
     274This feature allows \CFA programmers to take advantage of the existing panoply of C libraries to access thousands of external software features.
    277275Language developers often state that adequate \Index{library support} takes more work than designing and implementing the language itself.
    278276Fortunately, \CFA, like \Index*[C++]{\CC{}}, starts with immediate access to all exiting C libraries, and in many cases, can easily wrap library routines with simpler and safer interfaces, at zero or very low cost.
     
    322320However, it is necessary to differentiate between C and \CFA code because of name \Index{overload}ing, as for \CC.
    323321For example, the C math-library provides the following routines for computing the absolute value of the basic types: ©abs©, ©labs©, ©llabs©, ©fabs©, ©fabsf©, ©fabsl©, ©cabsf©, ©cabs©, and ©cabsl©.
    324 Whereas, \CFA wraps these routines into one overloaded name ©abs©:
    325 \begin{cfa}
    326 unsigned char ®abs®( signed char );                             §\C[3.5in]{// no C equivalent}§
    327 extern "C" { int ®abs®( int ); }                                §\C{// C abs
    328 unsigned long int ®abs®( long int );                    §\C{// C labs}§
    329 unsigned long long int ®abs®( long long int );  §\C{// C llabs}§
    330 float ®abs®( float );                                                   §\C{// C fabsf}§
    331 double ®abs®( double );                                                 §\C{// C fabs}§
    332 long double ®abs®( long double );                               §\C{// C fabsl}§
    333 float _Complex ®abs®( float _Complex );                 §\C{// C cabsf}§
    334 double _Complex ®abs®( double _Complex );               §\C{// C cabs}§
    335 long double _Complex ®abs®( long double _Complex ); §\C{// C cabsl}\CRT§
     322Whereas, \CFA wraps each of these routines into one overloaded name ©abs©:
     323\begin{cfa}
     324char ®abs®( char );
     325extern "C" { int ®abs®( int ); } §\C{// use default C routine for int
     326long int ®abs®( long int );
     327long long int ®abs®( long long int );
     328float ®abs®( float );
     329double ®abs®( double );
     330long double ®abs®( long double );
     331float _Complex ®abs®( float _Complex );
     332double _Complex ®abs®( double _Complex );
     333long double _Complex ®abs®( long double _Complex );
    336334\end{cfa}
    337335The problem is a \Index{name clash} between the C name ©abs© and the \CFA names ©abs©, resulting in two name linkages\index{C linkage}: ©extern "C"© and ©extern "Cforall"© (default).
     
    341339
    342340This example illustrates a core idea in \CFA: \emph{the \Index{power of a name}}.
    343 The name ``©abs©'' evokes the notion of absolute value and many mathematical types provide the notion of absolute value.
    344 Hence, knowing the name ©abs© is sufficient to apply it to any applicable type.
     341The name ``©abs©'' evokes the notion of absolute value, and many mathematical types provide the notion of absolute value.
     342Hence, knowing the name ©abs© is sufficient to apply it to any type where it is applicable.
    345343The time savings and safety of using one name uniformly versus $N$ unique names cannot be underestimated.
    346344
    347345
    348346\section{\CFA Compilation}
    349 
    350 \CFA is a \newterm{transpiler}, meaning it reads in a programming language (\CFA) as input and generates another programming language (C) as output, whereas a \newterm{compiler} reads in a programming language and generates assembler/machine code.
    351 Hence, \CFA is like the C preprocessor modifying a program and sending it on to another step for further transformation.
    352 The order of transformation is C preprocessor, \CFA, and finally GNU C compiler, which also has a number of transformation steps, such as assembler and linker.
    353347
    354348The command ©cfa© is used to compile a \CFA program and is based on the \Index{GNU} \Indexc{gcc} command, \eg:
     
    450444which conditionally includes the correct header file, if the program is compiled using \Indexc{gcc} or \Indexc{cfa}.
    451445
    452 The \CFA \Index{transpiler} has multiple internal steps.
    453 The following flags control how the \CFA transpiler works, the stages run, and printing within a stage.
     446The \CFA translator has multiple steps.
     447The following flags control how the translator works, the stages run, and printing within a stage.
    454448The majority of these flags are used by \CFA developers, but some are occasionally useful to programmers.
    455 Each option must be escaped with \Indexc{-XCFA}\index{transpiler option!-XCFA@{©-XCFA©}} to direct it to the \CFA compilation step, similar to the ©-Xlinker© flag for the linker, \eg:
     449Each option must be escaped with \Indexc{-XCFA}\index{translator option!-XCFA@{©-XCFA©}} to direct it to the compiler step, similar to the ©-Xlinker© flag for the linker, \eg:
    456450\begin{lstlisting}[language=sh]
    457451cfa §test§.cfa -CFA -XCFA -p # print translated code without printing the standard prelude
     
    464458\begin{description}[topsep=5pt,itemsep=0pt,parsep=0pt]
    465459\item
    466 \Indexc{-c}\index{transpiler option!-c@{©-c©}}, \Indexc{--colors}\index{transpiler option!--colors@{©--colors©}} \, diagnostic color: ©never©, ©always©, \lstinline[deletekeywords=auto]{auto}
    467 \item
    468 \Indexc{-g}\index{transpiler option!-g@{©-g©}}, \Indexc{--gdb}\index{transpiler option!--gdb@{©--gdb©}} \, wait for gdb to attach
    469 \item
    470 \Indexc{-h}\index{transpiler option!-h@{©-h©}}, \Indexc{--help}\index{transpiler option!--help@{©--help©}} \, print transpiler help message
    471 \item
    472 \Indexc{-i}\index{transpiler option!-i@{©-i©}}, \Indexc{--invariant}\index{transpiler option!--invariant@{©--invariant©}} \, invariant checking during AST passes
    473 \item
    474 \Indexc{-l}\index{transpiler option!-l@{©-l©}}, \Indexc{--libcfa}\index{transpiler option!--libcfa@{©--libcfa©}} \, generate ©libcfa.c©
    475 \item
    476 \Indexc{-L}\index{transpiler option!-L@{©-L©}}, \Indexc{--linemarks}\index{transpiler option!--linemarks@{©--linemarks©}} \, generate line marks
    477 \item
    478 \Indexc{-m}\index{transpiler option!-m@{©-m©}}, \Indexc{--no-main}\index{transpiler option!--no-main@{©--no-main©}} \, do not replace main
    479 \item
    480 \Indexc{-N}\index{transpiler option!-N@{©-N©}}, \Indexc{--no-linemarks}\index{transpiler option!--no-linemarks@{©--no-linemarks©}} \, do not generate line marks
    481 \item
    482 \Indexc{-n}\index{transpiler option!-n@{©-n©}}, \Indexc{--no-prelude}\index{transpiler option!--no-prelude@{©--no-prelude©}} \, do not read prelude
    483 \item
    484 \Indexc{-p}\index{transpiler option!-p@{©-p©}}, \Indexc{--prototypes}\index{transpiler option!--prototypes@{©--prototypes©}} \, do not generate prelude prototypes $\Rightarrow$ prelude not printed
    485 \item
    486 \Indexc{-d}\index{transpiler option!-d@{©-d©}}, \Indexc{--deterministic-out}\index{transpiler option!--deterministic-out@{©--deterministic-out©}} \, only print deterministic output
    487 \item
    488 \Indexc{-P}\index{transpiler option!-P@{©-P©}}, \Indexc{--print}\index{transpiler option!--print@{©--print©}} \, one of:
     460\Indexc{-c}\index{translator option!-c@{©-c©}}, \Indexc{--colors}\index{translator option!--colors@{©--colors©}} \, diagnostic color: ©never©, ©always©, \lstinline[deletekeywords=auto]{auto}
     461\item
     462\Indexc{-g}\index{translator option!-g@{©-g©}}, \Indexc{--gdb}\index{translator option!--gdb@{©--gdb©}} \, wait for gdb to attach
     463\item
     464\Indexc{-h}\index{translator option!-h@{©-h©}}, \Indexc{--help}\index{translator option!--help@{©--help©}} \, print translator help message
     465\item
     466\Indexc{-i}\index{translator option!-i@{©-i©}}, \Indexc{--invariant}\index{translator option!--invariant@{©--invariant©}} \, invariant checking during AST passes
     467\item
     468\Indexc{-l}\index{translator option!-l@{©-l©}}, \Indexc{--libcfa}\index{translator option!--libcfa@{©--libcfa©}} \, generate ©libcfa.c©
     469\item
     470\Indexc{-L}\index{translator option!-L@{©-L©}}, \Indexc{--linemarks}\index{translator option!--linemarks@{©--linemarks©}} \, generate line marks
     471\item
     472\Indexc{-m}\index{translator option!-m@{©-m©}}, \Indexc{--no-main}\index{translator option!--no-main@{©--no-main©}} \, do not replace main
     473\item
     474\Indexc{-N}\index{translator option!-N@{©-N©}}, \Indexc{--no-linemarks}\index{translator option!--no-linemarks@{©--no-linemarks©}} \, do not generate line marks
     475\item
     476\Indexc{-n}\index{translator option!-n@{©-n©}}, \Indexc{--no-prelude}\index{translator option!--no-prelude@{©--no-prelude©}} \, do not read prelude
     477\item
     478\Indexc{-p}\index{translator option!-p@{©-p©}}, \Indexc{--prototypes}\index{translator option!--prototypes@{©--prototypes©}} \, do not generate prelude prototypes $\Rightarrow$ prelude not printed
     479\item
     480\Indexc{-d}\index{translator option!-d@{©-d©}}, \Indexc{--deterministic-out}\index{translator option!--deterministic-out@{©--deterministic-out©}} \, only print deterministic output
     481\item
     482\Indexc{-P}\index{translator option!-P@{©-P©}}, \Indexc{--print}\index{translator option!--print@{©--print©}} \, one of:
    489483\begin{description}[topsep=0pt,itemsep=0pt,parsep=0pt]
    490484\item
    491 \Indexc{ascodegen}\index{transpiler option!-P@{©-P©}!©ascodegen©}\index{transpiler option!--print@{©-print©}!©ascodegen©} \, print AST as codegen rather than AST
    492 \item
    493 \Indexc{asterr}\index{transpiler option!-P@{©-P©}!©asterr©}\index{transpiler option!--print@{©-print©}!©asterr©} \, print AST on error
    494 \item
    495 \Indexc{declstats}\index{transpiler option!-P@{©-P©}!©declstats©}\index{transpiler option!--print@{©-print©}!©declstats©} \, print code property statistics
    496 \item
    497 \Indexc{parse}\index{transpiler option!-P@{©-P©}!©parse©}\index{transpiler option!--print@{©-print©}!©parse©} \, print yacc (parsing) debug information
    498 \item
    499 \Indexc{pretty}\index{transpiler option!-P@{©-P©}!©pretty©}\index{transpiler option!--print@{©-print©}!©pretty©} \, prettyprint for ©ascodegen© flag
    500 \item
    501 \Indexc{rproto}\index{transpiler option!-P@{©-P©}!©rproto©}\index{transpiler option!--print@{©-print©}!©rproto©} \, resolver-proto instance
    502 \item
    503 \Indexc{rsteps}\index{transpiler option!-P@{©-P©}!©rsteps©}\index{transpiler option!--print@{©-print©}!©rsteps©} \, print resolver steps
    504 \item
    505 \Indexc{ast}\index{transpiler option!-P@{©-P©}!©ast©}\index{transpiler option!--print@{©-print©}!©ast©} \, print AST after parsing
    506 \item
    507 \Indexc{excpdecl}\index{transpiler option!-P@{©-P©}!©excpdecl©}\index{transpiler option!--print@{©-print©}!©excpdecl©} \, print AST after translating exception decls
    508 \item
    509 \Indexc{symevt}\index{transpiler option!-P@{©-P©}!©symevt©}\index{transpiler option!--print@{©-print©}!©symevt©} \, print AST after symbol table events
    510 \item
    511 \Indexc{expralt}\index{transpiler option!-P@{©-P©}!©expralt©}\index{transpiler option!--print@{©-print©}!©expralt©} \, print AST after expressions alternatives
    512 \item
    513 \Indexc{valdecl}\index{transpiler option!-P@{©-P©}!©valdecl©}\index{transpiler option!--print@{©-print©}!©valdecl©} \, print AST after declaration validation pass
    514 \item
    515 \Indexc{bresolver}\index{transpiler option!-P@{©-P©}!©bresolver©}\index{transpiler option!--print@{©-print©}!©bresolver©} \, print AST before resolver step
    516 \item
    517 \Indexc{expranly}\index{transpiler option!-P@{©-P©}!©expranly©}\index{transpiler option!--print@{©-print©}!©expranly©} \, print AST after expression analysis
    518 \item
    519 \Indexc{ctordtor}\index{transpiler option!-P@{©-P©}!©ctordtor©}\index{transpiler option!--print@{©-print©}!©ctordtor©} \, print AST after ctor/dtor are replaced
    520 \item
    521 \Indexc{tuple}\index{transpiler option!-P@{©-P©}!©tuple©}\index{transpiler option!--print@{©-print©}!©tuple©} \, print AST after tuple expansion
    522 \item
    523 \Indexc{instgen}\index{transpiler option!-P@{©-P©}!©instgen©}\index{transpiler option!--print@{©-print©}!©instgen©} \, print AST after instantiate generics
    524 \item
    525 \Indexc{bbox}\index{transpiler option!-P@{©-P©}!©bbox©}\index{transpiler option!--print@{©-print©}!©bbox©} \, print AST before box pass
    526 \item
    527 \Indexc{bcodegen}\index{transpiler option!-P@{©-P©}!©bcodegen©}\index{transpiler option!--print@{©-print©}!©bcodegen©} \, print AST before code generation
     485\Indexc{ascodegen}\index{translator option!-P@{©-P©}!©ascodegen©}\index{translator option!--print@{©-print©}!©ascodegen©} \, print AST as codegen rather than AST
     486\item
     487\Indexc{asterr}\index{translator option!-P@{©-P©}!©asterr©}\index{translator option!--print@{©-print©}!©asterr©} \, print AST on error
     488\item
     489\Indexc{declstats}\index{translator option!-P@{©-P©}!©declstats©}\index{translator option!--print@{©-print©}!©declstats©} \, print code property statistics
     490\item
     491\Indexc{parse}\index{translator option!-P@{©-P©}!©parse©}\index{translator option!--print@{©-print©}!©parse©} \, print yacc (parsing) debug information
     492\item
     493\Indexc{pretty}\index{translator option!-P@{©-P©}!©pretty©}\index{translator option!--print@{©-print©}!©pretty©} \, prettyprint for ©ascodegen© flag
     494\item
     495\Indexc{rproto}\index{translator option!-P@{©-P©}!©rproto©}\index{translator option!--print@{©-print©}!©rproto©} \, resolver-proto instance
     496\item
     497\Indexc{rsteps}\index{translator option!-P@{©-P©}!©rsteps©}\index{translator option!--print@{©-print©}!©rsteps©} \, print resolver steps
     498\item
     499\Indexc{ast}\index{translator option!-P@{©-P©}!©ast©}\index{translator option!--print@{©-print©}!©ast©} \, print AST after parsing
     500\item
     501\Indexc{excpdecl}\index{translator option!-P@{©-P©}!©excpdecl©}\index{translator option!--print@{©-print©}!©excpdecl©} \, print AST after translating exception decls
     502\item
     503\Indexc{symevt}\index{translator option!-P@{©-P©}!©symevt©}\index{translator option!--print@{©-print©}!©symevt©} \, print AST after symbol table events
     504\item
     505\Indexc{expralt}\index{translator option!-P@{©-P©}!©expralt©}\index{translator option!--print@{©-print©}!©expralt©} \, print AST after expressions alternatives
     506\item
     507\Indexc{valdecl}\index{translator option!-P@{©-P©}!©valdecl©}\index{translator option!--print@{©-print©}!©valdecl©} \, print AST after declaration validation pass
     508\item
     509\Indexc{bresolver}\index{translator option!-P@{©-P©}!©bresolver©}\index{translator option!--print@{©-print©}!©bresolver©} \, print AST before resolver step
     510\item
     511\Indexc{expranly}\index{translator option!-P@{©-P©}!©expranly©}\index{translator option!--print@{©-print©}!©expranly©} \, print AST after expression analysis
     512\item
     513\Indexc{ctordtor}\index{translator option!-P@{©-P©}!©ctordtor©}\index{translator option!--print@{©-print©}!©ctordtor©} \, print AST after ctor/dtor are replaced
     514\item
     515\Indexc{tuple}\index{translator option!-P@{©-P©}!©tuple©}\index{translator option!--print@{©-print©}!©tuple©} \, print AST after tuple expansion
     516\item
     517\Indexc{instgen}\index{translator option!-P@{©-P©}!©instgen©}\index{translator option!--print@{©-print©}!©instgen©} \, print AST after instantiate generics
     518\item
     519\Indexc{bbox}\index{translator option!-P@{©-P©}!©bbox©}\index{translator option!--print@{©-print©}!©bbox©} \, print AST before box pass
     520\item
     521\Indexc{bcodegen}\index{translator option!-P@{©-P©}!©bcodegen©}\index{translator option!--print@{©-print©}!©bcodegen©} \, print AST before code generation
    528522\end{description}
    529523\item
    530524\Indexc{--prelude-dir} <directory> \, prelude directory for debug/nodebug
    531525\item
    532 \Indexc{-S}\index{transpiler option!-S@{©-S©}!©counters,heap,time,all,none©}, \Indexc{--statistics}\index{transpiler option!--statistics@{©--statistics©}!©counters,heap,time,all,none©} <option-list> \, enable profiling information: ©counters©, ©heap©, ©time©, ©all©, ©none©
    533 \item
    534 \Indexc{-t}\index{transpiler option!-t@{©-t©}}, \Indexc{--tree}\index{transpiler option!--tree@{©--tree©}} build in tree
     526\Indexc{-S}\index{translator option!-S@{©-S©}!©counters,heap,time,all,none©}, \Indexc{--statistics}\index{translator option!--statistics@{©--statistics©}!©counters,heap,time,all,none©} <option-list> \, enable profiling information: ©counters©, ©heap©, ©time©, ©all©, ©none©
     527\item
     528\Indexc{-t}\index{translator option!-t@{©-t©}}, \Indexc{--tree}\index{translator option!--tree@{©--tree©}} build in tree
    535529\end{description}
    536530
     
    546540\end{cfa}
    547541Existing C programs with keyword clashes can be converted by prefixing the keyword identifiers with double backquotes, and eventually the identifier name can be changed to a non-keyword name.
    548 \VRef[Figure]{f:HeaderFileInterposition} shows how clashes in existing C header-files \see{\VRef{s:StandardHeaders}} can be handled using preprocessor \newterm{interposition}: ©#include_next© and command-line ©-I filename©.
    549 Several common C header-files with keyword clashes are fixed in the standard \CFA header-library, so there is largely a seamless programming-experience.
     542\VRef[Figure]{f:HeaderFileInterposition} shows how clashes in existing C header-files \see{\VRef{s:StandardHeaders}} can be handled using preprocessor \newterm{interposition}: ©#include_next© and ©-I filename©.
     543Several common C header-files with keyword clashes are fixed in the standard \CFA header-library, so there is a seamless programming-experience.
    550544
    551545\begin{figure}
     
    597591the type suffixes ©U©, ©L©, \etc may start with an underscore ©1_U©, ©1_ll© or ©1.0E10_f©.
    598592\end{enumerate}
    599 It is significantly easier to read and enter long constants when they are broken up into smaller groupings (most cultures use comma and/or period among digits for the same purpose).
     593It is significantly easier to read and enter long constants when they are broken up into smaller groupings (many cultures use comma and/or period among digits for the same purpose).
    600594This extension is backwards compatible, matches with the use of underscore in variable names, and appears in \Index*{Ada} and \Index*{Java} 8.
    601595\CC uses the single quote (©'©) as a separator, restricted within a sequence of digits, \eg ©0xaa©©'©©ff©, ©3.141©©'©©592E1©©'©©1©.
    602 However, the drawback of the \CC approach is difficults parsing for IDEs between character and numeric constants, as quotes are no longer balanced (©'x'© and ©3.14©©'©©159©).
    603596
    604597
    605598\section{Exponentiation Operator}
    606599
    607 C, \CC, and Java (and other programming languages) have \emph{no} exponentiation operator\index{exponentiation!operator}\index{operator!exponentiation}, \ie $x^y$, and instead use a routine, like \Indexc{pow(x,y)}, to perform the exponentiation operation.
     600C, \CC, and Java (and other programming languages) have no exponentiation operator\index{exponentiation!operator}\index{operator!exponentiation}, \ie $x^y$, and instead use a routine, like \Indexc{pow(x,y)}, to perform the exponentiation operation.
    608601\CFA extends the basic operators with the exponentiation operator ©?©\R{©\\©}©?©\index{?\\?@©?@\@?©} and ©?©\R{©\\©}©=?©\index{?\\=?@©@\@=?©}, as in, ©x ©\R{©\\©}© y© and ©x ©\R{©\\©}©= y©, which means $x^y$ and $x \leftarrow x^y$.
    609602The priority of the exponentiation operator is between the cast and multiplicative operators, so that ©w * (int)x \ (int)y * z© is parenthesized as ©(w * (((int)x) \ ((int)y))) * z©.
    610603
    611604There are exponentiation operators for integral and floating types, including the builtin \Index{complex} types.
    612 Integral exponentiation\index{exponentiation!unsigned integral} is performed with repeated multiplication ($O(\log y)$ or shifting if the exponent is 2).
     605Integral exponentiation\index{exponentiation!unsigned integral} is performed with repeated multiplication ($O(\log y)$ multiplies or shifting if the exponent is 2).
    613606Overflow for a large exponent or negative exponent returns zero.
    614607Floating exponentiation\index{exponentiation!floating} is performed using \Index{logarithm}s\index{exponentiation!logarithm}, so the exponent cannot be negative.
     
    627620T ?®\®?( T ep, unsigned long int y );
    628621\end{cfa}
    629 A user type ©T© must define one (©1©), and multiplication (©*©) \see{\VRef{s:Operator}}.
     622A user type ©T© must define multiplication, one (©1©), and ©*©.
    630623
    631624
     
    638631\subsection{\texorpdfstring{\LstKeywordStyle{if} / \LstKeywordStyle{while} Statement}{if / while Statement}}
    639632
    640 The \Indexc{if} and \Indexc{while} expressions are extended with declarations, similar to the \Indexc{for} declaration expression.\footnote{
     633The \Indexc{if}/\Indexc{while} expression allows declarations, similar to \Indexc{for} declaration expression.\footnote{
    641634Declarations in the \Indexc{do}-©while© condition are not useful because they appear after the loop body.}
    642635\begin{cfa}
     
    660653\label{s:caseClause}
    661654
    662 C restricts the \Indexc{case} clause in a \Indexc{switch} statement to a single value.
     655C restricts a \Indexc{case} clause in \Indexc{switch} statement to a single value.
    663656For multiple ©case© clauses prefixing a statement within the ©switch© statement, it is necessary to have multiple ©case© clauses rather than multiple values.
    664657Requiring a ©case© clause for each value is not in the spirit of brevity normally associated with C.
     
    695688\end{tabular}
    696689\end{cquote}
    697 In addition, subranges are allowed to specify a contiguous set of case values.
     690In addition, subranges are allowed to specify case values.
    698691\begin{cquote}
    699692\begin{tabular}{@{}l@{\hspace{3em}}l@{\hspace{2em}}l@{\hspace{2em}}l@{}}
     
    808801\end{cfa}
    809802This situation is better handled by a list of case values \see{\VRef{s:caseClause}}.
    810 
    811 While fall-through itself is not a problem, the problem occurs when fall-through is the default, as this semantics is unintuitive for many programmers and is different from most programming languages with a ©switch© statement.
    812 Hence, default fall-through semantics results in programming errors as programmers often \emph{forget} the ©break© statement at the end of a ©case© clause, resulting in inadvertent fall-through.
     803While fall-through itself is not a problem, the problem occurs when fall-through is the default, as this semantics is unintuitive to many programmers and is different from most programming languages with a ©switch© statement.
     804Hence, default fall-through semantics results in a many programming errors as programmers often \emph{forget} the ©break© statement at the end of a ©case© clause, resulting in inadvertent fall-through.
    813805
    814806\item
     
    827819The technical problem results from the inability to ensure declaration and initialization of variables when blocks are not entered at the beginning.
    828820There are few arguments for this kind of control flow, and therefore, there is a strong impetus to eliminate it.
     821
    829822This C idiom is known as ``\Index*{Duff's device}''~\cite{Duff83}, from this example:
    830823\begin{cfa}
     
    1003996with the current \Indexc{switch}/\Indexc{choose} statement.
    1004997
    1005 
    1006 \subsection{Loop Control}
    1007 
    1008 Looping a fixed number of times, possibly with a loop index, occurs frequently.
    1009 \CFA condenses simply looping to facilitate coding speed and safety~\see{examples in \VRef[Figure]{f:LoopControlExamples}}.
    1010 
    1011 The \Indexc{for}, \Indexc{while}, and \Indexc{do} loop-control are extended to allow an empty conditional, which implies a comparison value of ©1© (true).
    1012 \begin{cfa}
    1013 while ( ®/* empty */®  )                                §\C{// while ( true )}§
    1014 for ( ®/* empty */®  )                                  §\C{// for ( ; true; )}§
    1015 do ... while ( ®/* empty */®  )                 §\C{// do ... while ( true )}§
    1016 \end{cfa}
    1017 
    1018998\begin{figure}
    1019999\begin{tabular}{@{}l@{\hspace{50pt}}|l@{}}
     
    10231003while () { sout | "empty"; break; }
    10241004do { sout | "empty"; break; } while ();
    1025 for () { sout | "empty"; break; }                                                       §\C{sout | nl | nlOff;}§
     1005for () { sout | "empty"; break; }                                                       §\C[3in]{sout | nl | nlOff;}§
    10261006
    10271007for ( 0 ) { sout | "A"; } sout | "zero";                                        §\C{sout | nl;}§
     
    10651045for ( i; 5 : j; @ -~ -5 ~ 2 : k; 1.5 ~ @ ) { sout | i | j | k; } §\C{sout | nl;}§
    10661046for ( i; 5 : j; @ -~ -5 ~ 2 : k; 1.5 ~ @ ) { sout | i | j | k; } §\C{sout | nl;}§
    1067 for ( i; 5 : k; 1.5 ~ @ : j; @ -~ -5 ~ 2 ) { sout | i | j | k; } §\C{sout | nl;}§
     1047for ( i; 5 : k; 1.5 ~ @ : j; @ -~ -5 ~ 2 ) { sout | i | j | k; } §\C{sout | nl;}\CRT§
    10681048\end{cfa}
    10691049&
     
    11281108% for ( T i ; 3~9 ) => for ( T i = 3 ; i < 9; i += 1 ) // using 1
    11291109
    1130 The ©for© control is extended with 4 new loop-control operators, which are not overloadable:
     1110
     1111\subsection{Loop Control}
     1112
     1113Looping a fixed number of times, possibly with a loop index, occurs frequently.
     1114\CFA condenses simply looping to facilitate coding speed and safety~\see{examples in \VRef[Figure]{f:LoopControlExamples}}.
     1115
     1116The \Indexc{for}, \Indexc{while}, and \Indexc{do} loop-control allow an empty conditional, which implies a comparison value of ©1© (true).
     1117\begin{cfa}
     1118while ( ®/* empty */®  )                                §\C{// while ( true )}§
     1119for ( ®/* empty */®  )                                  §\C{// for ( ; true; )}§
     1120do ... while ( ®/* empty */®  )                 §\C{// do ... while ( true )}§
     1121\end{cfa}
     1122
     1123The ©for© control has 4 new loop-control operators that are not overloadable:
    11311124\begin{description}[itemsep=0pt,parsep=0pt]
    11321125\item
     
    12021195Similarly, the high value cannot be elided is an anonymous loop index (©1 ~ @©), as there is no index to stop the loop.
    12031196\item
    1204 ©:© means add another index.
     1197©:© means low another index.
    12051198\begin{cfa}
    12061199for ( i; 5 ®:® j; 2 ~ 12 ~ 3 )                  §\C{// for ( typeof(i) i = 1, j = 2; i < 5 \&\& j < 12; i += 1, j += 3 )}§
     
    12191212\subsection{\texorpdfstring{Labelled \LstKeywordStyle{continue} / \LstKeywordStyle{break} Statement}{Labelled continue / break Statement}}
    12201213
    1221 C \Indexc{continue} and \Indexc{break} statements are restricted to one level of nesting for a particular control structure.
     1214C \Indexc{continue} and \Indexc{break} statements, for altering control flow, are restricted to one level of nesting for a particular control structure.
    12221215This restriction forces programmers to use \Indexc{goto} to achieve the equivalent control-flow for more than one level of nesting.
    12231216To prevent having to switch to the ©goto©, \CFA extends the \Indexc{continue}\index{continue@©continue©!labelled}\index{labelled!continue@©continue©} and \Indexc{break}\index{break@©break©!labelled}\index{labelled!break@©break©} with a target label to support static multi-level exit\index{multi-level exit}\index{static multi-level exit}~\cite{Buhr85}, as in Java.
     
    13091302Furthermore, the location of the label at the \emph{beginning} of the target control structure informs the reader (\Index{eye candy}) that complex control-flow is occurring in the body of the control structure.
    13101303With ©goto©, the label is at the end of the control structure, which fails to convey this important clue early enough to the reader.
    1311 Finally, using an explicit target for the transfer, instead of an implicit target, allows new constructs to be added or removed without affecting existing constructs.
     1304Finally, using an explicit target for the transfer instead of an implicit target allows new constructs to be added or removed without affecting existing constructs.
    13121305Otherwise, the implicit targets of the current ©continue© and ©break©, \ie the closest enclosing loop or ©switch©, change as certain constructs are added or removed.
    13131306
     
    13801373\begin{cfa}
    13811374Person p
    1382 ®p.®name ...;  ®p.®address ...;  ®p.®sex ...; §\C{// access containing fields}§
     1375®p.®name; ®p.®address; ®p.®sex;                 §\C{// access containing fields}§
    13831376\end{cfa}
    13841377which extends to multiple levels of qualification for nested aggregates and multiple aggregates.
    13851378\begin{cfa}
    13861379struct Ticket { ... } t;
    1387 ®p.name®.first ...;  ®p.address®.street ...; §\C{// access nested fields}§
    1388 ®t.®departure ...;  ®t.®cost ...;               §\C{// access multiple aggregate}§
     1380®p.name®.first; ®p.address®.street;             §\C{// access nested fields}§
     1381®t.®departure; ®t.®cost;                                §\C{// access multiple aggregate}§
    13891382\end{cfa}
    13901383Repeated aggregate qualification is tedious and makes code difficult to read.
     
    15651558
    15661559
    1567 \subsection{Non-local Exception}
     1560\section{Non-local Exception}
    15681561
    15691562\begin{cfa}
     
    23542347\subsection{Implicit String Conversions}
    23552348
    2356 The types ©char©, ©char *©, ©int©, ©double©, ©_Complex©, including different signness and sizes, implicitly convert to type ©string©.
    2357 \VRef[Figure]{f:ImplicitConversionsString} shows examples of implicit conversions between C strings, integral, floating-point and complex types to ©string©.
    2358 A conversions can be explicitly specified:
    2359 \begin{cfa}
    2360 s = string( "abc" );                            §\C{// converts char * to string}§
    2361 s = string( 5 );                                        §\C{// converts int to string}§
    2362 s = string( 5.5 );                                      §\C{// converts double to string}§
    2363 \end{cfa}
    2364 All conversions from ©string© to ©char *©, attempt to be safe:
    2365 either by requiring the maximum length of the ©char *© storage (©strncpy©) or allocating the ©char *© storage for the string characters (ownership), meaning the programmer must free the storage.
    2366 As well, a string is always null terminates, implying a minimum size of 1 character.
    2367 \begin{cquote}
    2368 \begin{tabular}{@{}l@{\hspace{1.75in}}|@{\hspace{15pt}}l@{}}
    2369 \begin{cfa}
    2370 string s = "abcde";
    2371 char cs[3];
    2372 strncpy( cs, s, sizeof(cs) );           §\C{sout | cs;}§
    2373 char * cp = s;                                          §\C{sout | cp;}§
    2374 delete( cp );
    2375 cp = s + ' ' + s;                                       §\C{sout | cp;}§
    2376 delete( cp );
    2377 \end{cfa}
    2378 &
    2379 \begin{cfa}
    2380 
    2381 
    2382 ab
    2383 abcde
    2384 
    2385 abcde abcde
    2386 
    2387 \end{cfa}
    2388 \end{tabular}
    2389 \end{cquote}
     2349The types ©char©, ©char *©, ©int©, ©double©, ©_Complex©, including signness and different sizes, implicitly convert to type ©string©.
     2350\VRef[Figure]{f:ImplicitStringConversions} shows examples of implicit conversion between C strings, integral, floating-point and complex types to ©string©
     2351The implicit conversions can be specified explicitly, as in:
     2352\begin{cfa}
     2353s = string( "abc" );            // converts char * to string
     2354s = string( 5 );                        // converts int to string
     2355s = string( 5.5 );                      // converts double to string
     2356\end{cfa}
     2357Conversions from ©string© to ©char *© are supported but with restrictions.
     2358Explicit As well, when a string is converted to a ©char *©, the storage for the ©char *© is created by the conversion operation, which must be subsequently deleted:
     2359\begin{cfa}
     2360string x = "abc";
     2361char *p = x;                            // convert from string to char *
     2362...
     2363delete p;                                       // free storage created for p
     2364\end{cfa}
    23902365
    23912366\begin{figure}
     
    23952370        string s;
    23962371        // conversion of char and char * to string
    2397         s = 'x';                                                §\C{sout | s;}§
    2398         s = "abc";                                              §\C{sout | s;}§
     2372        s = 'x';                                                §\CD{sout | s;}§
     2373        s = "abc";                                              §\CD{sout | s;}§
    23992374        char cs[5] = "abc";
    2400         s = cs;                                                 §\C{sout | s;}§
     2375        s = cs;                                                 §\CD{sout | s;}§
    24012376        // conversion of integral, floating-point, and complex to string
    2402         s = 45hh;                                               §\C{sout | s;}§
    2403         s = 45h;                                                §\C{sout | s;}§
    2404         s = -(ssize_t)MAX - 1;                  §\C{sout | s;}§
    2405         s = (size_t)MAX;                                §\C{sout | s;}§
    2406         s = 5.5;                                                §\C{sout | s;}§
    2407         s = 5.5L;                                               §\C{sout | s;}§
    2408         s = 5.5+3.4i;                                   §\C{sout | s;}§
    2409         s = 5.5L+3.4Li;                                 §\C{sout | s;}§
     2377        s = 45hh;                                               §\CD{sout | s;}§
     2378        s = 45h;                                                §\CD{sout | s;}§
     2379        s = -(ssize_t)MAX - 1;                  §\CD{sout | s;}§
     2380        s = (size_t)MAX;                                §\CD{sout | s;}§
     2381        s = 5.5;                                                §\CD{sout | s;}§
     2382        s = 5.5L;                                               §\CD{sout | s;}§
     2383        s = 5.5+3.4i;                                   §\CD{sout | s;}§
     2384        s = 5.5L+3.4Li;                                 §\CD{sout | s;}§
     2385        // safe conversion from string to char *
     2386        strncpy( cs, s, sizeof(cs) );   §\CD{sout | cs;}§
     2387        char * cp = s;                                  §\CD{sout | cp; // ownership}§
     2388        delete( cp );
     2389        cp = s + ' ' + s;                               §\CD{sout | cp; // ownership}§
     2390        delete( cp );
    24102391\end{cfa}
    24112392&
     
    242724085.5+3.4i
    242824095.5+3.4i
     2410
     24115.5+
     24125.5+3.4i
     2413
     24145.5+3.4i 5.5+3.4i
     2415
    24292416\end{cfa}
    24302417\end{tabular}
    2431 \caption{Implicit Conversions to String}
    2432 \label{f:ImplicitConversionsString}
     2418\caption{Implicit String Conversions}
     2419\label{f:ImplicitStringConversions}
    24332420\end{figure}
    24342421
    24352422
    2436 \subsection{Size (length)}
    2437 
    2438 The ©size© operation returns the length of a string.
    2439 \begin{cfa}
    2440 i = size( "" );                                         §\C{// i is assigned 0}§
    2441 i = size( "abc" );                                      §\C{// i is assigned 3}§
    2442 i = size( peter );                                      §\C{// i is assigned 5}§
    2443 \end{cfa}
    2444 
    2445 
    24462423\subsection{Comparison Operators}
    24472424
    2448 The binary \Index{relational operator}s, ©<©, ©<=©, ©>©, ©>=©, and \Index{equality operator}s, ©==©, ©!=©, compare strings using lexicographical ordering, where longer strings are greater than shorter strings.
     2425The binary relational and equality operators ©<©, ©<=©, ©>©, ©>=©, ©==©, ©!=© compare ©string© using lexicographical ordering, where longer strings are greater than shorter strings.
    24492426
    24502427
    24512428\subsection{Concatenation}
    24522429
    2453 The binary operators \Indexc{+} and \Indexc{+=} concatenate two strings, creating the sum of the strings.
    2454 \begin{cfa}
    2455 s = peter + ' ' + digit;                        §\C{// s is assigned "PETER 0123456789"}§
    2456 s += peter;                                                     §\C{// s is assigned "PETER 0123456789PETER"}§
    2457 \end{cfa}
     2430The binary operator ©+© concatenates two strings.
     2431\begin{cfa}
     2432s = peter + digit;                                      §\C{// s is assigned "PETER0123456789"}§
     2433s += peter;                                                     §\C{// s is assigned "PETER0123456789PETER"}§
     2434\end{cfa}
     2435There is also an assignment form ©+=©.
    24582436
    24592437
    24602438\subsection{Repetition}
    24612439
    2462 The binary operators \Indexc{*} and \Indexc{*=} repeat a string $N$ times.
    2463 If $N = 0$, a zero length string, ©""© is returned.
    2464 \begin{cfa}
    2465 s = 'x' * 3;                            §\C{// s is assigned "PETER PETER PETER "}§
    2466 s = (peter + ' ') * 3;                          §\C{// s is assigned "PETER PETER PETER "}§
     2440The binary operator \Indexc{*} returns a string that is the string repeated ©n© times.
     2441If ©n = 0©, a zero length string, ©""© is returned.
     2442\begin{cfa}
     2443s = (peter + ' ') * 3;                          §\C{// s is assigned "PETER PETER PETER"}§
     2444\end{cfa}
     2445There is also an assignment form ©*=©.
     2446
     2447
     2448\subsection{Length}
     2449
     2450The ©length© operation
     2451\begin{cfa}
     2452int length()
     2453\end{cfa}
     2454returns the length of a string variable.
     2455\begin{cfa}
     2456i = peter.length();                     §\C{// i is assigned the value 5}§
    24672457\end{cfa}
    24682458
    24692459
    24702460\subsection{Substring}
    2471 The substring operation returns a subset of the string starting at a position in the string and traversing a length.
    2472 \begin{cfa}
    2473 s = peter( 2, 3 );                                      §\C{// s is assigned "ETE"}§
    2474 s = peter( 4, -3 );                                     §\C{// s is assigned "ETE", length is opposite direction}§
    2475 s = peter( 2, 8 );                                      §\C{// s is assigned "ETER", length is clipped to 4}§
    2476 s = peter( 0, -1 );                                     §\C{// s is assigned "", beyond string so clipped to null}§
    2477 s = peter(-1, -1 );                                     §\C{// s is assigned "R", start and length are negative}§
    2478 \end{cfa}
     2461The substring operation:
     2462\begin{cfa}
     2463string operator () (int start, int lnth);
     2464\end{cfa}
     2465performs a substring operation that returns the string starting at a specified position (©start©) in the current string, and having the specified length (©lnth©).
    24792466A negative starting position is a specification from the right end of the string.
    24802467A negative length means that characters are selected in the opposite (right to left) direction from the starting position.
    24812468If the substring request extends beyond the beginning or end of the string, it is clipped (shortened) to the bounds of the string.
    24822469If the substring request is completely outside of the original string, a null string located at the end of the original string is returned.
     2470\begin{cfa}
     2471s = peter( 2, 3 );                      §\C{// s is assigned "ETE"}§
     2472s = peter( 4, -3 );                     §\C{// s is assigned "ETE", length is opposite direction}§
     2473s = peter( 2, 8 );                      §\C{// s is assigned "ETER", length is clipped to 4}§
     2474s = peter( 0, -1 );                     §\C{// s is assigned "", beyond string so clipped to null}§
     2475s = peter(-1, -1 );                     §\C{// s is assigned "R", start and length are negative}§
     2476\end{cfa}
    24832477The substring operation can also appear on the left hand side of the assignment operator.
    24842478The substring is replaced by the value on the right hand side of the assignment.
    24852479The length of the right-hand-side value may be shorter, the same length, or longer than the length of the substring that is selected on the left hand side of the assignment.
    2486 \begin{cfa}
    2487 digit( 3, 3 ) = "";                             §\C{// digit is assigned "0156789"}§
    2488 digit( 4, 3 ) = "xyz";                          §\C{// digit is assigned "015xyz9"}§
    2489 digit( 7, 0 ) = "***";                          §\C{// digit is assigned "015xyz***9"}§
    2490 digit(-4, 3 ) = "$$$";                          §\C{// digit is assigned "015xyz\$\$\$9"}§
     2480\begin{cfa}[mathescape=false]
     2481digit( 3, 3 ) = "";             §\C{// digit is assigned "0156789"}§
     2482digit( 4, 3 ) = "xyz";          §\C{// digit is assigned "015xyz9"}§
     2483digit( 7, 0 ) = "***";          §\C{// digit is assigned "015xyz***9"}§
     2484digit(-4, 3 ) = "$$$";          §\C{// digit is assigned "015xyz\$\$\$9"}§
    24912485\end{cfa}
    24922486A substring is treated as a pointer into the base (substringed) string rather than creating a copy of the subtext.
     
    25072501// e is a substring result passed by value
    25082502void test(string &x, string &a, string &b, string &c, string &d, string e) {
    2509                                                                         §\C{//   x                                a               b               c               d               e}§
    2510         a( 1, 2 ) = "aaa";                              §\C{// aaaxxxxxxxxxxx   aaax    axx             xxxxx   xxxxx   xxxxx}§
    2511         b( 2, 12 ) = "bbb";                             §\C{// aaabbbxxxxxxxxx  aaab    abbb    bbxxx   xxxxx   xxxxx}§
    2512         c( 4, 5 ) = "ccc";                              §\C{// aaabbbxcccxxxxxx aaab    abbb    bbxccc  ccxxx   xxxxx}§
    2513         c = "yyy";                                              §\C{// aaabyyyxxxxxx    aaab    abyy    yyy             xxxxx   xxxxx}§
    2514         d( 1, 3 ) = "ddd";                              §\C{// aaabyyyxdddxx    aaab    abyy    yyy             dddxx   xxxxx}§
    2515         e( 1, 3 ) = "eee";                              §\C{// aaabyyyxdddxx    aaab    abyy    yyy             dddxx   eeexx}§
    2516         x = e;                                                  §\C{// eeexx                    eeex    exx             x                               eeexx}§
     2503                                                        §\C{//   x                                a               b               c               d               e}§
     2504        a( 1, 2 ) = "aaa";              §\C{// aaaxxxxxxxxxxx   aaax    axx             xxxxx   xxxxx   xxxxx}§
     2505        b( 2, 12 ) = "bbb";             §\C{// aaabbbxxxxxxxxx  aaab    abbb    bbxxx   xxxxx   xxxxx}§
     2506        c( 4, 5 ) = "ccc";              §\C{// aaabbbxcccxxxxxx aaab    abbb    bbxccc  ccxxx   xxxxx}§
     2507        c = "yyy";                              §\C{// aaabyyyxxxxxx    aaab    abyy    yyy             xxxxx   xxxxx}§
     2508        d( 1, 3 ) = "ddd";              §\C{// aaabyyyxdddxx    aaab    abyy    yyy             dddxx   xxxxx}§
     2509        e( 1, 3 ) = "eee";              §\C{// aaabyyyxdddxx    aaab    abyy    yyy             dddxx   eeexx}§
     2510        x = e;                                  §\C{// eeexx                    eeex    exx             x                               eeexx}§
    25172511}
    25182512\end{cfa}
     
    25242518For example:
    25252519\begin{cfa}
    2526 s = peter( 2 );                                         §\C{// s is assigned "ETER"}§
    2527 peter( 2 ) = "IPER";                            §\C{// peter is assigned "PIPER"}§
     2520s = peter( 2 );                         §\C{// s is assigned "ETER"}§
     2521peter( 2 ) = "IPER";            §\C{// peter is assigned "PIPER"}§
    25282522\end{cfa}
    25292523It is also possible to substring using a string as the index for selecting the substring portion of the string.
     
    25332527For example:
    25342528\begin{cfa}[mathescape=false]
    2535 digit( "xyz$$$" ) = "678";                      §\C{// digit is assigned "0156789"}§
    2536 digit( "234") = "***";                          §\C{// digit is assigned "0156789***"}§
    2537 \end{cfa}
     2529digit( "xyz$$$" ) = "678";      §\C{// digit is assigned "0156789"}§
     2530digit( "234") = "***";          §\C{// digit is assigned "0156789***"}§
     2531\end{cfa}
     2532%$
    25382533
    25392534
     
    25752570A negative starting position is a specification from the right end of the string.
    25762571\begin{cfa}
    2577 i = peter.include( digitmask );         §\C{// i is assigned 1}§
    2578 i = peter.include( alphamask );         §\C{// i is assigned 6}§
     2572i = peter.include( digitmask ); §\C{// i is assigned 1}§
     2573i = peter.include( alphamask ); §\C{// i is assigned 6}§
    25792574\end{cfa}
    25802575
     
    26272622\begin{cfa}
    26282623// remove leading blanks
    2629 s = string( "   ABC" ).trim( " " );     §\C{// s is assigned "ABC",}§
     2624s = string( "   ABC" ).trim( " " );                     §\C{// s is assigned "ABC",}§
    26302625// remove trailing blanks
    2631 s = string( "ABC   " ).trim( " ", last ); §\C{// s is assigned "ABC",}§
     2626s = string( "ABC   " ).trim( " ", last );       §\C{// s is assigned "ABC",}§
    26322627\end{cfa}
    26332628
     
    26562651returns a string in which all occurrences of the ©from© string in the current string have been replaced by the ©to© string.
    26572652\begin{cfa}
    2658 s = peter.replace( "E", "XX" );         §\C{// s is assigned "PXXTXXR"}§
     2653s = peter.replace( "E", "XX" ); §\C{// s is assigned "PXXTXXR"}§
    26592654\end{cfa}
    26602655The replacement is done left-to-right.
    26612656When an instance of the ©from© string is found and changed to the ©to© string, it is NOT examined again for further replacement.
    26622657
    2663 \subsection{Returning N+1 on Failure}
     2658
     2659\section{Returning N+1 on Failure}
    26642660
    26652661Any of the string search routines can fail at some point during the search.
     
    26912687
    26922688
    2693 \subsection{C Compatibility}
    2694 
    2695 To ease conversion from C to \CFA, there are companion ©string© routines for C strings.
    2696 \VRef[Table]{t:CompanionStringRoutines} shows the C routines on the left that also work with ©string© and the rough equivalent ©string© opeation of the right.
    2697 Hence, it is possible to directly convert a block of C string operations into @string@ just by changing the
    2698 
    2699 \begin{table}
    2700 \begin{cquote}
    2701 \begin{tabular}{@{}l|l@{}}
    2702 \multicolumn{1}{c|}{©char []©}  & \multicolumn{1}{c}{©string©}  \\
    2703 \hline
    2704 ©strcpy©, ©strncpy©             & ©=©                                                                   \\
    2705 ©strcat©, ©strncat©             & ©+©                                                                   \\
    2706 ©strcmp©, ©strncmp©             & ©==©, ©!=©, ©<©, ©<=©, ©>©, ©>=©              \\
    2707 ©strlen©                                & ©size©                                                                \\
    2708 ©[]©                                    & ©[]©                                                                  \\
    2709 ©strstr©                                & ©find©                                                                \\
    2710 ©strcspn©                               & ©find_first_of©, ©find_last_of©               \\
    2711 ©strspc©                                & ©find_fist_not_of©, ©find_last_not_of©
    2712 \end{tabular}
    2713 \end{cquote}
    2714 \caption{Companion Routines for \CFA \lstinline{string} to C Strings}
    2715 \label{t:CompanionStringRoutines}
    2716 \end{table}
    2717 
    2718 For example, this block of C code can be converted to \CFA by simply changing the type of variable ©s© from ©char []© to ©string©.
    2719 \begin{cfa}
    2720         char s[32];
    2721         //string s;
    2722         strcpy( s, "abc" );                             PRINT( %s, s );
    2723         strncpy( s, "abcdef", 3 );              PRINT( %s, s );
    2724         strcat( s, "xyz" );                             PRINT( %s, s );
    2725         strncat( s, "uvwxyz", 3 );              PRINT( %s, s );
    2726         PRINT( %zd, strlen( s ) );
    2727         PRINT( %c, s[3] );
    2728         PRINT( %s, strstr( s, "yzu" ) ) ;
    2729         PRINT( %s, strstr( s, 'y' ) ) ;
    2730 \end{cfa}
    2731 However, the conversion fails with I/O because ©printf© cannot print a ©string© using format code ©%s© because \CFA strings are not null terminated.
    2732 
    2733 
    27342689\subsection{Input/Output Operators}
    27352690
     
    27522707Hence, enums may be overloaded with variable, enum, and function names.
    27532708\begin{cfa}
    2754 int Foo;                                                        §\C{// type/variable separate namespaces}§
     2709int Foo;                        §\C{// type/variable separate namespaces}§
    27552710enum Foo { Bar };
    2756 enum Goo { Bar };                                       §\C{// overload Foo.Bar}§
    2757 double Bar;                                                     §\C{// overload Foo.Bar, Goo.Bar}§
     2711enum Goo { Bar };       §\C[1.75in]{// overload Foo.Bar}§
     2712double Bar;                     §\C{// overload Foo.Bar, Goo.Bar}\CRT§
    27582713\end{cfa}
    27592714An anonymous enumeration injects enums with specific values into a scope.
     
    28282783The following examples illustrate the difference between the enumeration type and the type of its enums.
    28292784\begin{cfa}
    2830 Math m = PI;                                            §\C{// allowed}§
    2831 double d = PI;                                          §\C{// allowed, conversion to base type}§
    2832 m = E;                                                          §\C{// allowed}§
    2833 m = Alph;                                                       §\C{// {\color{red}disallowed}}§
    2834 m = 3.141597;                                           §\C{// {\color{red}disallowed}}§
    2835 d = m;                                                          §\C{// allowed}§
    2836 d = Alph;                                                       §\C{// {\color{red}disallowed}}§
    2837 Letter l = A;                                           §\C{// allowed}§
    2838 Greek g = Alph;                                         §\C{// allowed}§
    2839 l = Alph;                                                       §\C{// allowed, conversion to base type}§
    2840 g = A;                                                          §\C{// {\color{red}disallowed}}§
     2785Math m = PI;    §\C[1.5in]{// allowed}§
     2786double d = PI;  §\C{// allowed, conversion to base type}§
     2787m = E;                  §\C{// allowed}§
     2788m = Alph;               §\C{// {\color{red}disallowed}}§
     2789m = 3.141597;   §\C{// {\color{red}disallowed}}§
     2790d = m;                  §\C{// allowed}§
     2791d = Alph;               §\C{// {\color{red}disallowed}}§
     2792Letter l = A;   §\C{// allowed}§
     2793Greek g = Alph; §\C{// allowed}§
     2794l = Alph;               §\C{// allowed, conversion to base type}§
     2795g = A;                  §\C{// {\color{red}disallowed}}\CRT§
    28412796\end{cfa}
    28422797
     
    34073362For example, consider C's \Indexc{div} function, which returns the quotient and remainder for a division of an integer value.
    34083363\begin{cfa}
    3409 typedef struct { int quot, rem; } div_t; §\C{// from include stdlib.h}§
     3364typedef struct { int quot, rem; } div_t;        §\C[7cm]{// from include stdlib.h}§
    34103365div_t div( int num, int den );
    3411 div_t qr = div( 13, 5 );                                §\C{// return quotient/remainder aggregate}§
     3366div_t qr = div( 13, 5 ); §\C{// return quotient/remainder aggregate}§
    34123367printf( "%d %d\n", qr.quot, qr.rem ); §\C{// print quotient/remainder}§
    34133368\end{cfa}
     
    34203375For example, consider C's \Indexc{modf} function, which returns the integral and fractional part of a floating value.
    34213376\begin{cfa}
    3422 double modf( double x, double * i );    §\C{// from include math.h}§
     3377double modf( double x, double * i ); §\C{// from include math.h}§
    34233378double intp, frac = modf( 13.5, &intp ); §\C{// return integral and fractional components}§
    34243379printf( "%g %g\n", intp, frac ); §\C{// print integral/fractional components}§
     
    34493404When a function call is passed as an argument to another call, the best match of actual arguments to formal parameters is evaluated given all possible expression interpretations in the current scope.
    34503405\begin{cfa}
    3451 void g( int, int );                                             §\C{// 1}§
    3452 void g( double, double );                               §\C{// 2}§
    3453 g( div( 13, 5 ) );                                              §\C{// select 1}§
    3454 g( modf( 13.5 ) );                                              §\C{// select 2}§
     3406void g( int, int ); §\C{// 1}§
     3407void g( double, double ); §\C{// 2}§
     3408g( div( 13, 5 ) ); §\C{// select 1}§
     3409g( modf( 13.5 ) ); §\C{// select 2}§
    34553410\end{cfa}
    34563411In this case, there are two overloaded ©g© routines.
     
    34613416The previous examples can be rewritten passing the multiple returned-values directly to the ©printf© function call.
    34623417\begin{cfa}
    3463 [ int, int ] div( int x, int y );               §\C{// from include stdlib}§
     3418[ int, int ] div( int x, int y ); §\C{// from include stdlib}§
    34643419printf( "%d %d\n", div( 13, 5 ) ); §\C{// print quotient/remainder}§
    34653420
    3466 [ double, double ] modf( double x );    §\C{// from include math}§
     3421[ double, double ] modf( double x ); §\C{// from include math}§
    34673422printf( "%g %g\n", modf( 13.5 ) ); §\C{// print integral/fractional components}§
    34683423\end{cfa}
     
    34753430\begin{cfa}
    34763431int quot, rem;
    3477 [ quot, rem ] = div( 13, 5 );                   §\C{// assign multiple variables}§
    3478 printf( "%d %d\n", quot, rem );                 §\C{// print quotient/remainder}§
     3432[ quot, rem ] = div( 13, 5 ); §\C{// assign multiple variables}§
     3433printf( "%d %d\n", quot, rem ); §\C{// print quotient/remainder}\CRT§
    34793434\end{cfa}
    34803435Here, the multiple return-values are matched in much the same way as passing multiple return-values to multiple parameters in a call.
     
    38933848The general syntax of a lexical list is:
    38943849\begin{cfa}
    3895 [ §\emph{exprlist}§ ]
    3896 \end{cfa}
    3897 where \LstBasicStyle{\emph{exprlist}} is a list of one or more expressions separated by commas.
     3850[ $\emph{exprlist}$ ]
     3851\end{cfa}
     3852where ©$\emph{exprlist}$© is a list of one or more expressions separated by commas.
    38983853The brackets, ©[]©, allow differentiating between lexical lists and expressions containing the C comma operator.
    38993854The following are examples of lexical lists:
     
    39013856[ x, y, z ]
    39023857[ 2 ]
    3903 [ v + w, x * y, 3.14159, f() ]
     3858[ v+w, x*y, 3.14159, f() ]
    39043859\end{cfa}
    39053860Tuples are permitted to contain sub-tuples (\ie nesting), such as ©[ [ 14, 21 ], 9 ]©, which is a 2-element tuple whose first element is itself a tuple.
     
    39133868The general syntax of a tuple type is:
    39143869\begin{cfa}
    3915 [ §\emph{typelist}§ ]
    3916 \end{cfa}
    3917 where \LstBasicStyle{\emph{typelist}} is a list of one or more legal \CFA or C type specifications separated by commas, which may include other tuple type specifications.
     3870[ $\emph{typelist}$ ]
     3871\end{cfa}
     3872where ©$\emph{typelist}$© is a list of one or more legal \CFA or C type specifications separated by commas, which may include other tuple type specifications.
    39183873Examples of tuple types include:
    39193874\begin{cfa}
    39203875[ unsigned int, char ]
    39213876[ double, double, double ]
    3922 [ * int, int * ]                                                §\C{// mix of CFA and ANSI}§
     3877[ * int, int * ] §\C{// mix of CFA and ANSI}§
    39233878[ * [ 5 ] int, * * char, * [ [ int, int ] ] (int, int) ]
    39243879\end{cfa}
     
    39273882Examples of declarations using tuple types are:
    39283883\begin{cfa}
    3929 [ int, int ] x;                                                 §\C{// 2 element tuple, each element of type int}§
    3930 * [ char, char ] y;                                             §\C{// pointer to a 2 element tuple}§
     3884[ int, int ] x; §\C{// 2 element tuple, each element of type int}§
     3885* [ char, char ] y; §\C{// pointer to a 2 element tuple}§
    39313886[ [ int, int ] ] z ([ int, int ]);
    39323887\end{cfa}
     
    39453900[ int, int ] w1;
    39463901[ int, int, int ] w2;
    3947 [ void ] f (int, int, int);                             §\C{// three input parameters of type int}§
    3948 [ void ] g ([ int, int, int ]);                 §\C{// 3 element tuple as input}§
     3902[ void ] f (int, int, int); §\C{// three input parameters of type int}§
     3903[ void ] g ([ int, int, int ]); §\C{3 element tuple as input}§
    39493904f( [ 1, 2, 3 ] );
    39503905f( w1, 3 );
     
    40273982[ int, int, int, int ] w = [ 1, 2, 3, 4 ];
    40283983int x = 5;
    4029 [ x, w ] = [ w, x ];                                    §\C{// all four tuple coercions}§
     3984[ x, w ] = [ w, x ]; §\C{// all four tuple coercions}§
    40303985\end{cfa}
    40313986Starting on the right-hand tuple in the last assignment statement, w is opened, producing a tuple of four values;
     
    40454000Mass assignment has the following form:
    40464001\begin{cfa}
    4047 [ §\emph{lvalue}§, ... , §\emph{lvalue}§ ] = §\emph{expr}§;
     4002[ $\emph{lvalue}$, ... , $\emph{lvalue}$ ] = $\emph{expr}$;
    40484003\end{cfa}
    40494004\index{lvalue}
    4050 The left-hand side is a tuple of \LstBasicStyle{\emph{lvalues}}, which is a list of expressions each yielding an address, \ie any data object that can appear on the left-hand side of a conventional assignment statement.
    4051 \LstBasicStyle{\emph{expr}} is any standard arithmetic expression.
     4005The left-hand side is a tuple of \emph{lvalues}, which is a list of expressions each yielding an address, \ie any data object that can appear on the left-hand side of a conventional assignment statement.
     4006©$\emph{expr}$© is any standard arithmetic expression.
    40524007Clearly, the types of the entities being assigned must be type compatible with the value of the expression.
    40534008
     
    40864041Multiple assignment has the following form:
    40874042\begin{cfa}
    4088 [ §\emph{lvalue}§, ... , §\emph{lvalue}§ ] = [ §\emph{expr}§, ... , §\emph{expr}§ ];
     4043[ $\emph{lvalue}$, ... , $\emph{lvalue}$ ] = [ $\emph{expr}$, ... , $\emph{expr}$ ];
    40894044\end{cfa}
    40904045\index{lvalue}
     
    41174072both these examples produce indeterminate results:
    41184073\begin{cfa}
    4119 f( x++, x++ );                                                  §\C{// C routine call with side effects in arguments}§
    4120 [ v1, v2 ] = [ x++, x++ ];                              §\C{// side effects in right-hand side of multiple assignment}§
     4074f( x++, x++ ); §\C{// C routine call with side effects in arguments}§
     4075[ v1, v2 ] = [ x++, x++ ]; §\C{// side effects in right-hand side of multiple assignment}§
    41214076\end{cfa}
    41224077
     
    41284083Cascade assignment has the following form:
    41294084\begin{cfa}
    4130 §\emph{tuple}§ = §\emph{tuple}§ = ... = §\emph{tuple}§;
     4085$\emph{tuple}$ = $\emph{tuple}$ = ... = $\emph{tuple}$;
    41314086\end{cfa}
    41324087and it has the same parallel semantics as for mass and multiple assignment.
     
    41484103The goal of \CFA stream input/output (I/O) is to simplify the common cases\index{I/O!common case}, while fully supporting polymorphism and user defined types in a consistent way.
    41494104Stream I/O can be implicitly or explicitly formatted.
    4150 Implicit formatting means \CFA selects an I/O format for values that matches a variable's type.
    4151 Explicit formatting means additional I/O information is specified to control how a value is interpreted.
    4152 
     4105Implicit formatting means \CFA selects the output or input format for values that matches the variable's type.
     4106Explicit formatting means additional information is specified to augment how an output or input of value is interpreted.
    41534107\CFA formatting incorporates ideas from C ©printf©, \CC ©stream© manipulators, and Python implicit spacing and newline.
    41544108Specifically:
     
    41584112\CFA/\CC format manipulators are named, making them easier to read and remember.
    41594113\item
    4160 ©printf©/Python separate format codes from associated variables, making it difficult to match codes with variables.
     4114©printf©/Python separates format codes from associated variables, making it difficult to match codes with variables.
    41614115\CFA/\CC co-locate codes with associated variables, where \CFA has the tighter binding.
    41624116\item
    4163 Format manipulators in ©printf©/Python/\CFA have local effect, whereas \CC have global effect, except ©setw©.
    4164 Hence, it is common \CC programming practice to toggle manipulators on and then back to the default to prevent downstream side-effects.
     4117Format manipulators in \CFA have local effect, whereas \CC have global effect, except ©setw©.
     4118Hence, it is common programming practice to toggle manipulators on and then back to the default to prevent downstream side-effects.
    41654119Without this programming style, errors occur when moving prints, as manipulator effects incorrectly flow into the new location.
    41664120Furthermore, to guarantee no side-effects, manipulator values must be saved and restored across function calls.
    4167 \CC programers never do any of this.
    41684121\item
    41694122\CFA has more sophisticated implicit value spacing than Python, plus implicit newline at the end of a print.
    41704123\end{itemize}
    41714124
     4125The standard polymorphic I/Os stream are ©stdin©/©sin© (input), ©stdout©/©sout© and ©stderr©/©serr© (output) (like C++ ©cin©/©cout©/©cerr©).
     4126Polymorphic streams ©exit© and ©abort© provide implicit program termination without and with generating a stack trace and core file.
     4127Stream ©exit© implicitly returns ©EXIT_FAILURE© to the shell.
     4128\begin{cfa}
     4129®exit®   | "x (" | x | ") negative value.";   // terminate and return EXIT_FAILURE to shell
     4130®abort® | "x (" | x | ") negative value.";   // terminate and generate stack trace and core file
     4131\end{cfa}
     4132Note, \CFA stream variables ©stdin©, ©stdout©, ©stderr©, ©exit©, and ©abort© overload C variables ©stdin©, ©stdout©, ©stderr©, and functions ©exit© and ©abort©, respectively.
     4133The \CFA header file for the I/O library is \Indexc{fstream.hfa}.
     4134
    41724135
    41734136\subsection{Basic I/O}
    4174 
    4175 The standard polymorphic I/O streams are ©stdin©/©sin© (input), ©stdout©/©sout©, and ©stderr©/©serr© (output) (like C++ ©cin©/©cout©/©cerr©).
    4176 The standard I/O operator is the bit-wise (or) operator, ©'|'©, which is used to cascade multiple I/O operations.
    4177 The \CFA header file for the I/O library is \Indexc{fstream.hfa}.
    41784137
    41794138For implicit formatted output, the common case is printing a series of variables separated by whitespace.
     
    421841771®, ®2®, ®3 4®, ®5®, ®6
    42194178\end{cfa}
    4220 The bit-wise ©|© operator is used for I/O, rather \CC shift-operators, ©<<© and ©>>©, as it is the lowest-priority \emph{overloadable} operator, other than assignment.
    4221 (Operators ©||© and ©&&© are not overloadable in \CFA.)
     4179Finally, \CFA uses the logical-or operator for I/O as it is the lowest-priority \emph{overloadable} operator, other than assignment.
    42224180Therefore, fewer output expressions require parenthesis.
    42234181\begin{cquote}
     
    42424200\end{cquote}
    42434201There is a weak similarity between the \CFA logical-or operator and the \Index{Shell pipe-operator} for moving data, where data flows in the correct direction for input but the opposite direction for output.
    4244 Input and output use a uniform operator, ©|©, rather than \CC's ©<<© and ©>>© input/output operators to prevent this common error in \CC:
     4202Input and output use a uniform operator, ©|©, rather than \CC's ©<<© and ©>>© input/output operators, which prevents this common error in \CC:
    42454203\begin{C++}
    42464204cin << i; // why is this generating a lot of error messages?
    42474205\end{C++}
    4248 
    4249 Streams ©exit© and ©abort© provide output with immediate program termination without and with generating a stack trace and core file.
    4250 Stream ©exit© implicitly returns ©EXIT_FAILURE© to the shell.
    4251 \begin{cfa}
    4252 ®exit®   | "x (" | x | ") negative value.";     §\C{// print, terminate, and return EXIT\_FAILURE to shell}§
    4253 ®abort® | "x (" | x | ") negative value.";      §\C{// print, terminate, and generate stack trace and core file}§
    4254 \end{cfa}
    4255 Note, \CFA stream variables ©stdin©, ©stdout©, ©stderr©, ©exit©, and ©abort© overload C variables ©stdin©, ©stdout©, ©stderr©, and functions ©exit© and ©abort©, respectively.
    42564206
    42574207For implicit formatted input, the common case is reading a sequence of values separated by whitespace, where the type of an input constant must match with the type of the input variable.
     
    42964246\end{tabular}
    42974247\end{cquote}
    4298 The format of numeric input values in the same as C constants without a trailing type suffix, as the input value-type is denoted by the input variable.
    4299 For ©bool© type, the constants are ©true© and ©false©.
    4300 For integral types, any number of digits, optionally preceded by a sign (©+© or ©-©), where a
    4301 \begin{itemize}
    4302 \item
    4303 ©1©-©9© prefix introduces a decimal value (©0©-©9©),
    4304 \item
    4305 ©0© prefix introduces an octal value (©0©-©7©), and
    4306 \item
    4307 ©0x© or ©0X© prefix introduces a hexadecimal value (©0©-©f©) with lower or upper case letters.
    4308 \end{itemize}
    4309 For floating-point types, any number of decimal digits, optionally preceded by a sign (©+© or ©-©), optionally containing a decimal point, and optionally followed by an exponent, ©e© or ©E©, with signed (optional) decimal digits.
    4310 Floating-point values can also be written in hexadecimal format preceded by ©0x© or ©0X© with hexadecimal digits and exponent denoted by ©p© or ©P©.
    4311 In all cases, all whitespace characters are skipped until an appropriate value is found.
    4312 \Textbf{If an appropriate value is not found, the exception ©missing_data© is raised.}
    4313 
    4314 For the C-string type, there are two input forms: any number of \Textbf{non-whitespace} characters or a quoted sequence containing any characters except the closing quote, \ie there is no escape character supported in the string..
    4315 In both cases, the string is null terminated ©'\0'©.
    4316 For the quoted string, the start and end quote characters can be any character and do not have to match \see{\ref{XXX}}.
    4317 
    4318 \VRef[Figure]{f:IOStreamFunctions} shows the I/O stream operations for interacting with files other than ©cin©, ©cout©, and ©cerr©.
    4319 \begin{itemize}[topsep=4pt,itemsep=2pt,parsep=0pt]
    4320 \item
    4321 \Indexc{fail} tests the stream error-indicator, returning nonzero if it is set.
    4322 \item
    4323 \Indexc{clear} resets the stream error-indicator.
    4324 \item
    4325 \Indexc{flush} (©ofstream© only) causes any unwritten data for a stream to be written to the file.
    4326 \item
    4327 \Indexc{eof} (©ifstream© only) tests the end-of-file indicator for the stream pointed to by stream.
    4328 Returns true if the end-of-file indicator is set, otherwise false.
    4329 \item
    4330 \Indexc{open} binds the file with ©name© to a stream accessed with ©mode© (see ©fopen©).
    4331 \item
    4332 \Indexc{close} flushes the stream and closes the file.
    4333 \item
    4334 \Indexc{write} (©ofstream© only) writes ©size© bytes to the stream.
    4335 The bytes are written lazily when an internal buffer fills.
    4336 Eager buffer writes are done with ©flush©
    4337 \item
    4338 \Indexc{read} (©ifstream© only) reads ©size© bytes from the stream.
    4339 \item
    4340 \Indexc{ungetc} (©ifstream© only) pushes the character back to the input stream.
    4341 Pushed-back characters returned by subsequent reads in the reverse order of pushing.
    4342 \end{itemize}
    4343 The constructor functions:
    4344 \begin{itemize}[topsep=4pt,itemsep=2pt,parsep=0pt]
    4345 \item
    4346 create an unbound stream, which is subsequently bound to a file with ©open©.
    4347 \item
    4348 create a bound stream to the associated file with given ©mode©.
    4349 \end{itemize}
    4350 The destructor closes the stream.
    4351 
    4352 \begin{figure}
    4353 \begin{cfa}
    4354 // *********************************** ofstream ***********************************
    4355 bool fail( ofstream & );§\indexc{fail}\index{ofstream@©ofstream©!©fail©}§
    4356 void clear( ofstream & );§\indexc{clear}\index{ofstream@©ofstream©!©clear©}§
    4357 int flush( ofstream & );§\indexc{flush}\index{ofstream@©ofstream©!©flush©}§
    4358 void open( ofstream &, const char name[], const char mode[] = "w" );§\indexc{open}\index{ofstream@©ofstream©!©open©}§
    4359 void close( ofstream & );§\indexc{close}\index{ofstream@©ofstream©!©close©}§
    4360 ofstream & write( ofstream &, const char data[], size_t size );§\indexc{write}\index{ofstream@©ofstream©!©write©}§
    4361 void ?{}( ofstream & );§\index{ofstream@©ofstream©!©?{}©}§
    4362 void ?{}( ofstream &, const char name[], const char mode[] = "w" );
    4363 void ^?{}( ofstream & );§\index{ofstream@©ofstream©!©^?{}©}§
    4364 
    4365 // *********************************** ifstream ***********************************
    4366 bool fail( ifstream & is );§\indexc{fail}\index{ifstream@©ifstream©!©fail©}§
    4367 void clear( ifstream & );§\indexc{clear}\index{ifstream@©ifstream©!©clear©}§
    4368 bool eof( ifstream & is );§\indexc{eof}\index{ifstream@©ifstream©!©eof©}§
    4369 void open( ifstream & is, const char name[], const char mode[] = "r" );§\indexc{open}\index{ifstream@©ifstream©!©open©}§
    4370 void close( ifstream & is );§\indexc{close}\index{ifstream@©ifstream©!©close©}§
    4371 ifstream & read( ifstream & is, char data[], size_t size );§\indexc{read}\index{ifstream@©ifstream©!©read©}§
    4372 ifstream & ungetc( ifstream & is, char c );§\indexc{unget}\index{ifstream@©ifstream©!©unget©}§
    4373 void ?{}( ifstream & is );§\index{ifstream@©ifstream©!©?{}©}§
    4374 void ?{}( ifstream & is, const char name[], const char mode[] = "r" );
    4375 void ^?{}( ifstream & is );§\index{ifstream@©ifstream©!©^?{}©}§
    4376 \end{cfa}
    4377 \caption{I/O Stream Functions}
    4378 \label{f:IOStreamFunctions}
    4379 \end{figure}
    4380 
    4381 \VRef[Figure]{f:CFACommand-LineProcessing} demonstrates the file operations by showing the idiomatic \CFA command-line processing and copying an input file to an output file.
     4248
     4249\VRef[Figure]{f:CFACommand-LineProcessing} shows idiomatic \CFA command-line processing and copying an input file to an output file.
    43824250Note, a stream variable may be copied because it is a reference to an underlying stream data-structures.
    4383 \Textbf{All I/O errors are handled as exceptions}, but end-of-file is not an exception as C programmers are use to explicitly checking for it.
     4251All I/O errors are handles as exceptions, but end-of-file is not an exception as C programmers are use to explicitly checking for it.
    43844252
    43854253\begin{figure}
     
    43934261        try {
    43944262                choose ( argc ) {
    4395                   case 3, 2:
     4263                  case 2, 3:
    43964264                        ®open®( in, argv[1] );                  §\C{// open input file first as output creates file}§
    43974265                        if ( argc == 3 ) ®open®( out, argv[2] ); §\C{// do not create output unless input opens}§
     
    44224290\end{figure}
    44234291
     4292\VRef[Figure]{f:StreamFunctions} shows the stream operations.
     4293\begin{itemize}[topsep=4pt,itemsep=2pt,parsep=0pt]
     4294\item
     4295\Indexc{fail} tests the stream error-indicator, returning nonzero if it is set.
     4296\item
     4297\Indexc{clear} resets the stream error-indicator.
     4298\item
     4299\Indexc{flush} (©ofstream© only) causes any unwritten data for a stream to be written to the file.
     4300\item
     4301\Indexc{eof} (©ifstream© only) tests the end-of-file indicator for the stream pointed to by stream.
     4302Returns true if the end-of-file indicator is set, otherwise false.
     4303\item
     4304\Indexc{open} binds the file with ©name© to a stream accessed with ©mode© (see ©fopen©).
     4305\item
     4306\Indexc{close} flushes the stream and closes the file.
     4307\item
     4308\Indexc{write} (©ofstream© only) writes ©size© bytes to the stream.
     4309The bytes are written lazily when an internal buffer fills.
     4310Eager buffer writes are done with ©flush©
     4311\item
     4312\Indexc{read} (©ifstream© only) reads ©size© bytes from the stream.
     4313\item
     4314\Indexc{ungetc} (©ifstream© only) pushes the character back to the input stream.
     4315Pushed-back characters returned by subsequent reads in the reverse order of pushing.
     4316\end{itemize}
     4317The constructor functions:
     4318\begin{itemize}[topsep=4pt,itemsep=2pt,parsep=0pt]
     4319\item
     4320create an unbound stream, which is subsequently bound to a file with ©open©.
     4321\item
     4322create a bound stream to the associated file with given ©mode©.
     4323\end{itemize}
     4324The destructor closes the stream.
     4325
     4326\begin{figure}
     4327\begin{cfa}
     4328// *********************************** ofstream ***********************************
     4329
     4330bool fail( ofstream & );§\indexc{fail}\index{ofstream@©ofstream©!©fail©}§
     4331void clear( ofstream & );§\indexc{clear}\index{ofstream@©ofstream©!©clear©}§
     4332int flush( ofstream & );§\indexc{flush}\index{ofstream@©ofstream©!©flush©}§
     4333void open( ofstream &, const char name[], const char mode[] = "w" );§\indexc{open}\index{ofstream@©ofstream©!©open©}§
     4334void close( ofstream & );§\indexc{close}\index{ofstream@©ofstream©!©close©}§
     4335ofstream & write( ofstream &, const char data[], size_t size );§\indexc{write}\index{ofstream@©ofstream©!©write©}§
     4336
     4337void ?{}( ofstream & );§\index{ofstream@©ofstream©!©?{}©}§
     4338void ?{}( ofstream &, const char name[], const char mode[] = "w" );
     4339void ^?{}( ofstream & );§\index{ofstream@©ofstream©!©^?{}©}§
     4340
     4341// *********************************** ifstream ***********************************
     4342
     4343bool fail( ifstream & is );§\indexc{fail}\index{ifstream@©ifstream©!©fail©}§
     4344void clear( ifstream & );§\indexc{clear}\index{ifstream@©ifstream©!©clear©}§
     4345bool eof( ifstream & is );§\indexc{eof}\index{ifstream@©ifstream©!©eof©}§
     4346void open( ifstream & is, const char name[], const char mode[] = "r" );§\indexc{open}\index{ifstream@©ifstream©!©open©}§
     4347void close( ifstream & is );§\indexc{close}\index{ifstream@©ifstream©!©close©}§
     4348ifstream & read( ifstream & is, char data[], size_t size );§\indexc{read}\index{ifstream@©ifstream©!©read©}§
     4349ifstream & ungetc( ifstream & is, char c );§\indexc{unget}\index{ifstream@©ifstream©!©unget©}§
     4350
     4351void ?{}( ifstream & is );§\index{ifstream@©ifstream©!©?{}©}§
     4352void ?{}( ifstream & is, const char name[], const char mode[] = "r" );
     4353void ^?{}( ifstream & is );§\index{ifstream@©ifstream©!©^?{}©}§
     4354\end{cfa}
     4355\caption{Stream Functions}
     4356\label{f:StreamFunctions}
     4357\end{figure}
     4358
    44244359
    44254360\subsection{Implicit Separator}
    44264361
    44274362The \Index{implicit separator}\index{I/O!separator} character (space/blank) is a separator not a terminator for output.
    4428 The rules for implicitly adding a separator are:
     4363The rules for implicitly adding the separator are:
    44294364\begin{enumerate}
    44304365\item
     
    44554390\begin{cfa}
    44564391sout | 1 | ", x" | 2 | ". x" | 3 | "; x" | 4 | "! x" | 5 | "? x" | 6 | "% x"
    4457            | 7 | "§\LstStringStyle{\textcent}§ x" | 8 | "§\LstStringStyle{\guillemotright}§ x" | 9 | ") x" | 10 | "] x" | 11 | "} x";
     4392           | 7 | "$\LstStringStyle{\textcent}$ x" | 8 | "$\LstStringStyle{\guillemotright}$ x" | 9 | ") x" | 10 | "] x" | 11 | "} x";
    44584393\end{cfa}
    44594394\begin{cfa}[showspaces=true]
    4460 Input1®,® x 2®.® x 3®;® x 4®!® x 5®?® x 6®%® x 7§\R{\LstStringStyle{\textcent}}§ x 8§\R{\LstStringStyle{\guillemotright}}§ x 9®)® x 10®]® x 11®}® x
     43951®,® x 2®.® x 3®;® x 4®!® x 5®?® x 6®%® x 7$\R{\LstStringStyle{\textcent}}$ x 8$\R{\LstStringStyle{\guillemotright}}$ x 9®)® x 10®]® x 11®}® x
    44614396\end{cfa}
    44624397
    44634398\item
    44644399A separator does not appear after a C string ending with the (extended) \Index*{ASCII}\index{ASCII!extended} characters: \LstStringStyle{([\{=\$\textsterling\textyen\textexclamdown\textquestiondown\guillemotleft}, where \LstStringStyle{\textexclamdown\textquestiondown} are inverted opening exclamation and question marks, and \LstStringStyle{\guillemotleft} is an opening citation mark.
    4465 \begin{cfa}
    4466 sout | "x (" | 1 | "x [" | 2 | "x {" | 3 | "x =" | 4 | "x §\LstStringStyle{\textdollar}§" | 5 | "x §\LstStringStyle{\textsterling}§" | 6 | "x §\LstStringStyle{\textyen}§"
    4467            | 7 | "x §\LstStringStyle{\textexclamdown}§" | 8 | "x §\LstStringStyle{\textquestiondown}§" | 9 | "x §\LstStringStyle{\guillemotleft}§" | 10;
    4468 \end{cfa}
     4400%$
     4401\begin{cfa}
     4402sout | "x (" | 1 | "x [" | 2 | "x {" | 3 | "x =" | 4 | "x $\LstStringStyle{\textdollar}$" | 5 | "x $\LstStringStyle{\textsterling}$" | 6 | "x $\LstStringStyle{\textyen}$"
     4403           | 7 | "x $\LstStringStyle{\textexclamdown}$" | 8 | "x $\LstStringStyle{\textquestiondown}$" | 9 | "x $\LstStringStyle{\guillemotleft}$" | 10;
     4404\end{cfa}
     4405%$
    44694406\begin{cfa}[showspaces=true]
    4470 x ®(®1 x ®[®2 x ®{®3 x ®=®4 x §\LstStringStyle{\textdollar}§5 x §\R{\LstStringStyle{\textsterling}}§6 x §\R{\LstStringStyle{\textyen}}§7 x §\R{\LstStringStyle{\textexclamdown}}§8 x §\R{\LstStringStyle{\textquestiondown}}§9 x §\R{\LstStringStyle{\guillemotleft}}§10
    4471 \end{cfa}
     4407x ®(®1 x ®[®2 x ®{®3 x ®=®4 x $\LstStringStyle{\textdollar}$5 x $\R{\LstStringStyle{\textsterling}}$6 x $\R{\LstStringStyle{\textyen}}$7 x $\R{\LstStringStyle{\textexclamdown}}$8 x $\R{\LstStringStyle{\textquestiondown}}$9 x $\R{\LstStringStyle{\guillemotleft}}$10
     4408\end{cfa}
     4409%$
    44724410
    44734411\item
     
    44774415\end{cfa}
    44784416\begin{cfa}[showspaces=true,showtabs=true]
    4479 x®`®1®`®x§\R{\texttt{'}}§2§\R{\texttt{'}}§x§\R{\texttt{"}}§3§\R{\texttt{"}}§x®:®4®:®x® ®5® ®x®  ®6®     ®x
     4417x®`®1®`®x$\R{\texttt{'}}$2$\R{\texttt{'}}$x$\R{\texttt{"}}$3$\R{\texttt{"}}$x®:®4®:®x® ®5® ®x®  ®6®     ®x
    44804418\end{cfa}
    44814419
     
    44834421If a space is desired before or after one of the special string start/end characters, explicitly insert a space.
    44844422\begin{cfa}
    4485 sout | "x (§\R{\texttt{\textvisiblespace}}§" | 1 | "§\R{\texttt{\textvisiblespace}}§) x" | 2 | "§\R{\texttt{\textvisiblespace}}§, x" | 3 | "§\R{\texttt{\textvisiblespace}}§:x:§\R{\texttt{\textvisiblespace}}§" | 4;
     4423sout | "x ($\R{\texttt{\textvisiblespace}}$" | 1 | "$\R{\texttt{\textvisiblespace}}$) x" | 2 | "$\R{\texttt{\textvisiblespace}}$, x" | 3 | "$\R{\texttt{\textvisiblespace}}$:x:$\R{\texttt{\textvisiblespace}}$" | 4;
    44864424\end{cfa}
    44874425\begin{cfa}[showspaces=true,showtabs=true]
     
    45004438The separator string can be at most 16 characters including the ©'\0'© string terminator (15 printable characters).
    45014439\begin{cfa}[belowskip=0pt]
    4502 sepSet( sout, ", §\LstStringStyle{\textdollar}§" ); §\C{// set separator from " " to ", \$"}§
     4440sepSet( sout, ", $\LstStringStyle{\textdollar}$" ); §\C{// set separator from " " to ", \$"}§
    45034441sout | 1 | 2 | 3 | " \"" | ®sepVal® | "\"";
    45044442\end{cfa}
    45054443\begin{cfa}[showspaces=true,aboveskip=0pt]
    4506 1®, §\color{red}\LstStringStyle{\textdollar}§®2®, §\color{red}\LstStringStyle{\textdollar}§®3 ®", §\color{red}\LstStringStyle{\textdollar}§
     44441®, $\color{red}\LstStringStyle{\textdollar}$®2®, $\color{red}\LstStringStyle{\textdollar}$®3 ®", $\color{red}\LstStringStyle{\textdollar}$
    45074445\end{cfa}
    45084446\begin{cfa}[belowskip=0pt]
     
    46194557For example, in:
    46204558\begin{cfa}
    4621 int i, j;
    46224559sin | i | ®nl® | j;
    462345601 ®2®
     
    46254562\end{cfa}
    46264563variable ©i© is assigned 1, the 2 is skipped, and variable ©j© is assigned 3.
    4627 For example, in:
    4628 \begin{cquote}
    4629 \begin{tabular}{@{}l@{\hspace{3em}}l@{}}
    4630 \begin{cfa}
    4631 char ch
    4632 
    4633 sin | ch; // read X
    4634 
    4635 X
    4636 \end{cfa}
    4637 &
    4638 \begin{cfa}
    4639 
    4640 sin | ®nlOn®; // enable reading newlines
    4641 sin | ch; // read newline
    4642 
    4643 
    4644 \end{cfa}
    4645 \end{tabular}
    4646 \end{cquote}
    4647 the left example skips the newline and reads ©'X'© into ©ch©, while the right example reads the newline into ©ch©.
    46484564
    46494565For output:
     
    46734589
    46744590
    4675 \subsection{Output Manipulators}
     4591\subsection{Output Value Manipulators}
    46764592
    46774593The following \Index{manipulator}s control formatting (printing) of the argument output values.
     
    47274643\item
    47284644\Indexc{unit}( engineering-notation )\index{manipulator!unit@©unit©} print engineering exponent as a letter between the range $10^{-24}$ and $10^{24}$:
    4729 \begin{cquote}
    47304645©y© $\Rightarrow 10^{-24}$, ©z© $\Rightarrow 10^{-21}$, ©a© $\Rightarrow 10^{-18}$, ©f© $\Rightarrow 10^{-15}$, ©p© $\Rightarrow 10^{-12}$, ©n© $\Rightarrow 10^{-9}$, ©u© $\Rightarrow 10^{-6}$, ©m© $\Rightarrow 10^{-3}$, ©K© $\Rightarrow 10^{3}$, ©M© $\Rightarrow 10^{6}$, ©G© $\Rightarrow 10^{9}$, ©T© $\Rightarrow 10^{12}$, ©P© $\Rightarrow 10^{15}$, ©E© $\Rightarrow 10^{18}$, ©Z© $\Rightarrow 10^{21}$, ©Y© $\Rightarrow 10^{24}$.
    4731 \end{cquote}
    47324646For exponent $10^{0}$, no decimal point or letter is printed.
    47334647\begin{cfa}[belowskip=0pt]
     
    49294843// End: //
    49304844\end{comment}
    4931 
    4932 
    4933 \subsection{Input Manipulators}
    4934 
    4935 The following \Index{manipulator}s control scanning of input values (reading), and only affect the format of the argument.
    4936 
    4937 Certain manipulators support a \newterm{scanset}, which is a simple regular expression, where the matching set contains any Latin-1 character (8-bits) or character ranges using minus.
    4938 For example, the scanset \lstinline{"a-zA-Z -/?§"} matches any number of characters between ©'a'© and ©'z'©, between ©'A'© and ©'Z'©, between space and ©'/'©, and characters ©'?'© and (Latin-1) ©'§'©.
     4845%$
     4846
     4847
     4848\subsection{Input Value Manipulators}
     4849
     4850The format of numeric input values in the same as C constants without a trailing type suffix, as the input value-type is denoted by the input variable.
     4851For ©bool© type, the constants are ©true© and ©false©.
     4852For integral types, any number of digits, optionally preceded by a sign (©+© or ©-©), where a
     4853\begin{itemize}
     4854\item
     4855©1©-©9© prefix introduces a decimal value (©0©-©9©),
     4856\item
     4857©0© prefix introduces an octal value (©0©-©7©), and
     4858\item
     4859©0x© or ©0X© prefix introduces a hexadecimal value (©0©-©f©) with lower or upper case letters.
     4860\end{itemize}
     4861For floating-point types, any number of decimal digits, optionally preceded by a sign (©+© or ©-©), optionally containing a decimal point, and optionally followed by an exponent, ©e© or ©E©, with signed (optional) decimal digits.
     4862Floating-point values can also be written in hexadecimal format preceded by ©0x© or ©0X© with hexadecimal digits and exponent denoted by ©p© or ©P©.
     4863
     4864For the C-string type, the input values are \emph{not} the same as C-string constants, \ie double quotes bracketing arbitrary text with escape sequences.
     4865Instead, the next sequence of non-whitespace characters are read, and the input sequence is terminated with delimiter ©'\0'©.
     4866The string variable \emph{must} be large enough to contain the input sequence.
     4867To force programmers to consider buffer overruns for C-string input, C-strings can only be read with a width field, which should specify a size less than or equal to the C-string size, \eg:
     4868\begin{cfa}
     4869char line[64];
     4870sin | wdi( ®sizeof(line)®, line ); // must specify size
     4871\end{cfa}
     4872
     4873A scanset is a simple regular expression, where the matching set contains any Latin-1 character (8-bits) or character ranges using minus.
     4874For example, the scanset \lstinline{"a-zA-Z -/?§"} matches characters between ©'a'© and ©'z'©, between ©'A'© and ©'Z'©, between space and ©'/'©, and characters ©'?'© and (Latin-1) ©'§'©.
    49394875The following string is matched by this scanset:
    49404876\begin{cfa}
    4941 !&%$  abAA () ZZZ  ??  xx§\S\S\S§
     4877!&%$  abAA () ZZZ  ??  xx§§§§
    49424878\end{cfa}
    49434879To match a minus, put it as the first character, ©"-0-9"©.
    49444880Other complex forms of regular-expression matching are not supported.
    49454881
    4946 A string variable \emph{must} be large enough to contain the input sequence.
    4947 To force programmers to consider buffer overruns for C-string input, C-strings can only be read with a width field, which should specify a size less than or equal to the C-string size, \eg:
    4948 \begin{cfa}
    4949 char line[64];
    4950 sin | wdi( ®sizeof(line)®, line ); // must specify size
    4951 \end{cfa}
    4952 
    4953 Currently, there is no mechanism to detect if a value read exceeds the capwhen Most types are finite sized, \eg integral types only store value that fit into their corresponding storage, 8, 16, 32, 64, 128 bits.
    4954 Hence, an input value may be too large, and the result of the read is often considered undefined, which leads to difficlt to locate runtime errors.
    4955 All reads in \CFA check if values do not fit into the argument variable's type and raise the exception
    4956 All types are
    4957 
     4882The following \Index{manipulator}s control scanning of input values (reading), and only affect the format of the argument.
    49584883\begin{enumerate}
    49594884\item
    4960 \Indexc{skip}( scanset )\index{manipulator!skip@©skip©}, ©skip©( $N$ )
    4961 The first form uses a scanset to skip matching characters.
    4962 The second form skips the next $N$ characters, including newline.
     4885\Indexc{skip}( pattern )\index{manipulator!skip@©skip©}, ©skip©( length )
     4886The pattern is composed of white-space and non-white-space characters, where \emph{any} white-space character matches 0 or more input white-space characters (hence, consecutive white-space characters in the pattern are combined), and each non-white-space character matches exactly with an input character.
     4887The length is composed of the next $N$ characters, including the newline character.
    49634888If the match successes, the input characters are discarded, and input continues with the next character.
    49644889If the match fails, the input characters are left unread.
    49654890\begin{cfa}[belowskip=0pt]
    4966 char sk[§\,§] = "abc";
     4891char sk[$\,$] = "abc";
    49674892sin | "abc " | skip( sk ) | skip( 5 ); // match input sequence
    49684893\end{cfa}
     
    49744899
    49754900\item
    4976 \Indexc{wdi}( maximum, variable )\index{manipulator!wdi@©wdi©}
    4977 For all types except ©char *©, whitespace is skipped until an appropriate value is found for the specified variable type.
    4978 maximum is the maximum number of characters read for the current operation.
     4901\Indexc{wdi}( maximum, reference-value )\index{manipulator!wdi@©wdi©}
     4902For all types except ©char©, maximum is the maximum number of characters read for the current operation.
    49794903\begin{cfa}[belowskip=0pt]
    4980 char ch;   char ca[3];   int i;   double d;   
    4981 sin | wdi( sizeof(ch), ch ) | wdi( sizeof(ca), ca[0] ) | wdi( 3, i ) | wdi( 8, d );  // c == 'a', ca == "bcd", i == 123, d == 345.6
     4904char s[10];   int i;   double d;   
     4905sin | wdi( 4, s ) | wdi( 3, i ) | wdi( 8, d );  // c == "abcd", i == 123, d == 3.456E+2
    49824906\end{cfa}
    49834907\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
    49844908®abcd1233.456E+2®
    49854909\end{cfa}
    4986 Here, ©ca[0]© is type ©char©, so the width reads 3 characters \Textbf{without} a null terminator.
    4987 
    49884910Note, input ©wdi© cannot be overloaded with output ©wd© because both have the same parameters but return different types.
    49894911Currently, \CFA cannot distinguish between these two manipulators in the middle of an ©sout©/©sin© expression based on return type.
    4990 
    4991 \item
    4992 \Indexc{wdi}( maximum size, ©char s[]© )\index{manipulator!wdi@©wdi©}
    4993 For type ©char *©, maximum is the maximum number of characters read for the current operation.
    4994 Any number of non-whitespace characters, stopping at the first whitespace character found. A terminating null character is automatically added at the end of the stored sequence
    4995 \begin{cfa}[belowskip=0pt]
    4996 char cstr[10];
    4997 sin | wdi( sizeof(cstr), cstr );
    4998 \end{cfa}
    4999 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
    5000 ®abcd1233.456E+2®
    5001 \end{cfa}
    5002 
    5003 \item
    5004 \Indexc{wdi}( maximum size, maximum read, ©char s[]© )\index{manipulator!wdi@©wdi©}
    5005 For type ©char *©, maximum is the maximum number of characters read for the current operation.
    5006 \begin{cfa}[belowskip=0pt]
    5007 char ch;   char ca[3];   int i;   double d;   
    5008 sin | wdi( sizeof(ch), ch ) | wdi( sizeof(ca), ca[0] ) | wdi( 3, i ) | wdi( 8, d );  // c == 'a', ca == "bcd", i == 123, d == 345.6
    5009 \end{cfa}
    5010 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
    5011 ®abcd1233.456E+2®
    5012 \end{cfa}
    50134912
    50144913\item
     
    50474946\end{cfa}
    50484947
    5049 \Indexc{quoted}( char delimit, wdi-input-string )\index{manipulator!quoted@©quoted©}
    5050 Is an ©excl© with scanset ©"delimit"©, which consumes all characters up to the delimit character.
    5051 If the delimit character is omitted, it defaults to ©'\n'© (newline).
    5052 
    50534948\item
    50544949\Indexc{getline}( char delimit, wdi-input-string )\index{manipulator!getline@©getline©}
    5055 Is an ©excl© with scanset ©"delimit"©, which consumes all characters up to the delimit character.
     4950Is an @excl@ with scanset ©"delimit"©, which consumes all characters up to the delimit character.
    50564951If the delimit character is omitted, it defaults to ©'\n'© (newline).
    50574952\end{enumerate}
     
    50634958For example, if two threads execute the following:
    50644959\begin{cfa}
    5065 §\emph{thread\(_1\)}§ : sout | "abc " | "def ";
    5066 §\emph{thread\(_2\)}§ : sout | "uvw " | "xyz ";
     4960$\emph{thread\(_1\)}$ : sout | "abc " | "def ";
     4961$\emph{thread\(_2\)}$ : sout | "uvw " | "xyz ";
    50674962\end{cfa}
    50684963possible outputs are:
     
    51044999The common usage is the short form of the mutex statement\index{ostream@©ostream©!mutex@©mutex©} to lock a stream during a single cascaded I/O expression, \eg:
    51055000\begin{cfa}
    5106 §\emph{thread\(_1\)}§ : ®mutex( sout )® sout | "abc " | "def ";
    5107 §\emph{thread\(_2\)}§ : ®mutex( sout )® sout | "uvw " | "xyz ";
     5001$\emph{thread\(_1\)}$ : ®mutex( sout )® sout | "abc " | "def ";
     5002$\emph{thread\(_2\)}$ : ®mutex( sout )® sout | "uvw " | "xyz ";
    51085003\end{cfa}
    51095004Now, the order of the thread execution is still non-deterministic, but the output is constrained to two possible lines in either order.
     
    51665061Cultures use different syntax, called a \newterm{locale}, for printing numbers so they are easier to read, \eg:
    51675062\begin{cfa}
    5168 12®,®345®.®123          §\C[1.25in]{// comma separator, period decimal-point}§
     506312®,®345®.®123          $\C[1.25in]{// comma separator, period decimal-point}$
    5169506412®.®345®,®123          §\C{// period separator, comma decimal-point}§
    5170 12§\Sp§345®,®123®.®     §\C{// space separator, comma decimal-point, period terminator}\CRT§
     506512$\Sp$345®,®123®.®     §\C{// space separator, comma decimal-point, period terminator}\CRT§
    51715066\end{cfa}
    51725067A locale is selected with function ©setlocale©, and the corresponding locale package \emph{must} be installed on the underlying system;
     
    52145109Ukraine uk_UA.utf8
    5215511012 123 1 234 12 345 123 456 1 234 567
    5216 12®.® 123®,®1®.® 1§\Sp§234®,®12®.® 12§\Sp§ 345®,®123®.® 123§\Sp§ 456®,®1234®.® 1§\Sp§ 234§\Sp§567®,®12345®.®
     511112®.® 123®,®1®.® 1$\Sp$234®,®12®.® 12$\Sp$ 345®,®123®.® 123$\Sp$ 456®,®1234®.® 1$\Sp$ 234$\Sp$567®,®12345®.®
    52175112
    52185113Default locale off C
     
    55665461
    55675462\subsection{Operator}
    5568 \label{s:Operator}
    55695463
    55705464\CFA also allows operators to be overloaded, to simplify the use of user-defined types.
     
    74387332
    74397333\Index*[C++]{\CC{}} is a general-purpose programming language.
    7440 It is an imperative, object-oriented and generic programming language, while also providing facilities for low-level memory manipulation.
    7441 The primary focus of \CC was adding object-oriented programming to C, and this is the primary difference between \CC and \CFA.
     7334It has imperative, object-oriented and generic programming features, while also providing facilities for low-level memory manipulation. (Wikipedia)
     7335
     7336The primary focus of \CC seems to be adding object-oriented programming to C, and this is the primary difference between \CC and Do.
    74427337\CC uses classes to encapsulate data and the functions that operate on that data, and to hide the internal representation of the data.
    74437338\CFA uses modules instead to perform these same tasks.
     
    74577352\subsection{Go}
    74587353
    7459 \Index*{Go}, also commonly referred to as golang, is a programming language developed at Google in 2007~\cite{Go}.
     7354\Index*{Go}, also commonly referred to as golang, is a programming language developed at Google in 2007 [.].
    74607355It is a statically typed language with syntax loosely derived from that of C, adding garbage collection, type
    74617356safety, some structural typing capabilities, additional built-in types such as variable-length arrays and key-value maps, and a large standard library. (Wikipedia)
     
    75117406In \CFA, there are ambiguous cases with dereference and operator identifiers, \eg ©int *?*?()©, where the string ©*?*?© can be interpreted as:
    75127407\begin{cfa}
    7513 *?§\Sp§*?                                                               §\C{// dereference operator, dereference operator}§
    7514 *§\Sp§?*?                                                               §\C{// dereference, multiplication operator}§
     7408*?$\Sp$*? §\C{// dereference operator, dereference operator}§
     7409*$\Sp$?*? §\C{// dereference, multiplication operator}§
    75157410\end{cfa}
    75167411By default, the first interpretation is selected, which does not yield a meaningful parse.
     
    75217416The ambiguity occurs when the deference operator has no parameters:
    75227417\begin{cfa}
    7523 *?()§\R{\textvisiblespace...}§ ;
    7524 *?()§\R{\textvisiblespace...}§(...) ;
     7418*?()$\R{\textvisiblespace...}$ ;
     7419*?()$\R{\textvisiblespace...}$(...) ;
    75257420\end{cfa}
    75267421requiring arbitrary whitespace look-ahead for the routine-call parameter-list to disambiguate.
     
    75307425The remaining cases are with the increment/decrement operators and conditional expression, \eg:
    75317426\begin{cfa}
    7532 i++?§\R{\textvisiblespace...}§(...);
    7533 i?++§\R{\textvisiblespace...}§(...);
     7427i++?$\R{\textvisiblespace...}$(...);
     7428i?++$\R{\textvisiblespace...}$(...);
    75347429\end{cfa}
    75357430requiring arbitrary whitespace look-ahead for the operator parameter-list, even though that interpretation is an incorrect expression (juxtaposed identifiers).
    75367431Therefore, it is necessary to disambiguate these cases with a space:
    75377432\begin{cfa}
    7538 i++§\Sp§? i : 0;
    7539 i?§\Sp§++i : 0;
     7433i++$\Sp$? i : 0;
     7434i?$\Sp$++i : 0;
    75407435\end{cfa}
    75417436
     
    75647459\eg:
    75657460\begin{cfa}
    7566 x;                                                              §\C{// int x}§
    7567 *y;                                                             §\C{// int * y}§
    7568 f( p1, p2 );                                    §\C{// int f( int p1, int p2 );}§
    7569 g( p1, p2 ) int p1, p2;                 §\C{// int g( int p1, int p2 );}§
     7461x; §\C{// int x}§
     7462*y; §\C{// int *y}§
     7463f( p1, p2 ); §\C{// int f( int p1, int p2 );}§
     7464g( p1, p2 ) int p1, p2; §\C{// int g( int p1, int p2 );}§
    75707465\end{cfa}
    75717466\CFA continues to support K\&R routine definitions:
    75727467\begin{cfa}
    7573 f( a, b, c )                                    §\C{// default int return}§
    7574         int a, b; char c;                       §\C{// K\&R parameter declarations}§
     7468f( a, b, c ) §\C{// default int return}§
     7469        int a, b; char c §\C{// K\&R parameter declarations}§
    75757470{
    75767471        ...
     
    75917486int rtn( int i );
    75927487int rtn( char c );
    7593 rtn( 'x' );                                             §\C{// programmer expects 2nd rtn to be called}§
     7488rtn( 'x' ); §\C{// programmer expects 2nd rtn to be called}§
    75947489\end{cfa}
    75957490\item[Rationale:] it is more intuitive for the call to ©rtn© to match the second version of definition of ©rtn© rather than the first.
     
    76137508\item[Change:] make string literals ©const©:
    76147509\begin{cfa}
    7615 char * p = "abc";                               §\C{// valid in C, deprecated in \CFA}§
    7616 char * q = expr ? "abc" : "de"; §\C{// valid in C, invalid in \CFA}§
     7510char * p = "abc"; §\C{// valid in C, deprecated in \CFA}§
     7511char * q = expr ? "abc" : "de"; §\C{// valid in C, invalid in \CFA}§
    76177512\end{cfa}
    76187513The type of a string literal is changed from ©[] char© to ©const [] char©.
     
    76217516\begin{cfa}
    76227517char * p = "abc";
    7623 p[0] = 'w';                                             §\C{// segment fault or change constant literal}§
     7518p[0] = 'w'; §\C{// segment fault or change constant literal}§
    76247519\end{cfa}
    76257520The same problem occurs when passing a string literal to a routine that changes its argument.
     
    76337528\item[Change:] remove \newterm{tentative definitions}, which only occurs at file scope:
    76347529\begin{cfa}
    7635 int i;                                                  §\C{// forward definition}§
    7636 int *j = ®&i®;                                  §\C{// forward reference, valid in C, invalid in \CFA}§
    7637 int i = 0;                                              §\C{// definition}§
     7530int i; §\C{// forward definition}§
     7531int *j = ®&i®; §\C{// forward reference, valid in C, invalid in \CFA}§
     7532int i = 0; §\C{// definition}§
    76387533\end{cfa}
    76397534is valid in C, and invalid in \CFA because duplicate overloaded object definitions at the same scope level are disallowed.
     
    76417536\begin{cfa}
    76427537struct X { int i; struct X *next; };
    7643 static struct X a;                              §\C{// forward definition}§
    7644 static struct X b = { 0, ®&a® }; §\C{// forward reference, valid in C, invalid in \CFA}§
    7645 static struct X a = { 1, &b };  §\C{// definition}§
     7538static struct X a; §\C{// forward definition}§
     7539static struct X b = { 0, ®&a® };§\C{// forward reference, valid in C, invalid in \CFA}§
     7540static struct X a = { 1, &b }; §\C{// definition}§
    76467541\end{cfa}
    76477542\item[Rationale:] avoids having different initialization rules for builtin types and user-defined types.
     
    76577552enum ®Colour® { R, G, B, Y, C, M };
    76587553struct Person {
    7659         enum ®Colour® { R, G, B };      §\C{// nested type}§
    7660         struct Face {                           §\C{// nested type}§
    7661                 ®Colour® Eyes, Hair;    §\C{// type defined outside (1 level)}§
     7554        enum ®Colour® { R, G, B };      $\C[7cm]{// nested type}$
     7555        struct Face { §\C{// nested type}§
     7556                ®Colour® Eyes, Hair; §\C{// type defined outside (1 level)}§
    76627557        };
    7663         ®.Colour® shirt;                        §\C{// type defined outside (top level)}§
    7664         ®Colour® pants;                         §\C{// type defined same level}§
    7665         Face looks[10];                         §\C{// type defined same level}§
     7558        ®.Colour® shirt; §\C{// type defined outside (top level)}§
     7559        ®Colour® pants; §\C{// type defined same level}§
     7560        Face looks[10]; §\C{// type defined same level}§
    76667561};
    7667 ®Colour® c = R;                                 §\C{// type/enum defined same level}§
    7668 Person®.Colour® pc = Person®.®R;        §\C{// type/enum defined inside}§
    7669 Person®.®Face pretty;                   §\C{// type defined inside}§
     7562®Colour® c = R; §\C{// type/enum defined same level}§
     7563Person®.Colour® pc = Person®.®R;§\C{// type/enum defined inside}§
     7564Person®.®Face pretty; §\C{// type defined inside}\CRT§
    76707565\end{cfa}
    76717566In C, the name of the nested types belongs to the same scope as the name of the outermost enclosing structure, \ie the nested types are hoisted to the scope of the outer-most type, which is not useful and confusing.
     
    77447639\CFA introduces the following new \Index{keyword}s, which cannot be used as identifiers.
    77457640
    7746 \begin{cquote}
    7747 \Indexc{basetypeof}, \Indexc{choose}, \Indexc{coroutine}, \Indexc{disable},
    7748 \Indexc{enable}, \Indexc{exception}, \Indexc{fallthrough}, \Indexc{fallthru},
    7749 \Indexc{finally}, \Indexc{fixup}, \Indexc{forall},\Indexc{generator},
    7750 \Indexc{int128}, \Indexc{monitor}, \Indexc{mutex}, \Indexc{one_t},
    7751 \Indexc{report}, \Indexc{suspend}, \Indexc{throw}, \Indexc{throwResume},
    7752 \Indexc{trait}, \Indexc{try}, \Indexc{virtual}, \Indexc{waitfor},
    7753 \Indexc{when}, \Indexc{with}, \Indexc{zero_t}
    7754 \end{cquote}
    7755 \CFA introduces the following new \Index{quasi-keyword}s, which can be used as identifiers.
    7756 \begin{cquote}
    7757 \Indexc{catch}, \Indexc{catchResume}, \Indexc{finally}, \Indexc{fixup}, \Indexc{or}, \Indexc{timeout}
    7758 \end{cquote}
    7759 
    7760 \begin{comment}
    77617641\begin{cquote}
    77627642\begin{tabular}{@{}lllllll@{}}
     
    78277707\end{tabular}
    78287708\end{cquote}
    7829 \end{comment}
    7830 
    78317709
    78327710\section{Standard Headers}
    78337711\label{s:StandardHeaders}
    78347712
    7835 \Celeven prescribes the following standard header-files~\cite[\S~7.1.2]{C11}:
    7836 \begin{cquote}
    7837 \Indexc{assert.h}, \Indexc{complex.h}, \Indexc{ctype.h}, \Indexc{errno.h}, \Indexc{fenv.h},
    7838 \Indexc[deletekeywords=float]{float.h}, \Indexc{inttypes.h}, \Indexc{iso646.h}, \Indexc{limits.h},
    7839 \Indexc{locale.h}, \Indexc{math.h}, \Indexc{setjmp.h}, \Indexc{signal.h}, \Indexc{stdalign.h},
    7840 \Indexc{stdarg.h}, \Indexc{stdatomic.h}, \Indexc{stdbool.h}, \Indexc{stddef.h}, \Indexc{stdint.h},
    7841 \Indexc{stdio.h}, \Indexc{stdlib.h}, \Indexc{stdnoreturn.h}, \Indexc{string.h}, \Indexc{tgmath.h},
    7842 \Indexc{threads.h}, \Indexc{time.h}, \Indexc{uchar.h}, \Indexc{wchar.h}, \Indexc{wctype.h}
    7843 \end{cquote}
    7844 and \CFA adds to this list:
    7845 \begin{cquote}
    7846 \Indexc{gmp.h}, \Indexc{malloc.h}, \Indexc{unistd.h}
    7847 \end{cquote}
    7848 \begin{comment}
     7713\Celeven prescribes the following standard header-files~\cite[\S~7.1.2]{C11} and \CFA adds to this list:
    78497714\begin{cquote}
    78507715\begin{tabular}{@{}llllll|l@{}}
     
    79087773\end{tabular}
    79097774\end{cquote}
    7910 \end{comment}
    79117775For the prescribed head-files, \CFA uses header interposition to wraps these includes in an ©extern "C"©;
    79127776hence, names in these include files are not mangled\index{mangling!name} \see{\VRef{s:Interoperability}}.
     
    79737837Type-safe allocation is provided for all C allocation routines and new \CFA allocation routines, \eg in
    79747838\begin{cfa}
    7975 int * ip = (int *)malloc( sizeof(int) ); §\C{// C}§
    7976 int * ip = malloc();                                    §\C{// \CFA type-safe version of C malloc}§
    7977 int * ip = alloc();                                             §\C{// \CFA type-safe uniform alloc}§
     7839int * ip = (int *)malloc( sizeof(int) );                §\C{// C}§
     7840int * ip = malloc();                                                    §\C{// \CFA type-safe version of C malloc}§
     7841int * ip = alloc();                                                             §\C{// \CFA type-safe uniform alloc}§
    79787842\end{cfa}
    79797843the latter two allocations determine the allocation size from the type of ©p© (©int©) and cast the pointer to the allocated storage to ©int *©.
     
    79827846\begin{cfa}
    79837847struct S { int i; } __attribute__(( aligned( 128 ) )); // cache-line alignment
    7984 S * sp = malloc();                                              §\C{// honour type alignment}§
     7848S * sp = malloc();                                                              §\C{// honour type alignment}§
    79857849\end{cfa}
    79867850the storage allocation is implicitly aligned to 128 rather than the default 16.
     
    79977861\CFA memory management extends allocation to support constructors for initialization of allocated storage, \eg in
    79987862\begin{cfa}
    7999 struct S { int i; };                                    §\C{// cache-line alignment}§
     7863struct S { int i; };                                                    §\C{// cache-line alignment}§
    80007864void ?{}( S & s, int i ) { s.i = i; }
    80017865// assume ?|? operator for printing an S
    80027866
    8003 S & sp = *®new®( 3 );                                   §\C{// call constructor after allocation}§
     7867S & sp = *®new®( 3 );                                                   §\C{// call constructor after allocation}§
    80047868sout | sp.i;
    80057869®delete®( &sp );
    80067870
    8007 S * spa = ®anew®( 10, 5 );                              §\C{// allocate array and initialize each array element}§
     7871S * spa = ®anew®( 10, 5 );                                              §\C{// allocate array and initialize each array element}§
    80087872for ( i; 10 ) sout | spa[i] | nonl;
    80097873sout | nl;
     
    80177881extern "C" {
    80187882        // C unsafe allocation
    8019         void * malloc( size_t size );§\indexc{malloc}§
    8020         void * calloc( size_t dim, size_t size );§\indexc{calloc}§
    8021         void * realloc( void * ptr, size_t size );§\indexc{realloc}§
    8022         void * memalign( size_t align, size_t size );§\indexc{memalign}§
    8023         void * aligned_alloc( size_t align, size_t size );§\indexc{aligned_alloc}§
    8024         int posix_memalign( void ** ptr, size_t align, size_t size );§\indexc{posix_memalign}§
    8025         void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize );§\indexc{cmemalign}§ // CFA
     7883        void * malloc( size_t size );$\indexc{malloc}$
     7884        void * calloc( size_t dim, size_t size );$\indexc{calloc}$
     7885        void * realloc( void * ptr, size_t size );$\indexc{realloc}$
     7886        void * memalign( size_t align, size_t size );$\indexc{memalign}$
     7887        void * aligned_alloc( size_t align, size_t size );$\indexc{aligned_alloc}$
     7888        int posix_memalign( void ** ptr, size_t align, size_t size );$\indexc{posix_memalign}$
     7889        void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize );$\indexc{cmemalign}$ // CFA
    80267890
    80277891        // C unsafe initialization/copy
    8028         void * memset( void * dest, int c, size_t size );§\indexc{memset}§
    8029         void * memcpy( void * dest, const void * src, size_t size );§\indexc{memcpy}§
     7892        void * memset( void * dest, int c, size_t size );$\indexc{memset}$
     7893        void * memcpy( void * dest, const void * src, size_t size );$\indexc{memcpy}$
    80307894}
    80317895
     
    80337897
    80347898forall( dtype T | sized(T) ) {
    8035         // §\CFA§ safe equivalents, i.e., implicit size specification
     7899        // $\CFA$ safe equivalents, i.e., implicit size specification
    80367900        T * malloc( void );
    80377901        T * calloc( size_t dim );
     
    80427906        int posix_memalign( T ** ptr, size_t align );
    80437907
    8044         // §\CFA§ safe general allocation, fill, resize, alignment, array
    8045         T * alloc( void );§\indexc{alloc}§                                      §\C[3.5in]{// variable, T size}§
     7908        // $\CFA$ safe general allocation, fill, resize, alignment, array
     7909        T * alloc( void );$\indexc{alloc}$                                      $\C[3.5in]{// variable, T size}$
    80467910        T * alloc( size_t dim );                                                        §\C{// array[dim], T size elements}§
    80477911        T * alloc( T ptr[], size_t dim );                                       §\C{// realloc array[dim], T size elements}§
    80487912
    8049         T * alloc_set( char fill );§\indexc{alloc_set}§         §\C{// variable, T size, fill bytes with value}§
     7913        T * alloc_set( char fill );$\indexc{alloc_set}$         §\C{// variable, T size, fill bytes with value}§
    80507914        T * alloc_set( T fill );                                                        §\C{// variable, T size, fill with value}§
    80517915        T * alloc_set( size_t dim, char fill );                         §\C{// array[dim], T size elements, fill bytes with value}§
     
    80667930        T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ); §\C{// realloc new aligned array[dim], fill new bytes with value}§
    80677931
    8068         // §\CFA§ safe initialization/copy, i.e., implicit size specification
    8069         T * memset( T * dest, char fill );§\indexc{memset}§
    8070         T * memcpy( T * dest, const T * src );§\indexc{memcpy}§
    8071 
    8072         // §\CFA§ safe initialization/copy, i.e., implicit size specification, array types
     7932        // $\CFA$ safe initialization/copy, i.e., implicit size specification
     7933        T * memset( T * dest, char fill );$\indexc{memset}$
     7934        T * memcpy( T * dest, const T * src );$\indexc{memcpy}$
     7935
     7936        // $\CFA$ safe initialization/copy, i.e., implicit size specification, array types
    80737937        T * amemset( T dest[], char fill, size_t dim );
    80747938        T * amemcpy( T dest[], const T src[], size_t dim );
    80757939}
    80767940
    8077 // §\CFA§ allocation/deallocation and constructor/destructor, non-array types
    8078 forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );§\indexc{new}§
    8079 forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr );§\indexc{delete}§
     7941// $\CFA$ allocation/deallocation and constructor/destructor, non-array types
     7942forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );$\indexc{new}$
     7943forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr );$\indexc{delete}$
    80807944forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } )
    80817945  void delete( T * ptr, Params rest );
    80827946
    8083 // §\CFA§ allocation/deallocation and constructor/destructor, array types
    8084 forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p );§\indexc{anew}§
    8085 forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] );§\indexc{adelete}§
     7947// $\CFA$ allocation/deallocation and constructor/destructor, array types
     7948forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p );$\indexc{anew}$
     7949forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] );$\indexc{adelete}$
    80867950forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } )
    80877951  void adelete( size_t dim, T arr[], Params rest );
     
    80937957\leavevmode
    80947958\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    8095 int ato( const char * ptr );§\indexc{ato}§
     7959int ato( const char * ptr );$\indexc{ato}$
    80967960unsigned int ato( const char * ptr );
    80977961long int ato( const char * ptr );
     
    81267990\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    81277991forall( T | { int ?<?( T, T ); } ) §\C{// location}§
    8128 T * bsearch( T key, const T * arr, size_t dim );§\indexc{bsearch}§
     7992T * bsearch( T key, const T * arr, size_t dim );$\indexc{bsearch}$
    81297993
    81307994forall( T | { int ?<?( T, T ); } ) §\C{// position}§
     
    81327996
    81337997forall( T | { int ?<?( T, T ); } )
    8134 void qsort( const T * arr, size_t dim );§\indexc{qsort}§
     7998void qsort( const T * arr, size_t dim );$\indexc{qsort}$
    81357999
    81368000forall( E | { int ?<?( E, E ); } ) {
    8137         E * bsearch( E key, const E * vals, size_t dim );§\indexc{bsearch}§ §\C{// location}§
     8001        E * bsearch( E key, const E * vals, size_t dim );$\indexc{bsearch}$ §\C{// location}§
    81388002        size_t bsearch( E key, const E * vals, size_t dim );§\C{// position}§
    8139         E * bsearchl( E key, const E * vals, size_t dim );§\indexc{bsearchl}§
     8003        E * bsearchl( E key, const E * vals, size_t dim );$\indexc{bsearchl}$
    81408004        size_t bsearchl( E key, const E * vals, size_t dim );
    8141         E * bsearchu( E key, const E * vals, size_t dim );§\indexc{bsearchu}§
     8005        E * bsearchu( E key, const E * vals, size_t dim );$\indexc{bsearchu}$
    81428006        size_t bsearchu( E key, const E * vals, size_t dim );
    81438007}
     
    81538017
    81548018forall( E | { int ?<?( E, E ); } ) {
    8155         void qsort( E * vals, size_t dim );§\indexc{qsort}§
     8019        void qsort( E * vals, size_t dim );$\indexc{qsort}$
    81568020}
    81578021\end{cfa}
     
    81628026\leavevmode
    81638027\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    8164 unsigned char abs( signed char );§\indexc{abs}§
     8028unsigned char abs( signed char );$\indexc{abs}$
    81658029int abs( int );
    81668030unsigned long int abs( long int );
     
    81778041
    81788042
    8179 \subsection{C Random Numbers}
     8043\subsection{Random Numbers}
    81808044
    81818045\leavevmode
    81828046\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    8183 void srandom( unsigned int seed );§\indexc{srandom}§
    8184 char random( void );§\indexc{random}§
     8047void srandom( unsigned int seed );$\indexc{srandom}$
     8048char random( void );$\indexc{random}$
    81858049char random( char u ); §\C{// [0,u)}§
    81868050char random( char l, char u ); §\C{// [l,u]}§
     
    82098073\leavevmode
    82108074\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    8211 forall( T | { int ?<?( T, T ); } ) T min( T t1, T t2 );§\indexc{min}§
    8212 forall( T | { int ?>?( T, T ); } ) T max( T t1, T t2 );§\indexc{max}§
    8213 forall( T | { T min( T, T ); T max( T, T ); } ) T clamp( T value, T min_val, T max_val );§\indexc{clamp}§
    8214 forall( T ) void swap( T * t1, T * t2 );§\indexc{swap}§
     8075forall( T | { int ?<?( T, T ); } ) T min( T t1, T t2 );$\indexc{min}$
     8076forall( T | { int ?>?( T, T ); } ) T max( T t1, T t2 );$\indexc{max}$
     8077forall( T | { T min( T, T ); T max( T, T ); } ) T clamp( T value, T min_val, T max_val );$\indexc{clamp}$
     8078forall( T ) void swap( T * t1, T * t2 );$\indexc{swap}$
    82158079\end{cfa}
    82168080
     
    82268090\leavevmode
    82278091\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    8228 float ?%?( float, float );§\indexc{fmod}§
     8092float ?%?( float, float );$\indexc{fmod}$
    82298093float fmod( float, float );
    82308094double ?%?( double, double );
     
    82338097long double fmod( long double, long double );
    82348098
    8235 float remainder( float, float );§\indexc{remainder}§
     8099float remainder( float, float );$\indexc{remainder}$
    82368100double remainder( double, double );
    82378101long double remainder( long double, long double );
    82388102
    8239 float remquo( float, float, int * );§\indexc{remquo}§
     8103float remquo( float, float, int * );$\indexc{remquo}$
    82408104double remquo( double, double, int * );
    82418105long double remquo( long double, long double, int * );
     
    82488112[ int, long double ] div( long double, long double );
    82498113
    8250 float fma( float, float, float );§\indexc{fma}§
     8114float fma( float, float, float );$\indexc{fma}$
    82518115double fma( double, double, double );
    82528116long double fma( long double, long double, long double );
    82538117
    8254 float fdim( float, float );§\indexc{fdim}§
     8118float fdim( float, float );$\indexc{fdim}$
    82558119double fdim( double, double );
    82568120long double fdim( long double, long double );
    82578121
    8258 float nan( const char * );§\indexc{nan}§
     8122float nan( const char * );$\indexc{nan}$
    82598123double nan( const char * );
    82608124long double nan( const char * );
     
    82668130\leavevmode
    82678131\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    8268 float exp( float );§\indexc{exp}§
     8132float exp( float );$\indexc{exp}$
    82698133double exp( double );
    82708134long double exp( long double );
     
    82738137long double _Complex exp( long double _Complex );
    82748138
    8275 float exp2( float );§\indexc{exp2}§
     8139float exp2( float );$\indexc{exp2}$
    82768140double exp2( double );
    82778141long double exp2( long double );
     
    82808144// long double _Complex exp2( long double _Complex );
    82818145
    8282 float expm1( float );§\indexc{expm1}§
     8146float expm1( float );$\indexc{expm1}$
    82838147double expm1( double );
    82848148long double expm1( long double );
    82858149
    8286 float pow( float, float );§\indexc{pow}§
     8150float pow( float, float );$\indexc{pow}$
    82878151double pow( double, double );
    82888152long double pow( long double, long double );
     
    82978161\leavevmode
    82988162\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    8299 float log( float );§\indexc{log}§
     8163float log( float );$\indexc{log}$
    83008164double log( double );
    83018165long double log( long double );
     
    83048168long double _Complex log( long double _Complex );
    83058169
    8306 int log2( unsigned int );§\indexc{log2}§
     8170int log2( unsigned int );$\indexc{log2}$
    83078171long int log2( unsigned long int );
    83088172long long int log2( unsigned long long int )
     
    83148178// long double _Complex log2( long double _Complex );
    83158179
    8316 float log10( float );§\indexc{log10}§
     8180float log10( float );$\indexc{log10}$
    83178181double log10( double );
    83188182long double log10( long double );
     
    83218185// long double _Complex log10( long double _Complex );
    83228186
    8323 float log1p( float );§\indexc{log1p}§
     8187float log1p( float );$\indexc{log1p}$
    83248188double log1p( double );
    83258189long double log1p( long double );
    83268190
    8327 int ilogb( float );§\indexc{ilogb}§
     8191int ilogb( float );$\indexc{ilogb}$
    83288192int ilogb( double );
    83298193int ilogb( long double );
    83308194
    8331 float logb( float );§\indexc{logb}§
     8195float logb( float );$\indexc{logb}$
    83328196double logb( double );
    83338197long double logb( long double );
    83348198
    8335 float sqrt( float );§\indexc{sqrt}§
     8199float sqrt( float );$\indexc{sqrt}$
    83368200double sqrt( double );
    83378201long double sqrt( long double );
     
    83408204long double _Complex sqrt( long double _Complex );
    83418205
    8342 float cbrt( float );§\indexc{cbrt}§
     8206float cbrt( float );$\indexc{cbrt}$
    83438207double cbrt( double );
    83448208long double cbrt( long double );
    83458209
    8346 float hypot( float, float );§\indexc{hypot}§
     8210float hypot( float, float );$\indexc{hypot}$
    83478211double hypot( double, double );
    83488212long double hypot( long double, long double );
     
    83548218\leavevmode
    83558219\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    8356 float sin( float );§\indexc{sin}§
     8220float sin( float );$\indexc{sin}$
    83578221double sin( double );
    83588222long double sin( long double );
     
    83618225long double _Complex sin( long double _Complex );
    83628226
    8363 float cos( float );§\indexc{cos}§
     8227float cos( float );$\indexc{cos}$
    83648228double cos( double );
    83658229long double cos( long double );
     
    83688232long double _Complex cos( long double _Complex );
    83698233
    8370 float tan( float );§\indexc{tan}§
     8234float tan( float );$\indexc{tan}$
    83718235double tan( double );
    83728236long double tan( long double );
     
    83758239long double _Complex tan( long double _Complex );
    83768240
    8377 float asin( float );§\indexc{asin}§
     8241float asin( float );$\indexc{asin}$
    83788242double asin( double );
    83798243long double asin( long double );
     
    83828246long double _Complex asin( long double _Complex );
    83838247
    8384 float acos( float );§\indexc{acos}§
     8248float acos( float );$\indexc{acos}$
    83858249double acos( double );
    83868250long double acos( long double );
     
    83898253long double _Complex acos( long double _Complex );
    83908254
    8391 float atan( float );§\indexc{atan}§
     8255float atan( float );$\indexc{atan}$
    83928256double atan( double );
    83938257long double atan( long double );
     
    83968260long double _Complex atan( long double _Complex );
    83978261
    8398 float atan2( float, float );§\indexc{atan2}§
     8262float atan2( float, float );$\indexc{atan2}$
    83998263double atan2( double, double );
    84008264long double atan2( long double, long double );
    84018265
    84028266float atan( float, float ); §\C{// alternative name for atan2}§
    8403 double atan( double, double );§\indexc{atan}§
     8267double atan( double, double );$\indexc{atan}$
    84048268long double atan( long double, long double );
    84058269\end{cfa}
     
    84108274\leavevmode
    84118275\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    8412 float sinh( float );§\indexc{sinh}§
     8276float sinh( float );$\indexc{sinh}$
    84138277double sinh( double );
    84148278long double sinh( long double );
     
    84178281long double _Complex sinh( long double _Complex );
    84188282
    8419 float cosh( float );§\indexc{cosh}§
     8283float cosh( float );$\indexc{cosh}$
    84208284double cosh( double );
    84218285long double cosh( long double );
     
    84248288long double _Complex cosh( long double _Complex );
    84258289
    8426 float tanh( float );§\indexc{tanh}§
     8290float tanh( float );$\indexc{tanh}$
    84278291double tanh( double );
    84288292long double tanh( long double );
     
    84318295long double _Complex tanh( long double _Complex );
    84328296
    8433 float asinh( float );§\indexc{asinh}§
     8297float asinh( float );$\indexc{asinh}$
    84348298double asinh( double );
    84358299long double asinh( long double );
     
    84388302long double _Complex asinh( long double _Complex );
    84398303
    8440 float acosh( float );§\indexc{acosh}§
     8304float acosh( float );$\indexc{acosh}$
    84418305double acosh( double );
    84428306long double acosh( long double );
     
    84458309long double _Complex acosh( long double _Complex );
    84468310
    8447 float atanh( float );§\indexc{atanh}§
     8311float atanh( float );$\indexc{atanh}$
    84488312double atanh( double );
    84498313long double atanh( long double );
     
    84588322\leavevmode
    84598323\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    8460 float erf( float );§\indexc{erf}§
     8324float erf( float );$\indexc{erf}$
    84618325double erf( double );
    84628326long double erf( long double );
     
    84658329long double _Complex erf( long double _Complex );
    84668330
    8467 float erfc( float );§\indexc{erfc}§
     8331float erfc( float );$\indexc{erfc}$
    84688332double erfc( double );
    84698333long double erfc( long double );
     
    84728336long double _Complex erfc( long double _Complex );
    84738337
    8474 float lgamma( float );§\indexc{lgamma}§
     8338float lgamma( float );$\indexc{lgamma}$
    84758339double lgamma( double );
    84768340long double lgamma( long double );
     
    84798343long double lgamma( long double, int * );
    84808344
    8481 float tgamma( float );§\indexc{tgamma}§
     8345float tgamma( float );$\indexc{tgamma}$
    84828346double tgamma( double );
    84838347long double tgamma( long double );
     
    85258389unsigned long long int ceiling( unsigned long long int n, unsigned long long int align );
    85268390
    8527 float floor( float );§\indexc{floor}§
     8391float floor( float );$\indexc{floor}$
    85288392double floor( double );
    85298393long double floor( long double );
    85308394
    8531 float ceil( float );§\indexc{ceil}§
     8395float ceil( float );$\indexc{ceil}$
    85328396double ceil( double );
    85338397long double ceil( long double );
    85348398
    8535 float trunc( float );§\indexc{trunc}§
     8399float trunc( float );$\indexc{trunc}$
    85368400double trunc( double );
    85378401long double trunc( long double );
    85388402
    8539 float rint( float );§\indexc{rint}§
     8403float rint( float );$\indexc{rint}$
    85408404long double rint( long double );
    85418405long int rint( float );
     
    85468410long long int rint( long double );
    85478411
    8548 long int lrint( float );§\indexc{lrint}§
     8412long int lrint( float );$\indexc{lrint}$
    85498413long int lrint( double );
    85508414long int lrint( long double );
     
    85538417long long int llrint( long double );
    85548418
    8555 float nearbyint( float );§\indexc{nearbyint}§
     8419float nearbyint( float );$\indexc{nearbyint}$
    85568420double nearbyint( double );
    85578421long double nearbyint( long double );
    85588422
    8559 float round( float );§\indexc{round}§
     8423float round( float );$\indexc{round}$
    85608424long double round( long double );
    85618425long int round( float );
     
    85668430long long int round( long double );
    85678431
    8568 long int lround( float );§\indexc{lround}§
     8432long int lround( float );$\indexc{lround}$
    85698433long int lround( double );
    85708434long int lround( long double );
     
    85798443\leavevmode
    85808444\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    8581 float copysign( float, float );§\indexc{copysign}§
     8445float copysign( float, float );$\indexc{copysign}$
    85828446double copysign( double, double );
    85838447long double copysign( long double, long double );
    85848448
    8585 float frexp( float, int * );§\indexc{frexp}§
     8449float frexp( float, int * );$\indexc{frexp}$
    85868450double frexp( double, int * );
    85878451long double frexp( long double, int * );
    85888452
    8589 float ldexp( float, int );§\indexc{ldexp}§
     8453float ldexp( float, int );$\indexc{ldexp}$
    85908454double ldexp( double, int );
    85918455long double ldexp( long double, int );
    85928456
    8593 [ float, float ] modf( float );§\indexc{modf}§
     8457[ float, float ] modf( float );$\indexc{modf}$
    85948458float modf( float, float * );
    85958459[ double, double ] modf( double );
     
    85988462long double modf( long double, long double * );
    85998463
    8600 float nextafter( float, float );§\indexc{nextafter}§
     8464float nextafter( float, float );$\indexc{nextafter}$
    86018465double nextafter( double, double );
    86028466long double nextafter( long double, long double );
    86038467
    8604 float nexttoward( float, long double );§\indexc{nexttoward}§
     8468float nexttoward( float, long double );$\indexc{nexttoward}$
    86058469double nexttoward( double, long double );
    86068470long double nexttoward( long double, long double );
    86078471
    8608 float scalbn( float, int );§\indexc{scalbn}§
     8472float scalbn( float, int );$\indexc{scalbn}$
    86098473double scalbn( double, int );
    86108474long double scalbn( long double, int );
    86118475
    8612 float scalbln( float, long int );§\indexc{scalbln}§
     8476float scalbln( float, long int );$\indexc{scalbln}$
    86138477double scalbln( double, long int );
    86148478long double scalbln( long double, long int );
     
    88708734All \newterm{pseudo random-number generators} (\newterm{PRNG}) involve some technique to scramble bits of a value, \eg multiplicative recurrence:
    88718735\begin{cfa}
    8872 rand = 33967 * (rand + 1063); // scramble bits
     8736rand = 36973 * (rand & 65535) + (rand >> 16); // scramble bits
    88738737\end{cfa}
    88748738Multiplication of large values adds new least-significant bits and drops most-significant bits.
     
    88778741bits 63--32 (most)      & bits 31--0 (least)    \\
    88788742\hline
    8879 ©0x0©                           & ©0x3e8e36©                    \\
    8880 ©0x5f©                          & ©0x718c25e1©                  \\
    8881 ©0xad3e©                        & ©0x7b5f1dbe©                  \\
    8882 ©0xbc3b©                        & ©0xac69ff19©                  \\
    8883 ©0x1070f©                       & ©0x2d258dc6©
     87430x0                                     & 0x3e8e36                              \\
     87440x5f                            & 0x718c25e1                    \\
     87450xad3e                          & 0x7b5f1dbe                    \\
     87460xbc3b                          & 0xac69ff19                    \\
     87470x1070f                         & 0x2d258dc6                    \\
    88848748\end{tabular}
    88858749\end{quote}
     
    88878751The least-significant bits \emph{appear} random but the same bits are always generated given a fixed starting value, called the \newterm{seed} (value 0x3e8e36 above).
    88888752Hence, if a program uses the same seed, the same sequence of pseudo-random values is generated from the PRNG.
    8889 Often the seed is set to another random value like a program's process identifier (\Indexc{getpid}) or time when the program is run;
     8753Often the seed is set to another random value like a program's process identifier (©getpid©\index{getpid@©getpid©}) or time when the program is run;
    88908754hence, one random value bootstraps another.
    88918755Finally, a PRNG usually generates a range of large values, \eg ©[0, UINT_MAX]©, which are scaled using the modulus operator, \eg ©prng() % 5© produces random values in the range 0--4.
    88928756
    8893 \CFA provides 32/64-bit sequential PRNG type only accessible by a single thread (not thread-safe) and a set of global routines and companion thread PRNG functions accessible by multiple threads without contention.
    8894 To use the PRNG interface \Textbf{requires including \Indexc{stdlib.hfa}}.
     8757\CFA provides a sequential PRNG type only accessible by a single thread (not thread-safe) and a set of global and companion thread PRNG functions accessible by multiple threads without contention.
    88958758\begin{itemize}
    88968759\item
    8897 The ©PRNG© types for sequential programs, including coroutining, are:
    8898 \begin{cfa}
    8899 struct PRNG32 {};                                               §\C{// opaque type, no copy or assignment}§
    8900 void ?{}( PRNG32 & prng, uint32_t seed ); §\C{// fixed seed}§
    8901 void ?{}( PRNG32 & prng );                              §\C{// random seed}§
    8902 void set_seed( PRNG32 & prng, uint32_t seed ); §\C{// set seed}§
    8903 uint32_t get_seed( PRNG32 & prng );             §\C{// get seed}§
    8904 uint32_t prng( PRNG32 & prng );                 §\C{// [0,UINT\_MAX]}§
    8905 uint32_t prng( PRNG32 & prng, uint32_t u ); §\C{// [0,u)}§
    8906 uint32_t prng( PRNG32 & prng, uint32_t l, uint32_t u ); §\C{// [l,u]}§
    8907 uint32_t calls( PRNG32 & prng );                §\C{// number of calls}§
    8908 void copy( PRNG32 & dst, PRNG32 & src ); §\C{// checkpoint PRNG state}§
    8909 \end{cfa}
    8910 \begin{cfa}
    8911 struct PRNG64 {};                                               §\C{// opaque type, no copy or assignment}§
    8912 void ?{}( PRNG64 & prng, uint64_t seed ); §\C{// fixed seed}§
    8913 void ?{}( PRNG64 & prng );                              §\C{// random seed}§
    8914 void set_seed( PRNG64 & prng, uint64_t seed ); §\C{// set seed}§
    8915 uint64_t get_seed( PRNG64 & prng );             §\C{// get seed}§
    8916 uint64_t prng( PRNG64 & prng );                 §\C{// [0,UINT\_MAX]}§
    8917 uint64_t prng( PRNG64 & prng, uint64_t u ); §\C{// [0,u)}§
    8918 uint64_t prng( PRNG64 & prng, uint64_t l, uint64_t u ); §\C{// [l,u]}§
    8919 uint64_t calls( PRNG64 & prng );                §\C{// number of calls}§
    8920 void copy( PRNG64 & dst, PRNG64 & src ); §\C{// checkpoint PRNG state}§
    8921 \end{cfa}
    8922 The type ©PRNG© is aliased to ©PRNG64© on 64-bit architectures and ©PRNG32© on 32-bit architectures.
     8760The ©PRNG© type is for sequential programs, like coroutining:
     8761\begin{cfa}
     8762struct PRNG { ... }; $\C[3.75in]{// opaque type}$
     8763void ?{}( PRNG & prng ); §\C{// random seed}§
     8764void ?{}( PRNG & prng, uint32_t seed ); §\C{// fixed seed}§
     8765void set_seed( PRNG & prng, uint32_t seed ); §\C{// set seed}§
     8766uint32_t get_seed( PRNG & prng ); §\C{// get seed}§
     8767uint32_t prng( PRNG & prng ); §\C{// [0,UINT\_MAX]}§
     8768uint32_t prng( PRNG & prng, uint32_t u ); §\C{// [0,u)}§
     8769uint32_t prng( PRNG & prng, uint32_t l, uint32_t u ); §\C{// [l,u]}§
     8770uint32_t calls( PRNG & prng ); §\C{// number of calls}\CRT§
     8771\end{cfa}
    89238772A ©PRNG© object is used to randomize behaviour or values during execution, \eg in games, a character makes a random move or an object takes on a random value.
    89248773In this scenario, it is useful to have multiple ©PRNG© objects, \eg one per player or object.
    8925 However, sequential execution is still repeatable given the same starting seeds for all ©PRNG©s.
     8774However, sequential execution is still repeatable given the same starting seeds for all ©PRNG©s. 
    89268775\VRef[Figure]{f:SequentialPRNG} shows an example that creates two sequential ©PRNG©s, sets both to the same seed (1009), and illustrates the three forms for generating random values, where both ©PRNG©s generate the same sequence of values.
    8927 Note, to prevent accidental PRNG copying, the copy constructor and assignment are hidden.
    8928 To copy a PRNG for checkpointing, use the explicit ©copy© member.
    89298776
    89308777\begin{figure}
    89318778\begin{cfa}
    8932 PRNG sprng1, sprng2;                                    §\C{// select appropriate 32/64-bit PRNG}§
    8933 ®set_seed( sprng1, 1009 )®;   ®set_seed( sprng2, 1009 )®;
     8779PRNG prng1, prng2;
     8780®set_seed( prng1, 1009 )®;   ®set_seed( prng2, 1009 )®;
    89348781for ( 10 ) {
    89358782        // Do not cascade prng calls because side-effect functions called in arbitrary order.
    8936         sout | nlOff | ®prng( sprng1 )®;  sout | ®prng( sprng1, 5 )®;  sout | ®prng( sprng1, 0, 5 )® | '\t';
    8937         sout | ®prng( sprng2 )®;  sout | ®prng( sprng2, 5 )®;  sout | ®prng( sprng2, 0, 5 )® | nlOn;
     8783        sout | nlOff | ®prng( prng1 )®;  sout | ®prng( prng1, 5 )®;  sout | ®prng( prng1, 0, 5 )® | '\t';
     8784        sout | ®prng( prng2 )®;  sout | ®prng( prng2, 5 )®;  sout | ®prng( prng2, 0, 5 )® | nlOn;
    89388785}
    89398786\end{cfa}
     
    89748821The PRNG global and companion thread functions are for concurrent programming, such as randomizing execution in short-running programs, \eg ©yield( prng() % 5 )©.
    89758822\begin{cfa}
    8976 void set_seed( size_t seed );                   §\C{// set global seed}§
    8977 size_t get_seed();                                              §\C{// get global seed}§
    8978 // SLOWER, global routines
    8979 size_t prng( void );                                    §\C{// [0,UINT\_MAX]}§
    8980 size_t prng( size_t u );                                §\C{// [0,u)}§
    8981 size_t prng( size_t l, size_t u );              §\C{// [l,u]}§
    8982 // FASTER, thread members
    8983 size_t prng( §thread\LstStringStyle{\textdollar}§ & th );       §\C{// [0,UINT\_MAX]}§
    8984 size_t prng( §thread\LstStringStyle{\textdollar}§ & th, size_t u );     §\C{// [0,u)}§
    8985 size_t prng( §thread\LstStringStyle{\textdollar}§ & th, size_t l, size_t u );   §\C{// [l,u]}§
     8823void set_seed( uint32_t seed ); $\C[3.75in]{// set global seed}$
     8824uint32_t get_seed(); §\C{// get global seed}§
     8825// SLOWER
     8826uint32_t prng(); §\C{// [0,UINT\_MAX]}§
     8827uint32_t prng( uint32_t u ); §\C{// [0,u)}§
     8828uint32_t prng( uint32_t l, uint32_t u ); §\C{// [l,u]}§
     8829// FASTER
     8830uint32_t prng( $thread\LstStringStyle{\textdollar}$ & th );     §\C{// [0,UINT\_MAX]}§
     8831uint32_t prng( $thread\LstStringStyle{\textdollar}$ & th, uint32_t u ); §\C{// [0,u)}§
     8832uint32_t prng( $thread\LstStringStyle{\textdollar}$ & th, uint32_t l, uint32_t u );     §\C{// [l,u]}\CRT§
    89868833\end{cfa}
    89878834The only difference between the two sets of ©prng© routines is performance.
     
    89948841Hence, these threads generate different sequences of random numbers.
    89958842If each thread needs its own seed, use a sequential ©PRNG© in each thread.
    8996 The slower ©prng© global functions, \ie \emph{without} a thread argument, call ©active_thread© internally to indirectly access the current thread's PRNG state, while the faster ©prng© functions, \ie \emph{with} a thread argument, directly access the thread through the thread parameter.
     8843The slower ©prng© functions \emph{without} a thread argument call ©active_thread© internally to indirectly access the current thread's PRNG state, while the faster ©prng© functions \emph{with} a thread argument directly access the thread through the thread parameter.
    89978844If a thread pointer is available, \eg in thread main, eliminating the call to ©active_thread© significantly reduces the cost of accessing the thread's PRNG state.
    89988845\VRef[Figure]{f:ConcurrentPRNG} shows an example using the slower/faster concurrent PRNG in the program main and a thread.
     
    90098856int main() {
    90108857        set_seed( 1009 );
    9011         §\R{thread\LstStringStyle{\textdollar}}§ ®& th = *active_thread()®;  // program-main thread-address
     8858        $\R{thread\LstStringStyle{\textdollar}}$ ®& th = *active_thread()®;  // program-main thread-address
    90128859        for ( i; 10 ) {
    90138860                sout | nlOff | ®prng()®; sout | ®prng( 5 )®; sout | ®prng( 0, 5 )® | '\t';  // SLOWER
     
    92329079\hline
    92339080\begin{cfa}
    9234 #include <gmp.h>§\indexc{gmp.h}§
     9081#include <gmp.h>$\indexc{gmp.h}$
    92359082int main( void ) {
    92369083        ®gmp_printf®( "Factorial Numbers\n" );
     
    92469093&
    92479094\begin{cfa}
    9248 #include <gmp.hfa>§\indexc{gmp}§
     9095#include <gmp.hfa>$\indexc{gmp}$
    92499096int main( void ) {
    92509097        sout | "Factorial Numbers";
     
    93189165\begin{cfa}[belowskip=0pt]
    93199166// implementation
    9320 struct Rational {§\indexc{Rational}§
     9167struct Rational {$\indexc{Rational}$
    93219168        long int numerator, denominator; §\C{// invariant: denominator > 0}§
    93229169}; // Rational
Note: See TracChangeset for help on using the changeset viewer.