mid$ variations as are common in other basics

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

mid$ variations as are common in other basics

Post by blueznl »

(not sure, but i think this one has been mentioned before, in that case ignore it)

mid$() in pure does not allow some variations possible in other basics:

mid$(x$,5)="12345"
... would be insert 12345 at position 5 in string x$ (so the '1' would be the 5th character and c$ would become 5 characters longer)

mid$(x$,5,3)="12345"
... would replace character 3 characters with 12345 (thus making x$ 2 characters longer)

see also posting.php?mode=reply&t=12694
Last edited by blueznl on Wed Oct 06, 2004 3:04 pm, edited 1 time in total.
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

This kind of syntax is very wierd IHMO and will be not supported by PureBasic. A new command could be done to acieve this goal if you need it. ReplaceStringAt(String$, Position, Length, NewString$) or something similar.
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

That would be awesome:

Votes for:

InsertString(DestString$, Position, embedString$)
; length defined by embedString$

CutString(DestString$, StartPos [,CharCount])
; Removes up to CharCount chars from StartPos, Truncates if no CharCount

UpdateString(DestString$,fromPos,charCount,embedString$)

And whilst we're on strings, make the third arg of Mid(a$,pos,len) optional, defaulting to "rest of".

:)
@}--`--,-- A rose by any other name ..
sec
Enthusiast
Enthusiast
Posts: 792
Joined: Sat Aug 09, 2003 3:13 am
Location: 90-61-92 // EU or ASIA
Contact:

Post by sec »

>And whilst we're on strings, make the third arg of Mid(a$,pos,len) >optional, defaulting to "rest of".
len's value is -1 (or 0) for 'rest of' :)
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> len's value is -1 (or 0) for 'rest of'

What? No it's not. This doesn't show "56789":

Code: Select all

a$="123456789"
Debug Mid(a$,5,0)
Debug Mid(a$,5,-1)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

0 and -1... it's not but he wants :-) i think you've misread, pb :-)

i don't absolutely need the same syntax (as in mid$(..)=...) i just mentioned it as it has been in use in other basics

but i think we could do it with a single command, no need for ten different variations... then again, this is so simple, a procedure would suffice for most users, i guess...
Last edited by blueznl on Sun Oct 17, 2004 1:30 pm, edited 1 time in total.
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Randy Walker
Addict
Addict
Posts: 1008
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Post by Randy Walker »

Fred wrote:This kind of syntax is very wierd IHMO and will be not supported by PureBasic. A new command could be done to acieve this goal if you need it. ReplaceStringAt(String$, Position, Length, NewString$) or something similar.
Fred - it is common in GFA and several other basics to simply reverse the syntax to perform reverse functions. May seem odd to you, but made since to me -- not having to learn more commands... just swap syntax using the already familiar command.

Another example offered only as reference...

_win$(hnd_id) = "new button text"
equivelent to:
SetGadgetText(hnd_id,"new button text")

s$ = _win$(hnd_id)
equivelent to:
s$ = GetGadgetText(hnd_id)

In the examples above, _win$(hnd_id) could also be used to set/get window titles so this one command serves as four PB commands combined. Good and bad either way. One versitile command is simpler, but the code is not as "enherently" descriptive.

Many here seem to like the wordy approach. Either way, I would also find the ReplaceStringAt() funcionality very useful as I do a lot of string manipulations myself.
Getting to like your work more as I understand it... THANKS!! :)
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Randy Walker
Addict
Addict
Posts: 1008
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Post by Randy Walker »

sec wrote:>And whilst we're on strings, make the third arg of Mid(a$,pos,len) >optional, defaulting to "rest of".
len's value is -1 (or 0) for 'rest of' :)
Yessss... this is one handy modification to the reverse Mid() function and would also be a welcome addition.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

You can put a value in the 3rd parameter that is higher than the actual string length. Mid() will just stop at the 0 byte.
Like this you have a 'from there till the end' function.

It must work, i've seen it in Fred's own code :P

Timo
quidquid Latine dictum sit altum videtur
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> You can put a value in the 3rd parameter that is higher than the actual
> string length. Mid() will just stop at the 0 byte.

That's what I currently do. I have this constant defined: #ALL=63999
This means I can use Mid$ like so: a$=Mid$(b$,start,#ALL)
But it would be good to be able to leave off the #ALL part. ;)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Randy Walker
Addict
Addict
Posts: 1008
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Post by Randy Walker »

Freak, PB... Well then it looks like Mid() already offers a simple remedy for picking the tail off a string. Thanks for pointing it out to me. :)

Meanwhile I guess I can write a ReplaceStringAt() proc so my code will be compatible with the proposed function. I have many requests I would like to make, but don't feel right about it. Not familiar enough with these simple BP coding tricks I guess.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Randy Walker
Addict
Addict
Posts: 1008
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Post by Randy Walker »

Fred wrote:ReplaceStringAt(String$, Position, Length, NewString$) or something similar.
Not sure what you had in mind with the Length parameter. I don't see that it is necessary to supply this value from outside the function.

I think bluenzl would agree with this functionality. I did allow appending which he may not agree. Also someone more experienced could write something more efficient but this is what I came up with:

Code: Select all

Procedure.s ReplaceStringAt(host$, Strt, plg$)
  Protected host$, *pnt, Strt, bfr$, plg$, cnt
  If Len(host$) ; modify else return null string
    cnt = Len(host$ + plg$)
    bfr$=Space(cnt + 1)   ; including Chr(0)
    *pnt = @bfr$
    PokeS(*pnt, host$)     ; <<< place host$ to far left side of buffer
    If Strt > 0           ; <<< prevent poking ahead of *ptr
      If Len(bfr$)
        Strt = Strt - 1
        If Strt < cnt      ; must not allow 0 byte char between host$ and plg$
          cnt = Len(plg$)
          *pnt = *pnt + Strt + cnt - 1
          PokeB(*pnt, 0)
          Repeat
            cnt = cnt - 1
            PokeB(*pnt, PeekB(@plg$+cnt))    ; now Plug in replacement string
            *pnt = *pnt - 1
          Until cnt = 0
        EndIf    
      EndIf    
    EndIf    
  EndIf     ; Length of return is less than Or equal To Len(host$+plg$)
  ProcedureReturn Trim(bfr$)
EndProcedure

; Setup sample string
test$ = "I post my test string"
Debug test$
Debug ""
; > plug a string in starting at position 3
test$ = ReplaceStringAt(test$, 3, "plug")
Debug test$
Debug ""
; > plug in string starting at position 16 And exceed original length
test$ = ReplaceStringAt(test$, 16, "with long tail that overlaps")
Debug test$
Debug ""
; > plug in string starting at first character
test$ = ReplaceStringAt(test$, 1, "Prefix")
Debug test$
Debug ""

test$ = "18 characters long"
test$ = ReplaceStringAt(test$, 0, "not allowed so return original test$")
Debug test$
test$ = ReplaceStringAt(test$, 20, "start position exceeds original test$ length")
Debug test$
test$ = ReplaceStringAt(test$, 19, "allow to append to original test$ length")
Debug test$
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Post Reply