/// <summary> /// ConvertFrom /// </summary> public override object ConvertFrom( ITypeDescriptorContext context, CultureInfo cultureInfo, object value) { string stringValue = value as string; if (value == null) { throw new NotSupportedException(SR.Get(SRID.Converter_ConvertFromNotSupported)); } TokenizerHelper th = new TokenizerHelper(stringValue, cultureInfo); return new KeySpline( Convert.ToDouble(th.NextTokenRequired(), cultureInfo), Convert.ToDouble(th.NextTokenRequired(), cultureInfo), Convert.ToDouble(th.NextTokenRequired(), cultureInfo), Convert.ToDouble(th.NextTokenRequired(), cultureInfo)); }
static private Color ParseScRgbColor(string trimmedColor, IFormatProvider formatProvider) { if (!trimmedColor.StartsWith("sc#", StringComparison.Ordinal)) { throw new FormatException(SR.Get(SRID.Parsers_IllegalToken)); } string tokens = trimmedColor.Substring(3, trimmedColor.Length - 3); // The tokenizer helper will tokenize a list based on the IFormatProvider. TokenizerHelper th = new TokenizerHelper(tokens, formatProvider); float[] values = new float[4]; for (int i = 0; i < 3; i++) { values[i] = Convert.ToSingle(th.NextTokenRequired(), formatProvider); } if (th.NextToken()) { values[3] = Convert.ToSingle(th.GetCurrentToken(), formatProvider); // We should be out of tokens at this point if (th.NextToken()) { throw new FormatException(SR.Get(SRID.Parsers_IllegalToken)); } return(Color.FromScRgb(values[0], values[1], values[2], values[3])); } else { return(Color.FromScRgb(1.0f, values[0], values[1], values[2])); } }
static internal Thickness FromString(string s, CultureInfo cultureInfo) { TokenizerHelper th = new TokenizerHelper(s, cultureInfo); double[] lengths = new double[4]; int i = 0; // Peel off each double in the delimited list. while (th.NextToken()) { if (i >= 4) { i = 5; // Set i to a bad value. break; } lengths[i] = LengthConverter.FromString(th.GetCurrentToken(), cultureInfo); i++; } // We have a reasonable interpreation for one value (all four edges), two values (horizontal, vertical), // and four values (left, top, right, bottom). switch (i) { case 1: return new Thickness(lengths[0]); case 2: return new Thickness(lengths[0], lengths[1], lengths[0], lengths[1]); case 4: return new Thickness(lengths[0], lengths[1], lengths[2], lengths[3]); } throw new FormatException(SR.Get(SRID.InvalidStringThickness, s)); }
/// <summary> /// Parse - returns an instance converted from the provided string using /// the culture "en-US" /// <param name="source"> string with Point4D data </param> /// </summary> public static Point4D Parse(string source) { IFormatProvider formatProvider = System.Windows.Markup.TypeConverterHelper.InvariantEnglishUS; TokenizerHelper th = new TokenizerHelper(source, formatProvider); Point4D value; String firstToken = th.NextTokenRequired(); value = new Point4D( Convert.ToDouble(firstToken, formatProvider), Convert.ToDouble(th.NextTokenRequired(), formatProvider), Convert.ToDouble(th.NextTokenRequired(), formatProvider), Convert.ToDouble(th.NextTokenRequired(), formatProvider)); // There should be no more tokens in this string. th.LastTokenRequired(); return value; }
/// <summary> /// Parses a VirtualizationCacheLength from a string given the CultureInfo. /// </summary> /// <param name="s">String to parse from.</param> /// <param name="cultureInfo">Culture Info.</param> /// <returns>Newly created VirtualizationCacheLength instance.</returns> static internal VirtualizationCacheLength FromString(string s, CultureInfo cultureInfo) { TokenizerHelper th = new TokenizerHelper(s, cultureInfo); double[] lengths = new double[2]; int i = 0; // Peel off each double in the delimited list. while (th.NextToken()) { if (i >= 2) { i = 3; // Set i to a bad value. break; } lengths[i] = Double.Parse(th.GetCurrentToken(), cultureInfo); i++; } // We have a reasonable interpreation for one value (all four edges), two values (horizontal, vertical), // and four values (left, top, right, bottom). switch (i) { case 1: return new VirtualizationCacheLength(lengths[0]); case 2: // Should allowInfinity be false ??? (Rob) return new VirtualizationCacheLength(lengths[0], lengths[1]); } throw new FormatException(SR.Get(SRID.InvalidStringVirtualizationCacheLength, s)); }
/// <summary> /// Parse - returns an instance converted from the provided string using /// the culture "en-US" /// <param name="source"> string with Int32Rect data </param> /// </summary> public static Int32Rect Parse(string source) { IFormatProvider formatProvider = System.Windows.Markup.TypeConverterHelper.InvariantEnglishUS; TokenizerHelper th = new TokenizerHelper(source, formatProvider); Int32Rect value; String firstToken = th.NextTokenRequired(); // The token will already have had whitespace trimmed so we can do a // simple string compare. if (firstToken == "Empty") { value = Empty; } else { value = new Int32Rect( Convert.ToInt32(firstToken, formatProvider), Convert.ToInt32(th.NextTokenRequired(), formatProvider), Convert.ToInt32(th.NextTokenRequired(), formatProvider), Convert.ToInt32(th.NextTokenRequired(), formatProvider)); } // There should be no more tokens in this string. th.LastTokenRequired(); return value; }
internal override object ConvertFromCore(ITypeDescriptorContext td, CultureInfo ci, string value) { _tokenizer = new TokenizerHelper(value, '\0' /* quote char */, DelimiterChar); List<Point> list = new List<Point>(Math.Min(256, value.Length / EstimatedCharCountPerItem + 1)); while (_tokenizer.NextToken()) { list.Add((Point) converter.ConvertFrom(td, ci, _tokenizer.GetCurrentToken())); } return list; }
internal sealed override object ConvertFromCore(ITypeDescriptorContext td, CultureInfo ci, string value) { _tokenizer = new TokenizerHelper(value, '\0' /* quote char */, DelimiterChar); // Estimate the output list's capacity from length of the input string. List<double> list = new List<double>(Math.Min(256, value.Length / EstimatedCharCountPerItem + 1)); while (_tokenizer.NextToken()) { list.Add(Convert.ToDouble(_tokenizer.GetCurrentToken(), ci)); } return list; }
static internal CornerRadius FromString(string s, CultureInfo cultureInfo) { TokenizerHelper th = new TokenizerHelper(s, cultureInfo); double[] radii = new double[4]; int i = 0; // Peel off each Length in the delimited list. while (th.NextToken()) { if (i >= 4) { i = 5; // Set i to a bad value. break; } radii[i] = double.Parse(th.GetCurrentToken(), cultureInfo); i++; } // We have a reasonable interpreation for one value (all four edges) // and four values (left, top, right, bottom). switch (i) { case 1: return (new CornerRadius(radii[0])); case 4: return (new CornerRadius(radii[0], radii[1], radii[2], radii[3])); } throw new FormatException(SR.Get(SRID.InvalidStringCornerRadius, s)); }
static private Color ParseScRgbColor(string trimmedColor, IFormatProvider formatProvider) { if (!trimmedColor.StartsWith("sc#", StringComparison.Ordinal)) { throw new FormatException(SR.Get(SRID.Parsers_IllegalToken)); } string tokens = trimmedColor.Substring(3, trimmedColor.Length - 3); // The tokenizer helper will tokenize a list based on the IFormatProvider. TokenizerHelper th = new TokenizerHelper(tokens, formatProvider); float[] values = new float[4]; for (int i = 0; i < 3; i++) { values[i] = Convert.ToSingle(th.NextTokenRequired(), formatProvider); } if (th.NextToken()) { values[3] = Convert.ToSingle(th.GetCurrentToken(), formatProvider); // We should be out of tokens at this point if (th.NextToken()) { throw new FormatException(SR.Get(SRID.Parsers_IllegalToken)); } return Color.FromScRgb(values[0], values[1], values[2], values[3]); } else { return Color.FromScRgb(1.0f, values[0], values[1], values[2]); } }
static private Color ParseContextColor(string trimmedColor, IFormatProvider formatProvider, ITypeDescriptorContext context) { if (!trimmedColor.StartsWith(s_ContextColor, StringComparison.OrdinalIgnoreCase)) { throw new FormatException(SR.Get(SRID.Parsers_IllegalToken)); } string tokens = trimmedColor.Substring(s_ContextColor.Length); tokens = tokens.Trim(); string[] preSplit = tokens.Split(new Char[] { ' ' }); if (preSplit.GetLength(0)< 2) { throw new FormatException(SR.Get(SRID.Parsers_IllegalToken)); } tokens = tokens.Substring(preSplit[0].Length); TokenizerHelper th = new TokenizerHelper(tokens, formatProvider); string[] split = tokens.Split(new Char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries); int numTokens = split.GetLength(0); float alpha = Convert.ToSingle(th.NextTokenRequired(), formatProvider); float[] values = new float[numTokens - 1]; for (int i = 0; i < numTokens - 1; i++) { values[i] = Convert.ToSingle(th.NextTokenRequired(), formatProvider); } string profileString = preSplit[0]; UriHolder uriHolder = TypeConverterHelper.GetUriFromUriContext(context,profileString); Uri profileUri; if (uriHolder.BaseUri != null) { profileUri = new Uri(uriHolder.BaseUri, uriHolder.OriginalUri); } else { profileUri = uriHolder.OriginalUri; } Color result = Color.FromAValues(alpha, values, profileUri); // If the number of color values found does not match the number of channels in the profile, we must throw if (result.ColorContext.NumChannels != values.Length) { throw new FormatException(SR.Get(SRID.Parsers_IllegalToken)); } return result; }
public static List<IntegerMarkup> Parse(string source) { IFormatProvider formatProvider = TypeConverterHelper.InvariantEnglishUS; TokenizerHelper th = new TokenizerHelper(source, formatProvider); List<IntegerMarkup> resource = new List<IntegerMarkup>(); int value; while (th.NextToken()) { value = Convert.ToInt32(th.GetCurrentToken(), formatProvider); resource.Add( new IntegerMarkup(value) ); } return resource; }