Page 1 of 1

mid$ variations as are common in other basics

Posted: Wed Oct 06, 2004 8:22 am
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

Posted: Wed Oct 06, 2004 8:58 am
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.

Posted: Wed Oct 06, 2004 9:57 am
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".

:)

Posted: Wed Oct 06, 2004 10:22 am
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' :)

Posted: Wed Oct 06, 2004 10:50 am
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)

Posted: Wed Oct 06, 2004 3:03 pm
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...

Posted: Fri Oct 15, 2004 1:45 pm
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!! :)

Posted: Fri Oct 15, 2004 1:52 pm
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.

Posted: Fri Oct 15, 2004 11:59 pm
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

Posted: Sat Oct 16, 2004 1:18 am
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. ;)

Posted: Sat Oct 16, 2004 10:46 am
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.

Posted: Sun Oct 17, 2004 7:21 am
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$