我正在尝试调试一个问题,这个问题只在使用XE4编译后运行我的大型应用程序时出现,而在XE3中运行良好。该问题似乎会导致一些带引号的字符串(例如"MyString")即使已经被Web.HTTPProd中的TPageProducer“去引号”,仍然保留它们的引号。例如,考虑下面的代码,它是来自Delphi源代码单元Web.HTTPApp的一个小片段:
procedure ExtractHeaderFields(Separators, _WhiteSpace: TSysCharSet; Content: PChar;
Strings: TStrings; Decode: Boolean; StripQuotes: Boolean = False);
{$ENDIF NEXTGEN}
var
Head, Tail: PChar;
EOS, InQuote, LeadQuote: Boolean;
QuoteChar: Char;
ExtractedField: string;
{$IFNDEF NEXTGEN}
WhiteSpaceWithCRLF: TSysCharSet;
SeparatorsWithCRLF: TSysCharSet;
{$ENDIF !NEXTGEN}
function DoStripQuotes(const S: string): string;
var
I: Integer;
InStripQuote: Boolean;
StripQuoteChar: Char;
begin
Result := S;
InStripQuote := False;
StripQuoteChar := #0;
if StripQuotes then
begin
for I := Result.Length - 1 downto 0 do
if Result.Chars[I].IsInArray(['''', '"']) then
if InStripQuote and (StripQuoteChar = Result.Chars[I]) then
begin
Result.Remove(I, 1);
InStripQuote := False;
end
else if not InStripQuote then
begin
StripQuoteChar := Result.Chars[I];
InStripQuote := True;
Result.Remove(I, 1);
end
end;
end;
我看到在使用TPageProducer时会调用这个函数,我可以看到我的好源字符串进入上面的ExtractHeaderFields例程,然后进入“DoStripQuotes”函数。进入DoStripQuotes并观察“Result”时,即使调用Result.Remove(以去除引号),它也不会改变。当我将此“DoStripQuotes”例程移植到简单的测试应用程序中时,它无法编译,告诉我“Result.anything”不允许。那么我假设,在Web.HTTPProd的上下文中,虽然已定义为“string”,但Result必须是另一种类型的字符串。
因此我想这可能与我听说过的“不可变字符串”有关。我阅读了这篇SO问题,虽然我大致理解了其中的内容,但我需要更多实用的建议。
具体地,我想对以下问题得到答案:
- 如果允许符号Result.Length,则'Result'是什么类型的'string'?
- 是否有一种方法可以告诉编译器在一个单位中使用“XE3”兼容性? (这可能会让我看到问题的起源)。 我尝试过{$ZEROBASEDSTRINGS ON} / OFF,但这似乎会造成更大的混乱,我不知道该怎么做!
感谢任何帮助。
后来编辑:如下面所接受的答案中所述,这是VCL单元Web.HTTPApp.pas中的一个错误,应该在2645行附近两个位置中读取“Result:=Result.Remove(I,1)”,而不是“Result.Remove(I,1)”
TStringHelper
标记为这种指令的选项,编译器错误也应该被引发。 - LU RD