OSDN Git Service

add an option of the split function.
authorvisor <visor@users.sourceforge.jp>
Thu, 20 Jun 2013 13:37:16 +0000 (22:37 +0900)
committervisor <visor@users.sourceforge.jp>
Thu, 20 Jun 2013 13:37:16 +0000 (22:37 +0900)
lib/util_splitter.h
lib/util_wsplitter.h
modules/ml-string.cc

index cc3117d..59686b6 100644 (file)
@@ -58,6 +58,7 @@ class  Splitter {
                return false;
            }
        } else {
+           t = u = e;
            return false;
        }
     };
index 41f2f8d..a270656 100644 (file)
@@ -31,6 +31,23 @@ class  WSplitter {
            return false;
        }
     };
+    virtual bool  nextSep () {
+       b = u;
+       if (b != e) {
+           if (regex_search (b, e, m, *re, boost::regex_constants::match_single_line)) {
+               t = m[0].first;
+               u = m[0].second;
+               return true;
+           } else {
+               t = e;
+               u = e;
+               return false;
+           }
+       } else {
+           t = u = e;
+           return false;
+       }
+    };
     virtual ustring  cur () {
        std::wstring  x (b, t);
        return wtou (x);
index 359d8a7..f54cd02 100644 (file)
@@ -352,33 +352,42 @@ MNode*  ml_regexp_split (MNode* cell, MlEnv* mlenv) {
 MNode*  ml_split (MNode* cell, MlEnv* mlenv) {
     MNode*  arg = cell->cdr ();
     ustring  reg;
-    ustring  t;
+    ustring  text;
+    bool  keep = false;
     MNodeList  ans;
+    std::vector<MNode*>  params;
+    std::vector<MNode*>  keywords;
+    static paramList  kwlist[] = {
+       {CharConst ("keep"), true}, // 空フィールドの削除をしない
+       {NULL, 0, 0}
+    };
 
-    if (! arg)
-       throw (uErrorWrongNumber);
-
-    reg = eval_str (arg->car (), mlenv);
-    nextNodeNonNil (arg);
-    t = eval_str (arg->car (), mlenv);
-    nextNode (arg);
-
-    if (arg)
-       throw (uErrorWrongNumber);
+    setParams (arg, 2, &params, kwlist, &keywords, NULL);
+    reg = eval_str (params[0], mlenv);
+    text = eval_str (params[1], mlenv);
+    if (keywords[0] && eval_bool (keywords[0], mlenv))
+       keep = true;
 
     try {
-       std::wstring  wt = utow (t);
+       std::wstring  wt = utow (text);
        std::wstring  wreg = utow (reg);
        boost::wregex  wre (wreg);
        WSplitter  sp (wt, wre);
        size_t  m = wt.length () + 1;
 
-       while (sp.next ()) {
+       bool  (WSplitter::*nfn)();
+       if (keep)
+           nfn = &WSplitter::nextSep;
+       else
+           nfn = &WSplitter::next;
+       while ((sp.*nfn) ()) {
            ans.append (newMNode_str (new ustring (sp.cur ())));
            m --;
            if (m == 0)
                throw (uErrorRegexp);
        }
+       if (keep)
+           ans.append (newMNode_str (new ustring (sp.cur ())));
     } catch (boost::regex_error& err) {
        throw (uErrorRegexp);
     }