📄 stringparser.h
字号:
/* File: StringParser.h Contains: A couple of handy utilities for parsing a stream. */#ifndef __STRINGPARSER_H__#define __STRINGPARSER_H__
#ifdef __Win32__
#pragma warning(disable:4786)
#endif
#include "StrPtrLen.h"#include "MyAssert.h"#define STRINGPARSERTESTING 0////串的解析类//class StringParser{ public: //基于StrPtrLen来构建,不保存具体的数据 StringParser(StrPtrLen *inStream) : fStartGet(inStream == NULL ? NULL : inStream->Ptr), fEndGet(inStream == NULL ? NULL : inStream->Ptr + inStream->Len), fCurLineNumber(1), fStream(inStream) {} //析构 ~StringParser() {} // Built-in masks for common stop conditions static UInt8 sDigitMask[]; // stop when you hit a digit static UInt8 sWordMask[]; // stop when you hit a word static UInt8 sEOLMask[]; // stop when you hit an eol static UInt8 sEOLWhitespaceMask[]; // stop when you hit an EOL or whitespace static UInt8 sWhitespaceMask[]; // skip over whitespace //GetBuffer: //Returns a pointer to the string object StrPtrLen* GetStream() { return fStream; } //Expect: //These functions consume the given token/word if it is in the stream. //If not, they return FALSE. //In all other situations, TRUE is returned. //NOTE: if these functions return an error, the object goes into a state where //it cannot be guarenteed to function correctly. Bool Expect(char stopChar); Bool ExpectEOL(); //返回下一个字 void ConsumeWord(StrPtrLen* outString = NULL) { ConsumeUntil(outString, sNonWordMask); } //直到遇到停止的符号才返回,outString为新的位置 void ConsumeUntil(StrPtrLen* outString, char inStopChar); //Returns whatever integer is currently in the stream UInt32 ConsumeInteger(StrPtrLen* outString = NULL); Float32 ConsumeFloat(); //Keeps on going until non-whitespace void ConsumeWhitespace() { ConsumeUntil(NULL, sWhitespaceMask); } //Assumes 'stop' is a 255-char array of booleans. Set this array //to a mask of what the stop characters are. TRUE means stop character. //You may also pass in one of the many prepackaged masks defined above. void ConsumeUntil(StrPtrLen* spl, UInt8 *stop); //+ rt 8.19.99 //returns whatever is avaliable until non-whitespace void ConsumeUntilWhitespace(StrPtrLen* spl = NULL) { ConsumeUntil( spl, sEOLWhitespaceMask); } void ConsumeUntilDigit(StrPtrLen* spl = NULL) { ConsumeUntil( spl, sDigitMask); } void ConsumeLength(StrPtrLen* spl, Int32 numBytes); void ConsumeEOL(StrPtrLen* outString); //GetThru: //Works very similar to ConsumeUntil except that it moves past the stop token, //and if it can't find the stop token it returns FALSE Bool GetThru(StrPtrLen* spl, char stop); Bool GetThruEOL(StrPtrLen* spl); Bool ParserIsEmpty(StrPtrLen* outString); //Returns the current character, doesn't move past it. char PeekFast() { if (fStartGet) return *fStartGet; else return '\0'; } char operator[](int i) { Assert((fStartGet+i) < fEndGet);return fStartGet[i]; } //Returns some info about the stream UInt32 GetDataParsedLen() { Assert(fStartGet >= fStream->Ptr); return (UInt32)(fStartGet - fStream->Ptr); } UInt32 GetDataReceivedLen() { Assert(fEndGet >= fStream->Ptr); return (UInt32)(fEndGet - fStream->Ptr); } UInt32 GetDataRemaining() { Assert(fEndGet >= fStartGet); return (UInt32)(fEndGet - fStartGet); } char* GetCurrentPosition() { return fStartGet; } int GetCurrentLineNumber() { return fCurLineNumber; } // A utility for extracting quotes from the start and end of a parsed // string. (Warning: Do not call this method if you allocated your own // pointer for the Ptr field of the StrPtrLen class.) - [sfu] // // Not sure why this utility is here and not in the StrPtrLen class - [jm] static void UnQuote(StrPtrLen* outString); #if STRINGPARSERTESTING static Bool Test();#endif private: void AdvanceMark(); //built in masks for some common stop conditions static UInt8 sNonWordMask[]; char* fStartGet; char* fEndGet; int fCurLineNumber; StrPtrLen* fStream; };inline Bool StringParser::GetThru(StrPtrLen* outString, char inStopChar){ ConsumeUntil(outString, inStopChar); return Expect(inStopChar);}inline Bool StringParser::GetThruEOL(StrPtrLen* outString){ ConsumeUntil(outString, sEOLMask); return ExpectEOL();}#endif // __STRINGPARSER_H__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -