/
string_array.bi
102 lines (100 loc) · 4.54 KB
/
string_array.bi
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
'--------------------------------------------------------------------------------
'STRING ARRAY Copyright 2012-2015 Dustinian Camburides
'--------------------------------------------------------------------------------
'SUMMARY
'Purpose: A library of custom subroutines to create and manipulate a dnynamic array of strings.
'Author: Dustinian Camburides (dustinian@dustinian.com)
'Platform: FreeBASIC (www.freebasic.net)
'Revision: 1.1
'Updated: 4/15/2013
'License: GNU GPL3
'--------------------------------------------------------------------------------
'REVISION HISTORY
'1.1: Migrated from QB64 (www.qb64.net) to FreeBASIC (www.freebasic.net).
'1.0: First working version.
'--------------------------------------------------------------------------------
'PLANNED ENHNACEMENTS
'Major:
'None at this time.
'Minor:
'None at this time.
'--------------------------------------------------------------------------------
'LICENSE (GNU GPL3)
'This file is part of Text Transformation Language.
'Text Transformation Language is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
'Text Transformation Language is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
'You should have received a copy of the GNU General Public License along with Text Transformation Language. If not, see <http://www.gnu.org/licenses/>.
'--------------------------------------------------------------------------------
'PROCEDURES
Declare Sub Separate_Lines (Words() As String, ByVal Text As String, ByVal Separator As String)
Declare Sub Remove_Word(Words() As String, ByVal Index As Integer)
Declare Sub Remove_Blank_Words (Words() As String)
'--------------------------------------------------------------------------------
Sub Remove_Word(Words() As String, ByVal Index As Integer)
'SUMMARY:
'[Remove_Word] removes a word from a dynamic array of strings.
'INPUT:
'Words(): The dynamic array, where separate elements are stored.
'Index: The element to be removed from the dynamic array.
'VARIABLES:
Dim intWord As Integer 'The [Word] in the [Words] array.
'PROCESSING:
'For each [Word] after [Index]...
For intWord = Index to UBound(Words) - 1
'Replace the word with the subsequent word:
Words(intWord) = Words(intWord + 1)
'Next [Word]...
Next intWord
'ReDimension the [Words] array:
ReDim Preserve Words(UBound(Words) - 1) As String
End Sub
Sub Separate_Lines (Words() As String, ByVal Text As String, ByVal Separator As String)
'SUMMARY:
'[Separate_Lines] extracts individual sub-strings from a string.
'INPUT:
'Words(): The output array, where separate [sub-strings] are stored.
'Text: The input string being filtered.
'Separator: The character(s) that separates [sub-strings].
'VARIABLES:
Dim lngLocation As Long 'The location of the [Separator] within the [Text].
Dim lngStart As Long 'The starting point For the Next search For a [Separator] within the [Text].
Dim intLines As Integer 'The total number of lines in the dynamic array.
'INITIALIZE:
Text = Text + Separator
intLines = 0
lngStart = 1
lngLocation = INSTR(lngStart, Text, Separator)
'PROCESSING:
'While the [Separator] appears in the [Text]...
While (lngLocation > 0)
'Increment the number of lines:
intLines = intLines + 1
'Re-size the dynamic array:
ReDim Preserve Words(intLines) As String
'Extract the [sub-string] from the [Text]:
Words(intLines) = Mid$(Text, lngStart, (lngLocation - lngStart))
'Move the [Start] pointer beyond the end of the [sub-string]:
lngStart = lngLocation + 1
'Locate the Next [Separator] within the [Text]:
lngLocation = InStr(lngStart, Text, Separator)
'Next instance of [Separator]...
Wend
End Sub
Sub Remove_Blank_Words (Words() As String)
'SUMMARY:
'[Remove_Blank_Words] removes blank elements from a dynamic array of strings.
'INPUT/OUTPUT:
'Words(): The dynamic array, where separate elements are stored.
'VARIABLES:
Dim intElement As Integer 'The element within the array.
'PROCESSING:
'For each [Element] in the [Array]...
For intElement = UBound(Words) To LBound(Words) Step -1
'If the word is blank...
If Words(intElement) = "" Then
'Remove the word:
Remove_Word(Words(), intElement)
End If
'Next [Element]...
Next intElement
End Sub