Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2011-06

Best integer type to be used for string indexes

5 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Vladimir Zhirov

Hi,

Reviewing a bunch of my string processing functions made me wonder what integer type should I use for
position / length parameters.

I used to plain "Integer" for this task, but looking through RTL and LCL code I noticed that sometimes SizeInt and even PtrInt are also used:
- Length, Copy use Integer;
- Pos, Delete, Insert use SizeInt;
- UTF8Length, UTF8Pos, UTF8Copy, etc. use PtrInt.

PtrInt use seems to be generally discouraged by FPC docs but does it apply to this particular case?

The only benefit I see in SizeInt/PtrInt is larger size on 64-bit targets, that should theoretically make it possible to index larger strings. But is AnsiString type capable of holding more than High(Integer) bytes, or will it be so in the future?

Thus, the question basically is: what integer type is recommended to be used as index/length
for future-proof string processing functions?

Vladimir

Reply by email

Michael Van Canneyt
to Vladimir Zhirov
Vladimir Zhirov wrote 20 lines
Hi, Reviewing a bunch of my string processing functions made me wonder what integer type should I use for position / length parameters. I used to plain "Integer" for this task, but looking through RTL and LCL code I noticed that sometimes SizeInt and even PtrInt are also used: - Length, Copy use Integer; - Pos, Delete, Insert use SizeInt; - UTF8Length, UTF8Pos, UTF8Copy, etc. use PtrInt. PtrInt use seems to be generally discouraged by FPC docs but does it apply to this particular case? The only benefit I see in SizeInt/PtrInt is larger size on 64-bit targets, that should theoretically make it possible to index larger strings. But is AnsiString type capable of holding more than High(Integer) bytes, or will it be so in the future?

Ansistring is not capable of keeping more than High(Integer) bytes.

Thus, the question basically is: what integer type is recommended to be used as index/length for future-proof string processing functions?

SizeInt should do just fine.

Michael.

Reply by email

Mattias Gaertner
to Michael Van Canneyt
Show quoted text 26 lines
On Sat, 18 Jun 2011, Vladimir Zhirov wrote: > Hi, > > Reviewing a bunch of my string processing functions > made me wonder what integer type should I use for > position / length parameters. > > I used to plain "Integer" for this task, but looking through > RTL and LCL code I noticed that sometimes SizeInt and > even PtrInt are also used: > - Length, Copy use Integer; > - Pos, Delete, Insert use SizeInt; > - UTF8Length, UTF8Pos, UTF8Copy, etc. use PtrInt. > > PtrInt use seems to be generally discouraged by FPC docs > but does it apply to this particular case? > > The only benefit I see in SizeInt/PtrInt is larger size on 64-bit > targets, that should theoretically make it possible to index > larger strings. But is AnsiString type capable of holding more > than High(Integer) bytes, or will it be so in the future? Ansistring is not capable of keeping more than High(Integer) bytes.

That's not true. At least 3*high(integer) compiles and runs fine here on 64bit:

var
  s: ansistring;
begin
  SetLength(s,3*High(integer));
  s[length(s)]:='A';
  s[length(s)]:='B';
  writeln(s[length(s)],' ',length(s));
end.

I don't know where the real limit is.

Show quoted text 5 lines
> Thus, the question basically is: what integer type > is recommended to be used as index/length > for future-proof string processing functions? SizeInt should do just fine.

Yes, because SizeInt is int64 under 64bit.

Mattias

Reply by email

Michael Van Canneyt
to Mattias Gaertner
Mattias Gaertner wrote 43 lines
On Sun, 19 Jun 2011 11:28:22 +0200 (CEST) Michael Van Canneyt wrote: > > > On Sat, 18 Jun 2011, Vladimir Zhirov wrote: > >> Hi, >> >> Reviewing a bunch of my string processing functions >> made me wonder what integer type should I use for >> position / length parameters. >> >> I used to plain "Integer" for this task, but looking through >> RTL and LCL code I noticed that sometimes SizeInt and >> even PtrInt are also used: >> - Length, Copy use Integer; >> - Pos, Delete, Insert use SizeInt; >> - UTF8Length, UTF8Pos, UTF8Copy, etc. use PtrInt. >> >> PtrInt use seems to be generally discouraged by FPC docs >> but does it apply to this particular case? >> >> The only benefit I see in SizeInt/PtrInt is larger size on 64-bit >> targets, that should theoretically make it possible to index >> larger strings. But is AnsiString type capable of holding more >> than High(Integer) bytes, or will it be so in the future? > > Ansistring is not capable of keeping more than High(Integer) bytes. That's not true. At least 3*high(integer) compiles and runs fine here on 64bit: var s: ansistring; begin SetLength(s,3*High(integer)); s[length(s)]:='A'; s[length(s)]:='B'; writeln(s[length(s)],' ',length(s)); end. I don't know where the real limit is.

Hm. I checked again.

Indeed, the hidden ansistring record contains
TAnsiRec = Packed Record
Ref,

     Len   : SizeInt;
     First : Char;
   end;

Which in fact means that the limit is the OS memory limit.

Michael.

Reply by email

Mattias Gaertner
to Michael Van Canneyt
Show quoted text 26 lines
[...] >> Ansistring is not capable of keeping more than High(Integer) bytes. > > That's not true. At least 3*high(integer) compiles and runs fine here on > 64bit: > > var > s: ansistring; > begin > SetLength(s,3*High(integer)); > s[length(s)]:='A'; > s[length(s)]:='B'; > writeln(s[length(s)],' ',length(s)); > end. > > I don't know where the real limit is. Hm. I checked again. Indeed, the hidden ansistring record contains TAnsiRec = Packed Record Ref, Len : SizeInt; First : Char; end; Which in fact means that the limit is the OS memory limit.

Now it is no longer hidden. ;)

Mattias

Reply by email