/** *AbstractclassthatdefinesanAPIforiteration *ontextobjects. *Thisisaninterfaceforforwardandbackwarditeration *andrandomaccessintoatextobject. * *<p>TheAPIprovidesbackwardcompatibilitytotheJavaandolderICU *CharacterIteratorclassesbutextendsthemsignificantly: *<ol> *<li>CharacterIteratorisnowasubclassofForwardCharacterIterator.</li> *<li>WhiletheoldAPIfunctionsprovidedforwarditerationwith *"pre-increment"semantics,thenewonealsoprovidesfunctions *with"post-increment"semantics.Theyaremoreefficientandshould *bethepreferrediteratorfunctionsfornewimplementations. *Thebackwarditerationalwayshad"pre-decrement"semantics,which *areefficient.</li> *<li>JustlikeForwardCharacterIterator,itprovidesaccessto *bothcodeunitsandcodepoints.Codepointaccessversionsareavailable *fortheoldandthenewiterationsemantics.</li> *<li>Therearenewfunctionsforsettingandmovingthecurrentposition *withoutreturningacharacter,forefficiency.</li> *</ol> * *SeeForwardCharacterIteratorforexamplesforusingthenewforwarditeration *functions.Forbackwarditeration,thereisalsoahasPrevious()function *thatcanbeusedanalogouslytohasNext(). *Theoldfunctionsworkasbeforeandareshownbelow.</p> * *<p>Examplesforsomeofthenewfunctions:</p> * *ForwarditerationwithhasNext(): *\code *voidforward1(CharacterIterator&it){ *UChar32c; *for(it.setToStart();it.hasNext();){ *c=it.next32PostInc(); *// use c *} *} *\endcode *Forwarditerationmoresimilartoloopswiththeoldforwarditeration, *showingawaytoconvertsimplefor()loops: *\code *voidforward2(CharacterIterator&it){ *char16_tc; *for(c=it.firstPostInc();c!=CharacterIterator::DONE;c=it.nextPostInc()){ *// use c *} *} *\endcode *BackwarditerationwithsetToEnd()andhasPrevious(): *\code *voidbackward1(CharacterIterator&it){ *UChar32c; *for(it.setToEnd();it.hasPrevious();){ *c=it.previous32(); *// use c *} *} *\endcode *Backwarditerationwithamoretraditionalfor()loop: *\code *voidbackward2(CharacterIterator&it){ *char16_tc; *for(c=it.last();c!=CharacterIterator::DONE;c=it.previous()){ *// use c *} *} *\endcode * *Exampleforrandomaccess: *\code *voidrandom(CharacterIterator&it){ *// set to the third code point from the beginning *it.move32(3,CharacterIterator::kStart); *// get a code point from here without moving the position *UChar32c=it.current32(); *// get the position *int32_tpos=it.getIndex(); *// get the previous code unit *char16_tu=it.previous(); *// move back one more code unit *it.move(-1,CharacterIterator::kCurrent); *// set the position back to where it was *// and read the same code point c and move beyond it *it.setIndex(pos); *if(c!=it.next32PostInc()){ *exit(1);// CharacterIterator inconsistent *} *} *\endcode * *<p>Examples,especiallyfortheoldAPI:</p> * *Functionprocessingcharacters,inthisexamplesimpleoutput *<pre> *\code *voidprocessChar(char16_tc) *{ *cout<<""<<c; *} *\endcode *</pre> *Traversethetextfromstarttofinish *<pre> *\code *voidtraverseForward(CharacterIterator&iter) *{ *for(char16_tc=iter.first();c!=CharacterIterator::DONE;c=iter.next()){ *processChar(c); *} *} *\endcode *</pre> *Traversethetextbackwards,fromendtostart *<pre> *\code *voidtraverseBackward(CharacterIterator&iter) *{ *for(char16_tc=iter.last();c!=CharacterIterator::DONE;c=iter.previous()){ *processChar(c); *} *} *\endcode *</pre> *Traversebothforwardandbackwardfromagivenpositioninthetext. *CallstonotBoundary()inthisexamplerepresentssomeadditionalstoppingcriteria. *<pre> *\code *voidtraverseOut(CharacterIterator&iter,int32_tpos) *{ *char16_tc; *for(c=iter.setIndex(pos); *c!=CharacterIterator::DONE&&(Unicode::isLetter(c)||Unicode::isDigit(c)); *c=iter.next()){} *int32_tend=iter.getIndex(); *for(c=iter.setIndex(pos); *c!=CharacterIterator::DONE&&(Unicode::isLetter(c)||Unicode::isDigit(c)); *c=iter.previous()){} *int32_tstart=iter.getIndex()+1; * *cout<<"start:"<<start<<"end:"<<end<<endl; *for(c=iter.setIndex(start);iter.getIndex()<end;c=iter.next()){ *processChar(c); *} *} *\endcode *</pre> *CreatingaStringCharacterIteratorandcallingthetestfunctions *<pre> *\code *voidCharacterIterator_Example(void) *{ *cout<<endl<<"=====CharacterIterator_Example:====="<<endl; *UnicodeStringtext("EinkleinerSatz."); *StringCharacterIteratoriterator(text); *cout<<"-----traverseForward:-----------"<<endl; *traverseForward(iterator); *cout<<endl<<endl<<"-----traverseBackward:----------"<<endl; *traverseBackward(iterator); *cout<<endl<<endl<<"-----traverseOut:---------------"<<endl; *traverseOut(iterator,7); *cout<<endl<<endl<<"-----"<<endl; *} *\endcode *</pre> * *@stableICU2.0
*/ class U_COMMON_API CharacterIterator : public ForwardCharacterIterator { public: /** *Originenumerationforthemove()andmove32()functions. *@stableICU2.0
*/
enum EOrigin { kStart, kCurrent, kEnd };
/** *Movesthecurrentpositionrelativetothestartorendofthe *iterationrange,orrelativetothecurrentpositionitself. *Themovementisexpressedinnumbersofcodepointsforward *orbackwardbyspecifyingapositiveornegativedelta. *@paramdeltathepositionrelativetoorigin.Apositivedeltameansforward; *anegativedeltameansbackward. *@paramoriginOriginenumeration{kStart,kCurrent,kEnd} *@returnthenewposition *@stableICU2.0
*/ #ifdef move32 // One of the system headers right now is sometimes defining a conflicting macro we don't use #undef move32 #endif virtual int32_t move32(int32_t delta, EOrigin origin) = 0;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.