Advertisement
stuppid_bot

Encode/Decode Unicode

Sep 4th, 2014
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function (win, undef) {
  2.     var encodeMap = {
  3.         '\0': '0',
  4.         '\b': 'b',
  5.         '\t': 't',
  6.         '\n': 'n',
  7.         '\v': 'v',
  8.         '\f': 'f',
  9.         '\r': 'r',
  10.         '"': '"',
  11.         "'": "'",
  12.         '\\': '\\'
  13.     };
  14.  
  15.     var decodeMap = {};
  16.  
  17.     for (var p in encodeMap) {
  18.         decodeMap[encodeMap[p]] = p;
  19.     }
  20.  
  21.     win.encodeUnicode = function (s) {
  22.         var re = /[^\x20\x21\x23-\x26\x28-\x5b\x5d-\x7e]/g;
  23.         return s.replace(re, function (c) {
  24.             if (c in encodeMap) {
  25.                 return '\\' + encodeMap[c];
  26.             }
  27.             c = c.charCodeAt(0);
  28.             if (c < 128) {
  29.                 return '\\x' + (c < 16 ? '0' : '') + c.toString(16);
  30.             }
  31.             return '\\u' + ('000' + c.toString(16)).slice(-4);
  32.         });
  33.     };
  34.  
  35.     win.decodeUnicode = function (s) {
  36.         var re = /\\((?:[1-7][0-7]{0,2}|[0-7]{2,3}))|\\x([\da-f]{2})|\\u([\da-f]{4})|\\(.)/gi;
  37.         return s.replace(re, function (_, o, h, u, c) {
  38.             // console.log(arguments);
  39.             if (o !== undef) return String.fromCharCode(parseInt(o, 8));
  40.             if (h !== undef) return String.fromCharCode(parseInt(h, 16));
  41.             if (u !== undef) return String.fromCharCode(parseInt(u, 16));
  42.             if ('xXuU'.indexOf(c) != -1) throw new Error('bad sequence');
  43.             if (c in decodeMap) return decodeMap[c];
  44.             return c;
  45.         });
  46.     };
  47.  
  48.     win.testEncoder = function () {
  49.         assert(decodeUnicode('\\0') === '\0');
  50.         assert(decodeUnicode('\\b') === '\b');
  51.         assert(decodeUnicode('\\t') === '\t');
  52.         assert(decodeUnicode('\\n') === '\n');
  53.         assert(decodeUnicode('\\v') === '\v');
  54.         assert(decodeUnicode('\\f') === '\f');
  55.         assert(decodeUnicode('\\r') === '\r');
  56.         assert(decodeUnicode('\\"') === '"');
  57.         assert(decodeUnicode('\\\'') === '\'');
  58.         assert(decodeUnicode('\\\\') === '\\');
  59.         assert(decodeUnicode('\\xff') === '\xff');
  60.         assert(decodeUnicode('\\100') === '\100');
  61.         assert(decodeUnicode('\\u0100') === '\u0100');
  62.         console.log('ok');
  63.     };
  64. })(window);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement