stringbuffer_core.bmx 10 KB

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