InsertString: #PB_String_InPlace would be useful

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

InsertString: #PB_String_InPlace would be useful

Post by netmaestro »

ReplaceString has this option and today I find a need for it with InsertString, but it isn't available. I wrote my own:

Code: Select all

Procedure.s UpdateString(target$, insert$, pos)
  *target.Character = @target$ + (pos*SizeOf(character))
  *insert.Character = @insert$
  While *insert\c
    *target\c = *insert\c
    *target+SizeOf(Character)
    *insert+SizeOf(Character)
  Wend
  ProcedureReturn target$
EndProcedure

a$ = "Hello World!"
b$ = "Girls"

a$ = UpdateString(a$, b$, 6)

Debug a$
But I think having the core functionality would be superior to this.

[edit] Made code unicode-friendly before somebody yells at me :mrgreen:
BERESHEIT
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: InsertString: #PB_String_InPlace would be useful

Post by ts-soft »

netmaestro wrote:[edit] Made code unicode-friendly before somebody yells at me :mrgreen:
better is it :wink:
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: InsertString: #PB_String_InPlace would be useful

Post by STARGÅTE »

:?: that is no insert, it is an overwrite.

and your code don't work like ReplaceString with #PB_String_InPlace without return:

Code: Select all

Define Text.s = "Hallo World!"
ReplaceString(Text, "World", "Girls", #PB_String_InPlace)
Debug Text
what you want would be something like this:

Code: Select all

Procedure OverwriteString(*String, *NewString, Position.i)
	If MemoryStringLength(*NewString) + Position - 1 <= MemoryStringLength(*String) And Position > 0
		CopyMemory(*NewString, *String+(Position-1)*SizeOf(Character), MemoryStringLength(*NewString)*SizeOf(Character))
	EndIf
EndProcedure

Define Text.s = "Hello World!"
Define NewString.s = "Girls"
OverwriteString(@Text, @NewString, 7)
Debug Text
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: InsertString: #PB_String_InPlace would be useful

Post by netmaestro »

Destructive insert = overwrite, that's just semantics. I prefer it to return the result, which is why my code works as it does. If the team implements the flag, I expect it will work in place as ReplaceString does.
BERESHEIT
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: InsertString: #PB_String_InPlace would be useful

Post by netmaestro »

what you want would be something like this:
Actually, what I'd want would be something like this:

http://www.purebasic.fr/english/viewtop ... 59#p362759

where the execution time is cut by two thirds.
BERESHEIT
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: InsertString: #PB_String_InPlace would be useful

Post by Little John »

netmaestro wrote:

Code: Select all

Procedure.s UpdateString(target$, insert$, pos)
  *target.Character = @target$ + (pos*SizeOf(character))
  *insert.Character = @insert$
  While *insert\c
    *target\c = *insert\c
    *target+SizeOf(Character)
    *insert+SizeOf(Character)
  Wend
  ProcedureReturn target$
EndProcedure

a$ = "Hello World!"
b$ = "Girls"

a$ = UpdateString(a$, b$, 6)

Debug a$
If insert$ is too long, or pos is too big, respectively, then accidentally the excess characters of insert$ will be written behind the end of target$. And additional check will prevent this:

Code: Select all

While *insert\c And *target\c
Regards, Little John

PS: I saw that you wrote an assembly procedure now, but since I'm not good in understanding assembly at 8 o'clock in the morning, I commented on this PB code. :-)
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: InsertString: #PB_String_InPlace would be useful

Post by freak »

If you don't care about the length check on the target, you can just use PokeS() ;)
quidquid Latine dictum sit altum videtur
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: InsertString: #PB_String_InPlace would be useful

Post by netmaestro »

If you don't care about the length check on the target, you can just use PokeS()
That was my first thought, but I rejected it because PokeS() will write a terminating zero, effectively ending the string at the end of my insert, is that right? If not I went to a lot of trouble for nothing.
BERESHEIT
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: InsertString: #PB_String_InPlace would be useful

Post by luis »

It does so (adding the zero).
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: InsertString: #PB_String_InPlace would be useful

Post by Demivec »

netmaestro wrote:
If you don't care about the length check on the target, you can just use PokeS()
That was my first thought, but I rejected it because PokeS() will write a terminating zero, effectively ending the string at the end of my insert, is that right? If not I went to a lot of trouble for nothing.
@netmaestro: PokeS() always writes a terminating zero. I favor an overwrite method also. Preferably one replaces characters without making the target string longer or shorter. Does your ideal solution include making the target string longer if placing the new characters near the end?
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: InsertString: #PB_String_InPlace would be useful

Post by netmaestro »

Does your ideal solution include making the target string longer if placing the new characters near the end?
No, because it's for updating an ISAM file with fixed-length records. The length of the target string never changes.
BERESHEIT
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: InsertString: #PB_String_InPlace would be useful

Post by Little John »

netmaestro wrote:That was my first thought, but I rejected it because PokeS() will write a terminating zero, effectively ending the string at the end of my insert, is that right?
What about CopyMemory() or CopyMemoryString()?

Regards, Little John
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: InsertString: #PB_String_InPlace would be useful

Post by Demivec »

Little John wrote:
netmaestro wrote:That was my first thought, but I rejected it because PokeS() will write a terminating zero, effectively ending the string at the end of my insert, is that right?
What about CopyMemory() or CopyMemoryString()?
@Little John: CopyMemoryString() will always place a terminating zero also. CopyMemory() would be the only viable choice.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: InsertString: #PB_String_InPlace would be useful

Post by netmaestro »

CopyMemory is usable but the code you have to write to allow for unicode, while it works, looks ugly to me. CopyMemoryString also writes the terminating zero, so it's no use either. I just want the flag.
BERESHEIT
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: InsertString: #PB_String_InPlace would be useful

Post by wilbert »

It would make sense to have a flag for CopyMemoryString to not write the terminating zero.
Post Reply