Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zero based String

in system.sysutils.pas, in Rio they add this instruction:

var intPart: String;

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

But as far as i know, s.Chars[xx] will be always zero-based string so doing IntPart.Chars[length(IntPart)] in ['1','3','5','7','9'] will always go out of the boundary ?

it's must not be written instead

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

Or I miss something ?

like image 342
zeus Avatar asked Apr 30 '19 11:04

zeus


People also ask

What is a 0 indexed string?

The beginning character of a string corresponds to index 0 and the last character corresponds to the index (length of string)-1 . The length of a string is the number of characters it contains, including spaces, punctuation, and control characters.

What are zero-based functions?

A zero-based approach seeks to link organizational designs to strategic priorities (for example, areas for investment compared with efficiency optimization) instead of a “one-size-fits-all” solution across the business.

Are VB arrays zero-based?

All arrays must be zero-indexed. In Microsoft Visual Basic 6.0, you can define arrays with the lower bounds and upper bounds set to any integer. In Microsoft Visual Basic . NET, the lower bound of every array dimension is zero (0).


1 Answers

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

Yes, the TStringHelper routines are written to operate on strings as zero based, regardless of the compiler settings ({$ZEROBASEDSTRINGS ON/OFF}). See SysUtils.TStringHelper.Chars.

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

That is correct. It should have been Length(IntPart)-1.

Reported as: SysUtils.InternalTextToCurrency accesses a string out of bounds

Update: Fixed in Delphi 10.3.3 Rio.


Furthermore, when range check is on, IntPart.Chars[length(IntPart)] should have raised an exception. This is also bug (in TStringHelpers.Chars).

Reported as: TStringHelper.Chars does not issue a range error when index is out of bounds

like image 177
LU RD Avatar answered Oct 18 '22 04:10

LU RD