When I started Symbian, I made a string processing function. At that time, I used tdesc8 mid find and other functions to implement it. The code is redundant and inefficient,
Later, I came into contact with the tlex8 class and found that the string processing efficiency is good and the code page is concise!
BTW!
1.
Void string2number (){
Tlex8 str2num;
Tint number;
Tbuf8 <16> string (_ L8 ("1024 "));
Str2num. Assign (string );
Str2num. Val (number );
}
2.
Void getmarkedtoken (){
_ Lit8 (ksomeconst, "ABC | XYZ | 123 | ");
Tlex8 Lex (ksomeconst );
Tchar ch;
While (CH = Lex. Peek ())! = '| ')
Lex. Inc ();
Tbuf8 <10> token;
Token. Copy (lex. markedtoken ());
}
3.
Void customseparator (){
// As an example taking "," As a separator.
_ Lit8 (ksomeconststring, "first, second, third, fourth ,");
Tlex8 Lex (ksomeconststring );
Tchar ch;
Tbuf8 <32> token;
While (CH = Lex. Get ())! = 0 ){
While (CH = Lex. Peek ())! = ',')
Lex. Inc ();
Token. Copy (lex. markedtoken ());
/* Now we have the string as the token,
* Do something ..*/
Lex. Inc ();
Lex. Mark ();
}
}
4.
Void gettingtokens (){
Cdesc16array * array = new (eleave) cdesc16arrayflat (10 );
_ Token (kdata, "this is the token example ."
"You will get different tokens in array .");
Tlex Lex (kdata );
Do {
Tptrc token = Lex. nexttoken ();
If (token. Length () = 0)
Break;
Array-> appendl (token );
} While (1 );
}
Add the tlex8 source code in e32std. h and us_lex8.cpp.
# Include "us_std.h"
Export_c tlex8: tlex8 ()
: Inext (null), ibuf (null), iend (null), IMARK (null)
/**
Default constructor.
Constructs a tlex8, initialising its members to null.
*/
{}
Export_c void tlex8: Assign (const tuint8 * astring)
/**
Assigns a string to this object from another string.
@ Param astring a pointer to a string to be assigned.
*/
{
IMARK. iptr = inext = ibuf = astring;
Iend = ibuf + User: stringlength (astring );
}
Export_c void tlex8: Inc (tint anumber)
/**
Increments the next character position by anumber.
@ Param anumber the number of characters to increment the next character position
By.
@ Panic user 60, if the increment puts the next character position before
Start or beyond the end of the string.
*/
{
Inext + = anumber;
_ Assert_always (inext >=ibuf & inext <= iend, panic (etlex8incoutofrange ));
}
Export_c tchar tlex8: Get ()
/**
Gets the next character in the string, and increments the next
Character position.
@ Return next character to be read.0 if at the end of the string.
*/
{
If (EOS ())
Return (0 );
Return (* inext ++ );
}
Export_c tchar tlex8: PEEK () const
/**
Shows the next character to be returned by get ().
@ Return character to be returned by the next call to get (). 0 if at
End of the string.
@ See tlex8: Get
*/
{
If (EOS ())
Return (0 );
Return (* inext );
}
Export_c void tlex8: skipspace ()
/**
Moves the next character position past any white space (space or separator ).
Stops if at the end of string.
*/
{
While (! EOS () & PEEK (). isspace ())
Inext ++;
}
Export_c tptrc8 tlex8: markedtoken () const
//
// Extract the internally marked token
// There is the assumption here that IMARK is always valid
/**
Extracts the marked token.
Note that the function assumes that the current extraction mark is valid.
@ Return extracted token.
@ Panic user 63, if the specified mark is before the start or beyond the end
Of the string.
*/
{
Return (tptrc8 (IMARK. iptr, tokenlength ()));
}
Export_c tint tlex8: Val (tint8 & Aval)
/**
Parses the string to extract a signed 8-bit integer.
@ Param Aval on return, contains the extracted integer.
@ Return kerrnone if successful.
Kerrgeneral if the next character position is initially at the end of the string
Or no valid characters found initially.
Kerroverflow if there is sign overflow, I. e. converted value greater than limit.
If error codes kerrgeneral or kerroverflow are returned, the object's
Members are left unaltered.
*/
{
Tint32 V;
Tint r = boundedval (v, 0x7fu );
If (r = kerrnone)
Aval = (tint8) V;
Return (R );
}
Class tlex8
{
Public:
Import_c tlex8 ();
Inline tlex8 (const tuint8 * astring );
Inline tlex8 (const tdesc8 & ades );
Inline tlex8 & operator = (const tuint8 * astring );
Inline tlex8 & operator = (const tdesc8 & ades );
Inline tbool EOS () const;
Inline void mark (tlexmark8 & amark) const;
Inline void mark ();
Import_c void Inc ();
Import_c void Inc (tint anumber );
Import_c tchar get ();
Import_c tchar PEEK () const;
Import_c void unget ();
Inline void ungettomark ();
Import_c void ungettomark (const tlexmark8 amark );
Import_c void skipspace ();
Import_c tint tokenlength (const tlexmark8 amark) const;
Import_c tptrc8 markedtoken () const;
Import_c tptrc8 markedtoken (const tlexmark8 amark) const;
Inline tint markedoffset () const;
Import_c tint markedoffset (const tlexmark8 amark) const;
Import_c tint Val (tint8 & Aval );
Import_c tint Val (tint16 & Aval );
Import_c tint Val (tint32 & Aval );
Import_c tint Val (tint64 & Aval );
Inline tint Val (tint & Aval );
Tint Val (trealx & Aval );
Tint Val (trealx & Aval, tchar apoint );
PRIVATE:
Const tuint8 * inext;
Const tuint8 * ibuf;
Const tuint8 * iend;
Tlexmark8 IMARK;
_ Declare_test;
};