5 #include "motoroutput.h"
6 #include "util_const.h"
7 #include "util_random.h"
8 #include "util_string.h"
14 #include <boost/ptr_container/ptr_vector.hpp>
24 (concat STRING...) -> STRING
25 パラメータの文字列を連結して一つの文字列を返す。
28 //#AFUNC concat ml_concat
30 MNode* ml_concat (MNode* cell, MlEnv* mlenv) {
31 MNode* arg = cell->cdr ();
32 AutoDelete<ustring> a1;
38 a1 ()->append (eval_str (arg->car (), mlenv));
41 return newMNode_str (a1.release ());
46 (megabyte NUMBER) -> STRING
49 //#AFUNC megabyte ml_megabyte
51 MNode* ml_megabyte (MNode* cell, MlEnv* mlenv) {
52 MNode* arg = cell->cdr ();
57 throw (uErrorWrongNumber);
59 val = eval_double (arg->car (), mlenv);
62 throw (uErrorWrongNumber);
66 return newMNode_str (new ustring (u));
68 val = floor (val / 1024. * 10.) / 10.;
71 u.append (CharConst ("K"));
72 return newMNode_str (new ustring (u));
74 val = floor (val / 1024. * 10. ) / 10.;
77 u.append (CharConst ("M"));
78 return newMNode_str (new ustring (u));
80 val = floor (val / 1024. * 10.) / 10.;
83 u.append (CharConst ("G"));
84 return newMNode_str (new ustring (u));
86 val = floor (val / 1024. * 10.) / 10.;
89 u.append (CharConst ("T"));
90 return newMNode_str (new ustring (u));
92 val = floor (val / 1024. * 10.) / 10.;
94 u.append (CharConst ("P"));
95 return newMNode_str (new ustring (u));
100 (c3 INTEGER) -> STRING
105 MNode* ml_c3 (MNode* cell, MlEnv* mlenv) {
106 MNode* arg = cell->cdr ();
110 throw (uErrorWrongNumber);
112 u = eval_str (arg->car (), mlenv);
115 throw (uErrorWrongNumber);
117 return newMNode_str (new ustring (c3 (u)));
122 (regexp-match REGEX TEXT [#i | :i BOOL]) -> BOOL
125 //#AFUNC regexp-match ml_regexp_match
126 //#WIKIFUNC regexp-match
127 MNode* ml_regexp_match (MNode* cell, MlEnv* mlenv) {
128 MNode* arg = cell->cdr ();
131 boost::wregex::flag_type f = boost::regex_constants::normal;
133 std::vector<MNode*> params;
134 std::vector<MNode*> keywords;
135 static paramList kwlist[] = {
136 {CharConst ("i"), true},
140 setParams (arg, 2, ¶ms, kwlist, &keywords, NULL);
141 reg = eval_str (params[0], mlenv);
142 t = eval_str (params[1], mlenv);
143 if (eval_bool (keywords[0], mlenv))
144 f |= boost::regex_constants::icase;
147 mlenv->env->regtext = utow (t);
148 std::wstring wreg = utow (reg);
149 boost::wregex wre (wreg, f);
150 ans = regex_search (mlenv->env->regtext, mlenv->env->regmatch, wre, boost::regex_constants::match_single_line);
152 ans = wsearch_env (mlenv, t, mlenv->env->regmatch, reg, f);
154 return newMNode_bool (ans);
159 (match-string NUM) -> STRING
162 //#AFUNC match-string ml_match_string
163 //#WIKIFUNC match-string
164 MNode* ml_match_string (MNode* cell, MlEnv* mlenv) {
165 MNode* arg = cell->cdr ();
170 throw (uErrorWrongNumber);
171 n = eval_int (arg->car (), mlenv);
174 throw (uErrorWrongNumber);
176 if (0 <= n && n < mlenv->env->regmatch.size ()) {
177 ans = newMNode_str (new ustring (wtou (std::wstring (mlenv->env->regmatch[n].first, mlenv->env->regmatch[n].second))));
188 //#AFUNC prematch ml_prematch
190 MNode* ml_prematch (MNode* cell, MlEnv* mlenv) {
191 MNode* arg = cell->cdr ();
193 std::wstring::const_iterator b = mlenv->env->regtext.begin ();
196 throw (uErrorWrongNumber);
198 ans = newMNode_str (new ustring (wtou (std::wstring (b, mlenv->env->regmatch[0].first))));
205 (postmatch) -> STRING
208 //#AFUNC postmatch ml_postmatch
209 //#WIKIFUNC postmatch
210 MNode* ml_postmatch (MNode* cell, MlEnv* mlenv) {
211 MNode* arg = cell->cdr ();
213 std::wstring::const_iterator e = mlenv->env->regtext.end ();
216 throw (uErrorWrongNumber);
218 ans = newMNode_str (new ustring (wtou (std::wstring (mlenv->env->regmatch[0].second, e))));
225 (string-filter REGEX STRING [#i | :i BOOL] [:max NUM]) -> STRING
228 //#AFUNC string-filter ml_string_filter
229 //#WIKIFUNC string-filter
230 MNode* ml_string_filter (MNode* cell, MlEnv* mlenv) {
231 MNode* arg = cell->cdr ();
234 boost::wregex::flag_type f = boost::regex_constants::normal;
236 std::vector<MNode*> params;
237 std::vector<MNode*> keywords;
238 static paramList kwlist[] = {
239 {CharConst ("i"), true},
240 {CharConst ("max"), false},
244 setParams (arg, 2, ¶ms, kwlist, &keywords, NULL);
245 reg = eval_str (params[0], mlenv);
246 t = eval_str (params[1], mlenv);
247 if (eval_bool (keywords[0], mlenv))
248 f |= boost::regex_constants::icase;
249 if (eval_bool (keywords[1], mlenv)) {
250 max = eval_int (keywords[1], mlenv);
256 mlenv->env->regtext = utow (t);
257 std::wstring wreg = utow (reg);
258 boost::wregex wre (wreg, f);
259 // if (regex_search (mlenv->env->regtext, mlenv->env->regmatch, wre, boost::regex_constants::match_single_line)) {
261 if (wsearch_env (mlenv, t, mlenv->env->regmatch, reg, f)) {
262 // return newMNode_str (new ustring (wtou (std::wstring (mlenv->env->regmatch[0].first, mlenv->env->regmatch[0].second))));
263 ustring ans = wtou (std::wstring (mlenv->env->regmatch[0].first, mlenv->env->regmatch[0].second));
265 substring (ans, 0, max, true, ans);
267 return newMNode_str (new ustring (ans));
269 return newMNode_str (new ustring);
275 (split REGEX STRING) -> STRING_LIST
278 //#AFUNC split ml_split
280 MNode* ml_split (MNode* cell, MlEnv* mlenv) {
281 MNode* arg = cell->cdr ();
287 throw (uErrorWrongNumber);
289 reg = eval_str (arg->car (), mlenv);
290 nextNodeNonNil (arg);
291 t = eval_str (arg->car (), mlenv);
295 throw (uErrorWrongNumber);
298 std::wstring wt = utow (t);
299 std::wstring wreg = utow (reg);
300 boost::wregex wre (wreg);
301 WSplitter sp (wt, wre);
304 ans.append (newMNode_str (new ustring (sp.cur ())));
306 } catch (boost::regex_error& err) {
307 throw (uErrorRegexp);
309 return ans.release ();
314 (string-join TEXT [STRING | ARRAY | LIST]...) -> STRING
317 //#AFUNC string-join ml_string_join
318 //#WIKIFUNC string-join
319 MNode* ml_string_join (MNode* cell, MlEnv* mlenv) {
320 MNode* arg = cell->cdr ();
330 throw (uErrorWrongNumber);
332 sep = eval_str (arg->car (), mlenv);
333 nextNodeNonNil (arg);
336 val = eval (arg->car (), mlenv);
339 if (val () && val ()->isSym ()) {
340 var = val ()->to_string ();
341 n = mlenv->getArySize (var);
342 for (i = 1; i <= n; i ++) {
347 v = mlenv->getAry (var, i);
349 ans.append (v->to_string ());
351 } else if (val () && val ()->isCons ()) {
353 for (; a && a->isCons (); a = a->cdr ()) {
358 if (! isNil (a->car ()))
359 ans.append (a->car ()->to_string ());
362 var = val ()->to_string ();
370 return newMNode_str (new ustring (ans));
375 (password-match PASSWORD CRYPT) -> BOOL
378 //#AFUNC password-match ml_password_match
379 //#WIKIFUNC password-match
380 MNode* ml_password_match (MNode* cell, MlEnv* mlenv) {
381 MNode* arg = cell->cdr ();
386 throw (uErrorWrongNumber);
387 pass = eval_str (arg->car (), mlenv);
388 nextNodeNonNil (arg);
389 cpass = eval_str (arg->car (), mlenv);
392 throw (uErrorWrongNumber);
394 return newMNode_bool (passMatch (pass, cpass));
399 (password-crypt PASSWORD) -> STRING
402 //#AFUNC password-crypt ml_password_crypt
403 //#WIKIFUNC password-crypt
404 MNode* ml_password_crypt (MNode* cell, MlEnv* mlenv) {
405 MNode* arg = cell->cdr ();
409 throw (uErrorWrongNumber);
410 pass = eval_str (arg->car (), mlenv);
413 throw (uErrorWrongNumber);
415 return newMNode_str (new ustring (passCrypt (pass)));
420 (substring STR INDEX LENGTH) -> STRING
421 (substring STR INDEX) -> STRING
424 //#AFUNC substring ml_substring
425 //#WIKIFUNC substring
426 MNode* ml_substring (MNode* cell, MlEnv* mlenv) {
427 MNode* arg = cell->cdr ();
435 throw (uErrorWrongNumber);
436 str = eval_str (arg->car (), mlenv);
437 nextNodeNonNil (arg);
438 index = eval_int (arg->car (), mlenv);
442 length = eval_int (arg->car (), mlenv);
448 throw (uErrorWrongNumber);
450 substring (str, index, length, mode == 3, ans);
451 return newMNode_str (new ustring (ans));
456 (length STR) -> NUMBER
459 //#AFUNC length ml_length
461 MNode* ml_length (MNode* cell, MlEnv* mlenv) {
462 MNode* arg = cell->cdr ();
467 throw (uErrorWrongNumber);
468 str = eval_str (arg->car (), mlenv);
471 throw (uErrorWrongNumber);
473 ans = strLength (str);
474 return newMNode_num (ans);
479 (pad0 NUMBER STRING) -> STRING
480 (pad0 NUMBER STRING_LIST) -> STRING_LIST
481 (pad0 NUMBER_LIST STRING_LIST) -> STRING_LIST
484 //#AFUNC pad0 ml_pad0
486 MNode* ml_pad0 (MNode* cell, MlEnv* mlenv) {
487 MNode* arg = cell->cdr ();
496 throw (uErrorWrongNumber);
497 num = np = eval (arg->car (), mlenv);
498 nextNodeNonNil (arg);
499 val = vp = eval (arg->car (), mlenv);
502 throw (uErrorWrongNumber);
510 n = to_int (np->car ());
516 ans.append (newMNode_str (new ustring (zeroPad (n, to_string (vp->car ())))));
518 if (vp && ! vp->isCons ())
521 return ans.release ();
525 n = to_int (np->car ());
529 return newMNode_str (new ustring (zeroPad (n, to_string (vp))));
538 (ellipsis NUM STRING) -> STRING
541 //#AFUNC ellipsis ml_ellipsis
543 MNode* ml_ellipsis (MNode* cell, MlEnv* mlenv) {
544 MNode* arg = cell->cdr ();
549 throw (uErrorWrongNumber);
550 num = eval_int (arg->car (), mlenv);
551 nextNodeNonNil (arg);
552 str = eval_str (arg->car (), mlenv);
555 throw (uErrorWrongNumber);
557 str = ellipsis (str, num);
558 return newMNode_str (new ustring (str));
563 (string-format FORMAT LIST-OF-ARGS) -> STRING
564 (string-format FORMAT ARGS...) -> STRING
566 |h:format|h:sample|h:note|
568 |${''NUM'':hex:''NUM''}|${1:hex:4}||
569 |${''NUM'':HEX:''NUM''}|${1:HEX:4}||
570 |${''NUM'':int:''NUM''}|${1:int:5}||
571 |${''NUM'':int:''NUM'':c}|${1:int:5:c}||
572 |${''NUM'':int:''NUM'':comma}|${1:int:5:comma}||
573 |${''NUM'':int:''NUM'':clip}|${1:int:5:clip}||
574 |${''NUM'':int:''NUM'':0}|${1:int:5:0}||
575 |${''NUM'':float:''NUM'':''NUM''}|${1:float:4:3}||
576 |${''NUM'':string:''NUM''}|${1:string:20}||
577 |${''NUM'':string:''NUM'':right}|${1:string:20:right}||
578 |${''NUM'':month}|${1:month}|Jan, Feb,...|
579 |${''NUM'':Month}|${1:Month}|January, February,...|
580 |${''NUM'':week}|${1:week}|Sun, Mon,...|
581 |${''NUM'':Week}|${1:Week}|Sunday, Monday,...|
584 //#AFUNC string-format ml_string_format
585 //#WIKIFUNC string-format
586 MNode* ml_string_format (MNode* cell, MlEnv* mlenv) {
587 MNode* arg = cell->cdr ();
589 boost::ptr_vector<MNodePtr> par;
593 throw (uErrorWrongNumber);
594 format = eval_str (arg->car (), mlenv);
597 a = eval (arg->car (), mlenv);
598 if (a && a->isCons ()) {
602 par.push_back (new MNodePtr);
603 par.back () = a->car ();
607 par.push_back (new MNodePtr);
613 return newMNode_str (new ustring (formatString (format, par)));
618 (random-key) -> STRING
621 //#AFUNC random-key ml_random_key
622 //#WIKIFUNC random-key
623 MNode* ml_random_key (MNode* cell, MlEnv* mlenv) {
624 MNode* arg = cell->cdr ();
627 throw (uErrorWrongNumber);
629 return newMNode_str (new ustring (randomKey ()));
634 (to-string OBJECT) -> STRING
637 //#AFUNC to-string ml_to_string
638 //#WIKIFUNC to-string
639 MNode* ml_to_string (MNode* cell, MlEnv* mlenv) {
640 MNode* arg = cell->cdr ();
644 throw (uErrorWrongNumber);
645 text = eval_str (arg->car (), mlenv);
648 throw (uErrorWrongNumber);
650 return newMNode_str (new ustring (text));
655 (to-lisp STRING) -> OBJECT
658 //#AFUNC to-lisp ml_to_lisp
660 MNode* ml_to_lisp (MNode* cell, MlEnv* mlenv) {
661 MNode* arg = cell->cdr ();
666 throw (uErrorWrongNumber);
667 text = eval_str (arg->car (), mlenv);
670 throw (uErrorWrongNumber);
674 return mlenv->retval = ml.top.cdr ();