stringbuilder.bmx 32 KB

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