[Solved ]Multiply String / Equivalent of Space for strings?

Just starting out? Need help? Post your questions and find answers here.
hoerbie
Enthusiast
Enthusiast
Posts: 119
Joined: Fri Dec 06, 2013 11:57 am
Location: DE/BY/MUC

Re: [Solved ]Multiply String / Equivalent of Space for strings?

Post by hoerbie »

Thank you all, Stargate's test as console with Wilberts nice idea added as 4th:

MacOS BigSur on MacBook Pro (i7) compiled with PB5.73 64Bit:
148
231
191
105

Windows 10 on Acer Laptop (i5) compiled with PB5.73 32Bit:
183
273
255
122

Windows 10 on Acer Laptop (i5) compiled with PB6.00alpha3 64Bit (Asm):
331
500
274
126

Playing a lot with different string length and count, in average Wilberts method seems the fastest to me, but I didn't test the ReAllocate method of Stargate, because a static memory for me is not the normal use case of a short encapsulated function for one purpose.
And for normal use case the really simple first ReplaceString(Space()) method is not so bad, sadly the from PBs help promising in-place method is slower.
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: [Solved ]Multiply String / Equivalent of Space for strings?

Post by NicTheQuick »

The in-place method only makes sense if the search and the replace string have the same length. In all other cases shifting of characters behind or before can not be avoided.
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
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: [Solved ]Multiply String / Equivalent of Space for strings?

Post by Mijikai »

Here is my try :)
Note: It returns a string structure (*str.String).

Code: Select all

;PB 5.73 LTS

