stringbuilder.bmx 34 KB

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