Page 1 of 2

how is a string terminated ?

Posted: Fri Jan 01, 2021 8:24 pm
by ludoke
How is a string in an array of strings terminated ?
Do I have to add a certain character myself #CR$ ? this works .
Do I have to determine the string length first?
What is the best way ?

Code: Select all

Dim text.s(100)  ;array of string
l.i  ;length string
text(1)="text1"+ #CR$ ;add an extra #CR$
text(2)="This is text2"
l=Len(text(2))*2  ;length of string
*t=@text(2)
While n=< l                            ;this work ,but need length of string
  kar.a=PeekA(*t+n)
  Debug Chr(kar)
  n=n+2
Wend  
;------------------------------------
n=0   ;reset n
*t=@text(1)         
Repeat
   kar.a=PeekA(*t+n)
   Debug Chr(kar)
   n=n+2
Until kar=#CR     ;
debug kar             ;read 13 ,it works

Re: how is a string terminated ?

Posted: Fri Jan 01, 2021 9:29 pm
by mk-soft
An array of string is an array of pointer to string. A string variable is always the pointer to the string. Thus, the strings in the array are necessarily one after the other in the memory.
If there is not enough space for the new string in the array, a new memory is requested and the old one is released.

Code: Select all

Dim text.s(10)

text(0) = "Hello"
text(1) = "World"

*pArray = @text()
*text1 = PeekI(*pArray)
*text2 = PeekI(*pArray + SizeOf(integer))

Debug "Pointer 1 = " + Hex(*text1) + " String = " + PeekS(*text1)
Debug "Pointer 2 = " + Hex(*text2) + " String = " + PeekS(*text2)

text(0) = "Hello .......... xxxxxxxxxxxxxxxxxxxx "

*text1 = PeekI(*pArray)
*text2 = PeekI(*pArray + SizeOf(integer))

Debug "Pointer 1 = " + Hex(*text1) + " String = " + PeekS(*text1)
Debug "Pointer 2 = " + Hex(*text2) + " String = " + PeekS(*text2)

Re: how is a string terminated ?

Posted: Fri Jan 01, 2021 9:30 pm
by skywalk
They are terminated with two 0 bytes for unicode compilation.

Code: Select all

Structure s1
  s$[3]
EndStructure
Dim s$(2)
Define s.s1
For i = 0 To 2
  s$(i)   = Str(i) + "A"
  s\s$[i] = Str(i) + "S"
Next i
ShowMemoryViewer(@s$(0),255)
CallDebugger
ShowMemoryViewer(@s\s$,255)

Re: how is a string terminated ?

Posted: Fri Jan 01, 2021 9:55 pm
by mk-soft
Forgot to mention. How skywalk

Don't forget!
You cannot directly add any characters to the string in memory. As shown above, if there is not enough space, a new memory is allocated for the string.

Re: how is a string terminated ?

Posted: Sat Jan 02, 2021 9:53 am
by ludoke
thanks,

my problem is that I want to read a string from an array, character by character.

Depending on the character I have to do something.
I can first determine the length with LEN (string), but that is more time.

I can add a #CR when creating the strings in the array, in the example I added it later, but it turns out to work.
But maybe it doesn't always work as MK-soft says.

at Skywalk,
I found in C, strings are terminated with a null character (ASCII code 0).Is that what you mean ?

Re: how is a string terminated ?

Posted: Sat Jan 02, 2021 10:57 am
by RASHAD
Hi

Code: Select all

Dim st.s(1)
st(0) = "Hellow World"
For i = 0 To StringByteLength(st(0)) Step 2
  Debug Chr(PeekA(@st(0)+i))
Next

Re: how is a string terminated ?

