Bug 384898: Cleanup nsScanner. r=mrbkap, sr=jst

git-svn-id: svn://10.0.0.236/trunk@228788 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
sharparrow1%yahoo.com
2007-06-27 00:21:47 +00:00
parent 9676b09bdf
commit 9057c0b45b
6 changed files with 37 additions and 412 deletions

View File

@@ -94,7 +94,6 @@ nsScanner::nsScanner(const nsAString& anHTMLString, const nsACString& aCharset,
{
MOZ_COUNT_CTOR(nsScanner);
mTotalRead = anHTMLString.Length();
mSlidingBuffer = nsnull;
mCountRemaining = 0;
mFirstNonWhitespacePosition = -1;
@@ -121,6 +120,7 @@ nsScanner::nsScanner(nsString& aFilename,PRBool aCreateStream,
: mFilename(aFilename), mParser(nsnull)
{
MOZ_COUNT_CTOR(nsScanner);
NS_ASSERTION(!aCreateStream, "This is always true.");
mSlidingBuffer = nsnull;
@@ -136,59 +136,12 @@ nsScanner::nsScanner(nsString& aFilename,PRBool aCreateStream,
mIncremental = PR_TRUE;
mFirstNonWhitespacePosition = -1;
mCountRemaining = 0;
mTotalRead=0;
if(aCreateStream) {
nsCOMPtr<nsILocalFile> file;
nsCOMPtr<nsIInputStream> fileStream;
NS_NewLocalFile(aFilename, PR_TRUE, getter_AddRefs(file));
if (file)
NS_NewLocalFileInputStream(getter_AddRefs(mInputStream), file);
} //if
mUnicodeDecoder = 0;
mCharsetSource = kCharsetUninitialized;
SetDocumentCharset(aCharset, aSource);
}
/**
* Use this constructor if you want i/o to be stream based.
*
* @update gess 5/12/98
* @param aStream --
* @param assumeOwnership --
* @param aFilename --
* @return
*/
nsScanner::nsScanner(const nsAString& aFilename, nsIInputStream* aStream,
const nsACString& aCharset, PRInt32 aSource)
: mFilename(aFilename), mParser(nsnull)
{
MOZ_COUNT_CTOR(nsScanner);
mSlidingBuffer = nsnull;
// XXX This is a big hack. We need to initialize the iterators to something.
// What matters is that mCurrentPosition == mEndPosition, so that our methods
// believe that we are at EOF (see bug 182067). We null out mCurrentPosition
// so that we have some hope of catching null pointer dereferences associated
// with this hack. --darin
memset(&mCurrentPosition, 0, sizeof(mCurrentPosition));
mMarkPosition = mCurrentPosition;
mEndPosition = mCurrentPosition;
mIncremental = PR_FALSE;
mFirstNonWhitespacePosition = -1;
mCountRemaining = 0;
mTotalRead=0;
mInputStream=aStream;
mUnicodeDecoder = 0;
mCharsetSource = kCharsetUninitialized;
SetDocumentCharset(aCharset, aSource);
}
nsresult nsScanner::SetDocumentCharset(const nsACString& aCharset , PRInt32 aSource) {
nsresult res = NS_OK;
@@ -251,11 +204,6 @@ nsScanner::~nsScanner() {
MOZ_COUNT_DTOR(nsScanner);
if(mInputStream) {
mInputStream->Close();
mInputStream = 0;
}
NS_IF_RELEASE(mUnicodeDecoder);
}
@@ -313,7 +261,6 @@ PRBool nsScanner::UngetReadable(const nsAString& aBuffer) {
PRUint32 length = aBuffer.Length();
mCountRemaining += length; // Ref. bug 117441
mTotalRead += length;
return PR_TRUE;
}
@@ -325,7 +272,6 @@ PRBool nsScanner::UngetReadable(const nsAString& aBuffer) {
* @return error code
*/
nsresult nsScanner::Append(const nsAString& aBuffer) {
mTotalRead += aBuffer.Length();
AppendToBuffer(aBuffer);
return NS_OK;
}
@@ -342,7 +288,7 @@ nsresult nsScanner::Append(const char* aBuffer, PRUint32 aLen,
{
nsresult res=NS_OK;
PRUnichar *unichars, *start;
if(mUnicodeDecoder) {
if (mUnicodeDecoder) {
PRInt32 unicharBufLen = 0;
mUnicodeDecoder->GetMaxLength(aBuffer, aLen, &unicharBufLen);
nsScannerString::Buffer* buffer = nsScannerString::AllocBuffer(unicharBufLen + 1);
@@ -389,7 +335,6 @@ nsresult nsScanner::Append(const char* aBuffer, PRUint32 aLen,
buffer->SetDataLength(totalChars);
AppendToBuffer(buffer, aRequest);
mTotalRead += totalChars;
// Don't propagate return code of unicode decoder
// since it doesn't reflect on our success or failure
@@ -397,47 +342,13 @@ nsresult nsScanner::Append(const char* aBuffer, PRUint32 aLen,
res = NS_OK;
}
else {
AppendASCIItoBuffer(aBuffer, aLen, aRequest);
mTotalRead+=aLen;
NS_WARNING("No decoder found.");
res = NS_ERROR_FAILURE;
}
return res;
}
/**
* Grab data from underlying stream.
*
* @update gess4/3/98
* @return error code
*/
nsresult nsScanner::FillBuffer(void) {
nsresult result=NS_OK;
if(!mInputStream) {
result=kEOF;
}
else {
PRUint32 numread=0;
char buf[kBufsize+1];
buf[kBufsize]=0;
// XXX use ReadSegments to avoid extra buffer copy? --darin
result = mInputStream->Read(buf, kBufsize, &numread);
if (0 == numread) {
return kEOF;
}
if((0<numread) && NS_SUCCEEDED(result)) {
AppendASCIItoBuffer(buf, numread, nsnull);
}
mTotalRead+=numread;
}
return result;
}
/**
* retrieve next char from scanners internal input stream
*
@@ -446,22 +357,15 @@ nsresult nsScanner::FillBuffer(void) {
* @return error code reflecting read status
*/
nsresult nsScanner::GetChar(PRUnichar& aChar) {
nsresult result=NS_OK;
aChar=0;
if (!mSlidingBuffer) {
if (!mSlidingBuffer || mCurrentPosition == mEndPosition) {
aChar = 0;
return kEOF;
}
if (mCurrentPosition == mEndPosition) {
result = FillBuffer();
}
aChar = *mCurrentPosition++;
--mCountRemaining;
if(NS_OK == result){
aChar = *mCurrentPosition++;
--mCountRemaining;
}
return result;
return NS_OK;
}
@@ -474,47 +378,33 @@ nsresult nsScanner::GetChar(PRUnichar& aChar) {
* @return
*/
nsresult nsScanner::Peek(PRUnichar& aChar, PRUint32 aOffset) {
nsresult result=NS_OK;
aChar=0;
if (!mSlidingBuffer) {
aChar = 0;
if (!mSlidingBuffer || mCurrentPosition == mEndPosition) {
return kEOF;
}
if (mCurrentPosition == mEndPosition) {
result = FillBuffer();
if (aOffset > 0) {
if (mCountRemaining <= aOffset)
return kEOF;
nsScannerIterator pos = mCurrentPosition;
pos.advance(aOffset);
aChar=*pos;
}
else {
aChar=*mCurrentPosition;
}
if(NS_OK == result){
if (aOffset) {
while ((NS_OK == result) && (mCountRemaining <= aOffset)) {
result = FillBuffer();
}
if (NS_OK == result) {
nsScannerIterator pos = mCurrentPosition;
pos.advance(aOffset);
aChar=*pos;
}
}
else {
aChar=*mCurrentPosition;
}
}
return result;
return NS_OK;
}
nsresult nsScanner::Peek(nsAString& aStr, PRInt32 aNumChars, PRInt32 aOffset)
{
if (!mSlidingBuffer) {
if (!mSlidingBuffer || mCurrentPosition == mEndPosition) {
return kEOF;
}
if (mCurrentPosition == mEndPosition) {
return FillBuffer();
}
nsScannerIterator start, end;
start = mCurrentPosition;
@@ -591,7 +481,7 @@ nsresult nsScanner::SkipWhitespace(PRInt32& aNewlinesSkipped) {
if (skipped) {
SetPosition(current);
if (current == mEndPosition) {
result = FillBuffer();
result = kEOF;
}
}
@@ -628,68 +518,6 @@ nsresult nsScanner::SkipOver(PRUnichar aSkipChar){
}
/**
* Skip over chars as long as they're in aSkipSet
*
* @update gess 3/25/98
* @param aSkipSet is an ordered string.
* @return error code
*/
nsresult nsScanner::SkipOver(nsString& aSkipSet){
if (!mSlidingBuffer) {
return kEOF;
}
PRUnichar theChar=0;
nsresult result=NS_OK;
while(NS_OK==result) {
result=Peek(theChar);
if(NS_OK == result) {
PRInt32 pos=aSkipSet.FindChar(theChar);
if(kNotFound==pos) {
break;
}
GetChar(theChar);
}
else break;
} //while
return result;
}
/**
* Skip over chars until they're in aValidSet
*
* @update gess 3/25/98
* @param aValid set is an ordered string that
* contains chars you're looking for
* @return error code
*/
nsresult nsScanner::SkipTo(nsString& aValidSet){
if (!mSlidingBuffer) {
return kEOF;
}
PRUnichar ch=0;
nsresult result=NS_OK;
while(NS_OK==result) {
result=Peek(ch);
if(NS_OK == result) {
PRInt32 pos=aValidSet.FindChar(ch);
if(kNotFound!=pos) {
break;
}
GetChar(ch);
}
else break;
} //while
return result;
}
#if 0
void DoErrTest(nsString& aString) {
PRInt32 pos=aString.FindChar(0);
@@ -708,19 +536,6 @@ void DoErrTest(nsCString& aString) {
}
#endif
/**
* Skip over chars as long as they're in aValidSet
*
* @update gess 3/25/98
* @param aValidSet is an ordered string containing the
* characters you want to skip
* @return error code
*/
nsresult nsScanner::SkipPast(nsString& aValidSet){
NS_NOTYETIMPLEMENTED("Error: SkipPast not yet implemented.");
return NS_OK;
}
/**
* Consume characters until you run into space, a '<', a '>', or a '/'.
*
@@ -780,7 +595,7 @@ nsresult nsScanner::ReadTagIdentifier(nsScannerSharedSubstring& aString) {
SetPosition(current);
if (current == end) {
result = FillBuffer();
result = kEOF;
}
//DoErrTest(aString);
@@ -840,7 +655,7 @@ nsresult nsScanner::ReadEntityIdentifier(nsString& aString) {
SetPosition(current);
if (current == end) {
AppendUnicodeTo(origin, current, aString);
return FillBuffer();
return kEOF;
}
//DoErrTest(aString);
@@ -889,7 +704,7 @@ nsresult nsScanner::ReadNumber(nsString& aString,PRInt32 aBase) {
SetPosition(current);
if (current == end) {
AppendUnicodeTo(origin, current, aString);
return FillBuffer();
return kEOF;
}
//DoErrTest(aString);
@@ -967,7 +782,7 @@ nsresult nsScanner::ReadWhitespace(nsScannerSharedSubstring& aString,
SetPosition(current);
if (current == end) {
AppendUnicodeTo(origin, current, aString);
result = FillBuffer();
result = kEOF;
}
aHaveCR = haveCR;
@@ -1026,69 +841,12 @@ nsresult nsScanner::ReadWhitespace(nsScannerIterator& aStart,
if (current == end) {
aStart = origin;
aEnd = current;
result = FillBuffer();
result = kEOF;
}
return result;
}
/**
* Consume chars as long as they are <i>in</i> the
* given validSet of input chars.
*
* @update gess 3/25/98
* @param aString will contain the result of this method
* @param aValidSet is an ordered string that contains the
* valid characters
* @return error code
*/
nsresult nsScanner::ReadWhile(nsString& aString,
nsString& aValidSet,
PRBool addTerminal){
if (!mSlidingBuffer) {
return kEOF;
}
PRUnichar theChar=0;
nsresult result=Peek(theChar);
nsScannerIterator origin, current, end;
origin = mCurrentPosition;
current = origin;
end = mEndPosition;
while(current != end) {
theChar=*current;
if (theChar == '\0') {
ReplaceCharacter(current, sInvalid);
theChar = sInvalid;
}
if(theChar) {
PRInt32 pos=aValidSet.FindChar(theChar);
if(kNotFound==pos) {
if(addTerminal)
++current;
AppendUnicodeTo(origin, current, aString);
break;
}
}
++current;
}
SetPosition(current);
if (current == end) {
AppendUnicodeTo(origin, current, aString);
return FillBuffer();
}
//DoErrTest(aString);
return result;
}
/**
* Consume characters until you encounter one contained in given
* input set.
@@ -1156,7 +914,7 @@ nsresult nsScanner::ReadUntil(nsAString& aString,
// current = mEndPosition
SetPosition(current);
AppendUnicodeTo(origin, current, aString);
return FillBuffer();
return kEOF;
}
nsresult nsScanner::ReadUntil(nsScannerSharedSubstring& aString,
@@ -1216,7 +974,7 @@ nsresult nsScanner::ReadUntil(nsScannerSharedSubstring& aString,
// current = mEndPosition
SetPosition(current);
AppendUnicodeTo(origin, current, aString);
return FillBuffer();
return kEOF;
}
nsresult nsScanner::ReadUntil(nsScannerIterator& aStart,
@@ -1277,7 +1035,7 @@ nsresult nsScanner::ReadUntil(nsScannerIterator& aStart,
SetPosition(current);
aStart = origin;
aEnd = current;
return FillBuffer();
return kEOF;
}
/**
@@ -1328,7 +1086,7 @@ nsresult nsScanner::ReadUntil(nsAString& aString,
// current = mEndPosition
AppendUnicodeTo(origin, current, aString);
SetPosition(current);
return FillBuffer();
return kEOF;
}
@@ -1425,19 +1183,6 @@ void nsScanner::AppendToBuffer(nsScannerString::Buffer* aBuf,
}
}
void nsScanner::AppendASCIItoBuffer(const char* aData, PRUint32 aLen,
nsIRequest *aRequest)
{
nsScannerString::Buffer* buf = nsScannerString::AllocBuffer(aLen);
if (buf)
{
LossyConvertEncoding<char, PRUnichar> converter(buf->DataStart());
converter.write(aData, aLen);
converter.write_terminator();
AppendToBuffer(buf, aRequest);
}
}
/**
* call this to copy bytes out of the scanner that have not yet been consumed
* by the tokenization process.