stringbuffer_core.bmx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. ' Copyright (c) 2016-2019 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. Rem
  132. bbdoc: Finds first occurance of a sub string.
  133. returns: -1 if @subString not found.
  134. End Rem
  135. Method Find:Int(subString:String, startIndex:Int = 0)
  136. Return bmx_stringbuffer_find(buffer, subString, startIndex)
  137. End Method
  138. Rem
  139. bbdoc: Finds last occurance of a sub string.
  140. returns: -1 if @subString not found.
  141. End Rem
  142. Method FindLast:Int(subString:String, startIndex:Int = 0)
  143. Return bmx_stringbuffer_findlast(buffer, subString, startIndex)
  144. End Method
  145. Rem
  146. bbdoc: Removes leading and trailing non-printable characters from the string buffer.
  147. End Rem
  148. Method Trim:TStringBuffer()
  149. bmx_stringbuffer_trim(buffer)
  150. Return Self
  151. End Method
  152. Rem
  153. bbdoc: Replaces all occurances of @subString with @withString.
  154. End Rem
  155. Method Replace:TStringBuffer(subString:String, withString:String)
  156. bmx_stringbuffer_replace(buffer, subString, withString)
  157. Return Self
  158. End Method
  159. Rem
  160. bbdoc: Returns true if string starts with @subString.
  161. End Rem
  162. Method StartsWith:Int(subString:String)
  163. Return bmx_stringbuffer_startswith(buffer, subString)
  164. End Method
  165. Rem
  166. bbdoc: Returns true if string ends with @subString.
  167. End Rem
  168. Method EndsWith:Int(subString:String)
  169. Return bmx_stringbuffer_endswith(buffer, subString)
  170. End Method
  171. Rem
  172. bbdoc: Returns the char value in the buffer at the specified index.
  173. about: The first char value is at index 0, the next at index 1, and so on, as in array indexing.
  174. @index must be greater than or equal to 0, and less than the length of the buffer.
  175. End Rem
  176. Method CharAt:Int(index:Int)
  177. Return bmx_stringbuffer_charat(buffer, index)
  178. End Method
  179. Rem
  180. bbdoc: Returns true if string contains @subString.
  181. End Rem
  182. Method Contains:Int(subString:String)
  183. Return Find(subString) >= 0
  184. End Method
  185. Rem
  186. bbdoc: Joins @bits together by inserting this string buffer between each bit.
  187. returns: A new TStringBuffer object.
  188. End Rem
  189. Method Join:TStringBuffer(bits:String[])
  190. Local buf:TStringBuffer = New TStringBuffer
  191. bmx_stringbuffer_join(buffer, bits, buf.buffer)
  192. Return buf
  193. End Method
  194. Rem
  195. bbdoc: Converts all of the characters in the buffer to lower case.
  196. End Rem
  197. Method ToLower:TStringBuffer()
  198. bmx_stringbuffer_tolower(buffer)
  199. Return Self
  200. End Method
  201. Rem
  202. bbdoc: Converts all of the characters in the buffer to upper case.
  203. End Rem
  204. Method ToUpper:TStringBuffer()
  205. bmx_stringbuffer_toupper(buffer)
  206. Return Self
  207. End Method
  208. Rem
  209. bbdoc: Removes a range of characters from the string buffer.
  210. about: @startIndex is the first character to remove. @endIndex is the index after the last character to remove.
  211. End Rem
  212. Method Remove:TStringBuffer(startIndex:Int, endIndex:Int)
  213. bmx_stringbuffer_remove(buffer, startIndex, endIndex)
  214. Return Self
  215. End Method
  216. Rem
  217. bbdoc: Removes the character at the specified position in the buffer.
  218. about: The buffer is shortened by one character.
  219. End Rem
  220. Method RemoveCharAt:TStringBuffer(index:Int)
  221. bmx_stringbuffer_removecharat(buffer, index)
  222. Return Self
  223. End Method
  224. Rem
  225. bbdoc: Inserts text into the string buffer at the specified offset.
  226. End Rem
  227. Method Insert:TStringBuffer(offset:Int, value:String)
  228. bmx_stringbuffer_insert(buffer, offset, value)
  229. Return Self
  230. End Method
  231. Rem
  232. bbdoc: Reverses the characters of the string buffer.
  233. End Rem
  234. Method Reverse:TStringBuffer()
  235. bmx_stringbuffer_reverse(buffer)
  236. Return Self
  237. End Method
  238. Rem
  239. bbdoc: The character at the specified index is set to @char.
  240. about: @index must be greater than or equal to 0, and less than the length of the buffer.
  241. End Rem
  242. Method SetCharAt(index:Int, char:Int)
  243. bmx_stringbuffer_setcharat(buffer, index, char)
  244. End Method
  245. Rem
  246. bbdoc: Returns a substring of the string buffer given the specified indexes.
  247. about: @beginIndex is the first character of the substring.
  248. @endIndex is the index after the last character of the substring. If @endIndex is zero,
  249. will return everything from @beginIndex until the end of the string buffer.
  250. End Rem
  251. Method Substring:String(beginIndex:Int, endIndex:Int = 0)
  252. Return bmx_stringbuffer_substring(buffer, beginIndex, endIndex)
  253. End Method
  254. Rem
  255. bbdoc:
  256. End Rem
  257. Method Split:TSplitBuffer(separator:String)
  258. Local buf:TSplitBuffer = New TSplitBuffer
  259. buf.buffer = Self
  260. buf.splitPtr = bmx_stringbuffer_split(buffer, separator)
  261. Return buf
  262. End Method
  263. Rem
  264. bbdoc: Converts the string buffer to a String.
  265. End Rem
  266. Method ToString:String()
  267. Return bmx_stringbuffer_tostring(buffer)
  268. End Method
  269. Method Delete()
  270. If buffer Then
  271. bmx_stringbuffer_free(buffer)
  272. buffer = Null
  273. End If
  274. End Method
  275. End Type
  276. Rem
  277. bbdoc: An array of split text from a TStringBuffer.
  278. about: Note that the TSplitBuffer is only valid while its parent TStringBuffer is unchanged.
  279. Once you modify the TStringBuffer you should call Split() again.
  280. End Rem
  281. Type TSplitBuffer
  282. Field buffer:TStringBuffer
  283. Field splitPtr:Byte Ptr
  284. Rem
  285. bbdoc: The number of split elements.
  286. End Rem
  287. Method Length:Int()
  288. Return bmx_stringbuffer_splitbuffer_length(splitPtr)
  289. End Method
  290. Rem
  291. bbdoc: Returns the text for the given index in the split buffer.
  292. End Rem
  293. Method Text:String(index:Int)
  294. Return bmx_stringbuffer_splitbuffer_text(splitPtr, index)
  295. End Method
  296. Rem
  297. bbdoc: Creates a new string array of all the split elements.
  298. End Rem
  299. Method ToArray:String[]()
  300. Return bmx_stringbuffer_splitbuffer_toarray(splitPtr)
  301. End Method
  302. Method ObjectEnumerator:TSplitBufferEnum()
  303. Local enum:TSplitBufferEnum = New TSplitBufferEnum
  304. enum.buffer = Self
  305. enum.length = Length()
  306. Return enum
  307. End Method
  308. Method Delete()
  309. If splitPtr Then
  310. buffer = Null
  311. bmx_stringbuffer_splitbuffer_free(splitPtr)
  312. splitPtr = Null
  313. End If
  314. End Method
  315. End Type
  316. Type TSplitBufferEnum
  317. Field index:Int
  318. Field length:Int
  319. Field buffer:TSplitBuffer
  320. Method HasNext:Int()
  321. Return index < length
  322. End Method
  323. Method NextObject:Object()
  324. Local s:String = buffer.Text(index)
  325. index :+ 1
  326. Return s
  327. End Method
  328. End Type