Procedure.i StringRepeat(String.s,Count.i)
  Protected str_size.i
  Protected str_bytes.i
  Protected str_copy.i
  Protected *str_buffer.Integer
  Protected *str_offset
  str_size = StringByteLength(String)
  str_bytes = (Count * str_size) << 1
  *str_buffer = AllocateMemory(str_bytes + 16,#PB_Memory_NoClear)
  If *str_buffer
    str_bytes = str_size
    *str_buffer + 8
    *str_offset = *str_buffer
    CopyMemory(@String,*str_buffer,str_size)
    *str_offset + str_size
    str_copy = 1
    Repeat
      CopyMemory(*str_buffer,*str_offset,str_size)
      *str_offset + str_size
      str_size << 1
      str_copy << 1
    Until str_copy >= Count
    PokeU(*str_buffer + (str_bytes * Count),#Null)
    *str_buffer - 8
    *str_buffer\i = *str_buffer + 8
    ProcedureReturn *str_buffer
  EndIf
  ProcedureReturn #Null
EndProcedure

;Global *dummy.String
;*dummy = StringRepeat("123",5)
;Debug *dummy\s
Here is the test code with my sample added:

Code: Select all

otxt.s = "123 "
ocnt.i = 100000;0

Procedure.s StringRepeat1(txt.s, cnt.i)
	ProcedureReturn ReplaceString(Space(cnt)," ",txt)
EndProcedure

Procedure.s StringRepeat2(txt.s, cnt.i)
	Protected txl.i,new.s
	txl = Len(txt)
	new = Space(txl*cnt)
	ReplaceString(new,Space(txl),txt,#PB_String_InPlace)
	ProcedureReturn new
EndProcedure

;infratec: https://www.purebasic.fr/english/viewtopic.php?p=572925#p572925
Procedure.s StringRepeat3(String$, Count.i)
	Protected *Buffer, *Ptr, i.i
	*Buffer = AllocateMemory(StringByteLength(string$) * Count + SizeOf(Character), #PB_Memory_NoClear)
	If *Buffer
		*Ptr = *Buffer
		Count - 1
		CopyMemoryString(@String$, @*Ptr)
		For i = 1 To Count
			CopyMemoryString(@String$)
		Next i
	EndIf
	ProcedureReturn PeekS(*Buffer)
EndProcedure

;STARGÅTE: https://www.purebasic.fr/english/viewtopic.php?p=572933#p572933
Procedure.s StringRepeat4(String$, Count.i)
	Protected StrLen.i, *Ptr, i.i
	Static *Buffer
	StrLen = StringByteLength(string$)
	If StrLen
		If *Buffer = 0 Or StrLen * Count + SizeOf(Character) > MemorySize(*Buffer)
			*Buffer = ReAllocateMemory(*Buffer, StrLen * Count + SizeOf(Character), #PB_Memory_NoClear)
		EndIf
		If *Buffer
			*Ptr = *Buffer
			Count - 1
			CopyMemoryString(@String$, @*Ptr)
			For i = 1 To Count
				CopyMemoryString(@String$)
			Next i
		EndIf
	EndIf
	ProcedureReturn PeekS(*Buffer)
EndProcedure

;wilbert: https://www.purebasic.fr/english/viewtopic.php?p=572936#p572936
Procedure.s StringRepeat5(String$, Count.i)
	Protected Dim Buffer.c(Len(String$) * Count)
	Protected *Ptr = @Buffer(0)
	CopyMemoryString(#Empty$, @*Ptr)
	While Count
		CopyMemoryString(@String$)
		Count - 1
	Wend
	ProcedureReturn PeekS(@Buffer(0))
EndProcedure

;Mijikai https://www.purebasic.fr/english/viewtopic.php?p=573046#p573046
Procedure.i StringRepeat6(String.s,Count.i)
  Protected str_size.i
  Protected str_bytes.i
  Protected str_copy.i
  Protected *str_buffer.Integer
  Protected *str_offset
  str_size = StringByteLength(String)
  str_bytes = (Count * str_size) << 1
  *str_buffer = AllocateMemory(str_bytes + 16,#PB_Memory_NoClear)
  If *str_buffer
    str_bytes = str_size
    *str_buffer + 8
    *str_offset = *str_buffer
    CopyMemory(@String,*str_buffer,str_size)
    *str_offset + str_size
    str_copy = 1
    Repeat
      CopyMemory(*str_buffer,*str_offset,str_size)
      *str_offset + str_size
      str_size << 1
      str_copy << 1
    Until str_copy >= Count
    PokeU(*str_buffer + (str_bytes * Count),#Null)
    *str_buffer - 8
    *str_buffer\i = *str_buffer + 8
    ProcedureReturn *str_buffer
  EndIf
  ProcedureReturn #Null
EndProcedure

OpenConsole()
Define I

start.i = ElapsedMilliseconds()
For I = 1 To 1000
	ret.s = StringRepeat1(otxt, ocnt)
Next
PrintN( Str(ElapsedMilliseconds()-start) )

start.i = ElapsedMilliseconds()
For I = 1 To 1000
	ret.s = StringRepeat2(otxt, ocnt)
Next
PrintN( Str(ElapsedMilliseconds()-start) )

start.i = ElapsedMilliseconds()
For I =1 To 1000
	ret.s = StringRepeat3(otxt, ocnt)
Next
PrintN( Str(ElapsedMilliseconds()-start) )

start.i = ElapsedMilliseconds()
For I =1 To 1000
	ret.s = StringRepeat4(otxt, ocnt)
Next
PrintN( Str(ElapsedMilliseconds()-start) )

start.i = ElapsedMilliseconds()
For I =1 To 1000
	ret.s = StringRepeat5(otxt, ocnt)
Next
PrintN( Str(ElapsedMilliseconds()-start) )

start.i = ElapsedMilliseconds()
For I =1 To 1000
	*str_ret.String = StringRepeat6(otxt, ocnt)
Next
PrintN( Str(ElapsedMilliseconds()-start) )

Input()
Last edited by Mijikai on Fri Jul 30, 2021 6:03 pm, edited 1 time in total.
User avatar
Lord
Addict
Addict
Posts: 847
Joined: Tue May 26, 2009 2:11 pm

Re: [Solved ]Multiply String / Equivalent of Space for strings?

Post by Lord »

Hi!
Mijikai wrote:

Code: Select all

;PB 7.73 LTS

Procedure.i StringRepeat(String.s,Count.i)
...
...
Where can I get this new PB verion? :wink:
Image
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: [Solved ]Multiply String / Equivalent of Space for strings?

Post by Mijikai »

Lord wrote: Fri Jul 30, 2021 9:56 am Where can I get this new PB verion? :wink:
Ups XD
I will correct it thx.
Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

Re: [Solved ]Multiply String / Equivalent of Space for strings?

Post by Olli »

I do not understand these people who continue to give any sources after a 'solved' tag has rung.

Also, I do not understand myself, so...

Code: Select all

Procedure.S RepStr(n, x$)
 Length = Len(x$)
 Final = Length * n
 Repeat
  x$ + x$
  x$ + x$
  Length << 2
 Until Length > Final
 ProcedureReturn Left(x$, Final)
EndProcedure
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: [Solved ]Multiply String / Equivalent of Space for strings?

Post by BarryG »

Olli wrote: Sat Jul 31, 2021 1:34 amI do not understand these people who continue to give any sources after a 'solved' tag has rung.
Putting "Solved" can sometimes stop better code being posted, because people may just ignore the thread. For example:

viewtopic.php?f=16&t=77699

Imagine if the OP marked my answer as "Solved" for that thread. If he did, then Paul might have skipped the thread and not posted his shorter and superior cross-platform code; and I also wouldn't have learned that ICO images can be loaded as PNGs.

So, "Solved" tagging can actually be a bad idea for certain things.
Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

Re: [Solved ]Multiply String / Equivalent of Space for strings?

Post by Olli »

What a suppository...
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: [Solved ]Multiply String / Equivalent of Space for strings?

Post by BarryG »

Olli wrote: Sat Jul 31, 2021 1:56 amWhat a suppository...
No need to be nasty and rude. My point is very valid, and I've seen it happen before.
Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

Re: [Solved ]Multiply String / Equivalent of Space for strings?

Post by Olli »

I was talking about the code which appeared in the subject you target ! Very good. And your analysis is valid, else you did not read obviously a code I add after a 'solved' tag, as I seem to mock.

'rude' is a term I understand, and whom I understand the implicit limits : you are perfectly right. Sure the translating of my expressions shows the actual limits of Google Translate. And I must consider these limits, being aware about my english grammar level which is rising to an altitude at the antipodes.

Plus, I did not check my source code...
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: [Solved ]Multiply String / Equivalent of Space for strings?

Post by BarryG »

I don't quite follow what you mean, but this is what "suppository" means:

https://en.wikipedia.org/wiki/Suppository

It looks like you were saying that about my post where I discussed "Solved" tagging. Maybe you meant something else?
Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

Re: [Solved ]Multiply String / Equivalent of Space for strings?

Post by Olli »

Ow? I didn't think the limitations of the online translator were so exhaustive.

What a good guess for your main post (in french, guess = "supposition"). And, at the same time, what a good solution from Paul in the link you mentionned above.

Now, I hope my source code has not a so bad speed, comparing to the other shares...
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: [Solved ]Multiply String / Equivalent of Space for strings?

Post by #NULL »

Olli, I stopped reading your posts at some point because I have no idea what you are saying, never. Try deepl, it should be much better.
Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

Re: [Solved ]Multiply String / Equivalent of Space for strings?

Post by Olli »

#Null

I had tried the software DeepL but the online version was not available at the time of testing. With the evolution of the browser, the translation form now works. So thanks for the feedback.
Post Reply