PureBasic Forum
http://forums.purebasic.com/english/

Left () and Right () with negative numbers
http://forums.purebasic.com/english/viewtopic.php?f=3&t=72338
Page 1 of 1

Author:  miskox [ Fri Feb 22, 2019 10:24 am ]
Post subject:  Left () and Right () with negative numbers

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

Manual says:

Code:
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:
result$=Left ("abcdef", -1)


would return

Code:
abcde


(one character from the right removed).

This would probably mean shorter .exe because

Code:
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

Author:  ar-s [ Fri Feb 22, 2019 12:57 pm ]
Post subject:  Re: Left () and Right () with negative numbers

I agree. That could be cool.

Author:  mestnyi [ Fri Feb 22, 2019 1:20 pm ]
Post subject:  Re: Left () and Right () with negative numbers

+1

Author:  RSBasic [ Fri Feb 22, 2019 1:28 pm ]
Post subject:  Re: Left () and Right () with negative numbers

+1

Author:  mk-soft [ Fri Feb 22, 2019 1:41 pm ]
Post subject:  Re: Left () and Right () with negative numbers

+1

Update. Only macros
Code:
;-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)

Author:  Mijikai [ Fri Feb 22, 2019 2:06 pm ]
Post subject:  Re: Left () and Right () with negative numbers

+ 1

Author:  RobertRioja [ Tue Jun 11, 2019 9:47 pm ]
Post subject:  Re: Left () and Right () with negative numbers

This is a great idea which has already been implemented in other compilers. I hope it gets implemented in PB.

Author:  Demivec [ Wed Jun 12, 2019 1:37 am ]
Post subject:  Re: Left () and Right () with negative numbers

@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.

Author:  Little John [ Wed Jun 12, 2019 6:52 am ]
Post subject:  Re: Left () and Right () with negative numbers

Code:
Macro Left_negative (_string_, _lenght_)
   Left(_string_, Len(_string_) + (_lenght_))
EndMacro

Macro Right_negative (_string_, _lenght_)
   Mid(_string_, -(_lenght_) + 1)
EndMacro


; -- Demo
text$ = "12345678"

Debug Left_negative (text$, -2)
Debug Right_negative(text$, -2)

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/