Ignore:
Timestamp:
Apr 1, 2025, 9:20:48 AM (6 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
ee70ff5
Parents:
4223317
Message:

more string API updates

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/mike_brooks_MMath/string.tex

    r4223317 rf8913b7c  
    5959A \CFA string manages its length separately from the string, so there is no null (@'\0'@) terminating value at the end of a string value.
    6060Hence, a \CFA string cannot be passed to a C string manipulation routine, such as @strcat@.
    61 Like C strings, the characters in a @string@ are numbered starting from 0.
    62 
     61Like C strings, characters in a @string@ are numbered from the left starting at 0, and in \CFA numbered from the right starting at -1.
     62\begin{cquote}
     63\sf
     64\begin{tabular}{@{}rrrrrl@{}}
     65\small\tt a & \small\tt b & \small\tt c & \small\tt d & \small\tt e \\
     660 & 1 & 2 & 3 & 4 & left to right index \\
     67-5 & -4 & -3 & -2 & -1 & right to left index
     68\end{tabular}
     69\end{cquote}
    6370The following operations have been defined to manipulate an instance of type @string@.
    6471The discussion assumes the following declarations and assignment statements are executed.
     
    123130        s = 'x';
    124131        s = "abc";
    125         char cs[5] = "abc";
     132        char cs[] = "abc";
    126133        s = cs;
    127134        // conversion of integral, floating-point, and complex to string
     
    269276A negative length means that characters are selected in the opposite (right to left) direction from the starting position.
    270277If the substring request extends beyond the beginning or end of the string, it is clipped (shortened) to the bounds of the string.
    271 If the substring request is completely outside of the original string, a null string located at the end of the original string is returned.
    272 
    273 The substring operation can also appear on the left hand side of the assignment operator.
    274 The substring is replaced by the value on the right hand side of the assignment.
    275 The 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.
    276 \begin{cfa}
    277 digit( 3, 3 ) = "";                             $\C{// digit is assigned "0156789"}$
    278 digit( 4, 3 ) = "xyz";                          $\C{// digit is assigned "015xyz9"}$
    279 digit( 7, 0 ) = "***";                          $\C{// digit is assigned "015xyz***9"}$
    280 digit(-4, 3 ) = "$\tt\$\$\$$";          $\C{// digit is assigned "015xyz\$\$\$9"}$
    281 \end{cfa}
     278If the substring request is completely outside of the original string, a null string is returned.
     279
     280The substring operation can also appear on the left side of an assignment and replaced by the string value on the right side.
     281The length of the right string may be shorter, the same length, or longer than the length of left string.
     282Hence, the left string may decrease, stay the same, or increase in length.
     283\begin{cquote}
     284\setlength{\tabcolsep}{15pt}
     285\begin{tabular}{@{}l|l@{}}
     286\begin{cfa}
     287digit( 3, 3 ) = "";
     288digit( 4, 3 ) = "xyz";
     289digit( 7, 0 ) = "***";
     290digit(-4, 3 ) = "$\tt\$\$\$$";
     291\end{cfa}
     292&
     293\begin{cfa}
     2940126789
     2950126xyz
     2960126xyz
     297012$\$\$\$$z
     298\end{cfa}
     299\end{tabular}
     300\end{cquote}
    282301A substring is treated as a pointer into the base (substringed) string rather than creating a copy of the subtext.
    283 As with all pointers, if the item they are pointing at is changed, then the pointer is referring to the changed item.
     302Hence, if the referenced item is changed, then the pointer sees the change.
    284303Pointers to the result value of a substring operation are defined to always start at the same location in their base string as long as that starting location exists, independent of changes to themselves or the base string.
    285304However, if the base string value changes, this may affect the values of one or more of the substrings to that base string.
     
    315334\begin{cfa}
    316335s = name( 2 );                                          $\C{// s is assigned "ETER"}$
    317 name( 2 ) = "IPER";                             $\C{// name is assigned "PIPER"}$
     336name( 2 ) = "IPER";                                     $\C{// name is assigned "PIPER"}$
    318337\end{cfa}
    319338It is also possible to substring using a string as the index for selecting the substring portion of the string.
     
    332351The @index@ operation
    333352\begin{cfa}
    334 int index( const string &key, int start = 1, occurrence occ = first );
     353int index( const string & key, int start = 1, occurrence occ = first );
    335354\end{cfa}
    336355returns the position of the first or last occurrence of the @key@ (depending on the occurrence indicator @occ@ that is either @first@ or @last@) in the current string starting the search at position @start@.
     
    338357%If the @key@ has zero length, the value 1 is returned regardless of what the current string contains.
    339358A negative starting position is a specification from the right end of the string.
    340 \begin{cfa}
    341 i = digit.index( "567" );                       $\C{// i is assigned 3}$
    342 i = digit.index( "567", 7 );            $\C{// i is assigned 11}$
    343 i = digit.index( "567", -1, last );     $\C{// i is assigned 3}$
    344 i = name.index( "E", 5, last ); $\C{// i is assigned 4}$
    345 \end{cfa}
    346 
     359\begin{cquote}
     360\setlength{\tabcolsep}{15pt}
     361\begin{tabular}{@{}l|l@{}}
     362\begin{cfa}
     363i = find( digit, "567" );
     364i = find( digit, "567", 7 );
     365i = digit.index( "567", -1, last );
     366i = name.index( "E", 5, last );
     367\end{cfa}
     368&
     369\begin{cfa}
     3705
     37110
     372
     373
     374\end{cfa}
     375\end{tabular}
     376\end{cquote}
    347377The next two string operations test a string to see if it is or is not composed completely of a particular class of characters.
    348378For example, are the characters of a string all alphabetic or all numeric?
Note: See TracChangeset for help on using the changeset viewer.