flex-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ha...@apache.org
Subject git commit: [flex-asjs] [refs/heads/develop] - Added StringUtil
Date Fri, 08 Jul 2016 16:09:02 GMT
Repository: flex-asjs
Updated Branches:
  refs/heads/develop 18ee460ff -> 5aca9279e


Added StringUtil


Project: http://git-wip-us.apache.org/repos/asf/flex-asjs/repo
Commit: http://git-wip-us.apache.org/repos/asf/flex-asjs/commit/5aca9279
Tree: http://git-wip-us.apache.org/repos/asf/flex-asjs/tree/5aca9279
Diff: http://git-wip-us.apache.org/repos/asf/flex-asjs/diff/5aca9279

Branch: refs/heads/develop
Commit: 5aca9279e4ec211d90597179b7221c804f63a382
Parents: 18ee460
Author: Harbs <harbs@in-tools.com>
Authored: Fri Jul 8 19:08:55 2016 +0300
Committer: Harbs <harbs@in-tools.com>
Committed: Fri Jul 8 19:08:55 2016 +0300

----------------------------------------------------------------------
 .../projects/Core/src/main/flex/CoreClasses.as  |   3 +-
 .../flex/org/apache/flex/utils/StringTrimmer.as |   1 -
 .../flex/org/apache/flex/utils/StringUtil.as    | 255 +++++++++++++++++++
 3 files changed, 257 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/5aca9279/frameworks/projects/Core/src/main/flex/CoreClasses.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/Core/src/main/flex/CoreClasses.as b/frameworks/projects/Core/src/main/flex/CoreClasses.as
index 5796927..1a61358 100644
--- a/frameworks/projects/Core/src/main/flex/CoreClasses.as
+++ b/frameworks/projects/Core/src/main/flex/CoreClasses.as
@@ -125,11 +125,12 @@ internal class CoreClasses
 	{
 	    import org.apache.flex.utils.PNGEncoder; PNGEncoder;
     	import org.apache.flex.utils.SolidBorderUtil; SolidBorderUtil;
-	    import org.apache.flex.utils.StringTrimmer; StringTrimmer;
 		import org.apache.flex.utils.HTMLLoader; HTMLLoader;
 }
 	import org.apache.flex.utils.BrowserUtils; BrowserUtils;
 	import org.apache.flex.utils.Endian; Endian;
+	import org.apache.flex.utils.StringTrimmer; StringTrimmer;
+	import org.apache.flex.utils.StringUtil; StringUtil;
 	import org.apache.flex.utils.Timer; Timer;
 	import org.apache.flex.utils.UIUtils; UIUtils;
 

http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/5aca9279/frameworks/projects/Core/src/main/flex/org/apache/flex/utils/StringTrimmer.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/Core/src/main/flex/org/apache/flex/utils/StringTrimmer.as
b/frameworks/projects/Core/src/main/flex/org/apache/flex/utils/StringTrimmer.as
index fe8f581..bbc83f4 100644
--- a/frameworks/projects/Core/src/main/flex/org/apache/flex/utils/StringTrimmer.as
+++ b/frameworks/projects/Core/src/main/flex/org/apache/flex/utils/StringTrimmer.as
@@ -28,7 +28,6 @@ package org.apache.flex.utils
 	 *  @playerversion AIR 2.6
 	 *  @productversion FlexJS 0.0
 	 */
