从零开始的字符串

zero based String

system.sysutils.pas 中,他们在里约添加了这条指令:

var intPart: String;

... IntPart.Chars[length(IntPart)] in ['1','3','5','7','9'] ...

但据我所知,s.Chars[xx] 将始终是从零开始的字符串 这样做IntPart.Chars[length(IntPart)] in ['1','3','5','7','9'] 总是会 超出边界 ?

不能这样写

... IntPart.Chars[length(IntPart)-1] in ['1','3','5','7','9'] ...

或者我错过了什么?

But as far as i know, s.Chars[xx] will be always zero-based string

是的,TStringHelper 例程被编写为从零开始对字符串进行操作,而不管编译器设置如何 ({$ZEROBASEDSTRINGS ON/OFF})。参见 SysUtils.TStringHelper.Chars

so doing IntPart.Chars[length(IntPart)] in ['1','3','5','7','9'] will always go out of the boundary ?

没错。应该是 Length(IntPart)-1.

报告为: SysUtils.InternalTextToCurrency accesses a string out of bounds

更新: 已在 Delphi 10.3.3 Rio 中修复。


此外,当范围检查开启时,IntPart.Chars[length(IntPart)] 应该引发异常。这也是一个错误(在 TStringHelpers.Chars 中)。

报告为: TStringHelper.Chars does not issue a range error when index is out of bounds