stringbuilder.bmx 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. ' Copyright (c) 2018-2022 Bruce A Henderson
  2. '
  3. ' This software is provided 'as-is', without any express or implied
  4. ' warranty. In no event will the authors be held liable for any damages
  5. ' arising from the use of this software.
  6. '
  7. ' Permission is granted to anyone to use this software for any purpose,
  8. ' including commercial applications, and to alter it and redistribute it
  9. ' freely, subject to the following restrictions:
  10. '
  11. ' 1. The origin of this software must not be misrepresented; you must not
  12. ' claim that you wrote the original software. If you use this software
  13. ' in a product, an acknowledgment in the product documentation would be
  14. ' appreciated but is not required.
  15. ' 2. Altered source versions must be plainly marked as such, and must not be
  16. ' misrepresented as being the original software.
  17. ' 3. This notice may not be removed or altered from any source distribution.
  18. '
  19. SuperStrict
  20. Rem
  21. bbdoc: A string builder.
  22. End Rem
  23. Module BRL.StringBuilder
  24. ModuleInfo "Version: 1.17"
  25. ModuleInfo "License: zlib/libpng"
  26. ModuleInfo "Copyright: 2018-2022 Bruce A Henderson"
  27. ModuleInfo "History: 1.17"
  28. ModuleInfo "History: Added AppendCStringBytes() method."
  29. ModuleInfo "History: 1.16"
  30. ModuleInfo "History: Added AppendUTF32() and AppendUTF32Bytes() method."
  31. ModuleInfo "History: 1.15"
  32. ModuleInfo "History: Added AppendUTF8Bytes() method."
  33. ModuleInfo "History: 1.14"
  34. ModuleInfo "History: Added Hash() method."
  35. ModuleInfo "History: 1.13"
  36. ModuleInfo "History: Changes for low-level external use - header, exposing buffer."
  37. ModuleInfo "History: 1.12"
  38. ModuleInfo "History: Improved equality checks."
  39. ModuleInfo "History: 1.11"
  40. ModuleInfo "History: Added Format() methods."
  41. ModuleInfo "History: 1.10"
  42. ModuleInfo "History: Added JoinStrings() method."
  43. ModuleInfo "History: 1.09"
  44. ModuleInfo "History: Added ToUTF8String() and ToWString() methods."
  45. ModuleInfo "History: 1.08"
  46. ModuleInfo "History: Added LeftAlign() and RightAlign() methods."
  47. ModuleInfo "History: 1.07"
  48. ModuleInfo "History: Fixed AppendByte and AppendShort not using unsigned variants."
  49. ModuleInfo "History: 1.06"
  50. ModuleInfo "History: Implemented Compare(), and added overloads for = and <>."
  51. ModuleInfo "History: 1.05"
  52. ModuleInfo "History: NG Refactoring."
  53. ModuleInfo "History: Added overloaded Append() methods."
  54. ModuleInfo "History: 1.04"
  55. ModuleInfo "History: Added shorts appender."
  56. ModuleInfo "History: 1.03"
  57. ModuleInfo "History: Added overloaded constructor for providing instance specific initial capacity."
  58. ModuleInfo "History: 1.02"
  59. ModuleInfo "History: Added AppendCString() and AppendUTF8String() methods."
  60. ModuleInfo "History: 1.01"
  61. ModuleInfo "History: Added CharAt(), SetCharAt() and RemoveCharAt() methods."
  62. ModuleInfo "History: 1.00 Initial Release"
  63. Import "common.bmx"
  64. Rem
  65. bbdoc: A modifiable String.
  66. about: A string builder provides functionality to efficiently insert, replace, remove, append and reverse.
  67. It is an order of magnitude faster to append Strings to a TStringBuilder than it is to append Strings to Strings.
  68. End Rem
  69. Type TStringBuilder
  70. ' the char buffer
  71. Field buffer:Byte Ptr
  72. Private
  73. Field newLine:String
  74. Field nullText:String
  75. Global initialCapacity:Int = 16
  76. ?win32
  77. Const NEW_LINE:String = "~r~n"
  78. ?Not win32
  79. Const NEW_LINE:String = "~n"
  80. ?
  81. Public
  82. Rem
  83. bbdoc: Constructs a #TStringBuilder with the default capacity.
  84. End Rem
  85. Method New()
  86. buffer = bmx_stringbuilder_new(initialCapacity)
  87. End Method
  88. Rem
  89. bbdoc: Constructs a #TStringBuilder with a specified @initialCapacity.
  90. End Rem
  91. Method New(initialCapacity:Int)
  92. buffer = bmx_stringbuilder_new(initialCapacity)
  93. End Method
  94. Rem
  95. bbdoc: Constructs a #TStringBuilder initialized to the contents of @Text.
  96. End Rem
  97. Method New(Text:String)
  98. If Text.length > initialCapacity Then
  99. buffer = bmx_stringbuilder_new(Text.Length)
  100. Else
  101. buffer = bmx_stringbuilder_new(initialCapacity)
  102. End If
  103. bmx_stringbuilder_append_string(buffer, Text)
  104. End Method
  105. Rem
  106. bbdoc: Constructs a #TStringBuilder initialized to the contents of the specified string.
  107. End Rem
  108. Function Create:TStringBuilder(Text:String)
  109. Return New TStringBuilder(Text)
  110. End Function
  111. Rem
  112. bbdoc: Returns the length of the string the string builder would create.
  113. End Rem
  114. Method Length:Int()
  115. Return bmx_stringbuilder_count(buffer)
  116. End Method
  117. Rem
  118. bbdoc: Returns the total number of characters that the string builder can accommodate before needing to grow.
  119. End Rem
  120. Method Capacity:Int()
  121. Return bmx_stringbuilder_capacity(buffer)
  122. End Method
  123. Rem
  124. bbdoc: Sets the length of the string builder.
  125. about: If the length is less than the current length, the current text will be truncated. Otherwise,
  126. the capacity will be increased as necessary, although the actual length of text will remain the same.
  127. End Rem
  128. Method SetLength(length:Int)
  129. bmx_stringbuilder_setlength(buffer, length)
  130. End Method
  131. Rem
  132. bbdoc: Appends a #String onto the string builder.
  133. End Rem
  134. Method Append:TStringBuilder(value:String)
  135. bmx_stringbuilder_append_string(buffer, value)
  136. Return Self
  137. End Method
  138. Rem
  139. bbdoc: Appends a #String onto the string builder with new line at the end.
  140. End Rem
  141. Method AppendLine:TStringBuilder(value:String)
  142. Append(value)
  143. AppendNewLine()
  144. Return Self
  145. End Method
  146. Rem
  147. bbdoc: Appends a #Byte value to the string builder.
  148. End Rem
  149. Method AppendByte:TStringBuilder(value:Byte)
  150. bmx_stringbuilder_append_byte(buffer, value)
  151. Return Self
  152. End Method
  153. Rem
  154. bbdoc: Appends a #Byte value onto the string builder.
  155. End Rem
  156. Method Append:TStringBuilder(value:Byte)
  157. bmx_stringbuilder_append_byte(buffer, value)
  158. Return Self
  159. End Method
  160. Rem
  161. bbdoc: Appends an object onto the string builder.
  162. about: This generally calls the object's ToString() method.
  163. TStringBuilder objects are simply mem-copied.
  164. End Rem
  165. Method AppendObject:TStringBuilder(obj:Object)
  166. If TStringBuilder(obj) Then
  167. bmx_stringbuilder_append_stringbuffer(buffer, TStringBuilder(obj).buffer)
  168. Else
  169. If obj Then
  170. bmx_stringbuilder_append_string(buffer, obj.ToString())
  171. Else
  172. Return AppendNull()
  173. End If
  174. End If
  175. Return Self
  176. End Method
  177. Rem
  178. bbdoc: Appends an object onto the string builder.
  179. about: This generally calls the object's ToString() method.
  180. TStringBuilder objects are simply mem-copied.
  181. End Rem
  182. Method Append:TStringBuilder(obj:Object)
  183. If obj Then
  184. bmx_stringbuilder_append_string(buffer, obj.ToString())
  185. Else
  186. Return AppendNull()
  187. End If
  188. Return Self
  189. End Method
  190. Rem
  191. bbdoc: Appends a #TStringBuilder onto the string builder.
  192. End Rem
  193. Method Append:TStringBuilder(sb:TStringBuilder)
  194. bmx_stringbuilder_append_stringbuffer(buffer, sb.buffer)
  195. Return Self
  196. End Method
  197. Rem
  198. bbdoc: Appends a null-terminated C string onto the string builder.
  199. End Rem
  200. Method AppendCString:TStringBuilder(chars:Byte Ptr)
  201. bmx_stringbuilder_append_cstring(buffer, chars)
  202. Return Self
  203. End Method
  204. Rem
  205. bbdoc: Appends a C string of @length bytes onto the string builder.
  206. End Rem
  207. Method AppendCStringBytes:TStringBuilder(chars:Byte Ptr, length:Int)
  208. bmx_stringbuilder_append_cstringbytes(buffer, chars, length)
  209. Return Self
  210. End Method
  211. Rem
  212. bbdoc: Appends a #Double value to the string builder.
  213. End Rem
  214. Method AppendDouble:TStringBuilder(value:Double)
  215. bmx_stringbuilder_append_double(buffer, value)
  216. Return Self
  217. End Method
  218. Rem
  219. bbdoc: Appends a #Double value to the string builder.
  220. End Rem
  221. Method Append:TStringBuilder(value:Double)
  222. bmx_stringbuilder_append_double(buffer, value)
  223. Return Self
  224. End Method
  225. Rem
  226. bbdoc: Appends a #Float value to the string builder.
  227. End Rem
  228. Method AppendFloat:TStringBuilder(value:Float)
  229. bmx_stringbuilder_append_float(buffer, value)
  230. Return Self
  231. End Method
  232. Rem
  233. bbdoc: Appends a #Float value to the string builder.
  234. End Rem
  235. Method Append:TStringBuilder(value:Float)
  236. bmx_stringbuilder_append_float(buffer, value)
  237. Return Self
  238. End Method
  239. Rem
  240. bbdoc: Appends an #Int value to the string builder.
  241. End Rem
  242. Method AppendInt:TStringBuilder(value:Int)
  243. bmx_stringbuilder_append_int(buffer, value)
  244. Return Self
  245. End Method
  246. Rem
  247. bbdoc: Appends an #Int value to the string builder.
  248. End Rem
  249. Method Append:TStringBuilder(value:Int)
  250. bmx_stringbuilder_append_int(buffer, value)
  251. Return Self
  252. End Method
  253. Rem
  254. bbdoc: Appends a #Long value to the string builder.
  255. End Rem
  256. Method AppendLong:TStringBuilder(value:Long)
  257. bmx_stringbuilder_append_long(buffer, value)
  258. Return Self
  259. End Method
  260. Rem
  261. bbdoc: Appends a #Long value to the string builder.
  262. End Rem
  263. Method Append:TStringBuilder(value:Long)
  264. bmx_stringbuilder_append_long(buffer, value)
  265. Return Self
  266. End Method
  267. Rem
  268. bbdoc: Appends the new line string to the string builder.
  269. about: The new line string can be altered using #SetNewLineText. This might be used to force the output to always
  270. use Unix line endings even when on Windows.
  271. End Rem
  272. Method AppendNewLine:TStringBuilder()
  273. If newLine Then
  274. bmx_stringbuilder_append_string(buffer, newLine)
  275. Else
  276. bmx_stringbuilder_append_string(buffer, NEW_LINE)
  277. End If
  278. Return Self
  279. End Method
  280. Rem
  281. bbdoc: Appends the text representing null to the string builder.
  282. End Rem
  283. Method AppendNull:TStringBuilder()
  284. If nullText Then
  285. bmx_stringbuilder_append_string(buffer, nullText)
  286. End If
  287. Return Self
  288. End Method
  289. Rem
  290. bbdoc: Appends a Short value to the string builder.
  291. End Rem
  292. Method AppendShort:TStringBuilder(value:Short)
  293. bmx_stringbuilder_append_short(buffer, value)
  294. Return Self
  295. End Method
  296. Rem
  297. bbdoc: Appends a #Short value to the string builder.
  298. End Rem
  299. Method Append:TStringBuilder(value:Short)
  300. bmx_stringbuilder_append_short(buffer, value)
  301. Return Self
  302. End Method
  303. Rem
  304. bbdoc: Appends a #UInt value to the string builder.
  305. End Rem
  306. Method AppendUInt:TStringBuilder(value:UInt)
  307. bmx_stringbuilder_append_uint(buffer, value)
  308. Return Self
  309. End Method
  310. Rem
  311. bbdoc: Appends a #UInt value to the string builder.
  312. End Rem
  313. Method Append:TStringBuilder(value:UInt)
  314. bmx_stringbuilder_append_uint(buffer, value)
  315. Return Self
  316. End Method
  317. Rem
  318. bbdoc: Appends a #Ulong value to the string builder.
  319. End Rem
  320. Method AppendULong:TStringBuilder(value:ULong)
  321. bmx_stringbuilder_append_ulong(buffer, value)
  322. Return Self
  323. End Method
  324. Rem
  325. bbdoc: Appends a #Ulong value to the string builder.
  326. End Rem
  327. Method Append:TStringBuilder(value:ULong)
  328. bmx_stringbuilder_append_ulong(buffer, value)
  329. Return Self
  330. End Method
  331. Rem
  332. bbdoc: Appends a #Size_T value to the string builder.
  333. End Rem
  334. Method AppendSizet:TStringBuilder(value:Size_T)
  335. bmx_stringbuilder_append_sizet(buffer, value)
  336. Return Self
  337. End Method
  338. Rem
  339. bbdoc: Appends a #Size_T value to the string builder.
  340. End Rem
  341. Method Append:TStringBuilder(value:Size_T)
  342. bmx_stringbuilder_append_sizet(buffer, value)
  343. Return Self
  344. End Method
  345. Rem
  346. bbdoc: Appends a null-terminated UTF-8 string onto the string builder.
  347. End Rem
  348. Method AppendUTF8String:TStringBuilder(chars:Byte Ptr)
  349. bmx_stringbuilder_append_utf8string(buffer, chars)
  350. Return Self
  351. End Method
  352. Rem
  353. bbdoc: Appends a UTF-8 string of @length bytes onto the string builder.
  354. End Rem
  355. Method AppendUTF8Bytes:TStringBuilder(chars:Byte Ptr, length:Int)
  356. bmx_stringbuilder_append_utf8bytes(buffer, chars, length)
  357. Return Self
  358. End Method
  359. Rem
  360. bbdoc: Appends an array of shorts onto the string builder.
  361. End Rem
  362. Method AppendShorts:TStringBuilder(shorts:Short Ptr, length:Int)
  363. bmx_stringbuilder_append_shorts(buffer, shorts, length)
  364. Return Self
  365. End Method
  366. Rem
  367. bbdoc: Appends a character of the given @char code point to the string builder.
  368. End Rem
  369. Method AppendChar:TStringBuilder(char:Int)
  370. bmx_stringbuilder_append_char(buffer, char)
  371. Return Self
  372. End Method
  373. Rem
  374. bbdoc: Appends a null-terminated UTF-32 string onto the string builder.
  375. End Rem
  376. Method AppendUTF32String:TStringBuilder(chars:UInt Ptr)
  377. bmx_stringbuilder_append_utf32string(buffer, chars)
  378. Return Self
  379. End Method
  380. Rem
  381. bbdoc: Appends a UTF-32 string of @length characters (n bytes / 4) onto the string builder.
  382. End Rem
  383. Method AppendUTF32Bytes:TStringBuilder(chars:UInt Ptr, length:Int)
  384. bmx_stringbuilder_append_utf32bytes(buffer, chars, length)
  385. Return Self
  386. End Method
  387. Rem
  388. bbdoc: Compares the string builder with another object.
  389. about: If the other object is a string builder then, the contents of two are compared lexicographically, as
  390. determined by the unicode value of each character in order.
  391. If there is no difference, then the shorter of the two contents precedes the longer.
  392. If the other object is not a string builder, the standard object comparison is made.
  393. End Rem
  394. Method Compare:Int(o:Object) Override
  395. If TStringBuilder(o) Then
  396. Return bmx_stringbuilder_compare(buffer, TStringBuilder(o).buffer)
  397. End If
  398. Return Super.Compare(o)
  399. End Method
  400. Rem
  401. bbdoc: Finds first occurance of a sub string.
  402. returns: -1 if @subString not found.
  403. End Rem
  404. Method Find:Int(subString:String, startIndex:Int = 0)
  405. Return bmx_stringbuilder_find(buffer, subString, startIndex)
  406. End Method
  407. Rem
  408. bbdoc: Finds last occurance of a sub string.
  409. returns: -1 if @subString not found.
  410. End Rem
  411. Method FindLast:Int(subString:String, startIndex:Int = 0)
  412. Return bmx_stringbuilder_findlast(buffer, subString, startIndex)
  413. End Method
  414. Rem
  415. bbdoc: Appends a #String value to the string builder using the specified printf style @formatText.
  416. about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
  417. End Rem
  418. Method Format:TStringBuilder(formatText:String, value:String)
  419. bmx_stringbuilder_format_string(buffer, formatText, value)
  420. Return Self
  421. End Method
  422. Rem
  423. bbdoc: Appends a #Byte value to the string builder using the specified printf style @formatText.
  424. about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
  425. End Rem
  426. Method Format:TStringBuilder(formatText:String, value:Byte)
  427. bmx_stringbuilder_format_byte(buffer, formatText, value)
  428. Return Self
  429. End Method
  430. Rem
  431. bbdoc: Appends a #Short value to the string builder using the specified printf style @formatText.
  432. about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
  433. End Rem
  434. Method Format:TStringBuilder(formatText:String, value:Short)
  435. bmx_stringbuilder_format_short(buffer, formatText, value)
  436. Return Self
  437. End Method
  438. Rem
  439. bbdoc: Appends a #Int value to the string builder using the specified printf style @formatText.
  440. about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
  441. End Rem
  442. Method Format:TStringBuilder(formatText:String, value:Int)
  443. bmx_stringbuilder_format_int(buffer, formatText, value)
  444. Return Self
  445. End Method
  446. Rem
  447. bbdoc: Appends a #UInt value to the string builder using the specified printf style @formatText.
  448. about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
  449. End Rem
  450. Method Format:TStringBuilder(formatText:String, value:UInt)
  451. bmx_stringbuilder_format_uint(buffer, formatText, value)
  452. Return Self
  453. End Method
  454. Rem
  455. bbdoc: Appends a #Long value to the string builder using the specified printf style @formatText.
  456. about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
  457. End Rem
  458. Method Format:TStringBuilder(formatText:String, value:Long)
  459. bmx_stringbuilder_format_long(buffer, formatText, value)
  460. Return Self
  461. End Method
  462. Rem
  463. bbdoc: Appends a #ULong value to the string builder using the specified printf style @formatText.
  464. about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
  465. End Rem
  466. Method Format:TStringBuilder(formatText:String, value:ULong)
  467. bmx_stringbuilder_format_ulong(buffer, formatText, value)
  468. Return Self
  469. End Method
  470. Rem
  471. bbdoc: Appends a #Size_T value to the string builder using the specified printf style @formatText.
  472. about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
  473. End Rem
  474. Method Format:TStringBuilder(formatText:String, value:Size_T)
  475. bmx_stringbuilder_format_sizet(buffer, formatText, value)
  476. Return Self
  477. End Method
  478. Rem
  479. bbdoc: Appends a #Float value to the string builder using the specified printf style @formatText.
  480. about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
  481. End Rem
  482. Method Format:TStringBuilder(formatText:String, value:Float)
  483. bmx_stringbuilder_format_float(buffer, formatText, value)
  484. Return Self
  485. End Method
  486. Rem
  487. bbdoc: Appends a #Double value to the string builder using the specified printf style @formatText.
  488. about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
  489. End Rem
  490. Method Format:TStringBuilder(formatText:String, value:Double)
  491. bmx_stringbuilder_format_double(buffer, formatText, value)
  492. Return Self
  493. End Method
  494. Rem
  495. bbdoc: Appends a #String value to the string builder using the specified printf style @formatText.
  496. about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
  497. End Rem
  498. Method FormatString:TStringBuilder(formatText:String, value:String)
  499. bmx_stringbuilder_format_string(buffer, formatText, value)
  500. Return Self
  501. End Method
  502. Rem
  503. bbdoc: Appends a #Byte value to the string builder using the specified printf style @formatText.
  504. about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
  505. End Rem
  506. Method FormatByte:TStringBuilder(formatText:String, value:Byte)
  507. bmx_stringbuilder_format_byte(buffer, formatText, value)
  508. Return Self
  509. End Method
  510. Rem
  511. bbdoc: Appends a #Short value to the string builder using the specified printf style @formatText.
  512. about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
  513. End Rem
  514. Method FormatShort:TStringBuilder(formatText:String, value:Short)
  515. bmx_stringbuilder_format_short(buffer, formatText, value)
  516. Return Self
  517. End Method
  518. Rem
  519. bbdoc: Appends a #Int value to the string builder using the specified printf style @formatText.
  520. about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
  521. End Rem
  522. Method FormatInt:TStringBuilder(formatText:String, value:Int)
  523. bmx_stringbuilder_format_int(buffer, formatText, value)
  524. Return Self
  525. End Method
  526. Rem
  527. bbdoc: Appends a #UInt value to the string builder using the specified printf style @formatText.
  528. about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
  529. End Rem
  530. Method FormatUInt:TStringBuilder(formatText:String, value:UInt)
  531. bmx_stringbuilder_format_uint(buffer, formatText, value)
  532. Return Self
  533. End Method
  534. Rem
  535. bbdoc: Appends a #Long value to the string builder using the specified printf style @formatText.
  536. about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
  537. End Rem
  538. Method FormatLong:TStringBuilder(formatText:String, value:Long)
  539. bmx_stringbuilder_format_long(buffer, formatText, value)
  540. Return Self
  541. End Method
  542. Rem
  543. bbdoc: Appends a #ULong value to the string builder using the specified printf style @formatText.
  544. about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
  545. End Rem
  546. Method FormatULong:TStringBuilder(formatText:String, value:ULong)
  547. bmx_stringbuilder_format_ulong(buffer, formatText, value)
  548. Return Self
  549. End Method
  550. Rem
  551. bbdoc: Appends a #Size_T value to the string builder using the specified printf style @formatText.
  552. about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
  553. End Rem
  554. Method FormatSizeT:TStringBuilder(formatText:String, value:Size_T)
  555. bmx_stringbuilder_format_sizet(buffer, formatText, value)
  556. Return Self
  557. End Method
  558. Rem
  559. bbdoc: Appends a #Float value to the string builder using the specified printf style @formatText.
  560. about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
  561. End Rem
  562. Method FormatFloat:TStringBuilder(formatText:String, value:Float)
  563. bmx_stringbuilder_format_float(buffer, formatText, value)
  564. Return Self
  565. End Method
  566. Rem
  567. bbdoc: Appends a #Double value to the string builder using the specified printf style @formatText.
  568. about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
  569. End Rem
  570. Method FormatDouble:TStringBuilder(formatText:String, value:Double)
  571. bmx_stringbuilder_format_double(buffer, formatText, value)
  572. Return Self
  573. End Method
  574. Rem
  575. bbdoc: Returns the calculated hash for the content of the string builder.
  576. End Rem
  577. Method Hash:ULong()
  578. Return bmx_stringbuilder_hash(buffer)
  579. End Method
  580. Rem
  581. bbdoc: Extracts the leftmost characters from the string builder.
  582. about: This method extracts the left @length characters from the builder. If this many characters are not available, the whole builder is returned.
  583. Thus the returned string may be shorter than the length requested.
  584. End Rem
  585. Method Left:String(length:Int)
  586. Return bmx_stringbuilder_left(buffer, length)
  587. End Method
  588. Rem
  589. bbdoc: Removes leading and trailing non-printable characters from the string builder.
  590. End Rem
  591. Method Trim:TStringBuilder()
  592. bmx_stringbuilder_trim(buffer)
  593. Return Self
  594. End Method
  595. Rem
  596. bbdoc: Replaces all occurances of @subString with @withString.
  597. End Rem
  598. Method Replace:TStringBuilder(subString:String, withString:String)
  599. bmx_stringbuilder_replace(buffer, subString, withString)
  600. Return Self
  601. End Method
  602. Rem
  603. bbdoc: Returns true if string starts with @subString.
  604. End Rem
  605. Method StartsWith:Int(subString:String)
  606. Return bmx_stringbuilder_startswith(buffer, subString)
  607. End Method
  608. Rem
  609. bbdoc: Returns true if string ends with @subString.
  610. End Rem
  611. Method EndsWith:Int(subString:String)
  612. Return bmx_stringbuilder_endswith(buffer, subString)
  613. End Method
  614. Rem
  615. bbdoc: Returns the char value in the buffer at the specified index.
  616. about: The first char value is at index 0, the next at index 1, and so on, as in array indexing.
  617. @index must be greater than or equal to 0, and less than the length of the buffer.
  618. End Rem
  619. Method CharAt:Int(index:Int)
  620. Return bmx_stringbuilder_charat(buffer, index)
  621. End Method
  622. Rem
  623. bbdoc: Returns the char value in the buffer at the specified index.
  624. about: The first char value is at index 0, the next at index 1, and so on, as in array indexing.
  625. @index must be greater than or equal to 0, and less than the length of the buffer.
  626. End Rem
  627. Method Operator[]:Int(index:Int)
  628. ?debug
  629. If index < 0 Or index >= Length() Throw New TArrayBoundsException
  630. ?
  631. Return bmx_stringbuilder_charat(buffer, index)
  632. End Method
  633. Rem
  634. bbdoc: Returns true if string contains @subString.
  635. End Rem
  636. Method Contains:Int(subString:String)
  637. Return Find(subString) >= 0
  638. End Method
  639. Rem
  640. bbdoc: Joins @bits together by inserting this string builder between each bit.
  641. returns: @buf or a new TStringBuilder object of @buf is #Null.
  642. about: Optionally accepts a preassigned string builder for populating with the result of the join.
  643. End Rem
  644. Method Join:TStringBuilder(bits:String[], buf:TStringBuilder = Null)
  645. If Not buf Then
  646. buf = New TStringBuilder
  647. End If
  648. bmx_stringbuilder_join(buffer, bits, buf.buffer)
  649. Return buf
  650. End Method
  651. Rem
  652. bbdoc: Joins @bits together by inserting @joiner between each bit, appending to the end of this string builder.
  653. End Rem
  654. Method JoinStrings:TStringBuilder(bits:String[], joiner:String)
  655. bmx_stringbuilder_join_strings(buffer, bits, joiner)
  656. Return Self
  657. End Method
  658. Rem
  659. bbdoc: Converts all of the characters in the buffer to lower case.
  660. End Rem
  661. Method ToLower:TStringBuilder()
  662. bmx_stringbuilder_tolower(buffer)
  663. Return Self
  664. End Method
  665. Rem
  666. bbdoc: Converts all of the characters in the buffer to upper case.
  667. End Rem
  668. Method ToUpper:TStringBuilder()
  669. bmx_stringbuilder_toupper(buffer)
  670. Return Self
  671. End Method
  672. Rem
  673. bbdoc: Left aligns the buffer, adjusted to the specified @length.
  674. about: If buffer is longer than the specified length, the buffer is shortened to the specified length.
  675. If the buffer is shorter than the specified length, spaces are added to the right end of the buffer to produce the appropriate length.
  676. End Rem
  677. Method LeftAlign:TStringBuilder(length:Int)
  678. bmx_stringbuilder_leftalign(buffer, length)
  679. Return Self
  680. End Method
  681. Rem
  682. bbdoc: Right aligns the buffer, adjusted to the specified @length.
  683. about: If buffer is longer than the specified length, the buffer is shortened to the specified length.
  684. If the buffer is shorter than the specified length, spaces are added to the left end of the buffer to produce the appropriate length.
  685. End Rem
  686. Method RightAlign:TStringBuilder(length:Int)
  687. bmx_stringbuilder_rightalign(buffer, length)
  688. Return Self
  689. End Method
  690. Rem
  691. bbdoc: Removes a range of characters from the string builder.
  692. about: @startIndex is the first character to remove. @endIndex is the index after the last character to remove.
  693. End Rem
  694. Method Remove:TStringBuilder(startIndex:Int, endIndex:Int)
  695. bmx_stringbuilder_remove(buffer, startIndex, endIndex)
  696. Return Self
  697. End Method
  698. Rem
  699. bbdoc: Removes the character at the specified position in the buffer.
  700. about: The buffer is shortened by one character.
  701. End Rem
  702. Method RemoveCharAt:TStringBuilder(index:Int)
  703. bmx_stringbuilder_removecharat(buffer, index)
  704. Return Self
  705. End Method
  706. Rem
  707. bbdoc: Inserts text into the string builder at the specified offset.
  708. End Rem
  709. Method Insert:TStringBuilder(offset:Int, value:String)
  710. bmx_stringbuilder_insert(buffer, offset, value)
  711. Return Self
  712. End Method
  713. Rem
  714. bbdoc: Reverses the characters of the string builder.
  715. End Rem
  716. Method Reverse:TStringBuilder()
  717. bmx_stringbuilder_reverse(buffer)
  718. Return Self
  719. End Method
  720. Rem
  721. bbdoc: Extracts the rightmost characters from the string builder.
  722. about: This method extracts the right @length characters from the builder. If this many characters are not available, the whole builder is returned.
  723. Thus the returned string may be shorter than the length requested.
  724. End Rem
  725. Method Right:String(length:Int)
  726. Return bmx_stringbuilder_right(buffer, length)
  727. End Method
  728. Rem
  729. bbdoc: The character at the specified index is set to @char.
  730. about: @index must be greater than or equal to 0, and less than the length of the buffer.
  731. End Rem
  732. Method SetCharAt(index:Int, char:Int)
  733. bmx_stringbuilder_setcharat(buffer, index, char)
  734. End Method
  735. Rem
  736. bbdoc: The character at the specified index is set to @char.
  737. about: @index must be greater than or equal to 0, and less than the length of the buffer.
  738. End Rem
  739. Method Operator []= (index:Int, char:Int)
  740. ?debug
  741. If index < 0 Or index >= Length() Throw New TArrayBoundsException
  742. ?
  743. bmx_stringbuilder_setcharat(buffer, index, char)
  744. End Method
  745. Rem
  746. bbdoc: Sets the text to be appended when a new line is added.
  747. End Rem
  748. Method SetNewLineText:TStringBuilder(newLine:String)
  749. Self.newLine = newLine
  750. Return Self
  751. End Method
  752. Rem
  753. bbdoc: Sets the text to be appended when null is added.
  754. End Rem
  755. Method SetNullText:TStringBuilder(nullText:String)
  756. Self.nullText = nullText
  757. Return Self
  758. End Method
  759. Rem
  760. bbdoc: Returns a substring of the string builder given the specified indexes.
  761. about: @beginIndex is the first character of the substring.
  762. @endIndex is the index after the last character of the substring. If @endIndex is zero,
  763. will return everything from @beginIndex until the end of the string builder.
  764. End Rem
  765. Method Substring:String(beginIndex:Int, endIndex:Int = 0)
  766. Return bmx_stringbuilder_substring(buffer, beginIndex, endIndex)
  767. End Method
  768. Rem
  769. bbdoc: Creates a split buffer using the specified separator.
  770. about: The #TSplitBuffer can be used to iterate over the split text.
  771. End Rem
  772. Method Split:TSplitBuffer(separator:String)
  773. Local buf:TSplitBuffer = New TSplitBuffer
  774. buf.buffer = Self
  775. buf.splitPtr = bmx_stringbuilder_split(buffer, separator)
  776. Return buf
  777. End Method
  778. Rem
  779. bbdoc: Converts the string builder to a String.
  780. End Rem
  781. Method ToString:String() Override
  782. Return bmx_stringbuilder_tostring(buffer)
  783. End Method
  784. Rem
  785. bbdoc: Converts the value of the string builder to a UTF-8 formatted #Byte sequence.
  786. returns: A pointer to a sequence of Bytes, or #Null if the string builder is empty.
  787. about: The resulting Byte Ptr must be freed with #MemFree.
  788. End Rem
  789. Method ToUTF8String:Byte Ptr()
  790. Return bmx_stringbuilder_toutf8string(buffer)
  791. End Method
  792. Rem
  793. bbdoc: Converts the value of the string builder to a sequence of Shorts.
  794. returns: A pointer to a sequence of Shorts, or #Null if the string builder is empty.
  795. about: The resulting Short Ptr must be freed with #MemFree.
  796. End Rem
  797. Method ToWString:Short Ptr()
  798. Return bmx_stringbuilder_towstring(buffer)
  799. End Method
  800. Rem
  801. bbdoc: Returns #True if @obj is equal to this string builder.
  802. End Rem
  803. Method Operator =:Int (obj:Object)
  804. Return Compare(obj) = 0
  805. End Method
  806. Rem
  807. bbdoc: Returns #True if @sb is lexicographically equal to this string builder.
  808. End Rem
  809. Method Operator =:Int (sb:TStringBuilder)
  810. Return bmx_stringbuilder_equals(buffer, sb.buffer)
  811. End Method
  812. Rem
  813. bbdoc: Returns #True if @obj is not equal to this string builder.
  814. End Rem
  815. Method Operator <>:Int (obj:Object)
  816. Return Compare(obj) <> 0
  817. End Method
  818. Rem
  819. bbdoc: Returns #True if @sb is not lexicographically equal to this string builder.
  820. End Rem
  821. Method Operator <>:Int (sb:TStringBuilder)
  822. Return Not bmx_stringbuilder_equals(buffer, sb.buffer)
  823. End Method
  824. Method Delete()
  825. If buffer Then
  826. bmx_stringbuilder_free(buffer)
  827. buffer = Null
  828. End If
  829. End Method
  830. End Type
  831. Rem
  832. bbdoc: An array of split text from a TStringBuilder.
  833. about: Note that the #TSplitBuffer is only valid while its parent #TStringBuilder is unchanged.
  834. Once you modify the #TSplitBuffer you should call Split() again.
  835. End Rem
  836. Type TSplitBuffer
  837. Private
  838. Field buffer:TStringBuilder
  839. Field splitPtr:Byte Ptr
  840. Public
  841. Rem
  842. bbdoc: The number of split elements.
  843. End Rem
  844. Method Length:Int()
  845. Return bmx_stringbuilder_splitbuffer_length(splitPtr)
  846. End Method
  847. Rem
  848. bbdoc: Returns the text for the given index in the split buffer.
  849. End Rem
  850. Method Text:String(index:Int)
  851. Return bmx_stringbuilder_splitbuffer_text(splitPtr, index)
  852. End Method
  853. Rem
  854. bbdoc: Creates a new string array of all the split elements.
  855. End Rem
  856. Method ToArray:String[]()
  857. Return bmx_stringbuilder_splitbuffer_toarray(splitPtr)
  858. End Method
  859. Method ObjectEnumerator:TSplitBufferEnum()
  860. Local enumeration:TSplitBufferEnum = New TSplitBufferEnum
  861. enumeration.buffer = Self
  862. enumeration.length = Length()
  863. Return enumeration
  864. End Method
  865. Method Delete()
  866. If splitPtr Then
  867. buffer = Null
  868. bmx_stringbuilder_splitbuffer_free(splitPtr)
  869. splitPtr = Null
  870. End If
  871. End Method
  872. End Type
  873. Type TSplitBufferEnum
  874. Field index:Int
  875. Field length:Int
  876. Field buffer:TSplitBuffer
  877. Method HasNext:Int()
  878. Return index < length
  879. End Method
  880. Method NextObject:Object()
  881. Local s:String = buffer.Text(index)
  882. index :+ 1
  883. Return s
  884. End Method
  885. End Type