Posted: Sat Jan 02, 2021 11:15 am
by Saki
Well, with strings it's important that you understand exactly how it works, it's easy to make mistakes.
For example, you can also create a string from spaces, i.e. an empty one, which is then automatically terminated at the end.
In this string you can insert a #LF$ at any desired position or PokeW two zeros to terminate it.
If your strings never exceed a certain maximum you can pre define and cut them arbitrarily.
A small overhead does not matter.
If you use strings in PB you will always have an overhead, which is much bigger, but not disturbing.
The problem is when you destroy the termination at the end.
Then PB searches for the next occurring two zeros in memory.
You always have to look at what you need.

Re: how is a string terminated ?

Posted: Sat Jan 02, 2021 11:33 am
by ludoke
Rashad,

Dim st.s(1)
st(0) = "Hellow World"
n=Len(st(0))
Debug "len= "+n ; gives 12
For i = 0 To 39 Step 2
Debug Chr(PeekA(@st(0)+i))
Next


the length of the string =12 ,but 2 bytes/char=24 ?
It works with 22 but also with 24 till 39 ,then a strange character comes.

You need the length of the string,or you must have a special character at the end of the string to indicate the end.
I'm still trying to learn a lot.

As Saki,explain there are some extra places ,but I will add a #LF$ or #CR$ to find the end of the string .
Thanks to all .

Re: how is a string terminated ?

Posted: Sat Jan 02, 2021 12:06 pm
by Cezary
Maybe the safest (but slower) method would be not to use pointers?

Code: Select all

Dim st.s(1)
st(1) = "Hellow World"
For i = 0 To 14
  Debug Mid(st(1), i, 1) + " " + Asc(Mid(st(1), i, 1))
Next
In addition, the end of a string can be detected as long as there are no characters with code zero in it.

Re: how is a string terminated ?

Posted: Sat Jan 02, 2021 12:33 pm
by mk-soft
Also under Purebasic, the strings are terminated with a NULL byte (word unicode mode).
The data type char under PB is a word in unicode mode.

You can change individual characters, but you cannot add more characters.

Example over pointers like 'C'

Code: Select all

;-TOP

Structure ArrayOfChar
  c.c[0] ; The value NULL defines an array of unknown size.
EndStructure

Dim text.s(1)
text(0) = "Hello World!"

Define *pText.ArrayOfChar = @text(0)
Define index = 0

Repeat
  If *pText\c[index] = 0
    ;TODO
    Break
  EndIf
  
  Debug "Index " + index + " Char: " + *pText\c[index]
  
  Select *pText\c[index]
    Case ' '
      *pText\c[index] = '.'
      
  EndSelect
  
  index + 1
ForEver

Debug text(0)

Re: how is a string terminated ?

Posted: Sat Jan 02, 2021 12:45 pm
by Saki

Code: Select all

Dim st.s(1)
st(0) = "Hello World1234"
n=Len(st(0))
Debug "len= "+n
Repeat
  ii=PeekW(@st(0)+i)
  Debug Chr(ii)
  i+2
Until Not ii

Re: how is a string terminated ?

Posted: Sat Jan 02, 2021 1:02 pm
by NicTheQuick
This is how I usually iterate over strings:

Code: Select all

EnableExplicit

Define text.s = "Hello World"

Define *c.Character = @text

While *c\c
	Debug *c\c
	*c + SizeOf(Character)
Wend
The while-loop automatically stops if the null-characters was reached.

Re: how is a string terminated ?

Posted: Sat Jan 02, 2021 1:12 pm
by Saki
Nick, Yeah, more professional....
Perfect !

I would have done it the same way, but I didn't want to confuse him, because the question was very simple.

Re: how is a string terminated ?

Posted: Sat Jan 02, 2021 1:21 pm
by mk-soft
Mine is not professional. cry :cry: :mrgreen:

Re: how is a string terminated ?

Posted: Sat Jan 02, 2021 1:24 pm
by Saki
Sorry, your is also professional, how ever ! 8)
I had looked at it all only for a minute as I'm still working here....

But I must also confess that I often use the peek, poke methods out of sheer laziness as well :cry: