Symbian 下字符串解析类 TLex8 的使用

刚入手Symbian时做过一个字符串处理的函数,当时使用的TDesC8 的Mid Find 等函数来实现,代码比较冗余而且效率不高,

后来接触到了TLex8这个类,发现如果使用熟练的话对字符串处理的效率还是不错而且代码页比较简洁了!

 

BTW:开源就是好,对于不懂得东东可以直接跳过解释超烂的SDK直接从源码中寻找答案!

 

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);

_LIT(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);

}

 

贴上e32std.h与us_lex8.cpp中TLex8的部分源码

 

 

 

 

 

 

#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 the

                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 the

        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;

};

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章