-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComparer.ms
More file actions
175 lines (160 loc) · 4.62 KB
/
Comparer.ms
File metadata and controls
175 lines (160 loc) · 4.62 KB
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
/*! © 2022 imaoki | MIT License | https://github.com/imaoki */
/*-
2つのオブジェクトが等しいかどうかを比較する。
@remarks 戻り値が以下のような関係になるように実装する。
| 値 | 関係 |
| ---- | ------------------ |
| `-1` | `a`は`b`より小さい |
| `0` | `a`と`b`は等しい |
| `1` | `a`は`b`より大きい |
*/
struct ComparerStruct (
/*- @prop <DotNetMethod> */
private stringLogicalComparer,
/*
public fn CompareName a b = (),
public fn CompareNodeHandle a b = (),
public fn CompareProperty a b propName: = (),
public fn CompareStringLogical a b = (),
private fn simpleCompare a b = (),
*/
/*-
`Name`プロパティを比較する。
@param a <Any>
@param b <Any>
@returns <Integer>
@remarks 値が文字列の場合は`CompareStringLogical`を利用する。
*/
public fn CompareName a b = (
if classOf a.Name == String and classOf b.Name == String then (
this.CompareStringLogical a.Name b.Name
)
else (
this.simpleCompare a.Name b.Name
)
),
/*-
ノードハンドルを比較する。
@param a <Node>
@param b <Node>
@returns <Integer>
*/
public fn CompareNodeHandle a b = (
this.simpleCompare a.INode.Handle b.INode.Handle
),
/*-
任意のプロパティを比較する。
@param a <Any>
@param b <Any>
@param propName: <Name|String> プロパティ名。
@returns <Integer>
@remarks 値が文字列の場合は`CompareStringLogical`を利用する。
*/
public fn CompareProperty a b propName: = (
local aValue = getProperty a propName
local bValue = getProperty b propName
if classOf aValue == String and classOf bValue == String then (
this.CompareStringLogical aValue bValue
)
else (
this.simpleCompare aValue bValue
)
),
/*-
文字列中の数字を数値として比較する。
@param a <String>
@param b <String>
@returns <Integer>
@remarks
例
: ```maxscript
(
local strings = #(
"20string",
"2string",
"3string",
"st20ring",
"st2ring",
"st3ring",
"string2",
"string20",
"string3"
)
qsort strings (::ComparerStruct()).CompareStringLogical
for str in strings do format "-- \"%\"\n" str
ok
)
-- 実行結果
-- "2string"
-- "3string"
-- "20string"
-- "st2ring"
-- "st3ring"
-- "st20ring"
-- "string2"
-- "string3"
-- "string20"
```
*/
public fn CompareStringLogical a b = (
this.stringLogicalComparer a b
),
/*-
@param a <Any>
@param b <Any>
@returns <Integer>
*/
private fn simpleCompare a b = (
case of (
(a < b): -1
(a > b): 1
default: 0
)
),
/*- @returns <Name> */
public fn StructName = #ComparerStruct,
/*-
@param indent: <String>
@param out: <FileStream|StringStream|WindowStream> 出力先。既定値は`listener`。
@returns <OkClass>
*/
public fn Dump indent:"" out:listener = (
format "%ComparerStruct\n" indent to:out
ok
),
/*-
@param obj <Any>
@returns <BooleanClass>
@remarks 大文字と小文字を区別する。
*/
public fn Equals obj = (
local isEqualStructName = isStruct obj \
and isProperty obj #StructName \
and classOf obj.StructName == MAXScriptFunction \
and obj.StructName() == this.StructName()
local isEqualProperties = true
isEqualStructName and isEqualProperties
),
on Create do (
this.stringLogicalComparer = (
local code = StringStream ""
format "using System;\n" to:code
format "using System.Collections;\n" to:code
format "using System.Runtime.InteropServices;\n" to:code
format "public sealed class StringLogicalComparer : IComparer {\n" to:code
format " [DllImport(\"shlwapi.dll\", CharSet = CharSet.Unicode, ExactSpelling = true)]\n" to:code
format " static extern int StrCmpLogicalW(String a, String b);\n" to:code
format " public int Compare(object a, object b) {\n" to:code
format " return StrCmpLogicalW((String)a, (String)b);\n" to:code
format " }\n" to:code
format "}" to:code
code = code as String
local provider = DotNetObject "Microsoft.CSharp.CSharpCodeProvider"
local params = DotNetObject "System.CodeDom.Compiler.CompilerParameters"
params.GenerateInMemory = true
local compilerResults = provider.CompileAssemblyFromSource params #(code)
local assembly = compilerResults.CompiledAssembly
(assembly.CreateInstance "StringLogicalComparer").Compare
)
)
)