-    COMPILE::SWF
 	public class StringTrimmer
 	{
 		/**

http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/5aca9279/frameworks/projects/Core/src/main/flex/org/apache/flex/utils/StringUtil.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/Core/src/main/flex/org/apache/flex/utils/StringUtil.as b/frameworks/projects/Core/src/main/flex/org/apache/flex/utils/StringUtil.as
new file mode 100644
index 0000000..6c7007f
--- /dev/null
+++ b/frameworks/projects/Core/src/main/flex/org/apache/flex/utils/StringUtil.as
@@ -0,0 +1,255 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  Licensed to the Apache Software Foundation (ASF) under one or more
+//  contributor license agreements.  See the NOTICE file distributed with
+//  this work for additional information regarding copyright ownership.
+//  The ASF licenses this file to You under the Apache License, Version 2.0
+//  (the "License"); you may not use this file except in compliance with
+//  the License.  You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+//  Unless required by applicable law or agreed to in writing, software
+//  distributed under the License is distributed on an "AS IS" BASIS,
+//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+//  See the License for the specific language governing permissions and
+//  limitations under the License.
+//
+////////////////////////////////////////////////////////////////////////////////
+package org.apache.flex.utils
+{
+	/**
+	 *  The StringUtil utility class is an all-static class with methods for
+	 *  working with String objects.
+	 *  You do not create instances of StringUtil;
+	 *  instead you call methods such as 
+	 *  the <code>StringUtil.substitute()</code> method.  
+	 *  
+	 *  @langversion 3.0
+	 *  @playerversion Flash 9
+	 *  @playerversion AIR 1.1
+	 *  @productversion Flex 3
+	 *  @productversion FlexJS 0.0
+	 */
+	public class StringUtil extends StringTrimmer
+	{
+		public function StringUtil()
+		{
+			throw new Error("StringUtil should not be instantiated.");
+		}
+		
+		/**
+		 *  Substitutes "{n}" tokens within the specified string
+		 *  with the respective arguments passed in.
+		 * 
+		 *  Note that this uses String.replace and "$" can have special
+		 *  meaning in the argument strings escape by using "$$".
+		 *
+		 *  @param str The string to make substitutions in.
+		 *  This string can contain special tokens of the form
+		 *  <code>{n}</code>, where <code>n</code> is a zero based index,
+		 *  that will be replaced with the additional parameters
+		 *  found at that index if specified.
+		 *
+		 *  @param rest Additional parameters that can be substituted
+		 *  in the <code>str</code> parameter at each <code>{n}</code>
+		 *  location, where <code>n</code> is an integer (zero based)
+		 *  index value into the array of values specified.
+		 *  If the first parameter is an array this array will be used as
+		 *  a parameter list.
+		 *  This allows reuse of this routine in other methods that want to
+		 *  use the ... rest signature.
+		 *  For example <pre>
+		 *     public function myTracer(str:String, ... rest):void
+		 *     { 
+		 *         label.text += StringUtil.substitute(str, rest) + "\n";
+		 *     } </pre>
+		 *
+		 *  @return New string with all of the <code>{n}</code> tokens
+		 *  replaced with the respective arguments specified.
+		 *
+		 *  @example
+		 *
+		 *  var str:String = "here is some info '{0}' and {1}";
+		 *  trace(StringUtil.substitute(str, 15.4, true));
+		 *
+		 *  // this will output the following string:
+		 *  // "here is some info '15.4' and true"
+		 *  
+		 *  @langversion 3.0
+		 *  @playerversion Flash 9
+		 *  @playerversion AIR 1.1
+		 *  @productversion Flex 3
+		 *  @productversion FlexJS 0.0
+		 */
+		public static function substitute(str:String, ... rest):String
+		{
+			if (str == null) return '';
+			
+			// Replace all of the parameters in the msg string.
+			var len:uint = rest.length;
+			var args:Array;
+			if (len == 1 && rest[0] is Array)
+			{
+				args = rest[0] as Array;
+				len = args.length;
+			}
+			else
+			{
+				args = rest;
+			}
+			
+			for (var i:int = 0; i < len; i++)
+			{
+				str = str.replace(new RegExp("\\{"+i+"\\}", "g"), args[i]);
+			}
+			
+			return str;
+		}
+		
+		/**
+		 *  Returns a string consisting of a specified string
+		 *  concatenated with itself a specified number of times.
+		 *
+		 *  @param str The string to be repeated.
+		 *
+		 *  @param n The repeat count.
+		 *
+		 *  @return The repeated string.
+		 *  
+		 *  @langversion 3.0
+		 *  @playerversion Flash 10
+		 *  @playerversion AIR 1.5
+		 *  @productversion Flex 4.1
+		 *  @productversion FlexJS 0.0
+		 */
+		public static function repeat(str:String, n:int):String
+		{
+			if (n == 0)
+				return "";
+			var a:Array = [];
+			for (var i:int = 0; i < n; i++)
+			{
+				a.push(str);
+			}
+			return a.join("");
+		}
+		
+		/**
+		 *  Removes "unallowed" characters from a string.
+		 *  A "restriction string" such as <code>"A-Z0-9"</code>
+		 *  is used to specify which characters are allowed.
+		 *  This method uses the same logic as the <code>restrict</code>
+		 *  property of TextField.
+		 *
+		 *  @param str The input string.
+		 *
+		 *  @param restrict The restriction string.
+		 *
+		 *  @return The input string, minus any characters
+		 *  that are not allowed by the restriction string.
+		 *  
+		 *  @langversion 3.0
+		 *  @playerversion Flash 10
+		 *  @playerversion AIR 1.5
+		 *  @productversion Flex 4.1
+		 *  @productversion FlexJS 0.0
+		 */
+		public static function restrict(str:String, restrict:String):String
+		{
+			// A null 'restrict' string means all characters are allowed.
+			if (restrict == null)
+				return str;
+			
+			// An empty 'restrict' string means no characters are allowed.
+			if (restrict == "")
+				return "";
+			
+			// Otherwise, we need to test each character in 'str'
+			// to determine whether the 'restrict' string allows it.
+			var charCodes:Array = [];
+			
+			var n:int = str.length;
+			for (var i:int = 0; i < n; i++)
+			{
+				var charCode:uint = str.charCodeAt(i);
+				if (testCharacter(charCode, restrict))
+					charCodes.push(charCode);
+			}
+			
+			return String.fromCharCode.apply(null, charCodes);
+		}
+		
+		/**
+		 *  @private
+		 *  Helper method used by restrict() to test each character
+		 *  in the input string against the restriction string.
+		 *  The logic in this method implements the same algorithm
+		 *  as in TextField's 'restrict' property (which is quirky,
+		 *  such as how it handles a '-' at the beginning of the
+		 *  restriction string).
+		 */
+		private static function testCharacter(charCode:uint,
+											  restrict:String):Boolean
+		{
+			var allowIt:Boolean = false;
+			
+			var inBackSlash:Boolean = false;
+			var inRange:Boolean = false;
+			var setFlag:Boolean = true;
+			var lastCode:uint = 0;
+			
+			var n:int = restrict.length;
+			var code:uint;
+			
+			if (n > 0)
+			{
+				code = restrict.charCodeAt(0);
+				if (code == 94) // caret
+					allowIt = true;
+			}
+			
+			for (var i:int = 0; i < n; i++)
+			{
+				code = restrict.charCodeAt(i)
+				
+				var acceptCode:Boolean = false;
+				if (!inBackSlash)
+				{
+					if (code == 45) // hyphen
+						inRange = true;
+					else if (code == 94) // caret
+						setFlag = !setFlag;
+					else if (code == 92) // backslash
+						inBackSlash = true;
+					else
+						acceptCode = true;
+				}
+				else
+				{
+					acceptCode = true;
+					inBackSlash = false;
+				}
+				
+				if (acceptCode)
+				{
+					if (inRange)
+					{
+						if (lastCode <= charCode && charCode <= code)
+							allowIt = setFlag;
+						inRange = false;
+						lastCode = 0;
+					}
+					else
+					{
+						if (charCode == code)
+							allowIt = setFlag;
+						lastCode = code;
+					}
+				}
+			}
+			
+			return allowIt;
+		}
+	}
+}
\ No newline at end of file


Mime
View raw message