Ejemplo n.º 1
0
		private static CharSequence Js_slice(CharSequence target, object[] args)
		{
			if (args.Length != 0)
			{
				double begin = ScriptRuntime.ToInteger(args[0]);
				double end;
				int length = target.Length;
				if (begin < 0)
				{
					begin += length;
					if (begin < 0)
					{
						begin = 0;
					}
				}
				else
				{
					if (begin > length)
					{
						begin = length;
					}
				}
				if (args.Length == 1)
				{
					end = length;
				}
				else
				{
					end = ScriptRuntime.ToInteger(args[1]);
					if (end < 0)
					{
						end += length;
						if (end < 0)
						{
							end = 0;
						}
					}
					else
					{
						if (end > length)
						{
							end = length;
						}
					}
					if (end < begin)
					{
						end = begin;
					}
				}
				return target.SubSequence((int)begin, (int)end);
			}
			return target;
		}
Ejemplo n.º 2
0
		private static CharSequence Js_substring(Context cx, CharSequence target, object[] args)
		{
			int length = target.Length;
			double start = ScriptRuntime.ToInteger(args, 0);
			double end;
			if (start < 0)
			{
				start = 0;
			}
			else
			{
				if (start > length)
				{
					start = length;
				}
			}
			if (args.Length <= 1 || args[1] == Undefined.instance)
			{
				end = length;
			}
			else
			{
				end = ScriptRuntime.ToInteger(args[1]);
				if (end < 0)
				{
					end = 0;
				}
				else
				{
					if (end > length)
					{
						end = length;
					}
				}
				// swap if end < start
				if (end < start)
				{
					if (cx.GetLanguageVersion() != Context.VERSION_1_2)
					{
						double temp = start;
						start = end;
						end = temp;
					}
					else
					{
						// Emulate old JDK1.0 java.lang.String.substring()
						end = start;
					}
				}
			}
			return target.SubSequence((int)start, (int)end);
		}