Sfm2Xml.ByteReader.CopySpaceIfMatched C# (CSharp) Method

CopySpaceIfMatched() private method

Tests the given data at the given position for a match with given search data. If there is no match, or the match is at the end of the given data, the data is copied into a separate buffer. Otherwise, a space is copied over. This effectivly copies the given data to the output data, replacing matching searched-for data with a space, except at the end.
private CopySpaceIfMatched ( byte readData, long &readPos, long limitPos, byte &writeData, long &writePos, byte searchData ) : bool
readData byte The source data
readPos long The current place in readData to start work
limitPos long The last place in readData to read
writeData byte The output buffer
writePos long The current location to write to in the output buffer
searchData byte the data to search for
return bool
		private bool CopySpaceIfMatched(byte[] readData, ref long readPos, long limitPos,
			ref byte[] writeData, ref long writePos, byte[] searchData)
		{
			// Test if the byte sequence we are searching for exists in readData at the current read position:
			if (BytesMatch(readData, readPos, searchData))
			{
				// The search sequence exists in readData at the current position.
				// Test if the searched-for sequence constitutes the end of the readData:
				if (limitPos - readPos >= searchData.Length)
				{
					// We are not at the end, so we will just copy over a space, instead of the
					// readData itself:
					for (long i = 0; i < m_space.Length; i++)
						writeData[writePos++] = m_space[i];

					// Skip past the searchData:
					readPos += searchData.Length;
				}
				else
				{
					// We are at the end of the given readData, so the matching data does not
					// count for anything - we will copy it over raw:
					for (long i = 0; i < searchData.Length; i++)
						writeData[writePos++] = readData[readPos++];
				}

				return true;
			}

			// The byte sequence was not found, so return "no work done" value:
			return false;
		}
	}