⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gstring.htm

📁 自编的一个String类
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<TD WIDTH="33%">return current value of iterator, but increment value ofiterator</TD><TD WIDTH="33%">return current value of iterator, but increment value ofiterator</TD></TR><TR><TD WIDTH="33%"><TT>i--</TT></TD><TD WIDTH="33%">return current value of iterator, but decrement value ofiterator</TD><TD WIDTH="33%">return current value of iterator, but decrement value ofiterator</TD></TR><TR><TD WIDTH="33%"><TT>*i</TT></TD><TD WIDTH="33%">return the string corresponding to the iterator</TD><TD WIDTH="33%">return the string corresponding to the iterator</TD></TR><TR><TD WIDTH="33%"><TT>i==j</TT></TD><TD WIDTH="33%">true if the iterators are the same</TD><TD WIDTH="33%">true if the iterators are the same</TD></TR><TR><TD WIDTH="33%"><TT>i!=j</TT></TD><TD WIDTH="33%">true if the iterators are different</TD><TD WIDTH="33%">true if the iterators are different</TD></TR><TR><TD WIDTH="33%"><TT>i-&gt;</TT></TD><TD WIDTH="33%"><TT>(*i).</TT></TD><TD WIDTH="33%"><TT>(*i).</TT></TD></TR></TABLE></DIV><P>&nbsp;</P><H2><EM><A NAME="GString"></A>GString</EM> classes</H2><P>These comprise the family of classes for describing a pattern that one mightmatch a string to.</P><P>A GString expression is a C++ expression involving any of the operators<TT>+</TT>, <TT>|</TT>, <TT>&amp;</TT>, <TT>^</TT>, <TT>~</TT>, <TT>&lt;</TT>;and <TT>&gt;</TT> and character strings, Strings and other GString objects.</P><P>For a string to match a GString expression the entire string must bematched. This is in contrast to just a segment as is the case with stringexpressions in SED. For example the GString expression</P><PRE>   DOTS + &quot;quick&quot; + DOTS</PRE><P>does match <EM>The quick brown fox</EM> whereas</P><PRE>   GS + &quot;quick&quot;</PRE><P>does not. In these expressions <TT>+</TT> means concatenate; <EM>DOTS</EM>will match any string; <EM>GS</EM> is included purely to make the C++ compilerrecognise the character string as a GString expression.</P><P>GString expressions typically occur in any of three places. In the statement</P><PRE>   SL(gs1).s(gs2,gs3);</PRE><P>where <EM>gs1</EM>, <EM>gs2</EM> and <EM>gs3</EM> are GString expressions,<EM>gs1</EM> determines which strings from the StringList <EM>SL</EM> areselected for editing; <EM>gs2</EM> (which may be the same as <EM>gs1</EM>)represents the target for editing and <EM>gs3</EM> determines the results ofthe editing. If <EM>gs2</EM> had failed to match there would have been noediting. In this statement <EM>gs2</EM> and <EM>gs3</EM> must have exactly thesame pattern of operators, with two exceptions described below. Suppose I wishto find all strings in SL which the word <EM>quick</EM> and change<EM>brown</EM> to <EM>black</EM> in these strings. Then I could use theexpression</P><PRE>   SL(DOTS + &quot;quick&quot; + DOTS).s(DOTS+&quot;brown&quot;+DOTS,                               DOTS+&quot;black&quot;+DOTS);</PRE><P>where I have put the <EM>gs3</EM> expression under the <EM>gs2</EM>expression to make sure the operators match. In this particular instance itwould have been simpler to use</P><PRE>   SL(&quot;quick&quot;).sa(&quot;brown&quot;,&quot;black&quot;)</PRE><P>where <EM>sa</EM> means <EM>substitute all</EM>. Where only simple stringsare involved, the program does follow the SED convention of requiring only thatthe search string be included rather than having an exact match of the wholestring.</P><P>Here is the list of GString classes, objects and operators.</P><DIV ALIGN="left"><TABLE BORDER="1" WIDTH="100%" style="border-collapse: collapse" bordercolor="#111111" cellpadding="0" cellspacing="1"><TR><TD WIDTH="50%" valign="top">gs.Match(s)</TD><TD WIDTH="50%" valign="top">true if GString <i>gs</i> matches a String <i>s</i></TD></TR><TR><TD WIDTH="50%" valign="top"><TT>A | B</TT></TD><TD WIDTH="50%" valign="top">matches if either <EM>A</EM> or <EM>B</EM> matches</TD></TR><TR><TD WIDTH="50%" valign="top"><TT>A &amp; B</TT></TD><TD WIDTH="50%" valign="top">matches if both <EM>A</EM> and <EM>B</EM> match</TD></TR><TR><TD WIDTH="50%" valign="top"><TT>A ^ B</TT></TD><TD WIDTH="50%" valign="top">matches of one but not both of <EM>A</EM> and <EM>B</EM>match</TD></TR><TR><TD WIDTH="50%" valign="top"><TT>A + B</TT></TD><TD WIDTH="50%" valign="top">matches if the target string can be divided into two parts; thefirst matching <EM>A</EM> and the second matching <EM>B</EM>.</TD></TR><TR><TD WIDTH="50%" valign="top"><TT>A &gt; B</TT></TD><TD WIDTH="50%" valign="top">same as + but first match <EM>A</EM>, then see if <EM>B</EM>matches</TD></TR><TR><TD WIDTH="50%" valign="top"><TT>A &lt; B</TT></TD><TD WIDTH="50%" valign="top">same as + but first match <EM>B</EM>, then see if <EM>A</EM>matches</TD></TR><TR><TD WIDTH="50%" valign="top"><TT>~ A</TT></TD><TD WIDTH="50%" valign="top">matches if <EM>A</EM> does not match</TD></TR><TR><TD WIDTH="50%" valign="top"><TT>AnyString</TT></TD><TD WIDTH="50%" valign="top">class derived from GString: match any string (including a zero length string); remember thestring that is matched</TD></TR><TR><TD WIDTH="50%" valign="top"><TT>LongestString</TT></TD><TD WIDTH="50%" valign="top">class derived from GString: match any string but try to find the longest possibility;remember the string that is matched</TD></TR><TR><TD WIDTH="50%" valign="top"><TT>ShortestString</TT></TD><TD WIDTH="50%" valign="top">class derived from GString: match any string but try to find the shortest possibility;remember the string that is matched</TD></TR><TR><TD WIDTH="50%" valign="top"><TT>FixedLengthString(int n)</TT></TD><TD WIDTH="50%" valign="top">class derived from GString: match any string of length <EM>n</EM>, remember the string thatis matched</TD></TR><TR><TD WIDTH="50%" valign="top"><TT>.Value()</TT></TD><TD WIDTH="50%" valign="top">get string stored by any of previous 4 classes</TD></TR><TR><TD WIDTH="50%" valign="top"><TT>Dots</TT></TD><TD WIDTH="50%" valign="top">class derived from GString: match any string (including a zero length string)</TD></TR><TR><TD WIDTH="50%" valign="top"><TT>LongestDots</TT></TD><TD WIDTH="50%" valign="top">class derived from GString: match any string but try to find the longest possibility</TD></TR><TR><TD WIDTH="50%" valign="top"><TT>ShortestDots</TT></TD><TD WIDTH="50%" valign="top">class derived from GString: match any string but try to find the shortest possibility</TD></TR><TR><TD WIDTH="50%" valign="top"><TT>FixedLengthDots(int n)</TT></TD><TD WIDTH="50%" valign="top">class derived from GString: match any string of length <EM>n</EM></TD></TR><TR><TD WIDTH="50%" valign="top"><TT>WhiteSpace</TT></TD><TD WIDTH="50%" valign="top">class derived from GString: match white space - try to match as much as possible (lengthmust be greater than zero)</TD></TR><TR><TD WIDTH="50%" valign="top"><TT>OptionalWhiteSpace</TT></TD><TD WIDTH="50%" valign="top">class derived from GString: match white space - try to match as much as possible (lengthmay be zero)</TD></TR><TR><TD WIDTH="50%" valign="top"><TT>InitialGString</TT></TD><TD WIDTH="50%" valign="top">A dummy class to enable the C++ compiler to recognise acharacter string or a String as a GString object</TD></TR><TR><TD WIDTH="50%" valign="top"><TT>DOTS</TT></TD><TD WIDTH="50%" valign="top">A globally declared Dots object</TD></TR><TR><TD WIDTH="50%" valign="top"><TT>LDOTS</TT></TD><TD WIDTH="50%" valign="top">A globally declared LongestDots object</TD></TR><TR><TD WIDTH="50%" valign="top"><TT>SDOTS</TT></TD><TD WIDTH="50%" valign="top">A globally declared ShortestDots object</TD></TR><TR><TD WIDTH="50%" valign="top"><TT>DOT</TT></TD><TD WIDTH="50%" valign="top">A globally declared FixedLengthDots object with length 1</TD></TR><TR><TD WIDTH="50%" valign="top"><TT>WS</TT></TD><TD WIDTH="50%" valign="top">A globally declared WhiteSpace object</TD></TR><TR><TD WIDTH="50%" valign="top"><TT>OWS</TT></TD><TD WIDTH="50%" valign="top">A globally declared OptionalWhiteSpace object</TD></TR><TR><TD WIDTH="50%" valign="top"><TT>GS</TT></TD><TD WIDTH="50%" valign="top">A globally declared InitialGString object</TD></TR></TABLE></DIV><P>The <EM>objects</EM> constructed by LongestString, ShortestString, andFixedLengthString will match any string but remember the string that theymatched. This is returned in the editing phase of the program. If there is anyambiguity in the way the match is carried out LongestString will try to matchthe longest possible string and ShortestString will try to match the shortestpossible string. (The rules governing LongestString andShortestString need more refining).</P><P>Use <EM>DOTS</EM> to match any string. Use <EM>LDOTS</EM> or <EM>SDOT</EM>to remove ambiguities by matching the longest or shortest possible string.</P><P>Use <EM>DOT</EM> to match a single character.</P><P>The <EM>GS</EM> object should be used when a String object or a characterstring needs to be converted to a GString object and the C++ syntax rules don'tdo this automatically. For example,</P><PRE>   GS | &quot;cat&quot; | &quot;dog&quot;</PRE><P>means <EM>cat</EM> or <EM>dog</EM>,</P><PRE>   GS + &quot;<EM style="font-style: normal">The</EM>&quot; + DOTS + &quot;<EM style="font-style: normal">dog</EM>&quot; + DOTS</PRE><P>means <EM>The</EM> (or <EM>There</EM>, <EM>Then</EM> etc) at the beginningof the string and <EM>dog</EM> somewhere within the string. If the <EM>GS</EM>is left out you will get a C++ syntax error. The need for this trick is one ofthe downsides of embedding the editor in C++. At the moment <tt>+</tt>,<tt>|</tt>, <tt>&amp;</tt>, etc are implemented as member functions so you need a <i>GS</i> as shown in the preceding expressions and also in expressions such as</P><pre>   DOTS + &quot;one&quot; &gt; GS + &quot;two&quot; + DOTS</pre><p>In the final editing stage the values returned to the string are determinedby the second argument (<EM>gs3</EM>) of <EM>s</EM>. <EM>DOTS</EM> means copyfrom the corresponding part of the string before editing. A String object or acharacter string or&nbsp;means replace the corresponding text in the originaltext with this text. A LongestString, ShortestString, and FixedLengthStringobject means copy the text remembered by that object. In each case where thereare alternative matching strings in the GString expression only those stringsleading to the match are copied.</p><P>It doesn't seem possible to track expressions preceded by <TT>~</TT> in asensible way. So I don't try to do this. The whole <TT>~</TT> clause, includingthe <TT>~</TT> can be replaced by a single character string or by <EM>DOTS</EM>(if we want to copy the original string).</P><P>Some <A HREF="#Examples">examples</A> are given at the end of this document.</P><h4>Concatenated GStrings:</h4><P>Try to avoid concatenated sequences using <tt>+</tt> with more than 3objects of unknown length. Otherwise the speed may be very slow. If possibleuse <TT>&lt;</TT> or <TT>&gt;</TT> in place of <TT>+</TT> to improve speed. That is use</P><pre>   A &gt; B &gt; C &gt; D</pre><p>or</p><pre>   A &lt; B &lt; C &lt; D</pre><p>in preference to</p><pre>   A + B + C + D</pre><p>where <tt>A</tt>, <tt>B</tt>, <tt>C</tt> and <tt>D</tt> are <i>GStrings</i> and we are trying to match the concatenation of <tt>A</tt>, <tt>B</tt>, <tt>C</tt> and <tt>D</tt>. If the length of these <i>GStrings</i> can't be determined in advance the expression with <tt>+</tt> will search all possible combinations of the lengths until a match is found. This can require an excessive search time. If the expression with <TT>&gt;</TT> is used, then <tt>A</tt> is matched, scanning over possible lengths of <tt>A</tt>. If successful then <tt>B</tt> is matched and so on. If the expression with <tt>&lt;</tt> is used then the search starts with <tt>D</tt>. These will tend to be faster but with the possibility of missing a match. It is a good idea to have a string that is matched explicitly (as opposed to <i>DOTS</i>, for example) immediately to the left of each <tt>&gt;</tt> or to the right of each <tt>&lt;</tt>. For example,</p><pre>   GS + &quot;The&quot;  &gt;  DOTS + &quot;dog&quot;  &gt;  DOTS + &quot;.&quot;</pre><p>should be used in preference to</p><pre>   GS + &quot;The&quot; + DOTS  &gt;  GS + &quot;dog&quot; + DOTS  &gt;  &quot;.&quot;</pre><p>The second version will not match <i>DOTS</i> correctly and the match will almost certainly fail.</p><h4>Longest and shortest strings:</h4><p>The&nbsp;direction of the search (from left to right or right to left) which matching an expression with +, &lt; or &gt; is determined by the use <i>ShortestString</i>,<i>LongestString</i>, <i>SDOTS</i> or <i>LDOTS</i> objects. <i>WS</i> and <i>OWS</i> counts as a longest object when determining the direction of the search. It is easy to set up situations where the results are unpredictable. This is 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -