ultimix
string_utilities.js
Go to the documentation of this file.
1 
6 if( !ultimix )
7 {
8  ultimix = {};
9 }
10 
16 if( !ultimix.string_utilities )
17 {
18  ultimix.string_utilities = {};
19 }
20 
34 ultimix.string_utilities.str_replace = function( Search , Replace , Subject )
35 {
36  return( Subject.split( Search ).join( Replace ) );
37 }
38 
50 ultimix.string_utilities.print_record = function( Format , Record )
51 {
52  for( i in Record )
53  {
54  Format = ultimix.string_utilities.str_replace( '[' + i + ']' , Record[ i ] , Format );
55  }
56 
57  return( Format );
58 }
59 
69 ultimix.string_utilities.trail_data = function( Data , EncodedData )
70 {
71  switch( Data.length % 3 )
72  {
73  case( 1 ):
74  EncodedData = EncodedData.slice( 0 , -2 ) + '==';
75  break;
76  case( 2 ):
77  EncodedData = EncodedData.slice( 0 , -1 ) + '=';
78  break;
79  }
80 
81  return( EncodedData );
82 }
83 
93 ultimix.string_utilities.base64_encode = function( Data )
94 {
95  var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
96  var o1 , o2 , o3 , h1 , h2 , h3 , h4 , bits , i = 0 , EncodedData = '';
97  do
98  {
99  o1 = Data.charCodeAt( i++ );
100  o2 = Data.charCodeAt( i++ );
101  o3 = Data.charCodeAt( i++ );
102  bits = o1 << 16 | o2 << 8 | o3;
103  h1 = bits >> 18 & 0x3f;
104  h2 = bits >> 12 & 0x3f;
105  h3 = bits >> 6 & 0x3f;
106  h4 = bits & 0x3f;
107  EncodedData += b64.charAt( h1 ) + b64.charAt( h2 ) + b64.charAt( h3 ) + b64.charAt( h4 );
108  }
109  while ( i < Data.length );
110 
111  return( ultimix.string_utilities.trail_data( Data , EncodedData ) );
112 }
113 
131 ultimix.string_utilities.decoded_char = function( o1 , o2 , o3 , h3 , h4 )
132 {
133  if( h3 == 64 )
134  {
135  return( String.fromCharCode( o1 ) );
136  }
137  else if ( h4 == 64 )
138  {
139  return( String.fromCharCode( o1 , o2 ) );
140  }
141  else
142  {
143  return( String.fromCharCode( o1 , o2 , o3 ) );
144  }
145 }
146 
156 ultimix.string_utilities.base64_decode = function( Data )
157 {
158  var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
159  var o1 , o2 , o3 , h1 , h2 , h3 , h4 , bits , i = 0 , DecodedData = '';
160  do
161  {
162  h1 = b64.indexOf( Data.charAt( i++ ) );
163  h2 = b64.indexOf( Data.charAt( i++ ) );
164  h3 = b64.indexOf( Data.charAt( i++ ) );
165  h4 = b64.indexOf( Data.charAt( i++ ) );
166  bits = h1<<18 | h2<<12 | h3<<6 | h4;
167  o1 = bits>>16 & 0xff;
168  o2 = bits>>8 & 0xff;
169  o3 = bits & 0xff;
170 
171  DecodedData += ultimix.string_utilities.decoded_char( o1 , o2 , o3 , h3 , h4 );
172  }
173  while( i < Data.length );
174 
175  return( DecodedData );
176 }
177 
187 ultimix.string_utilities.valign_block = function( Content )
188 {
189  return( "<div class=\"valign_child\">" + Content + "</div><div class=\"valign_helper\"></div>" );
190 }
191 
203 ultimix.string_utilities.halign_block = function( Content , Width )
204 {
205  return(
206  "<div class=\"margin_0_auto\" style=\"display: block; width: " +
207  Width + "px; height: 100%;\">" + Content + "</div>"
208  );
209 }
210 
222 ultimix.string_utilities.trim = function( Str , Charlist )
223 {
224  Charlist = !Charlist ? ' \s\xA0' : Charlist.replace( /([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g , '\$1' );
225 
226  var re = new RegExp( '^[' + Charlist + ']+|[' + Charlist + ']+$' , 'g' );
227 
228  return( Str.replace( re , '' ) );
229 }