OSDN Git Service

bug fix and update.
authorvisor <visor@users.sourceforge.jp>
Tue, 7 Feb 2017 13:57:06 +0000 (22:57 +0900)
committervisor <visor@users.sourceforge.jp>
Tue, 7 Feb 2017 13:57:06 +0000 (22:57 +0900)
lib/util_string.cc
lib/util_string.h
modules/ml-encode.cc
modules/ml-string.cc
wiki/wikiline.cc

index baaa19a..e697051 100644 (file)
@@ -1001,12 +1001,13 @@ ustring  dtohex (double e, int pad, int base, bool upcase) {
            d[--pos] = digs[r];
        }
        if (pad > 0) {
-           for (int i = 128 - pos; i < pad; i ++) {
+           for (int i = 128 - pos; i < pad && i < 128; i ++) {
                d[--pos] = '0';
            }
        }
        ans.assign (d + pos, 128 - pos);
     } else {
+       /* *** */
     }
     return ans;
 }
@@ -1172,16 +1173,21 @@ ustring  toUpper (const ustring& str) {
     return boost::to_upper_copy (str);
 }
 
-ustring  hexEncode (const ustring& data) {
+ustring  hexEncode (const ustring& data, bool upcase) {
     ustring  ans;
     uiterator  b, e;
+    char  (*fn) (int);
 
+    if (upcase)
+       fn = hexchar_c;
+    else
+       fn = hexchar;
     ans.reserve (data.length () * 2);
     b = data.begin ();
     e = data.end ();
     for (; b < e; b ++) {
-       ans.append (1, hexchar ((*b >> 4) & 0x0f));
-       ans.append (1, hexchar (*b & 0x0f));
+       ans.append (1, fn ((*b >> 4) & 0x0f));
+       ans.append (1, fn (*b & 0x0f));
     }
     return ans;
 }
index f55aca3..187e1f5 100644 (file)
@@ -107,7 +107,7 @@ ustring  toLower (uiterator b, uiterator e);
 ustring  formatDateString (const ustring& format, struct tm& v);
 ustring  toLower (const ustring& str);
 ustring  toUpper (const ustring& str);
-ustring  hexEncode (const ustring& data);
+ustring  hexEncode (const ustring& data, bool upcase = false);
 ustring  hexDecode (const ustring& data);
 int  octchar (uiterator b);
 ustring  octchar (int c);
index 1695a9f..2f14282 100644 (file)
@@ -248,7 +248,7 @@ MNode*  ml_base64_encode (MNode* cell, MlEnv* mlenv) {
 
 /*DOC:
 ===base64-decode===
- (base64-decode STRING...) -> STRING
+ (base64-decode [#hex | #HEX] STRING...) -> STRING
 
 */
 //#AFUNC       base64-decode   ml_base64_decode
@@ -256,15 +256,32 @@ MNode*  ml_base64_encode (MNode* cell, MlEnv* mlenv) {
 MNode*  ml_base64_decode (MNode* cell, MlEnv* mlenv) {
     MNode*  arg = cell->cdr ();
     ustring  str;
+    bool  fhex = false;
+    bool  fHex = false;
+    std::vector<MNode*>  keywords;
+    MNode*  rest;
+    static paramList  kwlist[] = {
+       {CharConst ("hex"), true},
+       {CharConst ("HEX"), true},
+       {NULL, 0, 0}
+    };
 
-    while (arg) {
-       str.append (eval_str (arg->car (), mlenv));
-       nextNode (arg);
+    setParams (arg, 0, NULL, kwlist, &keywords, &rest);
+    evkw_bool (0, fhex);
+    evkw_bool (1, fHex);
+    while (rest) {
+       str.append (eval_str (rest->car (), mlenv));
+       nextNode (rest);
     }
-
     str = base64Decode (str.begin (), str.end ());
-
-    return newMNode_str (new ustring (fixUTF8 (str)));
+    if (fhex) {
+       str = hexEncode (str);
+    } else if (fHex) {
+       str = hexEncode (str, true);
+    } else {
+       str = fixUTF8 (str);
+    }
+    return newMNode_str (new ustring (str));
 }
 
 /*DOC:
@@ -311,8 +328,8 @@ MNode*  ml_base64_url_decode (MNode* cell, MlEnv* mlenv) {
 
 /*DOC:
 ===hex-encode===
- (hex-encode STRING...) -> STRING
- (string-to-hex STRING ...) -> STRING
+ (hex-encode [#HEX] STRING...) -> STRING
+ (string-to-hex [#HEX] STRING ...) -> STRING
 
 */
 //#AFUNC       hex-encode      ml_hex_encode
@@ -323,11 +340,20 @@ MNode*  ml_hex_encode (MNode* cell, MlEnv* mlenv) {
     MNode*  arg = cell->cdr ();
     ustring  text;
     ustring  ans;
+    bool  fHex = false;
+    std::vector<MNode*>  keywords;
+    MNode*  rest;
+    static paramList  kwlist[] = {
+       {CharConst ("HEX"), true},
+       {NULL, 0, 0}
+    };
 
-    while (arg) {
-       text = eval_str (arg->car (), mlenv);
-       nextNode (arg);
-       ans.append (hexEncode (text));
+    setParams (arg, 0, NULL, kwlist, &keywords, &rest);
+    evkw_bool (0, fHex);
+    while (rest) {
+       text = eval_str (rest->car (), mlenv);
+       nextNode (rest);
+       ans.append (hexEncode (text, fHex));
     }
     return newMNode_str (new ustring (ans));
 }
index b36c714..f18e824 100644 (file)
@@ -1101,10 +1101,11 @@ MNode*  ml_gmdate_format (MNode* cell, MlEnv* mlenv) {
 MNode*  ml_to_string (MNode* cell, MlEnv* mlenv) {
     MNode*  arg = cell->cdr ();
     bool  fhex = false;
-    bool  fuhex = false;
+    bool  fHex = false;
     bool  foct = false;
     bool  fbin = false;
     int  pad = 0;
+    MNodePtr  obj;
     ustring  text;
     std::vector<MNode*>  params;
     std::vector<MNode*>  keywords;
@@ -1118,21 +1119,27 @@ MNode*  ml_to_string (MNode* cell, MlEnv* mlenv) {
     };
 
     setParams (arg, 1, &params, kwlist, &keywords, NULL);
+    obj = eval (params[0], mlenv);
     evkw_bool (0, fhex);
-    evkw_bool (1, fuhex);
+    evkw_bool (1, fHex);
     evkw_bool (2, foct);
     evkw_bool (3, fbin);
     evkw_int (4, pad);
-    if (fhex) {
-       text = dtohex (eval_double (params[0], mlenv), pad, 16, false);
-    } else if (fuhex) {
-       text = dtohex (eval_double (params[0], mlenv), pad, 16, true);
-    } else if (foct) {
-       text = dtohex (eval_double (params[0], mlenv), pad, 8);
-    } else if (fbin) {
-       text = dtohex (eval_double (params[0], mlenv), pad, 2);
+    if (fhex || fHex || foct || fbin) {
+       if (isReal (obj ())) {
+           if (fhex) {
+               text = dtohex (to_double (obj ()), pad, 16, false);
+           } else if (fHex) {
+               text = dtohex (to_double (obj ()), pad, 16, true);
+           } else if (foct) {
+               text = dtohex (to_double (obj ()), pad, 8);
+           } else if (fbin) {
+               text = dtohex (to_double (obj ()), pad, 2);
+           }
+       } else {
+       }
     } else {
-       text = eval_str (params[0], mlenv);
+       text = to_string (obj ());
     }
     return newMNode_str (new ustring (text));
 }
index a291250..9323782 100644 (file)
@@ -928,7 +928,7 @@ bool  wl_pad0 (WikiMotorObjVecVec* args, WikiMotorObjVec& out, WikiFormat* wiki)
  [[c3:VALUE]]
 
 */
-//#WIKILINE    c3      wl_c3
+//#WIKILINE    c3      wl_c3
 bool  wl_c3 (WikiMotorObjVecVec* args, WikiMotorObjVec& out, WikiFormat* wiki) {
     WikiMotorObjVecVec::const_iterator  b = args->begin ();
     WikiMotorObjVecVec::const_iterator  e = args->end ();