Shouldn't this return an empty string?

Just starting out? Need help? Post your questions and find answers here.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Shouldn't this return an empty string?

Post by Mistrel »

I'm writing a preceding null in front of the string I'm poking. Shouldn't I get a blank string? The character doesn't seem to be written at all.

Code: Select all

*Mem=AllocateMemory(500)

String.s=""
For i=1 To 6
  String.s+"7"
Next i

;/ No preceding null is written
PokeS(*Mem+16,Chr(0)+String.s,Len(String.s)+1)

;/ Chr(6) is correctly written
;PokeS(*Mem+16,Chr(6)+String.s,Len(String.s)+1)

Debug PeekS(*Mem+16)

Debug PeekC(*Mem+16)
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Shouldn't this return an empty string?

Post by IdeasVacuum »

Try this:

Code: Select all

PokeS(*Mem, Chr(Null) + sString, (Len(sString) + 2) )
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Shouldn't this return an empty string?

Post by Demivec »

Mistrel wrote:I'm writing a preceding null in front of the string I'm poking. Shouldn't I get a blank string? The character doesn't seem to be written at all.
A null, or Chr(0), isn't a string it's a terminator. It shows the end of a string. That being said, I agree that it should function as you expected.

Compare these two:

Code: Select all

string.s = "777777"
;/ No preceding null is written
PokeS(*Mem+16,Chr(0)+string.s,Len(string.s)+1)
Debug PeekS(*Mem+16)
Debug PeekC(*Mem+16)

;/It works as you would expect with a string constant
PokeS(*Mem+16,Chr(0)+"333",Len("333")+1)
Debug PeekS(*Mem+16)
Debug PeekC(*Mem+16)
It looks like it may be a bug.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Shouldn't this return an empty string?

Post by IdeasVacuum »

A zero could be a bona fide member of a string? I don't know, but Null = Null and should always work.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Vitor_Boss®
User
User
Posts: 81
Joined: Thu Sep 23, 2010 4:22 am

Re: Shouldn't this return an empty string?

Post by Vitor_Boss® »

Demivec wrote:
Mistrel wrote:I'm writing a preceding null in front of the string I'm poking. Shouldn't I get a blank string? The character doesn't seem to be written at all.
A null, or Chr(0), isn't a string it's a terminator. It shows the end of a string. That being said, I agree that it should function as you expected.

Compare these two:

Code: Select all

string.s = "777777"
;/ No preceding null is written
PokeS(*Mem+16,Chr(0)+string.s,Len(string.s)+1)
Debug PeekS(*Mem+16)
Debug PeekC(*Mem+16)

;/It works as you would expect with a string constant
PokeS(*Mem+16,Chr(0)+"333",Len("333")+1)
Debug PeekS(*Mem+16)
Debug PeekC(*Mem+16)
It looks like it may be a bug.
If first code are compiled in ASCII the NULL char isn't write? On Unicode mode all char less than 255 use a Char(0) before the number, like 00FF for Chr(255), you tried use double null chars in unicode programs?
Sorry by bad English.
HP Pavilion DV6-2155DX: Intel i3-330m 2.13 / 4GB DDR3 / 500GB Sata2 HD / Display 15.6" LED / Win7 Ultimate x64 / PB 4.50 x86 demo.
freak
PureBasic Team
PureBasic Team
Posts: 5944
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Shouldn't this return an empty string?

Post by freak »

Chr(0) is cannot be part of a string. It marks the end of a string. So all you get is an empty string.

> Chr(0)+string.s

You add nothing to the beginning string.s. So you end up with the original string.

> Chr(0)+"333"

The reason this seems to work is because it is resolved at compile-time. Its a side-effect of the optimization for literal Chr() calls.
quidquid Latine dictum sit altum videtur
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Shouldn't this return an empty string?

Post by Mistrel »

freak wrote:You add nothing to the beginning string.s. So you end up with the original string.
I guess it makes sense. Chr(0) resolves to an empty string.

That's not exactly what I was expecting but it is the logical result. I've adjusted my code to PokeC the null and then PokeS the string.
freak wrote:The reason this seems to work is because it is resolved at compile-time. Its a side-effect of the optimization for literal Chr() calls.
Would you classify this as an actual bug or an acceptable one?
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Shouldn't this return an empty string?

Post by IdeasVacuum »

...I think we have all missed the obvious question then - why are you trying to prefix a null?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply