how is a string terminated ?

Just starting out? Need help? Post your questions and find answers here.
User avatar
NicTheQuick
Addict
Addict
Posts: 1503
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: how is a string terminated ?

Post by NicTheQuick »

@mk-soft:
The indexed version is nice if you need random access to the characters or if you often have to jump around in the string. But I think it also has a slightly overhead if all you need is to iterate once through the string because every time the index has to be recalculated and added to the base pointer of the string.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: how is a string terminated ?

Post by mk-soft »

You're right about that, NicTheQuick.

The handling of pointers must be practised, as one can also make mistakes very quickly here, which can lead to a crash of the programme.
PureBasic does not check the allowed access to the memory. That is the task of the programmer.

The changes from string to size must always be done with the string functions of PureBasic, as only this manages the memory for the string internally.
But this leads to the problem of working with pointers to the string, because you always get the pointer to the string and not the pointer to the variable where the pointer to the string is stored.

But here, too, there are possibilities to work with String ByREF (pointer to the string pointer).

I can also provide examples here if anyone is interested.

I don't want to demonise working with pointers here, because working with pointers is an elegant method of creating optimised programmes.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
NicTheQuick
Addict
Addict
Posts: 1503
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: how is a string terminated ?

Post by NicTheQuick »

mk-soft wrote:I can also provide examples here if anyone is interested.
I only know about this way:

Code: Select all

EnableExplicit

Define text.String\s = "Hello World"

; The pointer of the string variable
Debug @text

; The pointer to the content of the string
Debug @text\s

; And another way to get the pointer to the content
Debug PeekI(@text)
but I don't think it works with native strings. Except they are fixed strings.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: how is a string terminated ?

Post by RASHAD »

To neglect control characters

Code: Select all

Dim st.s(1)
st(0) = "Hellow "+Chr(8)+Chr(9)+"World"
For i = 0 To StringByteLength(st(0)) Step 2
  char = PeekA(@st(0)+i)
  If char > 31
    Debug Chr(PeekA(@st(0)+i))
  EndIf
Next
Egypt my love
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: how is a string terminated ?

Post by mk-soft »

Unfortunately there is no native function in Purebasic to get the address of a native variable. However, you can create this yourself via ASM.
So you can also pass native variables ByREF to a function.

Unfortunately it is not as nice to implement as *Var = VarPtr(Var).

Code: Select all

;-TOP

Macro VarPtr(_Var_, _Pointer_)
  EnableASM
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    lea rax, _Var_
    mov _Pointer_, rax
  CompilerElse
    lea eax, _Var_
    mov _Pointer_, eax
  CompilerEndIf  
  DisableASM
EndMacro

; ****

Procedure.i MyFunction(*StringByRef.string)
  
  *StringByRef\s + " I Like PureBasic ;)"
      
EndProcedure

; ----

Define text.s, *pText.string

text = "Hello World!"
VarPtr(text, *pText)

Debug "Adress of variable = " + *pText
Debug "Contents of variable = " + *pText\s

Debug "Function with String ByREF"
MyFunction(*pText)
; MyFunction(VarPtr(Text)) ; Features Request
Debug "Result = " + text

; ----

Define text2.String\s = "Structured string Variable!"
Debug "Function with String ByREF (Structured string Variable)"
MyFunction(text2)
Debug "Result = " + text2\s
Last edited by mk-soft on Sat Jan 02, 2021 3:58 pm, edited 1 time in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: how is a string terminated ?

Post by Saki »

Don't make it so complicated, otherwise the questioner will have a crisis. :wink: :lol:
地球上の平和
ludoke
Enthusiast
Enthusiast
Posts: 155
Joined: Fri Jul 08, 2016 5:35 pm
Location: Essen (Belgium)

Re: how is a string terminated ?

Post by ludoke »

I always want to make it as simple and as fast as possible.
I like the code from NicTheQuick,but I also want to thank the others for their thinking.

Code: Select all

EnableExplicit

Define text.s = "Hello World"
Define kar.s
Define *c.Character = @text

While *c\c
 ; Debug *c\c ; 87=W 
  kar.s= Chr(*c\c);instead of chr (*c\c) I can also use the ascii, which is again some code savings I think?
  Debug kar
If *c\c=87:Debug "87 found":endif  ;W found
If kar="W":Debug "W found":EndIf  
If kar="e":Debug "e found":EndIf
   *c + SizeOf(Character)
Wend
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: how is a string terminated ?

Post by mk-soft »

Its Unicode not ASCII since PB v5.5

You don't need the function Chr(...)

Code: Select all

If *c\c = 'W' :Debug "W (87) found":EndIf  ;W found
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: how is a string terminated ?

Post by Saki »

The Pointer method is not faster than the Peek-Poke method.

I often compared this because I wanted the fastest method.
Many years ago this should have been the case with PB, but I don't really know.
Peek-Poke is like Goto just very Basic like.

It becomes very slow with Peek$, Poke$, ReadString, WriteString,
this should be avoided if possible when dealing with larger amounts of data, if you want high speed. :wink:
地球上の平和
infratec
Always Here
Always Here
Posts: 7577
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: how is a string terminated ?

Post by infratec »

Saki wrote:The Pointer method is not faster than the Peek-Poke method.

Code: Select all

; a.c = PeekC(*Buffer)
  PUSH   dword [p_Buffer]
  CALL  _PB_PeekC@4
  MOV    word [v_a],ax

Code: Select all

; a.c = *Buffer\c
  MOV    ebp,dword [p_Buffer]
  MOVZX  eax,word [ebp]
  MOV    word [v_a],ax
Without knowing the code of _PB_PeekC@4 :

I'm 100% sure the pointer method is faster.

Compiled with /commented PB 5.73 x86 in Windows 10 x64
User avatar
Mijikai
Addict
Addict
Posts: 1517
Joined: Sun Sep 11, 2016 2:17 pm

Re: how is a string terminated ?

Post by Mijikai »

Pointers are faster.
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: how is a string terminated ?

Post by Saki »

In practical use with very large amounts of data I often use peek and poke first until the code runs smoothly.
Then usually comes the optimization.
The disappointment was always quite big to see that it looks visually better with pointers, but no speed advantage was achieved.
地球上の平和
Post Reply