Left () and Right () with negative numbers

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
miskox
User
User
Posts: 95
Joined: Sun Aug 27, 2017 7:37 pm
Location: Slovenia

Left () and Right () with negative numbers

Post by miskox »

I would like to suggest to support negative numbers with Left.

Manual says:

Code: Select all

Result \$ = Left ( String$ , Length )
It would be nice to have an option to just specify a negative number which would tell how many characters are removed from the source string.

For example:

Code: Select all

result$=Left ("abcdef", -1)
would return

Code: Select all

abcde
(one character from the right removed).

This would probably mean shorter .exe because

Code: Select all

result$=Left ("abcdef", Len ("abcdef") - 1)
is much longer (which at the end produces longer .exe files).

Same could apply for Right.

Maybe some other functions could use the same system.

Just think about it and maybe implement it.

In the mean time maybe documentation could be updated to include the range of the Length. For example:

"Length: value specified must be an integer higher than zero." (of course rephrase this)

Saso
User avatar
ar-s
Enthusiast
Enthusiast
Posts: 340
Joined: Sat Oct 06, 2007 11:20 pm
Location: France

Re: Left () and Right () with negative numbers

Post by ar-s »

I agree. That could be cool.
~Ar-S~
My Image Hoster for PB users
My webSite (french) with PB apps : LDVMULTIMEDIA
PB - 3.x / 5.7x / 6 - W11 x64 - Ryzen 7 3700x / #Rpi4

Code: Select all

r3p347 : 7ry : un71l d0n3 = 1
mestnyi
Addict
Addict
Posts: 995
Joined: Mon Nov 25, 2013 6:41 am

Re: Left () and Right () with negative numbers

Post by mestnyi »

+1
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Left () and Right () with negative numbers

Post by RSBasic »

+1
Image
Image
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Left () and Right () with negative numbers

Post by mk-soft »

+1

Update. Only macros

Code: Select all

;-TOP
;-TOP

Macro PB(Function)
  Function
EndMacro

Macro Left(String, Lenght)
  PB(Left)(String, Lenght + (Bool(Lenght<0)*Len(String)))
EndMacro

Macro Right(String, Lenght)
  PB(Right)(String, Lenght + (Bool(Lenght<0)*Len(String)))
EndMacro

; --------

r1.s ="12345678"

Debug Left(r1, 2)
Debug Left(r1, -2)
Debug Right(r1, 2)
Debug Right(r1, -2)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Left () and Right () with negative numbers

Post by Mijikai »

+ 1
RobertRioja
User
User
Posts: 71
Joined: Thu May 02, 2019 3:57 am
Location: USA
Contact:

Re: Left () and Right () with negative numbers

Post by RobertRioja »

This is a great idea which has already been implemented in other compilers. I hope it gets implemented in PB.
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Left () and Right () with negative numbers

Post by Demivec »

@mk-soft: I'm always glad to see code suggestions on how to obtain a solution through various means. Just a comment on your macro offering, they are useful as syntatic sugar but they will result in longer execution whether or not a negative number is used for length because the Len() will be calculated on every execution of the macro whether or not it is needed.
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Left () and Right () with negative numbers

Post by Little John »

Code: Select all

Macro LeftN (_string_, _length_)
   ; -- Left() with a negative number
   Left(_string_, Len(_string_) + (_length_))
EndMacro

Macro RightN (_string_, _length_)
   ; -- Right() with a negative number
   Mid(_string_, -(_length_) + 1)
EndMacro


; -- Demo
text$ = "12345678"

Debug LeftN (text$, -2)
Debug RightN(text$, -2)
Post Reply