First public contribution.
1 // Copyright (c) 1994-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32\euser\us_lex16.cpp
23 EXPORT_C TLex16::TLex16()
24 : iNext(NULL),iBuf(NULL),iEnd(NULL),iMark(NULL)
28 Constructs a TLex16, initialising its members to NULL.
35 EXPORT_C void TLex16::Assign(const TUint16 *aString)
37 Assigns a string to this object from another string.
39 @param aString A pointer to a string to be assigned.
43 iMark.iPtr=iNext=iBuf=aString;
44 iEnd=iBuf+User::StringLength(aString);
50 EXPORT_C void TLex16::Assign(const TDesC16 &aDes)
52 Assigns a string to this object from a descriptor.
54 @param aDes The descriptor to be assigned.
58 iMark.iPtr=iNext=iBuf=aDes.Ptr();
59 iEnd=iBuf+aDes.Length();
65 EXPORT_C void TLex16::UnGet()
67 Decrements the next character position, allowing the previously "got" character
70 @panic USER 64, if the previous character is before the start of the string.
74 __ASSERT_ALWAYS(iNext>iBuf,Panic(ETLex16UnGetUnderflow));
81 EXPORT_C void TLex16::UnGetToMark(const TLexMark16 aMark)
83 Sets the next character position to the supplied extraction mark position.
85 @param aMark Mark to copy to the next character position.
87 @panic USER 68, if the extraction mark is before the start or beyond the end
99 EXPORT_C void TLex16::Inc(TInt aNumber)
101 Increments the next character position by aNumber.
103 @param aNumber The number of characters to increment the next character
106 @panic USER 65, if the increment puts the next character position before the
107 start or beyond the end of the string.
112 __ASSERT_ALWAYS(iNext>=iBuf && iNext<=iEnd,Panic(ETLex16IncOutOfRange));
118 EXPORT_C void TLex16::Inc()
120 Increments to the next character position.
122 @panic USER 65, if the increment puts the next character position before the
123 start or beyond the end of the string.
129 __ASSERT_ALWAYS(iNext<=iEnd,Panic(ETLex16IncOutOfRange));
135 EXPORT_C TChar TLex16::Get()
137 Gets the next character in the string and increments the next
140 @return Next character to be read. 0 if at the end of the string.
152 EXPORT_C TChar TLex16::Peek() const
154 Shows the next character to be returned by Get().
156 @return Character to be returned by the next call to Get(). 0 if at the end of the
171 EXPORT_C void TLex16::SkipSpace()
173 Moves the next character position past any white space (space or separator).
175 Stops if at the end of string.
179 while (!Eos() && Peek().IsSpace())
186 EXPORT_C void TLex16::SkipAndMark(TInt aNumber,TLexMark16& aMark)
188 Moves the next character position a specified number of characters and copies
189 it to the specified extraction mark.
191 @param aNumber Number of characters to skip.
192 @param aMark On return, set to the next character position.
194 @panic USER 66, if the skip moves the next character position either to before
195 the start or beyond the end of the string.
200 __ASSERT_ALWAYS(iNext>=iBuf && iNext<=iEnd,Panic(ETLex16SkipOutOfRange));
207 EXPORT_C void TLex16::SkipSpaceAndMark(TLexMark16& aMark)
209 Moves the next character position past any white space and copies it to the
210 specified extraction mark.
212 Stops if at the end of the string.
214 @param aMark On return, contains a reference to the next character position.
225 EXPORT_C void TLex16::SkipCharacters()
227 Moves the next character position to the next white space (space or separator).
229 Stops if at the end of the string.
233 while (!Eos() && !Peek().IsSpace())
240 EXPORT_C TInt TLex16::TokenLength(const TLexMark16 aMark) const
242 Gets the length of the token starting at the specified extraction mark.
244 @param aMark Extraction mark indicating the start of the token.
246 @return Length of the token.
248 @panic USER 68, if the specified mark is before the start or beyond the end
254 return (iNext-aMark.iPtr);
260 EXPORT_C TPtrC16 TLex16::MarkedToken(const TLexMark16 aMark) const
262 Extracts the token, starting at the specified mark
264 @param aMark Extraction mark indicating the start of the token.
266 @return Extracted token.
268 @panic USER 68, if the specified mark is before the start or beyond the end
273 return(TPtrC16(aMark.iPtr,TokenLength(aMark)));
279 EXPORT_C TPtrC16 TLex16::MarkedToken() const
281 // Extract the internally marked token
282 // there is the assumption here that iMark is always valid
284 Extracts the marked token.
286 Note that the function assumes that the current extraction mark is valid.
288 @return Extracted token.
290 @panic USER 68, if the specified mark is before the start or beyond the end
295 return(TPtrC16(iMark.iPtr,TokenLength()));
301 EXPORT_C TPtrC16 TLex16::NextToken()
303 Strips any white space and extracts the next token.
305 @return Extracted token.
310 SkipSpaceAndMark(mark);
312 return(TPtrC16(mark.iPtr,iNext-mark.iPtr));
318 EXPORT_C TPtrC16 TLex16::Remainder() const
320 Gets a descriptor containing all the text from the next character position
321 to the end of the string.
323 @return Text from the next character position onwards.
325 @panic USER 17, if the value of (next character position - extraction mark) is
330 return(TPtrC16(iNext,iEnd-iNext));
336 EXPORT_C TPtrC16 TLex16::RemainderFromMark(const TLexMark16 aMark) const
338 Gets a descriptor containing all the text from the specified extraction mark
339 to the end of the string.
341 @param aMark Extraction mark indicating where remaining text starts.
343 @return Text from the specified extraction mark onwards.
345 @panic USER 17, if the value of (next character position - extraction mark) is
347 @panic USER 68, if the specified mark is before the start or beyond the end
353 return(TPtrC16(aMark.iPtr,iEnd-aMark.iPtr));
359 EXPORT_C TPtrC16 TLex16::RemainderFromMark() const
361 // Return the remainder from iMark
362 // There is an assumption here that the internal mark will always be valid
365 Gets a descriptor containing all the text from the extraction mark to the end
368 The function assumes that the current extraction mark is valid.
370 @return Text from the extraction mark onwards.
374 return(TPtrC16(iMark.iPtr,iEnd-iMark.iPtr));
380 EXPORT_C TInt TLex16::Offset() const
382 // Return the offset from iNext to the lex start.
385 Gets the offset of the next character position from the start of the string.
387 @return Offset of next character position.
391 return((TUint)(iNext-iBuf));
397 EXPORT_C TInt TLex16::MarkedOffset(const TLexMark16 aMark) const
399 Gets the offset of the specified extraction mark from the start of the string.
401 @param aMark Extraction mark.
403 @return The offset of the extraction mark.
405 @panic USER 68, if the specified mark is before the start or beyond the end
411 return((TUint)(aMark.iPtr-iBuf));
417 EXPORT_C TInt TLex16::BoundedVal(TUint32 &aVal,TRadix aRadix,TUint aLimit)
419 Parses the string to extract a 32-bit unsigned integer, using the
420 specified radix, and checks that it is within the specified limit.
422 The specified radix is one of binary, octal, decimal, or hexadecimal.
424 @param aVal On return, contains the extracted integer.
425 @param aRadix The radix to use when converting the number.
426 @param aLimit The upper limit.
428 @return KErrNone if successful.
429 KErrGeneral if the next character position is initially at the end of the string
430 or no valid characters found initially.
431 KErrOverflow if there is sign overflow, i.e. converted value greater than limit.
432 If error codes KErrGeneral or KErrOverflow are returned, the object's
433 members are left unaltered.
437 TUint l=aLimit/aRadix;
440 TLexMark16 mark(iNext);
454 if (c>=(TUint)aRadix)
462 return(KErrOverflow);
470 return(KErrOverflow);
482 return(KErrOverflow);
491 EXPORT_C TInt TLex16::BoundedVal(TInt32 &aVal,TInt aLimit)
493 Parses the string to extract a 32-bit signed integer, and checks that it is
494 within the specified limit.
496 @param aVal On return, contains the extracted integer.
497 @param aLimit The upper limit.
499 @return KErrNone if successful.
500 KErrGeneral if the next character position is initially at the end of the string
501 or no valid characters found initially.
502 KErrOverflow if there is sign overflow, i.e. converted value greater than limit.
503 If error codes KErrGeneral or KErrOverflow are returned, the object's
504 members are left unaltered.
511 TLexMark16 mark(iNext);
523 TInt r=BoundedVal(v,EDecimal,lim);
541 EXPORT_C TInt TLex16::BoundedVal(TInt64& aVal, TRadix aRadix, const TInt64& aLimit)
543 Parses the string to extract a 64-bit signed integer, using the
544 specified radix, and checks that it is within the specified limit.
546 The specified radix is one of binary, octal, decimal, or hexadecimal.
548 @param aVal On return, contains the extracted integer.
549 @param aRadix The radix to use when converting the number.
550 @param aLimit The upper limit.
552 @return KErrNone if successful.
553 KErrGeneral if the next character position is initially at the end of the string
554 or no valid characters found initially.
555 KErrOverflow if there is sign overflow, i.e. converted value greater than limit.
556 If error codes KErrGeneral or KErrOverflow are returned, the object's
557 members are left unaltered.
560 TUint64 rad = aRadix;
561 TUint64 lim = static_cast<TUint64>(aLimit);
567 TLexMark16 mark(iNext);
589 return(KErrOverflow);
597 return(KErrOverflow);
606 if (v > static_cast<TUint64>(aLimit))
609 return(KErrOverflow);
611 aVal = static_cast<TInt64>(v);
618 EXPORT_C TInt TLex16::BoundedVal(TInt64& aVal, const TInt64& aLimit)
620 Parses the string to extract a 64-bit signed integer, and checks that it
621 is within the specified limit.
623 @param aVal On return, contains the extracted integer.
624 @param aLimit The upper limit.
626 @return KErrNone if successful.
627 KErrGeneral if the next character position is initially at the end of the string
628 or no valid characters found initially.
629 KErrOverflow if there is sign overflow, i.e. converted value greater than limit.
630 If error codes KErrGeneral or KErrOverflow are returned, the object's
631 members are left unaltered.
638 TLexMark16 mark(iNext);
650 TInt r=BoundedVal(v,EDecimal,lim);
668 EXPORT_C TInt TLex16::Val(TInt8 &aVal)
670 Parses the string to extract a signed 8-bit integer.
672 @param aVal On return, contains the extracted integer.
674 @return KErrNone if successful.
675 KErrGeneral if the next character position is initially at the end of the string
676 or no valid characters found initially.
677 KErrOverflow if there is sign overflow, i.e. converted value greater than limit.
678 If error codes KErrGeneral or KErrOverflow are returned, the object's
679 members are left unaltered.
684 TInt r=BoundedVal(v,0x7fu);
693 EXPORT_C TInt TLex16::Val(TInt16 &aVal)
695 Parses the string to extract a signed 16-bit integer.
697 @param aVal On return, contains the extracted integer.
699 @return KErrNone if successful.
700 KErrGeneral if the next character position is initially at the end of the string
701 or no valid characters found initially.
702 KErrOverflow if there is sign overflow, i.e. converted value greater than limit.
703 If error codes KErrGeneral or KErrOverflow are returned, the object's
704 members are left unaltered.
709 TInt r=BoundedVal(v,0x7fffu);
718 EXPORT_C TInt TLex16::Val(TInt32 &aVal)
720 Parses the string to extract a signed 32-bit integer.
722 @param aVal On return, contains the extracted integer.
724 @return KErrNone if successful.
725 KErrGeneral if the next character position is initially at the end of the string
726 or no valid characters found initially.
727 KErrOverflow if there is sign overflow, i.e. converted value greater than limit.
728 If error codes KErrGeneral or KErrOverflow are returned, the object's
729 members are left unaltered.
734 TInt r=BoundedVal(v,0x7fffffffu);
743 EXPORT_C TInt TLex16::Val(TInt64& aVal)
745 Parses the string to extract a signed 64-bit integer.
747 @param aVal On return, contains the extracted integer.
749 @return KErrNone if successful.
750 KErrGeneral if the next character position is initially at the end of the string
751 or no valid characters found initially.
752 KErrOverflow if there is sign overflow, i.e. converted value greater than limit.
753 If error codes KErrGeneral or KErrOverflow are returned, the object's
754 members are left unaltered.
759 if (c=='-' || c=='+')
764 r=BoundedVal(aVal, EDecimal, UI64LIT(0x8000000000000000));
769 r=BoundedVal(aVal, UI64LIT(0x7fffffffffffffff));
776 EXPORT_C TInt TLex16::Val(TUint8 &aVal,TRadix aRadix)
778 Parses the string to extract an 8-bit unsigned integer, using the
781 The specified radix is one of binary, octal, decimal, or hexadecimal.
783 @param aVal On return, contains the extracted integer.
784 @param aRadix The radix to use when converting the number.
786 @return KErrNone if successful.
787 KErrGeneral if the next character position is initially at the end of the string
788 or no valid characters found initially.
789 KErrOverflow if there is sign overflow, i.e. converted value greater than limit.
790 If error codes KErrGeneral or KErrOverflow are returned, the object's
791 members are left unaltered.
796 TInt r=BoundedVal(v,aRadix,0xffu);
805 EXPORT_C TInt TLex16::Val(TUint16 &aVal,TRadix aRadix)
807 Parses the string to extract a 16-bit unsigned integer, using the
810 The specified radix is one of binary, octal, decimal, or hexadecimal.
812 @param aVal On return, contains the extracted integer.
813 @param aRadix The radix to use when converting the number.
815 @return KErrNone if successful.
816 KErrGeneral if the next character position is initially at the end of the string
817 or no valid characters found initially.
818 KErrOverflow if there is sign overflow, i.e. converted value greater than limit.
819 If error codes KErrGeneral or KErrOverflow are returned, the object's
820 members are left unaltered.
825 TInt r=BoundedVal(v,aRadix,0xffffu);
834 EXPORT_C TInt TLex16::Val(TUint32 &aVal,TRadix aRadix)
836 Parses the string to extract a 32-bit unsigned integer, using the
839 The specified radix is one of binary, octal, decimal, or hexadecimal.
841 @param aVal On return, contains the extracted integer.
842 @param aRadix The radix to use when converting the number.
844 @return KErrNone if successful.
845 KErrGeneral if the next character position is initially at the end of the string
846 or no valid characters found initially.
847 KErrOverflow if there is sign overflow, i.e. converted value greater than limit.
848 If error codes KErrGeneral or KErrOverflow are returned, the object's
849 members are left unaltered.
854 TInt r=BoundedVal(v,aRadix,0xffffffffu);
863 EXPORT_C TInt TLex16::Val(TInt64& aVal, TRadix aRadix)
865 Parses the string to extract a 64-bit integer (treated as unsigned), using the
868 The specified radix is one of binary, octal, decimal, or hexadecimal.
870 @param aVal On return, contains the extracted integer.
871 @param aRadix The radix to use when converting the number.
873 @return KErrNone if successful.
874 KErrGeneral if the next character position is initially at the end of the string
875 or no valid characters found initially.
876 KErrOverflow if there is sign overflow, i.e. converted value greater than limit.
877 If error codes KErrGeneral or KErrOverflow are returned, the object's
878 members are left unaltered.
882 return BoundedVal(aVal,aRadix,TInt64(UI64LIT(0xffffffffffffffff)));
888 void TLex16::ValidateMark(const TLexMark16 aMark) const
890 // Asserts that the mark is valid for this lex
893 __ASSERT_ALWAYS(aMark.iPtr>=iBuf && aMark.iPtr<=iEnd,Panic(ETLex16MarkOutOfRange));