Skip to content

Instantly share code, notes, and snippets.

@srohde
Created December 10, 2009 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save srohde/253422 to your computer and use it in GitHub Desktop.
Save srohde/253422 to your computer and use it in GitHub Desktop.
### Eclipse Workspace Patch 1.0
#P oauth_core
Index: src/org/iotashan/utils/URLEncoding.as
===================================================================
--- src/org/iotashan/utils/URLEncoding.as (revision 13)
+++ src/org/iotashan/utils/URLEncoding.as (working copy)
@@ -1,75 +1,65 @@
-package org.iotashan.utils
-{
+package org.iotashan.utils {
+
/**
* Encodes and decodes strings into URL format.
- *
+ *
* Code ported from the javascript code at:
* http://cass-hacks.com/articles/code/js_url_encode_decode/
- *
+ *
* UTF-8 bug fixed by Denis Borisenko:
* http://blog.dborisenko.com/en/2009/09/05/extended-utf-8-in-oauth-actionscript-library/
- */
- public class URLEncoding
- {
- public function URLEncoding()
- {
+ */
+ public class URLEncoding {
+ public function URLEncoding() {
}
/**
* Does proper UTF-8 encoding
*/
- public static function utf8Encode(string:String):String {
- string = string.replace(/rn/g,'n');
- string = string.replace(/r/g,'n');
-
+ public static function utf8Encode( string : String ) : String {
+ string = string.replace( /\r\n/g, '\n' );
+ string = string.replace( /\r/g, '\n' );
+
+
var utfString:String = '';
-
- for (var i:int = 0 ; i < string.length ; i++)
- {
- var chr:Number = string.charCodeAt(i);
- if (chr < 128)
- {
- utfString += String.fromCharCode(chr);
- }
- else if ((chr > 127) && (chr < 2048))
- {
- utfString += String.fromCharCode((chr >> 6) | 192);
- utfString += String.fromCharCode((chr & 63) | 128);
- }
- else
- {
- utfString += String.fromCharCode((chr >> 12) | 224);
- utfString += String.fromCharCode(((chr >> 6) & 63) | 128);
- utfString += String.fromCharCode((chr & 63) | 128);
+
+ for ( var i:int = 0; i < string.length; i++ ) {
+ var chr:Number = string.charCodeAt( i );
+ if ( chr < 128 ) {
+ utfString += String.fromCharCode( chr );
+ } else if ( (chr > 127) && (chr < 2048) ) {
+ utfString += String.fromCharCode( ( chr >> 6 ) | 192 );
+ utfString += String.fromCharCode( ( chr & 63 ) | 128 );
+ } else {
+ utfString += String.fromCharCode( ( chr >> 12 ) | 224 );
+ utfString += String.fromCharCode( ( (chr >> 6) & 63 ) | 128 );
+ utfString += String.fromCharCode( ( chr & 63 ) | 128 );
}
}
-
+
return utfString;
}
/**
* Does the URL encoding
*/
- public static function urlEncode(string:String):String {
+ public static function urlEncode( string : String ) : String {
var urlString:String = '';
-
- for (var i:int = 0 ; i < string.length ; i++)
- {
- var chr:Number = string.charCodeAt(i);
-
- if ((chr >= 48 && chr <= 57) || // 09
+
+ for ( var i:int = 0; i < string.length; i++ ) {
+ var chr:Number = string.charCodeAt( i );
+
+ if ( (chr >= 48 && chr <= 57) || // 09
(chr >= 65 && chr <= 90) || // AZ
(chr >= 97 && chr <= 122) || // az
chr == 45 || // -
chr == 95 || // _
chr == 46 || // .
- chr == 126) // ~
- {
- urlString += String.fromCharCode(chr);
- }
- else
+ chr == 126 ) // ~
{
- urlString += '%' + chr.toString(16).toUpperCase();
+ urlString += String.fromCharCode( chr );
+ } else {
+ urlString += '%' + chr.toString( 16 ).toUpperCase();
}
}
@@ -79,16 +69,16 @@
/**
* Properly URL encodes a string, taking into account UTF-8
*/
- public static function encode(string:String):String {
- return urlEncode(utf8Encode(string));
+ public static function encode( string : String ) : String {
+ return urlEncode( utf8Encode( string ) );
}
/**
* Decodes a string from a URL compliant format.
- *
+ *
* @param encodedString String to be decoded
- */
- public static function decode(encodedString:String):* {
+ */
+ public static function decode( encodedString : String ) : * {
var output:String = encodedString;
var myregexp:RegExp = /(%[^%]{2})/;
@@ -99,13 +89,13 @@
// change "+" to spaces
var plusPattern:RegExp = /\+/gm;
- output = output.replace(plusPattern," ");
+ output = output.replace( plusPattern, " " );
// convert all other characters
- while ((match = myregexp.exec(output)) != null && match.length > 1 && match[1] != '') {
- binVal = parseInt(match[1].substr(1),16);
- thisString = String.fromCharCode(binVal);
- output = output.replace(match[1], thisString);
+ while ( (match = myregexp.exec( output )) != null && match.length > 1 && match[1] != '' ) {
+ binVal = parseInt( match[ 1 ].substr( 1 ), 16 );
+ thisString = String.fromCharCode( binVal );
+ output = output.replace( match[ 1 ], thisString );
}
return output;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment