substrings

General FreeBASIC programming questions.
jimg
Posts: 24
Joined: Jan 16, 2020 19:43
Location: Oregon

Re: substrings

Post by jimg »

The point was to have the syntax intrinsically available, short, simple, clean. Not to have to use a function or macro.
But I pretty much knew if was a forlorn hope when I asked, unless it would strike a cord in the FreeBasic developers.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: substrings

Post by dodicat »

A limitation with property, you can have only one (working) parameter.
Here I try to mimic the statement property of mid, using a udt, but it is ugly coding, packing two into one.

Code: Select all


Type st
    Declare Property middle( As Long Ptr) As String
    Declare Property middle( As Long Ptr,As String)
    Declare Function m(As Long,As Long) As Long Ptr
    Declare Operator Cast() As String
    Declare Constructor(As String="")
    #define md(s,c,d) Mid(s,c+1,d-c+1)
    private: as string s
End Type

Constructor st(g As String)
s=g
End Constructor

Function st.m(f As Long,L As Long) As Long Ptr
    Static As Long Ptr p:p=New Long[2]
    p[0]=f
    p[1]=L
    Return p
End Function

Property st.middle(n As Long Ptr) As String
Property= md(s,n[0],n[1]) 
End Property

Property st.middle(n As Long Ptr,g As String)
md(s,n[0],n[1])=g
End Property

Operator st.cast() As String
Return s
End Operator

Dim As st g="abcdefghijklmnopqrstuvwxyz"


Print g.middle(g.m(2,7))         'function property
g.middle(g.m(10,19))="STATEMENT" 'statement property (setter)
Print g

Print 
sleep

 
Maybe there is a neater way!
jimg
When you pose a problem do you not expect members to respond?
Maybe you should email developers directly if you don't want the hoi polloi involved.
jimg
Posts: 24
Joined: Jan 16, 2020 19:43
Location: Oregon

Re: substrings

Post by jimg »

Yes, I'm quite happy with the response from users!
I looked but couldn't find a place on the forum to request changes to freebasic.
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: substrings

Post by badidea »

jimg wrote:I looked but couldn't find a place on the forum to request changes to freebasic.
You can try https://sourceforge.net/p/fbc/feature-requests/ but I don't think that it will be high on the priority list.
jimg
Posts: 24
Joined: Jan 16, 2020 19:43
Location: Oregon

Re: substrings

Post by jimg »

Thank you. I just learning my way around this forum and couldn't find that. I have submitted a request.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: substrings

Post by MrSwiss »

jimg wrote:The point was to have the syntax intrinsically available, short, simple, clean. Not to have to use a function or macro.
This must be a really bad joke, because:
FB-Manual about mid() - wrote:Declare Function Mid ( ByRef str as Const String, ByVal start as integer, ByVal n as integer ) as String
Aka: Function / Sub / Macro - are the very building-blocks, of the language itself (KeyWords).
There is nothing unclean, about writing your own, if none of the "built-in" doesn't do, what you want.
jimg
Posts: 24
Joined: Jan 16, 2020 19:43
Location: Oregon

Re: substrings

Post by jimg »

Hmmmm.

I was saying, to access the substring you would write

a$[c,d]

how would you write the same thing using a function/sub/macro? I don't see how it could be as clean, and probably not any better than using the mid function.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: substrings

Post by MrSwiss »

jimg wrote:a$[c,d]
Won't work because:
a) string index operator [] can only take one argument (index).
b) this means, you'll need another construct (given by the language)
c) conclusion: you'll need a Function or a Macro ...
(in order to use two different indices, at once)

Btw: the $ in dialect "FB" is now disallowed.
jimg wrote:how would you write the same thing using a function/sub/macro?
Is already done (just go back a few posts, called SubString() Function)
dodicat provided a Macro, what more do you want?
(just insisting on something, won't get you very far)
jimg
Posts: 24
Joined: Jan 16, 2020 19:43
Location: Oregon

Re: substrings

Post by jimg »

MrSwiss wrote:
jimg wrote:a$[c,d]
Won't work because:
a) string index operator [] can only take one argument (index).
b) this means, you'll need another construct (given by the language)
Precisely what I'm proposing. A compiler update to allow the syntax of [startcol, optional end col]
That's all I ever asked for.
c) conclusion: you'll need a Function or a Macro ...
(in order to use two different indices, at once)

