retro.bmx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. Strict
  2. Rem
  3. bbdoc: BASIC/BASIC compatibility
  4. End Rem
  5. Module BRL.Retro
  6. ModuleInfo "Version: 1.09"
  7. ModuleInfo "Author: Mark Sibly"
  8. ModuleInfo "License: zlib/libpng"
  9. ModuleInfo "Copyright: Blitz Research Ltd"
  10. ModuleInfo "Modserver: BRL"
  11. ModuleInfo "History: 1.09 Release"
  12. ModuleInfo "History: Cleaned up Mid$"
  13. ModuleInfo "History: 1.08 Release"
  14. ModuleInfo "History: Deleted network stuff"
  15. Import BRL.Basic
  16. Rem
  17. bbdoc: Extract substring from a string
  18. returns: A sequence of characters from @str starting at position @pos and of length @size
  19. about:
  20. The Mid command returns a substring of a String.
  21. Given an existing string, a @position from the start of the string and
  22. an optional @size, #Mid creates a new string equal to the section specified.
  23. If no size if given, #Mid returns the characters in the existing string from
  24. @position to the end of the string.
  25. For compatibility with classic BASIC, the @pos parameter is 'one based'.
  26. End Rem
  27. Function Mid:String( str:String,pos,size=-1 )
  28. If pos>Len( str ) Return Null
  29. pos:-1
  30. If( size<0 ) Return str[pos..]
  31. If pos<0 size=size+pos;pos=0
  32. If pos+size>Len( str ) size=Len( str )-pos
  33. Return str[pos..pos+size]
  34. End Function
  35. Rem
  36. bbdoc: Find a string within a string
  37. returns: The position within @str of the first matching occurance of @sub
  38. about:
  39. The @start parameter allows you to specifying a starting index for the search.
  40. For compatiblity with classic BASIC, the @start parameter and returned position
  41. are both 'one based'.
  42. End Rem
  43. Function Instr( str:String,sub:String,start=1 )
  44. Return str.Find( sub,start-1 )+1
  45. End Function
  46. Rem
  47. bbdoc: Extract characters from the beginning of a string
  48. returns: @size leftmost characers of @str
  49. about:
  50. The Left command returns a substring of a String.
  51. Given an existing String and a @size, Left returns the first @size
  52. characters from the start of the String in a new String.
  53. End Rem
  54. Function Left:String( str:String,n )
  55. If n>Len(str) n=Len(str)
  56. Return str[..n]
  57. End Function
  58. Rem
  59. bbdoc: Extract characters from the end of a string
  60. returns: @size rightmost characters of @str
  61. about:
  62. The Right command returns a substring of a String.
  63. Given an existing String and a @size, Right returns the last @size
  64. characters from the end of the String.
  65. End Rem
  66. Function Right:String( str:String,n )
  67. If n>Len(str) n=Len(str)
  68. Return str[Len(str)-n..]
  69. End Function
  70. Rem
  71. bbdoc: Left justify string
  72. returns: A string of length @n, padded with spaces
  73. endrem
  74. Function LSet:String( str:String,n )
  75. Return str[..n]
  76. End Function
  77. Rem
  78. bbdoc: Right justify string
  79. returns: A string of length @n, padded with spaces
  80. endrem
  81. Function RSet:String( str:String,n )
  82. Return str[Len(str)-n..]
  83. End Function
  84. Rem
  85. bbdoc: Performs a search and replace function
  86. returns: A string with all instances of @sub replaced by @replace
  87. about:
  88. The Replace command replaces all instances of one string with another.
  89. End Rem
  90. Function Replace:String( str:String,sub:String,replaceWith:String )
  91. Return str.Replace( sub,replaceWith )
  92. End Function
  93. Rem
  94. bbdoc: Remove unprintable characters from ends a string
  95. returns: @str with leading and trailing unprintable characters removed
  96. End Rem
  97. Function Trim:String( str:String )
  98. Return str.Trim()
  99. End Function
  100. Rem
  101. bbdoc: Convert string to lowercase
  102. returns: Lowercase equivalent of @str
  103. End Rem
  104. Function Lower:String( str:String )
  105. Return str.ToLower()
  106. End Function
  107. Rem
  108. bbdoc: Convert string to uppercase
  109. returns: Uppercase equivalent of @str
  110. End Rem
  111. Function Upper:String( str:String )
  112. Return str.ToUpper()
  113. End Function
  114. Rem
  115. bbdoc: Convert an integer value to a hexadecimal string
  116. returns: The hexadecimal string representation of @val
  117. End Rem
  118. Function Hex:String( val )
  119. Local buf:Short[8]
  120. For Local k=7 To 0 Step -1
  121. Local n=(val&15)+Asc("0")
  122. If n>Asc("9") n=n+(Asc("A")-Asc("9")-1)
  123. buf[k]=n
  124. val:Shr 4
  125. Next
  126. Return String.FromShorts( buf,8 )
  127. End Function
  128. Rem
  129. bbdoc: Convert an integer value to a binary string
  130. returns: The binary string representation of @val
  131. End Rem
  132. Function Bin:String( val )
  133. Local buf:Short[32]
  134. For Local k=31 To 0 Step -1
  135. buf[k]=(val&1)+Asc("0")
  136. val:Shr 1
  137. Next
  138. Return String.FromShorts( buf,32 )
  139. End Function
  140. Rem
  141. bbdoc: Convert a 64 bit long integer value to a hexadecimal string
  142. returns: The hexadecimal string representation of @val
  143. End Rem
  144. Function LongHex:String( val:Long )
  145. Return Hex( Int(val Shr 32) )+Hex( Int(val) )
  146. End Function
  147. Rem
  148. bbdoc: Convert a 64 bit long integer value to a binary string
  149. returns: The binary string representation of @val
  150. End Rem
  151. Function LongBin:String( val:Long )
  152. Return Bin( Int(val Shr 32) )+Bin( Int(val) )
  153. End Function