-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtext.split.fmfn
76 lines (56 loc) · 2.65 KB
/
text.split.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
/*
Purpose: Converts a string into a list, using the provided length. To split in reverse use a negative theLength.
Returns: List
Name: text.split ( theText; theLength )
Parameters:
( theText ) text
( theLength ) text [defaults to one] Use a negative number to split from the right.
Dependencies: NONE
References: Modeled after PHP's str_split: http://www.php.net/manual/en/function.str-split.php
2012-10-01, JPS, Created.
2012-12-13, JPS, Changed the global variable ($$FUNCTION.SPLIT.TEXT.RESULTS), to local variables ($FUNCTION.EXPLODE.TEXT.RESULTS).
2012-12-13, JPS, Added the ability to split in reverse using a negative theLength.
*/
Let (
[
//Get the absolute length
absoluteLength = Abs ( Int ( theLength ));
// Check theLength
theLength = If ( absoluteLength > 0; Int ( theLength ); 1 );
//Get the length of the text
textLength = Length ( theText );
//Get the first value and trim it
firstValue = Case (
textLength >= absoluteLength and theLength > 0;
Trim ( Left ( theText ; absoluteLength ));
textLength >= absoluteLength and theLength < 0;
Trim ( Right ( theText; absoluteLength ));
textLength < absoluteLength;
Trim ( theText )
);
//Save the current value
$FUNCTION.SPLIT.TEXT.RESULTS = If ( theLength > 0; List ( $FUNCTION.SPLIT.TEXT.RESULTS; firstValue ); List ( firstValue; $FUNCTION.SPLIT.TEXT.RESULTS ));
//Get the remaining text
remainingText = If ( theLength > 0; Trim ( Right ( theText; textLength - absoluteLength )); Trim ( Left ( theText; textLength - absoluteLength )))
];
Case (
//If theText is empty return nothing
IsEmpty ( theText ) and IsEmpty ( $FUNCTION.SPLIT.TEXT.RESULTS );
"";
//If the text is smaller than theLength return the text
textLength < absoluteLength and IsEmpty ( $FUNCTION.SPLIT.TEXT.RESULTS );
theText;
//If the remaining text is smaller than the length, return the results
textLength <= absoluteLength;
Let (
[
theList = $FUNCTION.SPLIT.TEXT.RESULTS;
//Clear the global
$FUNCTION.SPLIT.TEXT.RESULTS = ""
];
List ( theList; remainingText )
);
//Continue parsing
text.split ( remainingText; theLength )
)
)