"Btw: the $ in dialect "FB" is now disallowed."
And as for the a$, I just did it to make it clear I was using a string. I have no need for anything beyond fblite, but that's another argument altogether that I do not want to get into at this point.
jimg wrote:how would you write the same thing using a function/sub/macro?
Is already done (just go back a few posts, called SubString() Function)
dodicat provided a Macro, what more do you want?
(just insisting on someting, won't get you very far)

As I said above. I'm asking for a compiler update. Obviously this is not the place to ask. I'm just making it clear what I was asking. I've also posted in the proper place.

I don't see how you could make a function or macro that would be any better than the mid function. Again, not what I'm after.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: substrings

Post by jj2007 »

Built into the compiler, and crystal clear:

Code: Select all

print Left$("Добро пожаловать", 5);
print Mid$("Добро пожаловать", 6, 7);
print Right$("Добро пожаловать", 6)
Output: Добро пожалоловать

And really, I don't care about those who cry bitterly that the $ sign is too BASIC. I use it all the time because the clear distinction between strings and numbers makes my code more readable.
oyster
Posts: 274
Joined: Oct 11, 2005 10:46

Re: substrings

Post by oyster »

I often do work with text, this slice style is more readable, especially if I make a train of operations.

however I do not think this syntax will be implemented in freebasic. As far as I know, freebasic is born to make QB alive in today's os, once this goal is achieved, I feel that the development got to slow down.

Yes, fbc has supplied the features to write applications, I often hear that freebasic is a language with BASIC syntax but C heart, it is harder to learn than what we did with traditional BASIC. But it could be better if the (impatient) user, for example me, can find a way with less code/keyboard-typing to write the program by using some sugar syntax. For example,

1. "for each" in VB.net to enumerate, or enumerate in python( https://docs.python.org/2.3/whatsnew/se ... erate.html )

2. 'in' to judge whether something is in another,

3. slice(https://www.digitalocean.com/community/ ... n-python-3), with the only syntax, we can do LEFT$, RIGHT$, MID$, choose element every Nth character, reverse a string

4. we can use three all of the above on string and array, or even customized type.

5. string syntax for multiline text, so that we do not need to add CR/LF at the ends of every line manually. Perhaps, in the time when QB lived, the computer can not deal too long string easily, so QB breaks them down line by line. But nowadays, we often meet long text with many paragraphs, for example, webpage source, XML, CSS and so on. The old-school QB's way to write such text is too clumsy and low efficiency. Yes, we can use "\n" in "!string", but for long text, it will still be hard to read, there is an example

https://github.com/PaulSquires/WinFBE/b ... o.bas#L404

6. as for comment, how about to use "#" to instead "'"( the single quote mark), so that we can write

Code: Select all

aString = 'string in this way, so we can use "Double quote mark" in it easily'
7. string interpolation(https://docs.microsoft.com/en-us/dotnet ... terpolated) or f-string(https://realpython.com/python-f-strings ... -in-python)

8. native multi-dimensional array/matrix operator. But this may be done via the operator overload perhaps.

Maybe someone would say "hey, you are talking about python, not freebasic. Go to your f*king python". Yes, but think about !"string" in freebasic, which is not BASIC's syntax; why freebasic accepted it? For convenience and productivity, I think.

Well, I know it is hard to add new syntax, too hard from my ability, so I think this just will always be a dream. Furthermore, the idea to break some old syntax will make someone very very angry. Please don't blame me. I said it is a dream.
FB_user_22
Posts: 3
Joined: Jan 30, 2022 18:30

Re: substrings

Post by FB_user_22 »

I am with jimg.

True BASIC supports two forms:

b$= a$(2:4)
b$= a$[2:4]

Both forms return "bcd" for a$ = "abcde"

These are more compact and easier to read/write than:
b$= mid(a$,2,3)

The second form improves readability in this situation:
b$ = "12345"
x = val(b$[2:4])

The notation in True BASIC avoids having to do the math for -- and can prevent off-by-one errors in -- the third parameter of the "mid" function.
Post Reply