-
Notifications
You must be signed in to change notification settings - Fork 13
/
stringbuffer_core.bmx
370 lines (321 loc) · 10.1 KB
/
stringbuffer_core.bmx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
' Copyright (c) 2016-2017 Bruce A Henderson
'
' Permission is hereby granted, free of charge, to any person obtaining a copy
' of this software and associated documentation files (the "Software"), to deal
' in the Software without restriction, including without limitation the rights
' to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
' copies of the Software, and to permit persons to whom the Software is
' furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in
' all copies or substantial portions of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
' OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
' THE SOFTWARE.
'
SuperStrict
Rem
bbdoc: A string buffer.
End Rem
'Module BaH.StringBuffer
'ModuleInfo "Version: 1.03"
'ModuleInfo "License: MIT"
'ModuleInfo "Copyright: 2016 Bruce A Henderson"
'ModuleInfo "History: 1.03"
'ModuleInfo "History: Added overloaded constructor for providing instance specific initial capacity."
'ModuleInfo "History: 1.02"
'ModuleInfo "History: Added AppendCString() and AppendUTF8String() methods."
'ModuleInfo "History: 1.01"
'ModuleInfo "History: Added CharAt(), SetCharAt() and RemoveCharAt() methods."
'ModuleInfo "History: 1.00 Initial Release"
Import "stringbuffer_common.bmx"
Rem
bbdoc: A modifiable String.
about: A string buffer provides functionality to efficiently insert, replace, remove, append and reverse.
It is an order of magnitude faster to append Strings to a TStringBuffer than it is to append Strings to Strings.
End Rem
Type TStringBuffer
' the char buffer
Field buffer:Byte Ptr
Global initialCapacity:Int = 16
Method New()
buffer = bmx_stringbuffer_new(initialCapacity)
End Method
?bmxng
Method New(initialCapacity:Int)
buffer = bmx_stringbuffer_new(initialCapacity)
End Method
?
Rem
bbdoc: Constructs a string buffer initialized to the contents of the specified string.
End Rem
Function Create:TStringBuffer(Text:String)
?Not bmxng
Local this:TStringBuffer = New TStringBuffer
?bmxng
Local this:TStringBuffer
If text.length > initialCapacity Then
this = New TStringBuffer(text.length)
Else
this = New TStringBuffer
End If
?
Return this.Append(Text)
End Function
Rem
bbdoc: Returns the length of the string the string buffer would create.
End Rem
Method Length:Int()
Return bmx_stringbuffer_count(buffer)
End Method
Rem
bbdoc: Returns the total number of characters that the string buffer can accommodate before needing to grow.
End Rem
Method Capacity:Int()
Return bmx_stringbuffer_capacity(buffer)
End Method
Rem
bbdoc: Sets the length of the string buffer.
about: If the length is less than the current length, the current text will be truncated. Otherwise,
the capacity will be increased as necessary, although the actual length of text will remain the same.
End Rem
Method SetLength(length:Int)
bmx_stringbuffer_setlength(buffer, length)
End Method
Rem
bbdoc: Appends the text onto the string buffer.
End Rem
Method Append:TStringBuffer(value:String)
bmx_stringbuffer_append_string(buffer, value)
Return Self
End Method
Rem
bbdoc: Appends an object onto the string buffer.
about: This generally calls the object's ToString() method.
TStringBuffer objects are simply mem-copied.
End Rem
Method AppendObject:TStringBuffer(obj:Object)
If TStringBuffer(obj) Then
bmx_stringbuffer_append_stringbuffer(buffer, TStringBuffer(obj).buffer)
Else
bmx_stringbuffer_append_string(buffer, obj.ToString())
End If
Return Self
End Method
Rem
bbdoc: Appends a null-terminated C string onto the string buffer.
End Rem
Method AppendCString:TStringBuffer(chars:Byte Ptr)
bmx_stringbuffer_append_cstring(buffer, chars)
Return Self
End Method
Rem
bbdoc: Appends a null-terminated UTF-8 string onto the string buffer.
End Rem
Method AppendUTF8String:TStringBuffer(chars:Byte Ptr)
bmx_stringbuffer_append_utf8string(buffer, chars)
Return Self
End Method
Rem
bbdoc: Finds first occurance of a sub string.
returns: -1 if @subString not found.
End Rem
Method Find:Int(subString:String, startIndex:Int = 0)
Return bmx_stringbuffer_find(buffer, subString, startIndex)
End Method
Rem
bbdoc: Finds last occurance of a sub string.
returns: -1 if @subString not found.
End Rem
Method FindLast:Int(subString:String, startIndex:Int = 0)
Return bmx_stringbuffer_findlast(buffer, subString, startIndex)
End Method
Rem
bbdoc: Removes leading and trailing non-printable characters from the string buffer.
End Rem
Method Trim:TStringBuffer()
bmx_stringbuffer_trim(buffer)
Return Self
End Method
Rem
bbdoc: Replaces all occurances of @subString with @withString.
End Rem
Method Replace:TStringBuffer(subString:String, withString:String)
bmx_stringbuffer_replace(buffer, subString, withString)
Return Self
End Method
Rem
bbdoc: Returns true if string starts with @subString.
End Rem
Method StartsWith:Int(subString:String)
Return bmx_stringbuffer_startswith(buffer, subString)
End Method
Rem
bbdoc: Returns true if string ends with @subString.
End Rem
Method EndsWith:Int(subString:String)
Return bmx_stringbuffer_endswith(buffer, subString)
End Method
Rem
bbdoc: Returns the char value in the buffer at the specified index.
about: The first char value is at index 0, the next at index 1, and so on, as in array indexing.
@index must be greater than or equal to 0, and less than the length of the buffer.
End Rem
Method CharAt:Int(index:Int)
Return bmx_stringbuffer_charat(buffer, index)
End Method
Rem
bbdoc: Returns true if string contains @subString.
End Rem
Method Contains:Int(subString:String)
Return Find(subString) >= 0
End Method
Rem
bbdoc: Joins @bits together by inserting this string buffer between each bit.
returns: A new TStringBuffer object.
End Rem
Method Join:TStringBuffer(bits:String[])
Local buf:TStringBuffer = New TStringBuffer
bmx_stringbuffer_join(buffer, bits, buf.buffer)
Return buf
End Method
Rem
bbdoc: Converts all of the characters in the buffer to lower case.
End Rem
Method ToLower:TStringBuffer()
bmx_stringbuffer_tolower(buffer)
Return Self
End Method
Rem
bbdoc: Converts all of the characters in the buffer to upper case.
End Rem
Method ToUpper:TStringBuffer()
bmx_stringbuffer_toupper(buffer)
Return Self
End Method
Rem
bbdoc: Removes a range of characters from the string buffer.
about: @startIndex is the first character to remove. @endIndex is the index after the last character to remove.
End Rem
Method Remove:TStringBuffer(startIndex:Int, endIndex:Int)
bmx_stringbuffer_remove(buffer, startIndex, endIndex)
Return Self
End Method
Rem
bbdoc: Removes the character at the specified position in the buffer.
about: The buffer is shortened by one character.
End Rem
Method RemoveCharAt:TStringBuffer(index:Int)
bmx_stringbuffer_removecharat(buffer, index)
Return Self
End Method
Rem
bbdoc: Inserts text into the string buffer at the specified offset.
End Rem
Method Insert:TStringBuffer(offset:Int, value:String)
bmx_stringbuffer_insert(buffer, offset, value)
Return Self
End Method
Rem
bbdoc: Reverses the characters of the string buffer.
End Rem
Method Reverse:TStringBuffer()
bmx_stringbuffer_reverse(buffer)
Return Self
End Method
Rem
bbdoc: The character at the specified index is set to @char.
about: @index must be greater than or equal to 0, and less than the length of the buffer.
End Rem
Method SetCharAt(index:Int, char:Int)
bmx_stringbuffer_setcharat(buffer, index, char)
End Method
Rem
bbdoc: Returns a substring of the string buffer given the specified indexes.
about: @beginIndex is the first character of the substring.
@endIndex is the index after the last character of the substring. If @endIndex is zero,
will return everything from @beginIndex until the end of the string buffer.
End Rem
Method Substring:String(beginIndex:Int, endIndex:Int = 0)
Return bmx_stringbuffer_substring(buffer, beginIndex, endIndex)
End Method
Rem
bbdoc:
End Rem
Method Split:TSplitBuffer(separator:String)
Local buf:TSplitBuffer = New TSplitBuffer
buf.buffer = Self
buf.splitPtr = bmx_stringbuffer_split(buffer, separator)
Return buf
End Method
Rem
bbdoc: Converts the string buffer to a String.
End Rem
Method ToString:String()
Return bmx_stringbuffer_tostring(buffer)
End Method
Method Delete()
If buffer Then
bmx_stringbuffer_free(buffer)
buffer = Null
End If
End Method
End Type
Rem
bbdoc: An array of split text from a TStringBuffer.
about: Note that the TSplitBuffer is only valid while its parent TStringBuffer is unchanged.
Once you modify the TStringBuffer you should call Split() again.
End Rem
Type TSplitBuffer
Field buffer:TStringBuffer
Field splitPtr:Byte Ptr
Rem
bbdoc: The number of split elements.
End Rem
Method Length:Int()
Return bmx_stringbuffer_splitbuffer_length(splitPtr)
End Method
Rem
bbdoc: Returns the text for the given index in the split buffer.
End Rem
Method Text:String(index:Int)
Return bmx_stringbuffer_splitbuffer_text(splitPtr, index)
End Method
Rem
bbdoc: Creates a new string array of all the split elements.
End Rem
Method ToArray:String[]()
Return bmx_stringbuffer_splitbuffer_toarray(splitPtr)
End Method
Method ObjectEnumerator:TSplitBufferEnum()
Local enumerator:TSplitBufferEnum = New TSplitBufferEnum
enumerator.buffer = Self
enumerator.length = Length()
Return enumerator
End Method
Method Delete()
If splitPtr Then
buffer = Null
bmx_stringbuffer_splitbuffer_free(splitPtr)
splitPtr = Null
End If
End Method
End Type
Type TSplitBufferEnum
Field index:Int
Field length:Int
Field buffer:TSplitBuffer
Method HasNext:Int()
Return index < length
End Method
Method NextObject:Object()
Local s:String = buffer.Text(index)
index :+ 1
Return s
End Method
End Type