-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtext.explode.fmfn
99 lines (75 loc) · 3.74 KB
/
text.explode.fmfn
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
/*
Purpose: Splits a string using the delimiter into a list.
Returns: List
Name: text.explode ( theDelimiter; theText )
Parameters: ( theDelimiter ) text
( theText ) text
Dependencies: NONE
References: Modeled after PHP's explode: http://us.php.net/manual/en/function.explode.php
NOTE: If the provided delimiter is a ¶ the function will return the original text. Also,
it will strip any existing ¶ as they would be ambiguous.
WARNING: text.explode breaks when using COL + NUL (it strips out NULs)
2016-05-01, JPS, Fixed bug with trailing delimiter breaking the function.
2014-08-12, JPS, Returns are now encode as \¶ instead of being replaced with "".
2013-01-23, JPS, Fixed a MAJOR bug that caused the function to break when the delimiterCount = 0 or when the delimiterLength > 1.
2012-12-11, JPS, Fixed a MAJOR bug that caused the function to break if theText ended with theDelimiter, and there was only one delimiter.
2012-12-04, JPS, Fixed a MAJOR bug that caused the function to break if theText ended with theDelimiter
2012-11-13, JPS, Changed the global variable ($$FUNCTION.EXPLODE.TEXT.RESULTS), to local variables ($FUNCTION.EXPLODE.TEXT.RESULTS).
2013-01-23, JPS, Fixed a bug that caused the function to return an empty result when theDelimiter was a return.
2012-09-21, JPS, Re-wrote to use a global variable to increase the recursion limit.
2012-09-12, JPS, Created.
*/
Let (
[
//Get the delimiter length
delimiterLength = Length ( theDelimiter );
// Remove leading and trailing delimiter
theText = If (
Left ( theText; delimiterLength ) = theDelimiter;
Right ( theText; length(theText) - delimiterLength );
/* ELSE */
theText
);
theText = If (
Right ( theText; delimiterLength ) = theDelimiter;
Left ( theText; length(theText) - delimiterLength);
/* ELSE */
theText
);
//Get the length of the text
textLength = Length ( theText );
//Check for the delimiter
delimiterCount = PatternCount ( theText ; theDelimiter );
//Clean up theText
theText = If ( theDelimiter <> ¶; Substitute ( theText; ¶; "\¶" ); theText );
//Get the location of the first delimiter
delimiterPosition = Position ( theText ; theDelimiter ; 1 ; 1 );
//Get the first value and trim it
firstValue = Trim ( Left ( theText ; delimiterPosition - 1 ));
//Save the current value
$FUNCTION.EXPLODE.TEXT.RESULTS = List ( $FUNCTION.EXPLODE.TEXT.RESULTS; firstValue );
//Get the remaining text
remainingText = Trim ( Right ( theText; textLength - ( delimiterPosition + delimiterLength - 1 )));
$$DEBUG.text.explode.remainingText = ""
];
Case (
//If theText is empty return nothing
IsEmpty ( theText ) and ValueCount ( $FUNCTION.EXPLODE.TEXT.RESULTS ) = 0;
"";
//If theDelimiter is empty return the original string
( IsEmpty ( theDelimiter ) or theDelimiter = ¶ or delimiterCount = 0 ) and ValueCount ( $FUNCTION.EXPLODE.TEXT.RESULTS ) = 0;
theText;
//If there is no delimiter return the results
delimiterCount <= 1 or delimiterPosition = textLength;
Let (
[
theList = $FUNCTION.EXPLODE.TEXT.RESULTS;
//Clear the local variable
$FUNCTION.EXPLODE.TEXT.RESULTS = ""
];
List ( theList; remainingText )
);
//Continue parsing
text.explode ( theDelimiter; remainingText )
)
)