Recent

Author Topic: Ansistrings zero element  (Read 11192 times)

anna

  • Sr. Member
  • ****
  • Posts: 426
Ansistrings zero element
« on: October 16, 2010, 11:17:32 am »
I'm using Lazarus-0.9.29-27550-fpc-2.4.0-20101003-win32.exe

I don't remember when actually it was changed...

But previously this code works fine:
Code: [Select]
{$H+}  
var
  s:string;
begin
  s:='123';
  writeln(s[0]); {must display [b]1[/b]}
end.


Now I get error:
Error: Element zero of an ansi\wide- or longstring can't be accessed, use (set)length instead


Why?!?!

Zeroth element of shortstring contain its length not of ansistrings!!!
« Last Edit: October 16, 2010, 11:20:04 am by anna »
WinXP SP3 Pro Russian 32-bit (5.1.2600)

anna

  • Sr. Member
  • ****
  • Posts: 426
Re: Ansistrings zero element
« Reply #1 on: October 16, 2010, 11:24:20 am »
It annoyed me, that I must always remember about UTF-ANSI convertion. But now I must monitor what is first element 0th or 1st in addition.

Developers, STOP strings changins!!!!!!!!!!!!!!
WinXP SP3 Pro Russian 32-bit (5.1.2600)

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1927
Re: Ansistrings zero element
« Reply #2 on: October 16, 2010, 12:01:18 pm »
The first character of an AnsiString has always been at position 1 afair!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
This is not new!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

;-)


anna

  • Sr. Member
  • ****
  • Posts: 426
Re: Ansistrings zero element
« Reply #3 on: October 16, 2010, 12:57:54 pm »
Please, run attached project. When execution will stop on a breakpoint, open Evaluate/Modify window and type s[0] and press Evaluate button
WinXP SP3 Pro Russian 32-bit (5.1.2600)

cdbc

  • Hero Member
  • *****
  • Posts: 1090
    • http://www.cdbc.dk
Re: Ansistrings zero element
« Reply #4 on: October 16, 2010, 12:59:13 pm »
@theo +1
@anna: get your facts strait before you wine!
Pascal strings have always been 1-based! 0-th element is a pointer to the entire string (memory alloc).
Regards Benny
« Last Edit: October 16, 2010, 01:09:36 pm by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

anna

  • Sr. Member
  • ****
  • Posts: 426
Re: Ansistrings zero element
« Reply #5 on: October 16, 2010, 01:10:13 pm »
@theo +1
@anna: get your facts strait before you wine!

Regards Benny
Null-terminated strings always are zero-based.
AnsiString is a pointer so length may be more 64 KBytes.
WinXP SP3 Pro Russian 32-bit (5.1.2600)

anna

  • Sr. Member
  • ****
  • Posts: 426
Re: Ansistrings zero element
« Reply #6 on: October 16, 2010, 01:11:34 pm »
Pchar is also pointer. Pchar it seems to be correct .  Yet...
« Last Edit: October 16, 2010, 01:14:07 pm by anna »
WinXP SP3 Pro Russian 32-bit (5.1.2600)

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9913
  • Debugger - SynEdit - and more
    • wiki
Re: Ansistrings zero element
« Reply #7 on: October 16, 2010, 01:38:34 pm »
string => always 1 based, always been
pchar => pointer-to-array-of-char => 0 based, arrays are 0 based by default

GDB:
@anna: you are right in gdb a[0] fo9r string works

but it's a bug.
GDB is a debugger originally written for C, not pascal. GDB has some ability to deal with any language, including pascal.

Unfortunately at current it's not possible to tell gdb that pascal strings are 1 based. Or maybe it is possible (dwarf 3), but not yet implemented.

So in gdb all string indices are wrong by 1.

And yes, it makes debugging strings very inconvenient. But there is no quick fix:
- dwarf3 is under development, it will one day be supported by fpc. and probably gdb supports it too. But it doesn;t look like anytime soon
- The alternative is to write an entire debugger from scratch. There are some projects, but again not even close to anytime soon.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11459
  • FPC developer.
Re: Ansistrings zero element
« Reply #8 on: October 16, 2010, 01:46:15 pm »
@anna: you are right in gdb a[0] fo9r string works

Accessing element 0 of a string is supported for shortstring only. And in {$H+} string is not shortstring.

Moreover, even though it still supported (after 16 years legacy, since deprecated since TP/D1 times), one should always use length() and setlength() to get/set the length of strings.

This behaviour has been like that since 1997-98 when ansistring was introduced.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9913
  • Debugger - SynEdit - and more
    • wiki
Re: Ansistrings zero element
« Reply #9 on: October 16, 2010, 02:12:00 pm »
@anna: you are right in gdb a[0] fo9r string works
Accessing element 0 of a string is supported for shortstring only. And in {$H+} string is not shortstring.

@marcov: Unfortunately you cut my quote short. I explained that in GDB due to a *bug* the first character in the string "a" is indeed returned by a[0].

In your example a[0] for shortstring is not the first char, but a special field containing the length of the string.

However Anna was asking for the first char in "a"
Code: [Select]
  writeln(s[0]); {must display [b]1[/b]}

and later pointed to the debugger as a >>working<< example of a[0]
open Evaluate/Modify window and type s[0] and press Evaluate button

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2584
Re: Ansistrings zero element
« Reply #10 on: October 16, 2010, 02:59:38 pm »
unfortunately in the debug info, strings are encoded as pchar. So for gdb there is no difference between a pchar and a string, therefore the index starts at 0.
Lazarus cannot help here since it doesn't know if a pchar is a "real" pchar or a string
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

 

TinyPortal © 2005-2018