2
0

stringbuffer_core.bmx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. ' Copyright (c) 2016-2023 Bruce A Henderson
  2. '
  3. ' Permission is hereby granted, free of charge, to any person obtaining a copy
  4. ' of this software and associated documentation files (the "Software"), to deal
  5. ' in the Software without restriction, including without limitation the rights
  6. ' to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. ' copies of the Software, and to permit persons to whom the Software is
  8. ' furnished to do so, subject to the following conditions:
  9. '
  10. ' The above copyright notice and this permission notice shall be included in
  11. ' all copies or substantial portions of the Software.
  12. '
  13. ' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. ' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. ' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. ' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. ' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. ' OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. ' THE SOFTWARE.
  20. '
  21. SuperStrict
  22. Rem
  23. bbdoc: A string buffer.
  24. End Rem
  25. 'Module BaH.StringBuffer
  26. 'ModuleInfo "Version: 1.03"
  27. 'ModuleInfo "License: MIT"
  28. 'ModuleInfo "Copyright: 2016 Bruce A Henderson"
  29. 'ModuleInfo "History: 1.03"
  30. 'ModuleInfo "History: Added overloaded constructor for providing instance specific initial capacity."
  31. 'ModuleInfo "History: 1.02"
  32. 'ModuleInfo "History: Added AppendCString() and AppendUTF8String() methods."
  33. 'ModuleInfo "History: 1.01"
  34. 'ModuleInfo "History: Added CharAt(), SetCharAt() and RemoveCharAt() methods."
  35. 'ModuleInfo "History: 1.00 Initial Release"
  36. Import "stringbuffer_common.bmx"
  37. Rem
  38. bbdoc: A modifiable String.
  39. about: A string buffer provides functionality to efficiently insert, replace, remove, append and reverse.
  40. It is an order of magnitude faster to append Strings to a TStringBuffer than it is to append Strings to Strings.
  41. End Rem
  42. Type TStringBuffer
  43. ' the char buffer
  44. Field buffer:Byte Ptr
  45. Global initialCapacity:Int = 16
  46. Method New()
  47. buffer = bmx_stringbuffer_new(initialCapacity)
  48. End Method
  49. ?bmxng
  50. Method New(initialCapacity:Int)
  51. buffer = bmx_stringbuffer_new(initialCapacity)
  52. End Method
  53. ?
  54. Rem
  55. bbdoc: Constructs a string buffer initialized to the contents of the specified string.
  56. End Rem
  57. Function Create:TStringBuffer(Text:String)
  58. ?Not bmxng
  59. Local this:TStringBuffer = New TStringBuffer
  60. ?bmxng
  61. Local this:TStringBuffer
  62. If text.length > initialCapacity Then
  63. this = New TStringBuffer(text.length)
  64. Else
  65. this = New TStringBuffer
  66. End If
  67. ?
  68. Return this.Append(Text)
  69. End Function
  70. Rem
  71. bbdoc: Returns the length of the string the string buffer would create.
  72. End Rem
  73. Method Length:Int()
  74. Return bmx_stringbuffer_count(buffer)
  75. End Method
  76. Rem
  77. bbdoc: Returns the total number of characters that the string buffer can accommodate before needing to grow.
  78. End Rem
  79. Method Capacity:Int()
  80. Return bmx_stringbuffer_capacity(buffer)
  81. End Method
  82. Rem
  83. bbdoc: Sets the length of the string buffer.
  84. about: If the length is less than the current length, the current text will be truncated. Otherwise,
  85. the capacity will be increased as necessary, although the actual length of text will remain the same.
  86. End Rem
  87. Method SetLength(length:Int)
  88. bmx_stringbuffer_setlength(buffer, length)
  89. End Method
  90. Rem
  91. bbdoc: Appends the text onto the string buffer.
  92. End Rem
  93. Method Append:TStringBuffer(value:String)
  94. bmx_stringbuffer_append_string(buffer, value)
  95. Return Self
  96. End Method
  97. Rem
  98. bbdoc: Appends an object onto the string buffer.
  99. about: This generally calls the object's ToString() method.
  100. TStringBuffer objects are simply mem-copied.
  101. End Rem
  102. Method AppendObject:TStringBuffer(obj:Object)
  103. If TStringBuffer(obj) Then
  104. bmx_stringbuffer_append_stringbuffer(buffer, TStringBuffer(obj).buffer)
  105. Else
  106. bmx_stringbuffer_append_string(buffer, obj.ToString())
  107. End If
  108. Return Self
  109. End Method
  110. Rem
  111. bbdoc: Appends a null-terminated C string onto the string buffer.
  112. End Rem
  113. Method AppendCString:TStringBuffer(chars:Byte Ptr)
  114. bmx_stringbuffer_append_cstring(buffer, chars)
  115. Return Self
  116. End Method
  117. Rem
  118. bbdoc: Appends a null-terminated UTF-8 string onto the string buffer.
  119. End Rem
  120. Method AppendUTF8String:TStringBuffer(chars:Byte Ptr)
  121. bmx_stringbuffer_append_utf8string(buffer, chars)
  122. Return Self
  123. End Method
  124. Rem
  125. bbdoc: Appends an array of shorts onto the string builder.
  126. End Rem
  127. Method AppendShorts:TStringBuffer(shorts:Short Ptr, length:Int)
  128. bmx_stringbuffer_append_shorts(buffer, shorts, length)
  129. Return Self
  130. End Method
  131. Method AppendChar:TStringBuffer(char:Int)
  132. bmx_stringbuffer_append_char(buffer, char)
  133. Return Self
  134. End Method
  135. Rem
  136. bbdoc: Finds first occurance of a sub string.
  137. returns: -1 if @subString not found.
  138. End Rem
  139. Method Find:Int(subString:String, startIndex:Int = 0)
  140. Return bmx_stringbuffer_find(buffer, subString, startIndex)
  141. End Method
  142. Rem
  143. bbdoc: Finds last occurance of a sub string.
  144. returns: -1 if @subString not found.
  145. End Rem
  146. Method FindLast:Int(subString:String, startIndex:Int = 0)
  147. Return bmx_stringbuffer_findlast(buffer, subString, startIndex)
  148. End Method
  149. Rem
  150. bbdoc: Removes leading and trailing non-printable characters from the string buffer.
  151. End Rem
  152. Method Trim:TStringBuffer()
  153. bmx_stringbuffer_trim(buffer)
  154. Return Self
  155. End Method
  156. Rem
  157. bbdoc: Replaces all occurances of @subString with @withString.
  158. End Rem
  159. Method Replace:TStringBuffer(subString:String, withString:String)
  160. bmx_stringbuffer_replace(buffer, subString, withString)
  161. Return Self
  162. End Method
  163. Rem
  164. bbdoc: Returns true if string starts with @subString.
  165. End Rem
  166. Method StartsWith:Int(subString:String)
  167. Return bmx_stringbuffer_startswith(buffer, subString)
  168. End Method
  169. Rem
  170. bbdoc: Returns true if string ends with @subString.
  171. End Rem
  172. Method EndsWith:Int(subString:String)
  173. Return bmx_stringbuffer_endswith(buffer, subString)
  174. End Method
  175. Rem
  176. bbdoc: Returns the char value in the buffer at the specified index.
  177. about: The first char value is at index 0, the next at index 1, and so on, as in array indexing.
  178. @index must be greater than or equal to 0, and less than the length of the buffer.
  179. End Rem
  180. Method CharAt:Int(index:Int)
  181. Return bmx_stringbuffer_charat(buffer, index)
  182. End Method
  183. Rem
  184. bbdoc: Returns true if string contains @subString.
  185. End Rem
  186. Method Contains:Int(subString:String)
  187. Return Find(subString) >= 0
  188. End Method
  189. Rem
  190. bbdoc: Joins @bits together by inserting this string buffer between each bit.
  191. returns: A new TStringBuffer object.
  192. End Rem
  193. Method Join:TStringBuffer(bits:String[])
  194. Local buf:TStringBuffer = New TStringBuffer
  195. bmx_stringbuffer_join(buffer, bits, buf.buffer)
  196. Return buf
  197. End Method
  198. Rem
  199. bbdoc: Converts all of the characters in the buffer to lower case.
  200. End Rem
  201. Method ToLower:TStringBuffer()
  202. bmx_stringbuffer_tolower(buffer)
  203. Return Self
  204. End Method
  205. Rem
  206. bbdoc: Converts all of the characters in the buffer to upper case.
  207. End Rem
  208. Method ToUpper:TStringBuffer()
  209. bmx_stringbuffer_toupper(buffer)
  210. Return Self
  211. End Method
  212. Rem
  213. bbdoc: Removes a range of characters from the string buffer.
  214. about: @startIndex is the first character to remove. @endIndex is the index after the last character to remove.
  215. End Rem
  216. Method Remove:TStringBuffer(startIndex:Int, endIndex:Int)
  217. bmx_stringbuffer_remove(buffer, startIndex, endIndex)
  218. Return Self
  219. End Method
  220. Rem
  221. bbdoc: Removes the character at the specified position in the buffer.
  222. about: The buffer is shortened by one character.
  223. End Rem
  224. Method RemoveCharAt:TStringBuffer(index:Int)
  225. bmx_stringbuffer_removecharat(buffer, index)
  226. Return Self
  227. End Method
  228. Rem
  229. bbdoc: Inserts text into the string buffer at the specified offset.
  230. End Rem
  231. Method Insert:TStringBuffer(offset:Int, value:String)
  232. bmx_stringbuffer_insert(buffer, offset, value)
  233. Return Self
  234. End Method
  235. Rem
  236. bbdoc: Reverses the characters of the string buffer.
  237. End Rem
  238. Method Reverse:TStringBuffer()
  239. bmx_stringbuffer_reverse(buffer)
  240. Return Self
  241. End Method
  242. Rem
  243. bbdoc: The character at the specified index is set to @char.
  244. about: @index must be greater than or equal to 0, and less than the length of the buffer.
  245. End Rem
  246. Method SetCharAt(index:Int, char:Int)
  247. bmx_stringbuffer_setcharat(buffer, index, char)
  248. End Method
  249. Rem
  250. bbdoc: Returns a substring of the string buffer given the specified indexes.
  251. about: @beginIndex is the first character of the substring.
  252. @endIndex is the index after the last character of the substring. If @endIndex is zero,
  253. will return everything from @beginIndex until the end of the string buffer.
  254. End Rem
  255. Method Substring:String(beginIndex:Int, endIndex:Int = 0)
  256. Return bmx_stringbuffer_substring(buffer, beginIndex, endIndex)
  257. End Method
  258. Rem
  259. bbdoc:
  260. End Rem
  261. Method Split:TSplitBuffer(separator:String)
  262. Local buf:TSplitBuffer = New TSplitBuffer
  263. buf.buffer = Self
  264. buf.splitPtr = bmx_stringbuffer_split(buffer, separator)
  265. Return buf
  266. End Method
  267. Rem
  268. bbdoc: Converts the string buffer to a String.
  269. End Rem
  270. Method ToString:String()
  271. Return bmx_stringbuffer_tostring(buffer)
  272. End Method
  273. Method Delete()
  274. If buffer Then
  275. bmx_stringbuffer_free(buffer)
  276. buffer = Null
  277. End If
  278. End Method
  279. End Type
  280. Rem
  281. bbdoc: An array of split text from a TStringBuffer.
  282. about: Note that the TSplitBuffer is only valid while its parent TStringBuffer is unchanged.
  283. Once you modify the TStringBuffer you should call Split() again.
  284. End Rem
  285. Type TSplitBuffer
  286. Field buffer:TStringBuffer
  287. Field splitPtr:Byte Ptr
  288. Rem
  289. bbdoc: The number of split elements.
  290. End Rem
  291. Method Length:Int()
  292. Return bmx_stringbuffer_splitbuffer_length(splitPtr)
  293. End Method
  294. Rem
  295. bbdoc: Returns the text for the given index in the split buffer.
  296. End Rem
  297. Method Text:String(index:Int)
  298. Return bmx_stringbuffer_splitbuffer_text(splitPtr, index)
  299. End Method
  300. Rem
  301. bbdoc: Creates a new string array of all the split elements.
  302. End Rem
  303. Method ToArray:String[]()
  304. Return bmx_stringbuffer_splitbuffer_toarray(splitPtr)
  305. End Method
  306. Method ObjectEnumerator:TSplitBufferEnum()
  307. Local enumerator:TSplitBufferEnum = New TSplitBufferEnum
  308. enumerator.buffer = Self
  309. enumerator.length = Length()
  310. Return enumerator
  311. End Method
  312. Method Delete()
  313. If splitPtr Then
  314. buffer = Null
  315. bmx_stringbuffer_splitbuffer_free(splitPtr)
  316. splitPtr = Null
  317. End If
  318. End Method
  319. End Type
  320. Type TSplitBufferEnum
  321. Field index:Int
  322. Field length:Int
  323. Field buffer:TSplitBuffer
  324. Method HasNext:Int()
  325. Return index < length
  326. End Method
  327. Method NextObject:Object()
  328. Local s:String = buffer.Text(index)
  329. index :+ 1
  330. Return s
  331. End Method
  332. End Type