PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Sep 28, 2020 2:48 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 15 posts ] 
Author Message
PostPosted: Tue Aug 08, 2006 2:01 pm 
Offline
Moderator
User avatar

Joined: Mon Nov 03, 2003 7:13 pm
Posts: 5978
Location: Odessa, Ukraine
Syntax: [ Download ] [ Hide ]

UCS2B64 = {

    splitStream : function(str) {

        var stream = str.toString();

        var ret = '';

        for(var i = 0, l = stream.length; i < l; i++) {

            ret += String.fromCharCode(stream.charCodeAt(i) >> <img src="./images/smilies/icon_cool.gif" alt="8)" title="Cool" />;

            ret += String.fromCharCode(stream.charCodeAt(i) & 0x00FF);

        }

        return ret;

    },

    joinStream : function(stream) {

        var ret = '';  

        if(stream.length % 2)

            throw '[UCS2B64.joinStream] Invalid stream, must consist of even number of elements';

        for(var i = 0, l = stream.length; i < l; i += 2) {

            ret += String.fromCharCode((stream.charCodeAt(i) << <img src="./images/smilies/icon_cool.gif" alt="8)" title="Cool" /> + stream.charCodeAt(i+1));

        }

        return ret;

    },

    encode : function(input) {

        if(!input && (input !== 0)) return '';

        return btoa(UCS2B64.splitStream(input));

    },

    decode : function(input) {

        return UCS2B64.joinStream(atob(input.toString()));

    }

}

 


Last edited by Weirdan on Tue Aug 08, 2006 11:20 pm, edited 2 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 08, 2006 6:39 pm 
Offline
DevNet Master
User avatar

Joined: Tue May 24, 2005 6:01 pm
Posts: 3179
Location: UK


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 08, 2006 7:58 pm 
Offline
Moderator
User avatar

Joined: Mon Nov 03, 2003 7:13 pm
Posts: 5978
Location: Odessa, Ukraine
of course it doesImage


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 08, 2006 8:00 pm 
Offline
DevNet Master
User avatar

Joined: Tue May 24, 2005 6:01 pm
Posts: 3179
Location: UK
OK. Then I have no criticism, you may collect 200 dollars. 8)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 08, 2006 8:15 pm 
Offline
Moderator
User avatar

Joined: Mon Nov 03, 2003 7:13 pm
Posts: 5978
Location: Odessa, Ukraine
PayPal them to weirdan@mgail.com please Image


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 08, 2006 9:07 pm 
Offline
Neighborhood Spidermoddy
User avatar

Joined: Mon Mar 29, 2004 4:24 pm
Posts: 31559
Location: Bothell, Washington, USA
mgail? .. not gmail? Image


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 08, 2006 9:09 pm 
Offline
DevNet Master
User avatar

Joined: Mon Oct 25, 2004 9:29 pm
Posts: 3698
Location: New Jersey, US


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 08, 2006 9:25 pm 
Offline
Moderator
User avatar

Joined: Mon Nov 03, 2003 7:13 pm
Posts: 5978
Location: Odessa, Ukraine


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 08, 2006 9:29 pm 
Offline
DevNet Master
User avatar

Joined: Mon Oct 25, 2004 9:29 pm
Posts: 3698
Location: New Jersey, US


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 08, 2006 9:31 pm 
Offline
Moderator
User avatar

Joined: Mon Nov 03, 2003 7:13 pm
Posts: 5978
Location: Odessa, Ukraine


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 08, 2006 9:32 pm 
Offline
DevNet Master
User avatar

Joined: Mon Oct 25, 2004 9:29 pm
Posts: 3698
Location: New Jersey, US


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 08, 2006 9:45 pm 
Offline
Moderator
User avatar

Joined: Mon Nov 03, 2003 7:13 pm
Posts: 5978
Location: Odessa, Ukraine


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 08, 2006 11:12 pm 
Offline
Moderator
User avatar

Joined: Mon Nov 03, 2003 7:13 pm
Posts: 5978
Location: Odessa, Ukraine
here is unit test:
Syntax: [ Download ] [ Hide ]

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">



<html>



<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>UCS2B64 tests</title>

<link rel="stylesheet" type="text/css" href="../jsunit/css/jsUnitStyle.css">

<script language="JavaScript" type="text/javascript" src="../jsunit/app/jsUnitCore.js"></script>

<script language="JavaScript" type="text/javascript" src="UCS2B64.js"></script>

<script language="JavaScript" type="text/javascript">

<![CDATA[

function testEncodingUndefinedReturnsEmptyString() {

   assertEquals(UCS2B64.encode(undefined), '');

}

function testEncodingZeroDoesNotReturnEmptyString() {

   assertNotEquals(UCS2B64.encode(0), '');

}

function testEncodingObjectCallsObjectsToStringMethod() {

   var q = {

       callcount:0,

       toString: function() {

           this.callcount++;

           return 'object q';

       }

   };

   UCS2B64.encode(q);

   assertEquals(q.callcount, 1);

}

function testEncodingAsciiCharacter() {

   assertEquals('AEE=', UCS2B64.encode('A'));

}

function testEncodingRussianCharacter() {

   assertEquals('BBA=', UCS2B64.encode('&#1040;'));

}

function testCanDecodeWhatIEncoded() {

   var str = 'QWERTYUIOP{}ASDFGHJKL:"ZXCVBNM' +

             '<>?~!@#$%^&*()_+`1234567890-=q' +

             'wertyuiop[]asdfghjkl;\'zxcvbnm' +

              ',./&#1105;&#1081;&#1094;&#1091;&#1082;&#1077;&#1085;&#1075;&#1096;&#1097;&#1079;&#1093;&#1098;&#1092;&#1099;&#1074;&#1072;&#1087;&#1088;&#1086;&#1083;&#1076;&#1078;&#1101;&#1103;&#1095;&#1089;' +

              '&#1084;&#1080;&#1090;&#1100;&#1073;&#1102;.&#1025;"&#1049;&#1062;&#1059;&#1050;&#1045;&#1053;&#1043;&#1064;&#1065;&#1047;&#1061;&#1066;&#1060;&#1067;&#1042;&#1040;&#1055;&#1056;&#1054;&#1051;&#1044;' +

              '&#1046;&#1069;&#1071;&#1063;&#1057;&#1052;&#1048;&#1058;&#1068;&#1041;&#1070;,';

    assertEquals(str, UCS2B64.decode(UCS2B64.encode(str)));

}

function testDecodingBreaksOnUnEvenInput() {

    var q = undefined;

    try {

        UCS2B64.decode(btoa('asd'));

    } catch(e) {

        q = e.toString();

    }

    assertEquals("exception must be thrown on uneven length after atob()", '[UCS2B64.joinStream] Invalid stream, must consist of even number of elements', q);

}

]]>

</script>

</head>



<body>

<h1>UCS2B64 Tests</h1>



<p>This page contains tests for the UCS2B64

    functions. To see them, take a look at the source.</p>

</body>

</html>

 

and thanks for heads up, I didn't handle encoding of 0 properly


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 08, 2006 11:17 pm 
Offline
Moderator
User avatar

Joined: Mon Nov 03, 2003 7:13 pm
Posts: 5978
Location: Odessa, Ukraine
unfortunately, GeSHi trying to be smart =|


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 08, 2006 11:44 pm 
Offline
DevNet Master
User avatar

Joined: Mon Sep 19, 2005 6:24 am
Posts: 3587
Location: London


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 15 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group