Miscellaneous handy goodies - grymmjack - 12-28-2022
Well, damn.
I had a very nicely formatted post all queued up and the browser crashed when I right clicked on a word to check its definition and spelling in this text area.
Anyway.
I will return to this to explain but mostly this is self-explanatory stuff.
Code: (Select All) ' For concatenating integers and stripping spaces
FUNCTION n$ (integ%)
n$ = _TRIM$(STR$(integ%))
END FUNCTION
' For showing friendly versions of my boolean constants
FUNCTION b$ (integ%)
IF integ% = -1 THEN
b$ = "TRUE"
ELSEIF integ% = 0 THEN
b$ = "FALSE"
ENDIF
END FUNCTION
' For concatenating longs and stripping spaces
FUNCTION ln$ (longval!)
ln$ = _TRIM$(STR$(longval!))
END FUNCTION
' For incrementing integers - x=inc(1) takes up 3 more chars than x=x+1 but inc is a bit easier for me to read.
FUNCTION inc% (value%)
inc% = value% + 1
END FUNCTION
' Same as above but decrement
FUNCTION dec% (value%)
dec% = value% - 1
END FUNCTION
' Inverting int
FUNCTION inv% (value%)
inv% = value% * -1
END FUNCTION
' Force int to be not less than min
FUNCTION min% (value%, minimum%)
IF value% < minimum% THEN value% = minimum%
min% = value%
END FUNCTION
' Force int to be not more than max
FUNCTION max% (value%, maximum%)
IF value% > maximum% THEN value% = maximum%
max% = value%
END FUNCTION
' Force int to be between a min and a max, when greater - clamp it between min and max
FUNCTION clamp% (value%, minimum%, maximum%)
IF value% > maximum% THEN
clamp% = maximum%
ELSEIF value% < minimum% THEN
clamp% = minimum%
ELSE
clamp% = value%
END IF
END FUNCTION
' Determine if a int is in range of a min and a max
FUNCTION in_range% (value%, minimum%, maximum%)
IF value% >= minimum% AND value% <= maximum% THEN
in_range% = TRUE
ELSE
in_range% = FALSE
END IF
END FUNCTION
' Randomize the sign of an int
FUNCTION rand_sign% ()
DIM r AS INTEGER
r% = -1 + INT(RND*2)
IF r% = 0 THEN r% = 1
rand_sign% = r%
END FUNCTION
' Create a random integer between min and max
FUNCTION rand_in_range% (minimum%, maximum%)
rand_in_range% = INT(RND * (maximum% - minimum% + 1)) + 1
END FUNCTION
' Randomly choose an int from an array of ints
FUNCTION rand_int_choice% (arr_choices%())
DIM AS INTEGER minimum, maximum
minimum% = LBOUND(arr_choices%) : maximum% = UBOUND(arr_choices%)
rand_int_choice% = arr_choices%(rand_in_range(minimum%, maximum%))
END FUNCTION
' Randomly choose a string from an array of strings
FUNCTION rand_str_choice$ (arr_choices$())
DIM AS INTEGER minimum, maximum
minimum% = LBOUND(arr_choices$) : maximum% = UBOUND(arr_choices$)
rand_str_choice$ = arr_choices$(rand_in_range(minimum%, maximum%))
END FUNCTION
RE: Miscellaneous handy goodies - grymmjack - 12-28-2022
And...
How I wish we could have optional parameters in subs and funcs.
How I wish for a VARIANT type because lazy! then I could make one function or sub to handle all types of numbers, instead of requiring n$ and ln$ for example. One for ints, and one for longs.
We are the music makers! We are the dreamers of dreams!
RE: Miscellaneous handy goodies - SMcNeill - 12-28-2022
(12-28-2022, 01:46 AM)grymmjack Wrote: And...
How I wish we could have optional parameters in subs and funcs.
How I wish for a VARIANT type because lazy! then I could make one function or sub to handle all types of numbers, instead of requiring n$ and ln$ for example. One for ints, and one for longs.
We are the music makers! We are the dreamers of dreams!
Use _MEM. Quick Example: MemSort -- https://qb64phoenix.com/forum/showthread.php?tid=75
RE: Miscellaneous handy goodies - OldMoses - 12-28-2022
+1 on Steve's _MEM comment, and he is the _MEM guru here...
I particularly like it for manipulating all sorts of arrays; integers, longs, fixed length strings, UDTs, etc.
BTW, here is how I generally approach integer value limiting.
Code: (Select All) 'Limit an integer argument to a maximum value
FUNCTION MaxOf% (value AS INTEGER, max AS INTEGER)
MaxOf% = -value * (value <= max) - max * (value > max)
END FUNCTION 'MaxOf%
'Limit an integer argument to a minimum value
FUNCTION MinOf% (value AS INTEGER, minimum AS INTEGER)
MinOf% = -value * (value >= minimum) - minimum * (value < minimum)
END FUNCTION 'MinOf%
'Limit an integer argument between minimum and maximum bounds
FUNCTION MinMax% (value AS INTEGER, min AS INTEGER, max AS INTEGER)
MinMax% = MaxOf%(MinOf%(value, min), max)
END FUNCTION 'MinMax%
RE: Miscellaneous handy goodies - mdijkens - 12-28-2022
(12-28-2022, 01:44 AM)grymmjack Wrote: Code: (Select All) ' Inverting int
FUNCTION inv% (value%)
inv% = value% * -1
END FUNCTION
Code: (Select All) inv% = -value%
RE: Miscellaneous handy goodies - SpriggsySpriggs - 12-28-2022
Reminds me of what I did to show off _MEM here: Overloaded functions and any arguments (alephc.xyz)
Code: (Select All) Option _Explicit
_Title "Overloaded Functions - AS ANY"
Screen _NewImage(640, 480, 32)
_ScreenMove _Middle
Dim As _MEM test(1 To 17)
Dim As Long longtest: longtest = 435
Dim As Single singletest: singletest = 1.2
Dim As _Float floattest: floattest = 4.65
Dim As String * 21 stringtest: stringtest = "This is a string test"
Dim As _Offset offsettest: offsettest = 1234567
'Dim As Long imagetest: imagetest = _LoadImage(".\face no background.png", 32) 'replace with an image that you have
'Dim As Long soundtest: soundtest = _SndOpen(".\Dalshabet With. Bigtone.mp3") 'replace with a song that you have
Dim As String * 13 stringarraytest(1 To 3)
stringarraytest(1) = "Array test 1"
stringarraytest(2) = "Array test 2"
stringarraytest(3) = "Array test 3"
Dim As _Unsigned _Offset unsignedoffsetarraytest(1 To 2)
unsignedoffsetarraytest(1) = 123456789
unsignedoffsetarraytest(2) = 787970792
Dim As _Unsigned _Offset unsignedoffsettest: unsignedoffsettest = 1234523
Dim As _Float floatarraytest(1 To 3)
floatarraytest(1) = 3.56
floatarraytest(2) = 14.7548
floatarraytest(3) = 56.24124
Dim As Double doublearraytest(1 To 3)
doublearraytest(1) = 1.25
doublearraytest(2) = 2.34
doublearraytest(3) = 5.52
Dim As Single singlearraytest(1 To 3)
singlearraytest(1) = 2.12
singlearraytest(2) = 6.87
singlearraytest(3) = 9.65
Dim As _Unsigned _Byte unsignedbytearraytest(1 To 4)
unsignedbytearraytest(1) = 255
unsignedbytearraytest(2) = 124
unsignedbytearraytest(3) = 98
unsignedbytearraytest(4) = 34
'test(1) = _MemImage(imagetest)
test(2) = _Mem(singletest)
test(3) = _Mem(floattest)
test(4) = _Mem(stringtest)
test(5) = _Mem(offsettest)
test(6) = _Mem(longtest)
Dim As Double doubletest: doubletest = 2.578
test(7) = _Mem(doubletest)
'test(7) = _MemSound(soundtest, 1) 'Left channel
'test(8) = _MemSound(soundtest, 2) 'Right channel
test(9) = _Mem(stringarraytest())
test(10) = _Mem(unsignedoffsetarraytest())
test(11) = _Mem(unsignedoffsettest)
test(12) = _Mem(floatarraytest())
test(13) = _Mem(doublearraytest())
test(14) = _Mem(singlearraytest())
test(15) = _Mem(unsignedbytearraytest())
test(16) = _MemNew(4)
_MemPut test(16), test(16).OFFSET, longtest As LONG
test(17) = _MemNew(14)
_MemPut test(17), test(17).OFFSET, "This is a test"
Call anyArg(test())
Dim As _Unsigned Integer x
For x = LBound(test) To UBound(test)
If _MemExists(test(x)) Then
_MemFree test(x)
End If
Next
Erase test
Sub anyArg (args() As _MEM)
Dim As _Unsigned Integer x, y
Dim As _Unsigned _Offset z
Dim As _Unsigned Long size, elementsize
For x = LBound(args) To UBound(args)
If _MemExists(args(x)) Then
z = 0
size = Val(Str$(args(x).SIZE))
elementsize = Val(Str$(args(x).ELEMENTSIZE))
If _ReadBit(args(x).TYPE, 7) And _ReadBit(args(x).TYPE, 13) = 0 Then '_BYTE, INTEGER, LONG, _INTEGER64
If _ReadBit(args(x).TYPE, 10) Then
If _ReadBit(args(x).TYPE, 16) Then
Select Case args(x).ELEMENTSIZE
Case 1
Dim As _Unsigned _Byte unsignedbytearray(1 To (size / elementsize))
For y = LBound(unsignedbytearray) To UBound(unsignedbytearray)
_MemGet args(x), args(x).OFFSET + z, unsignedbytearray(y)
z = z + args(x).ELEMENTSIZE
Print unsignedbytearray(y), "UBYTE ARRAY"
Next
Exit Select
Case 2
Dim As _Unsigned Integer unsignedintarray(1 To (size / elementsize))
For y = LBound(unsignedintarray) To UBound(unsignedintarray)
_MemGet args(x), args(x).OFFSET + z, unsignedintarray(y)
z = z + args(x).ELEMENTSIZE
Print unsignedintarray(y), "USHORT ARRAY"
Next
Exit Select
Case 4
Dim As _Unsigned Long unsignedlongarray(1 To (size / elementsize))
For y = LBound(unsignedlongarray) To UBound(unsignedlongarray)
_MemGet args(x), args(x).OFFSET + z, unsignedlongarray(y)
z = z + args(x).ELEMENTSIZE
Print unsignedlongarray(y), "ULONG ARRAY"
Next
Exit Select
Case 8
Dim As _Unsigned _Integer64 unsignedint64array(1 To (size / elementsize))
For y = LBound(unsignedint64array) To UBound(unsignedint64array)
_MemGet args(x), args(x).OFFSET + z, unsignedint64array(y)
z = z + args(x).ELEMENTSIZE
Print unsignedint64array(y), "UINT64 ARRAY"
Next
Exit Select
End Select
Else
Select Case args(x).SIZE
Case 1
Print _MemGet(args(x), args(x).OFFSET, _Unsigned _Byte), "UBYTE"
Exit Select
Case 2
Print _MemGet(args(x), args(x).OFFSET, _Unsigned Integer), "USHORT"
Exit Select
Case 4
Print _MemGet(args(x), args(x).OFFSET, _Unsigned Long), "ULONG"
Exit Select
Case 8
Print _MemGet(args(x), args(x).OFFSET, _Unsigned _Integer64), "UINT64"
Exit Select
End Select
End If
Else
If _ReadBit(args(x).TYPE, 16) Then
Select Case args(x).ELEMENTSIZE
Case 1
Dim As _Byte bytearray(1 To (size / elementsize))
For y = LBound(bytearray) To UBound(bytearray)
_MemGet args(x), args(x).OFFSET + z, bytearray(y)
z = z + args(x).ELEMENTSIZE
Print bytearray(y), "BYTE ARRAY"
Next
Exit Select
Case 2
Dim As Integer intarray(1 To (size / elementsize))
For y = LBound(intarray) To UBound(intarray)
_MemGet args(x), args(x).OFFSET + z, intarray(y)
z = z + args(x).ELEMENTSIZE
Print unsignedintarray(y), "SHORT ARRAY"
Next
Exit Select
Case 4
Dim As Long longarray(1 To (size / elementsize))
For y = LBound(longarray) To UBound(longarray)
_MemGet args(x), args(x).OFFSET + z, longarray(y)
z = z + args(x).ELEMENTSIZE
Print longarray(y), "LONG ARRAY"
Next
Exit Select
Case 8
Dim As _Integer64 int64array(1 To (size / elementsize))
For y = LBound(int64array) To UBound(int64array)
_MemGet args(x), args(x).OFFSET + z, int64array(y)
z = z + args(x).ELEMENTSIZE
Print int64array(y), "INT64 ARRAY"
Next
Exit Select
End Select
Else
Select Case args(x).SIZE
Case 1
Print _MemGet(args(x), args(x).OFFSET, _Byte), "BYTE"
Exit Select
Case 2
Print _MemGet(args(x), args(x).OFFSET, Integer), "SHORT"
Exit Select
Case 4
Print _MemGet(args(x), args(x).OFFSET, Long), "LONG"
Exit Select
Case 8
Print _MemGet(args(x), args(x).OFFSET, _Integer64), "INT64"
Exit Select
End Select
End If
End If
ElseIf _ReadBit(args(x).TYPE, 8) Then 'SINGLE, DOUBLE, FLOAT
If _ReadBit(args(x).TYPE, 16) Then
Select Case args(x).ELEMENTSIZE
Case 4
Dim As Single singlearray(1 To (size / elementsize))
For y = LBound(singlearray) To UBound(singlearray)
_MemGet args(x), args(x).OFFSET + z, singlearray(y)
z = z + args(x).ELEMENTSIZE
Print singlearray(y), "SINGLE ARRAY"
Next
Exit Select
Case 8
Dim As Double doublearray(1 To (size / elementsize))
For y = LBound(doublearray) To UBound(doublearray)
_MemGet args(x), args(x).OFFSET + z, doublearray(y)
z = z + args(x).ELEMENTSIZE
Print doublearray(y), "DOUBLE ARRAY"
Next
Exit Select
Case 32
Dim As _Float floatarray(1 To (size / elementsize))
For y = LBound(floatarray) To UBound(floatarray)
_MemGet args(x), args(x).OFFSET + z, floatarray(y)
z = z + args(x).ELEMENTSIZE / 2
Print floatarray(y), "FLOAT ARRAY"
Next
Exit Select
End Select
Else
Select Case args(x).SIZE
Case 4
Print _MemGet(args(x), args(x).OFFSET, Single), "SINGLE"
Exit Select
Case 8
Print _MemGet(args(x), args(x).OFFSET, Double), "DOUBLE"
Exit Select
Case 32
Print _MemGet(args(x), args(x).OFFSET, _Float), "FLOAT"
Exit Select
End Select
End If
ElseIf _ReadBit(args(x).TYPE, 9) Then 'STRING
If _ReadBit(args(x).TYPE, 16) Then
Dim As String stringarray(1 To (size / elementsize))
For y = LBound(stringarray) To UBound(stringarray)
stringarray(y) = Space$(args(x).ELEMENTSIZE)
_MemGet args(x), (args(x).OFFSET) + (y * args(x).ELEMENTSIZE - args(x).ELEMENTSIZE), stringarray(y)
Print stringarray(y), "STRING ARRAY"
Next
Else
Dim As String stringtest: stringtest = Space$(args(x).ELEMENTSIZE)
_MemGet args(x), args(x).OFFSET, stringtest
Print stringtest
End If
ElseIf _ReadBit(args(x).TYPE, 13) And _ReadBit(args(x).TYPE, 7) Then '_OFFSET
If _ReadBit(args(x).TYPE, 10) Then
If _ReadBit(args(x).TYPE, 16) Then
Dim As _Unsigned _Offset unsignedoffsetarray(1 To (size / elementsize))
For y = LBound(unsignedoffsetarray) To UBound(unsignedoffsetarray)
_MemGet args(x), args(x).OFFSET + z, unsignedoffsetarray(y)
z = z + args(x).ELEMENTSIZE
Print unsignedoffsetarray(y), "ULONG_PTR ARRAY"
Next
Else
Print _MemGet(args(x), args(x).OFFSET, _Unsigned _Offset), "ULONG_PTR"
End If
Else
If _ReadBit(args(x).TYPE, 16) Then
Dim As _Offset offsetarray(1 To (size / elementsize))
For y = LBound(offsetarray) To UBound(offsetarray)
_MemGet args(x), args(x).OFFSET + z, offsetarray(y)
z = z + args(x).ELEMENTSIZE
Print unsignedoffsetarray(y), "LONG_PTR ARRAY"
Next
Else
Print _MemGet(args(x), args(x).OFFSET, _Offset), "LONG_PTR"
End If
End If
ElseIf args(x).TYPE = 0 And args(x).SIZE > 0 Then '_MEMSOUND
If Not _SndPlaying(args(x).SOUND) Then
_SndPlay (args(x).SOUND)
End If
Print "SOUND", args(x).SIZE, args(x).ELEMENTSIZE
ElseIf _ReadBit(args(x).TYPE, 14) Then
Print args(x).SIZE, "MEM"
'todo
End If
If _ReadBit(args(x).TYPE, 11) Then '_MEMIMAGE
Screen args(x).IMAGE
End If
End If
Next
End Sub
RE: Miscellaneous handy goodies - SpriggsySpriggs - 12-28-2022
This version might be easier to read with the $NoPrefix turned on:
Code: (Select All) Option Explicit
$NoPrefix
$Console:Only
Title "Overloaded Functions - AS ANY"
Dim As MEM test(1 To 17)
Dim As Long longtest: longtest = 435
Dim As Single singletest: singletest = 1.2
Dim As Float floattest: floattest = 4.65
Dim As String * 21 stringtest: stringtest = "This is a string test"
Dim As Offset offsettest: offsettest = 1234567
'Dim As Long imagetest: imagetest = _LoadImage(".\face no background.png", 32) 'replace with an image that you have
'Dim As Long soundtest: soundtest = _SndOpen(".\Dalshabet With. Bigtone.mp3") 'replace with a song that you have
Dim As String * 13 stringarraytest(1 To 3)
stringarraytest(1) = "Array test 1"
stringarraytest(2) = "Array test 2"
stringarraytest(3) = "Array test 3"
Dim As Unsigned Offset unsignedoffsetarraytest(1 To 2)
unsignedoffsetarraytest(1) = 123456789
unsignedoffsetarraytest(2) = 787970792
Dim As Unsigned Offset unsignedoffsettest: unsignedoffsettest = 1234523
Dim As Float floatarraytest(1 To 3)
floatarraytest(1) = 3.56
floatarraytest(2) = 14.7548
floatarraytest(3) = 56.24124
Dim As Double doublearraytest(1 To 3)
doublearraytest(1) = 1.25
doublearraytest(2) = 2.34
doublearraytest(3) = 5.52
Dim As Single singlearraytest(1 To 3)
singlearraytest(1) = 2.12
singlearraytest(2) = 6.87
singlearraytest(3) = 9.65
Dim As Unsigned Byte unsignedbytearraytest(1 To 4)
unsignedbytearraytest(1) = 255
unsignedbytearraytest(2) = 124
unsignedbytearraytest(3) = 98
unsignedbytearraytest(4) = 34
'test(1) = MEMImage(imagetest)
test(2) = Mem(singletest)
test(3) = Mem(floattest)
test(4) = Mem(stringtest)
test(5) = Mem(offsettest)
test(6) = Mem(longtest)
Dim As Double doubletest: doubletest = 2.578
test(7) = Mem(doubletest)
'test(7) = MEMSound(soundtest, 1) 'Left channel
'test(8) = MEMSound(soundtest, 2) 'Right channel
test(9) = Mem(stringarraytest())
test(10) = Mem(unsignedoffsetarraytest())
test(11) = Mem(unsignedoffsettest)
test(12) = Mem(floatarraytest())
test(13) = Mem(doublearraytest())
test(14) = Mem(singlearraytest())
test(15) = Mem(unsignedbytearraytest())
test(16) = MemNew(4)
MemPut test(16), test(16).OFFSET, longtest As LONG
test(17) = MemNew(14)
MemPut test(17), test(17).OFFSET, "This is a test"
Call anyArg(test())
Dim As Unsigned Integer x
For x = LBound(test) To UBound(test)
If MemExists(test(x)) Then
MemFree test(x)
End If
Next
Erase test
Sub anyArg (args() As MEM)
Dim As Unsigned Integer x, y
Dim As Unsigned Offset z
Dim As Unsigned Long size, elementsize
For x = LBound(args) To UBound(args)
If MemExists(args(x)) Then
z = 0
size = Val(Str$(args(x).SIZE))
elementsize = Val(Str$(args(x).ELEMENTSIZE))
If ReadBit(args(x).TYPE, 7) And ReadBit(args(x).TYPE, 13) = 0 Then '_BYTE, INTEGER, LONG, _INTEGER64
If ReadBit(args(x).TYPE, 10) Then
If ReadBit(args(x).TYPE, 16) Then
Select Case args(x).ELEMENTSIZE
Case 1
Dim As Unsigned Byte unsignedbytearray(1 To (size / elementsize))
For y = LBound(unsignedbytearray) To UBound(unsignedbytearray)
MemGet args(x), args(x).OFFSET + z, unsignedbytearray(y)
z = z + args(x).ELEMENTSIZE
Print unsignedbytearray(y), "UBYTE ARRAY"
Next
Exit Select
Case 2
Dim As Unsigned Integer unsignedintarray(1 To (size / elementsize))
For y = LBound(unsignedintarray) To UBound(unsignedintarray)
MemGet args(x), args(x).OFFSET + z, unsignedintarray(y)
z = z + args(x).ELEMENTSIZE
Print unsignedintarray(y), "USHORT ARRAY"
Next
Exit Select
Case 4
Dim As Unsigned Long unsignedlongarray(1 To (size / elementsize))
For y = LBound(unsignedlongarray) To UBound(unsignedlongarray)
MemGet args(x), args(x).OFFSET + z, unsignedlongarray(y)
z = z + args(x).ELEMENTSIZE
Print unsignedlongarray(y), "ULONG ARRAY"
Next
Exit Select
Case 8
Dim As Unsigned Integer64 unsignedint64array(1 To (size / elementsize))
For y = LBound(unsignedint64array) To UBound(unsignedint64array)
MemGet args(x), args(x).OFFSET + z, unsignedint64array(y)
z = z + args(x).ELEMENTSIZE
Print unsignedint64array(y), "UINT64 ARRAY"
Next
Exit Select
End Select
Else
Select Case args(x).SIZE
Case 1
Print MemGet(args(x), args(x).OFFSET, Unsigned Byte), "UBYTE"
Exit Select
Case 2
Print MemGet(args(x), args(x).OFFSET, Unsigned Integer), "USHORT"
Exit Select
Case 4
Print MemGet(args(x), args(x).OFFSET, Unsigned Long), "ULONG"
Exit Select
Case 8
Print MemGet(args(x), args(x).OFFSET, Unsigned Integer64), "UINT64"
Exit Select
End Select
End If
Else
If ReadBit(args(x).TYPE, 16) Then
Select Case args(x).ELEMENTSIZE
Case 1
Dim As Byte bytearray(1 To (size / elementsize))
For y = LBound(bytearray) To UBound(bytearray)
MemGet args(x), args(x).OFFSET + z, bytearray(y)
z = z + args(x).ELEMENTSIZE
Print bytearray(y), "BYTE ARRAY"
Next
Exit Select
Case 2
Dim As Integer intarray(1 To (size / elementsize))
For y = LBound(intarray) To UBound(intarray)
MemGet args(x), args(x).OFFSET + z, intarray(y)
z = z + args(x).ELEMENTSIZE
Print unsignedintarray(y), "SHORT ARRAY"
Next
Exit Select
Case 4
Dim As Long longarray(1 To (size / elementsize))
For y = LBound(longarray) To UBound(longarray)
MemGet args(x), args(x).OFFSET + z, longarray(y)
z = z + args(x).ELEMENTSIZE
Print longarray(y), "LONG ARRAY"
Next
Exit Select
Case 8
Dim As Integer64 int64array(1 To (size / elementsize))
For y = LBound(int64array) To UBound(int64array)
MemGet args(x), args(x).OFFSET + z, int64array(y)
z = z + args(x).ELEMENTSIZE
Print int64array(y), "INT64 ARRAY"
Next
Exit Select
End Select
Else
Select Case args(x).SIZE
Case 1
Print MemGet(args(x), args(x).OFFSET, Byte), "BYTE"
Exit Select
Case 2
Print MemGet(args(x), args(x).OFFSET, Integer), "SHORT"
Exit Select
Case 4
Print MemGet(args(x), args(x).OFFSET, Long), "LONG"
Exit Select
Case 8
Print MemGet(args(x), args(x).OFFSET, Integer64), "INT64"
Exit Select
End Select
End If
End If
ElseIf ReadBit(args(x).TYPE, 8) Then 'SINGLE, DOUBLE, FLOAT
If ReadBit(args(x).TYPE, 16) Then
Select Case args(x).ELEMENTSIZE
Case 4
Dim As Single singlearray(1 To (size / elementsize))
For y = LBound(singlearray) To UBound(singlearray)
MemGet args(x), args(x).OFFSET + z, singlearray(y)
z = z + args(x).ELEMENTSIZE
Print singlearray(y), "SINGLE ARRAY"
Next
Exit Select
Case 8
Dim As Double doublearray(1 To (size / elementsize))
For y = LBound(doublearray) To UBound(doublearray)
MemGet args(x), args(x).OFFSET + z, doublearray(y)
z = z + args(x).ELEMENTSIZE
Print doublearray(y), "DOUBLE ARRAY"
Next
Exit Select
Case 32
Dim As Float floatarray(1 To (size / elementsize))
For y = LBound(floatarray) To UBound(floatarray)
MemGet args(x), args(x).OFFSET + z, floatarray(y)
z = z + args(x).ELEMENTSIZE / 2
Print floatarray(y), "FLOAT ARRAY"
Next
Exit Select
End Select
Else
Select Case args(x).SIZE
Case 4
Print MemGet(args(x), args(x).OFFSET, Single), "SINGLE"
Exit Select
Case 8
Print MemGet(args(x), args(x).OFFSET, Double), "DOUBLE"
Exit Select
Case 32
Print MemGet(args(x), args(x).OFFSET, Float), "FLOAT"
Exit Select
End Select
End If
ElseIf ReadBit(args(x).TYPE, 9) Then 'STRING
If ReadBit(args(x).TYPE, 16) Then
Dim As String stringarray(1 To (size / elementsize))
For y = LBound(stringarray) To UBound(stringarray)
stringarray(y) = Space$(args(x).ELEMENTSIZE)
MemGet args(x), (args(x).OFFSET) + (y * args(x).ELEMENTSIZE - args(x).ELEMENTSIZE), stringarray(y)
Print stringarray(y), "STRING ARRAY"
Next
Else
Dim As String stringtest: stringtest = Space$(args(x).ELEMENTSIZE)
MemGet args(x), args(x).OFFSET, stringtest
Print stringtest
End If
ElseIf ReadBit(args(x).TYPE, 13) And ReadBit(args(x).TYPE, 7) Then '_OFFSET
If ReadBit(args(x).TYPE, 10) Then
If ReadBit(args(x).TYPE, 16) Then
Dim As Unsigned Offset unsignedoffsetarray(1 To (size / elementsize))
For y = LBound(unsignedoffsetarray) To UBound(unsignedoffsetarray)
MemGet args(x), args(x).OFFSET + z, unsignedoffsetarray(y)
z = z + args(x).ELEMENTSIZE
Print unsignedoffsetarray(y), "ULONG_PTR ARRAY"
Next
Else
Print MemGet(args(x), args(x).OFFSET, Unsigned Offset), "ULONG_PTR"
End If
Else
If ReadBit(args(x).TYPE, 16) Then
Dim As Offset offsetarray(1 To (size / elementsize))
For y = LBound(offsetarray) To UBound(offsetarray)
MemGet args(x), args(x).OFFSET + z, offsetarray(y)
z = z + args(x).ELEMENTSIZE
Print unsignedoffsetarray(y), "LONG_PTR ARRAY"
Next
Else
Print MemGet(args(x), args(x).OFFSET, Offset), "LONG_PTR"
End If
End If
ElseIf args(x).TYPE = 0 And args(x).SIZE > 0 Then 'MEMSOUND
If Not SndPlaying(args(x).SOUND) Then
SndPlay (args(x).SOUND)
End If
Print "SOUND", args(x).SIZE, args(x).ELEMENTSIZE
ElseIf ReadBit(args(x).TYPE, 14) Then
Print args(x).SIZE, "MEM"
'todo
End If
If ReadBit(args(x).TYPE, 11) Then 'MEMIMAGE
Screen args(x).IMAGE
End If
End If
Next
End Sub
RE: Miscellaneous handy goodies - madscijr - 12-28-2022
(12-28-2022, 01:46 AM)grymmjack Wrote: And...
How I wish we could have optional parameters in subs and funcs.
How I wish for a VARIANT type because lazy! then I could make one function or sub to handle all types of numbers, instead of requiring n$ and ln$ for example. One for ints, and one for longs.
We are the music makers! We are the dreamers of dreams!
Hear, hear!
I second that motion, lol.
(I have to try that _MEM trick!)
RE: Miscellaneous handy goodies - mnrvovrfc - 03-27-2023
(12-28-2022, 01:44 AM)grymmjack Wrote: I will return to this to explain but mostly this is self-explanatory stuff.
If I were offering this code to other people I'd turn all those % into &&, and give a better name to that ln$ and change the input parameter type to && instead of !. This is so that it's useful with any integer type. What if you needed pretty-print for an _INTEGER64?
Also I prefer the "value&& * (-1)" instead of "-value&&" that somebody else proposed. It clearly explains what is going on. But then again this is a "toolbox" not meant for beginners trying to learn BASIC programming.
I was going to write something else just above but the following works, does give me -5 in Lua:
Code: (Select All) value = 5
print(-value)
Those metatables are as slick as the programmer wants them to be...
RE: Miscellaneous handy goodies - aurel - 04-02-2023
well in some "modern" languages i see on github
there are
i32
i64
or
int32
int64
f32 as float or single
etc
etcc
etc,,,
|