-
Notifications
You must be signed in to change notification settings - Fork 36
/
#Filter.fmfn
92 lines (87 loc) · 2.67 KB
/
#Filter.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
/**
* =====================================
* #Filter ( parameters ; filterParameters )
*
* RETURNS:
* A list of Let notation name-value pairs from parameters with names in
* filterParameters. Any pairs with names not in filterParameters will not
* be included.
*
* PARAMETERS:
* parameters: A string of serialized name-value pair data in Let notation.
* filterParameters: A return-delimited list of parameter names to include
* in the result.
*
* EXAMPLE:
* #Assign ( #Filter (
* # ( "name" ; "value" )
* & # ( "foo" ; "bar" );
* List ( "name" ; "otherName" )
* ) )
* // variable $name assigned "value"; $foo and $otherName are unaffected
*
* DEPENDENCIES: none
*
* HISTORY:
* MODIFIED on 2013-12-24 by Jeremy Bante <http://scr.im/jbante> to improve
* efficiency.
* MODIFIED on 2013-12-23 by John Jones <john.christopher@alumni.virginia.edu>
* to use Position/Middle. Modified to add trailing return if it doesn't
* exist.
* CREATED on 2012-11-28 by Jeremy Bante <http://scr.im/jbante>.
* =====================================
*/
Case (
/* Step 0, set-up */
not $#Filter.step ;
Let ( [
~empty = IsEmpty ( parameters ) or IsEmpty ( filterParameters ) ;
filterParameters = // add "$" prefix for matching
Substitute ( ¶ & filterParameters ; ¶ ; "¶$" ) ;
filterParameters = // add "$$" prefix for backwards-compatibility
filterParameters
& Substitute ( filterParameters ; ¶ ; "¶$" )
& ¶ ;
$#Filter.length = Length ( parameters ) ;
$#Filter.step = If ( ~empty ; 2 ; /* Else */ 1 )
] ;
#Filter ( parameters ; filterParameters )
) ;
/* Step 1, check each parameter */
$#Filter.step = 1 ;
Let ( [
~start = $#Filter.end + 1 ;
$#Filter.end = Position ( parameters ; ¶ ; ~start ; 1 ) ;
~endOfList = $#Filter.end = 0 ;
~length =
If ( ~endOfList ;
$#Filter.length + 1 ;
/* Else */ $#Filter.end
)
- ~start ;
~pair = Middle ( parameters ; ~start ; ~length ) ;
~name = Left ( ~pair ; Position ( ~pair ; " = " ; 1 ; 1 ) - 1 ) ;
~include = // only include ~pair if ~name is in filterParameters
Position ( filterParameters ; ¶ & ~name & ¶ ; 1 ; 1 ) > 0 ;
$#Filter.result =
$#Filter.result
& If ( ~include ; ~pair & ¶ ) ;
~endOfList = ~endOfList or $#Filter.end ≥ $#Filter.length ;
$#Filter.step =
If ( ~endOfList ; $#Filter.step + 1 ; /* Else */ $#Filter.step )
] ;
#Filter ( parameters ; filterParameters )
) ;
/* Step 2, clean-up and return result */
$#Filter.step = 2 ;
Let ( [
~result = $#Filter.result ;
// purge variables
$#Filter.end = "" ;
$#Filter.length = "" ;
$#Filter.result = "" ;
$#Filter.step = ""
] ;
~result
)
)