substrings

For other topics related to the FreeBASIC project or its community.
jimg
Posts: 10
Joined: Jan 16, 2020 19:43

Re: substrings

Postby jimg » Feb 11, 2020 17:49

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: 6139
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: substrings

Postby dodicat » Feb 12, 2020 18:04

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: 10
Joined: Jan 16, 2020 19:43

Re: substrings

Postby jimg » Feb 12, 2020 19:52

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: 1764
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: substrings

Postby badidea » Feb 12, 2020 20:02

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: 10
Joined: Jan 16, 2020 19:43

Re: substrings

Postby jimg » Feb 13, 2020 16:38

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

Re: substrings

Postby MrSwiss » Feb 13, 2020 17:17

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: 10
Joined: Jan 16, 2020 19:43

Re: substrings

Postby jimg » Feb 13, 2020 20:15

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: 3348
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: substrings

Postby MrSwiss » Feb 13, 2020 20:27

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: 10
Joined: Jan 16, 2020 19:43

Re: substrings

Postby jimg » Feb 13, 2020 21:08

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: 1316
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: substrings

Postby jj2007 » Feb 14, 2020 2:24

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.

Return to “Community Discussion”

Who is online

Users browsing this forum: No registered users and 2 guests