Wrapping, capping, toggling, and slicing, oh my!
#2
(Round of applause)

The "plus_limit" is something that appeared for me a lot, I mean a lot, in my source code in any language.

The "toggle" I call "tert" in my version of Lua, could be written even simpler:

Code: (Select All)
function toggle&& (valu&&, one&&, two&&)
if valu&& = one&& then toggle&& = two&& : exit function
toggle&& = one&&
end function

It would be even better if it could support any data type.

I have one of my own that some of you might have already seen elsewhere in my source code:

Code: (Select All)
FUNCTION LeftLen$ (tx$, numchar&)
IF tx$ = "" THEN
    LeftLen$ = ""
ELSEIF numchar& > 0 THEN
    LeftLen$ = LEFT$(tx$, LEN(tx$) - numchar&)
ELSE
    LeftLen$ = tx$
END IF
END FUNCTION

It takes "numchar&" bytes away from the end of the string. Quick example, cannot think well right now due to lack of sleep:

Code: (Select All)
a$ = "Pete"
print a$; "'s "; lcase$(LeftLen$(a$, 1))

I have also written a "SEG1$()" which acts more like "string.sub()" in Lua, ie. give the starting and ending inclusive points to get a fragment of a string, and supports zero and negative values. It had to be "SEG1$()" not "SEG$()" because of "DEF SEG".

Code: (Select All)
''this works a bit differently from "string.sub()" in Lua
''for either position, 0 means the length of "astr$" and -1 means the penultimate position
FUNCTION SEG1$ (astr$, stapos AS LONG, endpos AS LONG)
DIM sp AS LONG, ep AS LONG, L AS LONG
IF astr$ = "" THEN SEG1$ = "": EXIT FUNCTION
sp = stapos
ep = endpos
L = LEN(astr$)
IF sp < 1 THEN sp = L + sp
IF ep < 1 THEN ep = L + ep
IF (sp < 1 AND ep < 1) OR (sp > L AND ep > L) THEN SEG1$ = "": EXIT FUNCTION
IF sp < 1 THEN sp = 1
IF ep < 1 THEN ep = 1
IF sp > L THEN sp = L
IF ep > L THEN ep = L
IF sp > ep THEN SWAP sp, ep
SEG1$ = MID$(astr$, sp, ep - sp + 1)
END FUNCTION

Sorry because it's not indented, blame the forum liking to eat spaces at the front of lines sometimes.
Reply


Messages In This Thread
RE: Wrapping, capping, toggling, and slicing, oh my! - by mnrvovrfc - 12-03-2022, 11:40 AM



Users browsing this thread: 1 Guest(s)