Oxygen Basic

Programming => Problems & Solutions => Topic started by: on October 04, 2018, 06:59:33 PM

Title: mid statement
Post by: on October 04, 2018, 06:59:33 PM
Is there a mid statement? Apparently not, because this does not compile.

Code: [Select]
DIM s as string = "1234567890"
mid(s, 2, 3) = "xxx"
print s

Found this:

Code: [Select]
DIM s as string = "1234567890"
mid s, 2, "x"
print s

Weird syntax and apparently limited to one character.
Title: Re: mid statement
Post by: JRS on October 04, 2018, 07:04:02 PM
You're trying to make an assignment to a function.
Title: Re: mid statement
Post by: José Roca on October 04, 2018, 07:07:43 PM
I'm trying to know if, besides the mid function, there is a mid statement.
Title: Re: mid statement
Post by: Aurel on October 04, 2018, 10:11:13 PM
mid statement ?
what is that ...replace string
or replace substring with given length ?
Title: Re: mid statement
Post by: JRS on October 04, 2018, 10:26:16 PM
In SB, MID (https://www.scriptbasic.org/docs/ug/ug_25.122.html) is used to extract a sub-string.  REPLACE() (https://www.scriptbasic.org/docs/ug/ug_25.144.html) is used to modify an existing string.

I would rewrite your MID assignment like so.

Code: Script BASIC
  1. s = LEFT(s, 1) & "xxx" & MID(s, 2)
  2.  

Title: Re: mid statement
Post by: Charles Pegge on October 05, 2018, 12:28:03 AM
José,

Yes, and the mid statement can take 2 forms:

mid(s, 2) = "xxx" 'pseudo assignment

mid(s, 2,"xxx") 'underlying form

Code: [Select]
DIM s as string = "1234567890"
mid(s, 2) = "xxx"
print s
mid(s, 2,"xxx")
print s

Title: Re: mid statement
Post by: Aurel on October 05, 2018, 04:51:34 AM
Quote
mid(s, 2) = "xxx" 'pseudo assignment
:o
what i don't know for that
Title: Re: mid statement
Post by: on October 05, 2018, 05:46:42 AM
José,

Yes, and the mid statement can take 2 forms:

mid(s, 2) = "xxx" 'pseudo assignment

mid(s, 2,"xxx") 'underlying form

Code: [Select]
DIM s as string = "1234567890"
mid(s, 2) = "xxx"
print s
mid(s, 2,"xxx")
print s

Thanks, Charles. Works like with other dialects, although it is missing the second form, that optionally indicates the number of charcters to replace: Mid( text, start, length ) = expression. I use that second form very often because it ensures that only the wanted number of characters are replaced. Without it, I will have to check first if the replacement string has the appropriate length.

Code: [Select]
s = LEFT(s, 1) & "xxx" & MID(s, 2)

This does not do what I intend, besides being very inefficient.

I use the MID statement (no the MID function) very often because it is very efficient since it does not create temporary strings and does not use concatenations.

Title: Re: mid statement
Post by: José Roca on October 05, 2018, 06:16:28 AM
Quote
mid(s, 2) = "xxx" 'pseudo assignment
:o
what i don't know for that

It is never too late to learn something new. Its use dates back to immemorial times.
Title: Re: mid statement
Post by: JRS on October 05, 2018, 08:15:42 AM
What would this do?

MID(s,2,2) = "xxx"
Title: Re: mid statement
Post by: José Roca on October 05, 2018, 09:49:42 AM
It is

MID(s,2,3) = "xxx"

What would this do?

To change the contents of the second, third and fourth characters of the string to "xxx".

s is the string, 2 the starting position, 3 the number of characters to change and "xxx" the characters to assign (they must be the same length that the specified number of characters to replace; if it is longer, only the first three will be taken). The string does not shrink or grow. It is a way to do a fast replacement without using pointers.

It has been a Basic's intrinsic statement for more time that I can remember.
Title: Re: mid statement
Post by: JRS on October 05, 2018, 10:00:58 AM
I think you answered my question.

It will use as many characters from the assignment string specified in the MID statement and disregard the rest. Correct?
Title: Re: mid statement
Post by: on October 05, 2018, 10:13:09 AM
It depends on the implementation. O2 does not accept the third parameter and assumes that the number of characters to replace are equal to the length of the replacement string.

Therefore, if you use

MID(s,2) = "xxx"

it will replace three characters, starting at the second position.

and if you use

MID(s,2) = "wxyz"

it will replace four.

RTFM :)
https://github.com/JoseRoca/WinPBX/blob/master/docs/Oxygen/Intrinsic%20Functions.md#mid2
Title: Re: mid statement
Post by: JRS on October 05, 2018, 10:34:08 AM
I think Charles needs to add the 3rd argument to MID and it control how many of the characters used from the assignment string. Allowing both methods would be great.
Title: Re: mid statement
Post by: Charles Pegge on October 05, 2018, 08:06:15 PM
Yes, I can incorporate that format. But I would like to know whether the last (source string) parameter of mid in your code, José, is usually a native string, or an asciiz string? There is an issue of optimisation.
Title: Re: mid statement
Post by: on October 05, 2018, 08:58:46 PM
With or without an "end" parameter, it should work no matter of the type of string assigned.

This works:

Code: [Select]
dim s as string2 = "1234567890"
dim s2 as string2 = "00"
mid(s, 3) = s2
print s

But this does not:

Code: [Select]
dim s as string2 = "1234567890"
mid(s, 3) = "00"
print s

The assigned string should be converted to the same encoding (ansi or unicode) that the target (s).
Title: Re: mid statement
Post by: Charles Pegge on October 06, 2018, 02:36:23 AM
I think the best solution will be to use these functions, which have the desired parameters and polymorphs:

Code: [Select]

macro midset(cw)
================
  sys ps=strptr s
  sys pw=strptr w
  int ls=cw*len s
  int lw=cw*len w
  i*=cw
  n*=cw
  if i<0 then i=ls+i+cw 'index from right
  i-=cw 'base 0
  if n>lw then n=lw
  if n+i>ls
    n=ls-i
    if n<0 then return
  end if
  copy ps+i,pw,n
end macro

function amid(char*s,int i, int n,char* w)
==========================================
midset(1)
end function

function amid(string s,int i, int n,string w)
=============================================
midset(1)
end function

function wmid(wchar*s,int i, int n,wchar* w)
============================================
midset(2)
end function

function wmid(wstring s,int i, int n,wstring w)
===============================================
midset(2)
end function


string s="1234567890"
amid (s,2,3)="abcd"
print s
'
wstring s="1234567890"
wstring w="abcd"
wmid (s,2,3)=w
print s
'
char s="1234567890"
amid (s,2,3)="abcd"
print s
'
wchar s="1234567890"
wstring w="abcd"
wmid (s,2,3)=w
print s