⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 strings.as

📁 用于flash/flex的 as3的 2D图形图像图表的动态生成
💻 AS
📖 第 1 页 / 共 2 页
字号:
package flare.util
{
	import flash.utils.ByteArray;
	
	/**
	 * Utility methods for working with String instances. The
	 * <code>format</code> method provides a powerful mechanism for formatting
	 * and templating strings.
	 */
	public class Strings
	{	
		/**
		 * Constructor, throws an error if called, as this is an abstract class.
		 */
		public function Strings() {
			throw new Error("This is an abstract class.");
		}
		
		/**
		 * Creates a new string by repeating an input string.
		 * @param s the string to repeat
		 * @param reps the number of times to repeat the string
		 * @return a new String containing the repeated input
		 */
		public static function repeat(s:String, reps:int):String
		{
			if (reps == 1) return s;
			
			var b:ByteArray = new ByteArray();
			for (var i:uint=0; i<reps; ++i)
				b.writeUTFBytes(s);
			b.position = 0;
			return b.readUTFBytes(b.length);
		}
		
		/**
		 * Aligns a string by inserting padding space characters as needed.
		 * @param s the string to align
		 * @param align an integer indicating both the desired length of
		 *  the string (the absolute value of the input) and the alignment
		 *  style (negative for left alignment, positive for right alignment)
		 * @return the aligned string, padded or truncated as necessary
		 */
		public static function align(s:String, align:int):String
		{
			var left:Boolean = align < 0;
			var len:int = left?-align:align, slen:int = s.length;
			if (slen > len) {
				return left ? s.substr(0,len) : s.substr(slen-len, len);
			} else {
				var pad:String = repeat(' ',len-slen);
				return left ? s + pad : pad + s;
			}
		}
		
		/**
		 * Pads a number with a specified number of "0" digits on
		 * the left-hand-side of the number.
		 * @param x an input number
		 * @param digits the number of "0" digits to pad by
		 * @return a padded string representation of the input number
		 */
		public static function pad(x:Number, digits:int):String
		{
			var neg:Boolean = (x < 0);
			x = Math.abs(x);
			var e:int = 1 + int(Math.log(x) / Math.LN10);
			var s:String = String(x);
			for (; e<digits; e++) { s = '0' + s; }
			return neg ? "-"+s : s;
		}
		
		/**
		 * Pads a string with zeroes up to given length.
		 * @param s the string to pad
		 * @param n the target length of the padded string
		 * @return the padded string. If the input string is already equal or
		 *  longer than n characters it is returned unaltered. Otherwise, it
		 *  is left-padded with zeroes up to form an n-character string.
		 */
		public static function padString(s:String, n:int):String
		{
			return (s.length < n ? repeat("0",n-s.length) + s : s);
		}
		
		// --------------------------------------------------------------------

		/** Default formatting string for numbers. */
		public static const DEFAULT_NUMBER:String = "0.########";

		private static const _BACKSLASH :Number = '\\'.charCodeAt(0);
		private static const _LBRACE    :Number = '{'.charCodeAt(0);
		private static const _RBRACE    :Number = '}'.charCodeAt(0);
		private static const _QUOTE     :Number = '"'.charCodeAt(0);
		private static const _APOSTROPHE:Number = '\''.charCodeAt(0);
	
		/**
		 * Outputs a formatted string using a set of input arguments and string
		 * formatting directives. This method uses the String formatting
		 * conventions of the .NET framework, providing a very flexible system
		 * for mapping input values into various string representations. For
		 * examples and reference documentation for string formatting options,
		 * see
		 * <a href="http://blog.stevex.net/index.php/string-formatting-in-csharp/">
		 * this example page</a> or
		 * <a href="http://msdn2.microsoft.com/en-us/library/fbxft59x.aspx">
		 * Microsoft's documentation</a>.
		 * @param fmt a formatting string. Format strings include markup
		 *  indicating where input arguments should be placed in the string,
		 *  along with optional formatting directives. For example,
		 *  <code>{1}, {0}</code> writes out the second value argument, a
		 *  comma, and then the first value argument.
		 * @param args value arguments to be placed within the formatting
		 *  string.
		 * @return the formatted string.
		 */
		public static function format(fmt:String, ...args):String
		{
			var b:ByteArray = new ByteArray(), a:Array;
			var esc:Boolean = false, val:*;
			var c:Number, idx:int, ialign:int;
			var idx0:int, idx1:int, idx2:int;
			var s:String, si:String, sa:String, sf:String;
			
			for (var i:uint=0; i<fmt.length; ++i) {
				c = fmt.charCodeAt(i);
				if (c == _BACKSLASH) {
					// note escape char
					if (esc) b.writeUTFBytes('\\');
					esc = true;
				}
				else if (c == _LBRACE) {
					// process pattern
					if (esc) {
						b.writeUTFBytes('{');
						esc = false;
					} else {
						// get pattern boundary
						idx = fmt.indexOf("}", i);
						if (idx < 0)
							throw new ArgumentError("Invalid format string.");
						
						// extract pattern
						s = fmt.substring(i+1, idx);
						
						idx2 = s.indexOf(":");
						idx1 = s.indexOf(",");
						idx0 = Math.min(idx1<0 ? int.MAX_VALUE : idx1,
										idx2<0 ? int.MAX_VALUE : idx2);
						
						si = idx0==int.MAX_VALUE ? s : s.substring(0,idx0);
						sa = idx1<0 || idx1 > idx2 ? null : s.substring(idx1+1, idx2<0?s.length:idx2);
						sf = idx2<0 ? null : s.substring(idx2+1);
						
						try {
							if (sa != null) { ialign = int(sa); }
							if ((idx0=uint(si))==0 && si!="0") {
								val = Property.$(si).getValue(args[0]);
							} else {
								val = args[idx0];
							}
							pattern(b, sf, val);
						} catch (x:*) {
							throw new ArgumentError("Invalid format string.");
						}
						i = idx;
					}
				} else {
					// by default, copy value to buffer
					b.writeUTFBytes(fmt.charAt(i));
				}
			}
			
			b.position = 0;
			s = b.readUTFBytes(b.length);
			
			// finally adjust string alignment as needed
			return (sa != null ? align(s, ialign) : s);
		}
			
		private static function pattern(b:ByteArray, pat:String, value:*):void
		{
			if (pat == null) {
				b.writeUTFBytes(String(value));
			} else if (value is Date) {
				datePattern(b, pat, value as Date);
			} else if (value is Number) {
				numberPattern(b, pat, Number(value));
			} else {
				b.writeUTFBytes(String(value));
			}
		}
		
		private static function count(s:String, c:Number, i:int):int
		{
			var n:int = 0;
			for (n=0; i<s.length && s.charCodeAt(i)==c; ++i, ++n);
			return n;
		}

		// -- Date Formatting -------------------------------------------------

		/** Array of full names for days of the week. */
		public static var DAY_NAMES:Array =
			["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
			 "Friday", "Saturday"];
		/** Array of abbreviated names for days of the week. */
		public static var DAY_ABBREVS:Array =
			["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
		/** Array of full names for months of the year. */
		public static var MONTH_NAMES:Array =
			["January", "February", "March", "April", "May", "June",
			 "July", "August", "September", "October", "November", "December"];
		/** Array of abbreviated names for months of the year. */
		public static var MONTH_ABBREVS:Array = 
			["Jan", "Feb", "Mar", "Apr", "May", "Jun",
			 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
		/** Abbreviated string indicating "PM" time. */
		public static var PM1:String = "P";
		/** Full string indicating "PM" time. */
		public static var PM2:String = "PM";
		/** Abbreviated string indicating "AM" time. */
		public static var AM1:String = "A";
		/** Full string indicating "AM" time. */
		public static var AM2:String = "AM";
		/** String indicating "AD" time. */
		public static var AD:String = "AD";
		/** String indicating "BC" time. */
		public static var BC:String = "BC";		

		private static const _DATE:Number = 'd'.charCodeAt(0);
		private static const _FRAC:Number = 'f'.charCodeAt(0);
		private static const _FRAZ:Number = 'F'.charCodeAt(0);
		private static const _ERA :Number = 'g'.charCodeAt(0);
		private static const _HOUR:Number = 'h'.charCodeAt(0);
		private static const _HR24:Number = 'H'.charCodeAt(0);
		private static const _MINS:Number = 'm'.charCodeAt(0);
		private static const _MOS :Number = 'M'.charCodeAt(0);
		private static const _SECS:Number = 's'.charCodeAt(0);
		private static const _AMPM:Number = 't'.charCodeAt(0);
		private static const _YEAR:Number = 'y'.charCodeAt(0);
		private static const _ZONE:Number = 'z'.charCodeAt(0);

		/**
		 * Hashtable of standard formatting flags and their formatting patterns
		 */
		private static const _STD_DATE:Object = {
			d: "MM/dd/yyyy",
			D: "dddd, dd MMMM yyyy",
			f: "dddd, dd MMMM yyyy HH:mm",
			F: "dddd, dd MMMM yyyy HH:mm:ss",
			g: "MM/dd/yyyy HH:mm",
			G: "MM/dd/yyyy HH:mm:ss",
			M: "MMMM dd",
			m: "MMMM dd",
			R: "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'",
			r: "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'",
			s: "yyyy-MM-ddTHH:mm:ss",
			t: "HH:mm",
			T: "HH:mm:ss",
			u: "yyyy-MM-dd HH:mm:ssZ",
			U: "yyyy-MM-dd HH:mm:ssZ", // must convert to UTC!
			Y: "yyyy MMMM",
			y: "yyyy MMMM"
		};

		private static function datePattern(b:ByteArray, p:String, d:Date):void
		{
			var a:int, i:int, j:int, n:int, c:Number, s:String;
			
			// check for standard format flag, retrieve pattern if needed
			if (p.length == 1) {
				if (p == "U") d = Dates.toUTC(d);
				p = _STD_DATE[p];
				if (p == null) throw new ArgumentError("Invalid date format: "+p);
			}
			
			// process custom formatting pattern
			for (i=0; i<p.length;) {
				c = p.charCodeAt(i);
				for (n=0,j=i; j<p.length && p.charCodeAt(j)==c; ++j, ++n);
				
				if (c == _DATE) {
					if (n >= 4) {
						b.writeUTFBytes(DAY_NAMES[d.day]);
					} else if (n == 3) {
						b.writeUTFBytes(DAY_ABBREVS[d.day]);
					} else if (n == 2) {
						b.writeUTFBytes(pad(d.date, 2));
					} else {
						b.writeUTFBytes(String(d.date));
					}
				}
				else if (c == _ERA) {
					b.writeUTFBytes(d.fullYear<0 ? BC : AD);
				}
				else if (c == _FRAC) {
					a = int(Math.round(Math.pow(10,n) * (d.time/1000 % 1)));
					b.writeUTFBytes(String(a));
				}
				else if (c == _FRAZ) {
					a = int(Math.round(Math.pow(10,n) * (d.time/1000 % 1)));
					s = String(a);
					for (a=s.length; s.charCodeAt(a-1)==_ZERO; --a);
					b.writeUTFBytes(s.substring(0,a));
				}
				else if (c == _HOUR) {
					a = (a=(int(d.hours)%12)) == 0 ? 12 : a;
					b.writeUTFBytes(n==2 ? pad(a,2) : String(a));
				}
				else if (c == _HR24) {
					a = int(d.hours);
					b.writeUTFBytes(n==2 ? pad(a,2) : String(a));
				}
				else if (c == _MINS) {
					a = int(d.minutes);
					b.writeUTFBytes(n==2 ? pad(a,2) : String(a));
				}
				else if (c == _MOS) {
					if (n >= 4) {
						b.writeUTFBytes(MONTH_NAMES[d.month]);
					} else if (n == 3) {
						b.writeUTFBytes(MONTH_ABBREVS[d.month]);
					} else {
						a = int(d.month+1);
						b.writeUTFBytes(n==2 ? pad(a,2) : String(a));
					}
				}
				else if (c == _SECS) {
					a = int(d.seconds);
					b.writeUTFBytes(n==2 ? pad(a,2) : String(a));
				}
				else if (c == _AMPM) {
					s = d.hours > 11 ? (n==2 ? PM2 : PM1) : (n==2 ? AM2 : AM1);
					b.writeUTFBytes(s);
				}
				else if (c == _YEAR) {
					if (n == 1) {
						b.writeUTFBytes(String(int(d.fullYear) % 100));
					} else {
						a = int(d.fullYear) % int(Math.pow(10,n));
						b.writeUTFBytes(pad(a, n));
					}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -