OSDN Git Service

hex-to-dec function.
authorvisor <visor@users.sourceforge.jp>
Tue, 3 Jan 2017 12:43:47 +0000 (21:43 +0900)
committervisor <visor@users.sourceforge.jp>
Tue, 3 Jan 2017 12:43:47 +0000 (21:43 +0900)
update the split function.

modules/ml-string.cc
modules/ml-string.h

index 5dc961e..b06d6f2 100644 (file)
@@ -581,28 +581,39 @@ MNode*  ml_split (MNode* cell, MlEnv* mlenv) {
     else
        ans = new ListMakerList;
 
-    try {
-       std::wstring  wt = utow (text);
-       std::wstring  wreg = utow (reg);
-       boost::wregex  wre (wreg);
-       WSplitter  sp (wt, wre);
-       size_t  m = wt.length () + 1;
-
-       bool  (WSplitter::*nfn)();
-       if (flagKeep)
-           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 (reg.length () == 0) {
+       uiterator  b = text.begin ();
+       uiterator  e = text.end ();
+       uiterator  s;
+       while (b < e) {
+           s = b;
+           nextChar (b, e);
+           ans.append (newMNode_str (new ustring (s, b)));
+       }
+    } else {
+       try {
+           std::wstring  wt = utow (text);
+           std::wstring  wreg = utow (reg);
+           boost::wregex  wre (wreg);
+           WSplitter  sp (wt, wre);
+           size_t  m = wt.length () + 1;
+           
+           bool  (WSplitter::*nfn)();
+           if (flagKeep)
+               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 (flagKeep)
+               ans.append (newMNode_str (new ustring (sp.cur ())));
+       } catch (boost::regex_error& err) {
+           throw (uErrorRegexp);
        }
-       if (flagKeep)
-           ans.append (newMNode_str (new ustring (sp.cur ())));
-    } catch (boost::regex_error& err) {
-       throw (uErrorRegexp);
     }
     return ans.release ();
 }
@@ -1360,3 +1371,26 @@ MNode*  ml_to_lower (MNode* cell, MlEnv* mlenv) {
 
     return newMNode_str (new ustring (toLower (text)));
 }
+
+/*DOC:
+===hex-to-dec===
+ (hex-to-dec STRING) -> NUMBER
+
+*/
+//#AFUNC       hex-to-dec      ml_hex_to_dec
+//#WIKIFUNC    hex-to-dec
+MNode*  ml_hex_to_dec (MNode* cell, MlEnv* mlenv) {
+    MNode*  arg = cell->cdr ();
+    ustring  text;
+
+    if (! arg)
+       throw (uErrorWrongNumber);
+    text = eval_str (arg->car (), mlenv);
+    nextNode (arg);
+    if (arg)
+       throw (uErrorWrongNumber);
+    if (text.length () > 8)
+       throw (uErrorBadArg);
+
+    return newMNode_num (hextoul (text.begin (), text.end ()));
+}
index 95a3b7f..732c958 100644 (file)
@@ -44,5 +44,6 @@ MNode*  ml_is_ascii63 (MNode* cell, MlEnv* mlenv);
 MNode*  ml_sort_string (MNode* cell, MlEnv* mlenv);
 MNode*  ml_to_upper (MNode* cell, MlEnv* mlenv);
 MNode*  ml_to_lower (MNode* cell, MlEnv* mlenv);
+MNode*  ml_hex_to_dec (MNode* cell, MlEnv* mlenv);
 
 #endif /* ML_STRING_H */