diff --git a/UEnhancedMemoryStream.pas b/UEnhancedMemoryStream.pas index d07bb6f..399ba9a 100644 --- a/UEnhancedMemoryStream.pas +++ b/UEnhancedMemoryStream.pas @@ -115,61 +115,105 @@ end; function TEnhancedMemoryStream.ReadStringBigUniNull: WideString; var buffer: PWideChar; - length: Integer; + i, j, maxChars: Integer; begin Result := ''; - buffer := Pointer(PtrInt(Memory) + Position); - length := 0; - while (buffer[length] <> #0) and (length < (Size - Position)) do + buffer := Pointer(PtrInt(FMemory) + FPosition); + maxChars := (FSize - (FPosition - FLockOffset)) div 2; + if maxChars <= 0 then Exit; + SetLength(Result, maxChars * 2); + i := 0; + j := 0; + while (i < maxChars) and (buffer[i] <> #0) do begin - if (SwapWideChar(buffer[length]) = #10) and (SwapWideChar(buffer[length - 1]) <> #13) then - Result := Result + #13; - Result := Result + SwapWideChar(buffer[length]); - if (SwapWideChar(buffer[length]) = #13) and (SwapWideChar(buffer[length + 1]) <> #10) then - Result := Result + #10; - inc(length); + if (SwapWideChar(buffer[i]) = #10) and ((i = 0) or (SwapWideChar(buffer[i - 1]) <> #13)) then + begin + Inc(j); + Result[j] := #13; + end; + Inc(j); + Result[j] := SwapWideChar(buffer[i]); + if (SwapWideChar(buffer[i]) = #13) and ((i + 1 >= maxChars) or (SwapWideChar(buffer[i + 1]) <> #10)) then + begin + Inc(j); + Result[j] := #10; + end; + Inc(i); end; - Position := Position + (Length + 1) * 2; + SetLength(Result, j); + if i < maxChars then + FPosition := FPosition + (i + 1) * 2 + else + FPosition := FPosition + i * 2; end; function TEnhancedMemoryStream.ReadStringFixed(ALength: Integer): string; var buffer: PChar; - length: Integer; + i, j: Integer; begin Result := ''; buffer := Pointer(PtrInt(FMemory) + FPosition); - length := 0; - while (length < ALength) and (length < (FSize - (FPosition - FLockOffset))) do + if ALength <= 0 then Exit; + if ALength > (FSize - (FPosition - FLockOffset)) then + ALength := FSize - (FPosition - FLockOffset); + if ALength <= 0 then Exit; + SetLength(Result, ALength * 2); + i := 0; + j := 0; + while i < ALength do begin - if (buffer[length] = #10) and (buffer[length - 1] <> #13) then - Result := Result + #13; - Result := Result + buffer[length]; - if (buffer[length] = #13) and (buffer[length + 1] <> #10) then - Result := Result + #10; - inc(length); + if (buffer[i] = #10) and ((i = 0) or (buffer[i - 1] <> #13)) then + begin + Inc(j); + Result[j] := #13; + end; + Inc(j); + Result[j] := buffer[i]; + if (buffer[i] = #13) and ((i + 1 >= ALength) or (buffer[i + 1] <> #10)) then + begin + Inc(j); + Result[j] := #10; + end; + Inc(i); end; - FPosition := FPosition + length + 1; + SetLength(Result, j); + FPosition := FPosition + i; end; function TEnhancedMemoryStream.ReadStringNull: string; var buffer: PByteArray; - length: Integer; + i, j, maxLen: Integer; begin Result := ''; buffer := Pointer(PtrInt(FMemory) + FPosition); - length := 0; - while (buffer^[length] <> 0) and (length < (FSize - (FPosition - FLockOffset))) do + maxLen := FSize - (FPosition - FLockOffset); + if maxLen <= 0 then Exit; + SetLength(Result, maxLen * 2); + i := 0; + j := 0; + while (i < maxLen) and (buffer^[i] <> 0) do begin - if (buffer^[length] = 10) and (buffer^[length - 1] <> 13) then - Result := Result + #13; - Result := Result + Char(buffer^[length]); - if (buffer^[length] = 13) and (buffer^[length + 1] <> 10) then - Result := Result + #10; - inc(length); + if (buffer^[i] = 10) and ((i = 0) or (buffer^[i - 1] <> 13)) then + begin + Inc(j); + Result[j] := #13; + end; + Inc(j); + Result[j] := Char(buffer^[i]); + if (buffer^[i] = 13) and ((i + 1 >= maxLen) or (buffer^[i + 1] <> 10)) then + begin + Inc(j); + Result[j] := #10; + end; + Inc(i); end; - FPosition := FPosition + length + 1; + SetLength(Result, j); + if i < maxLen then + FPosition := FPosition + i + 1 + else + FPosition := FPosition + i; end; function TEnhancedMemoryStream.ReadWord: Word;