public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
 {
     if (value is string)
     {
         return(CpuSet.Parse((string)value));
     }
     return(base.ConvertFrom(context, culture, value));
 }
Exemplo n.º 2
0
        public static bool Parse(string set, out CpuSet cpus, bool tryParse)
        {
            cpus = default(CpuSet);
            if (set == null)
            {
                if (tryParse)
                {
                    return(false);
                }
                throw new ArgumentNullException(nameof(set));
            }
            if (set.Length == 0)
            {
                cpus = new CpuSet(Array.Empty <int>());
                return(true);
            }
            int index   = 0;
            var cpuList = new List <int>();

            do
            {
                int start;
                if (!TryParseNumber(set, ref index, out start))
                {
                    return(ParseFailed(tryParse, $"Can not parse number at {index}"));
                }
                if (index == set.Length)
                {
                    cpuList.Add(start);
                    break;
                }
                else if (set[index] == ',')
                {
                    cpuList.Add(start);
                    index++;
                    continue;
                }
                else if (set[index] == '-')
                {
                    index++;
                    int end;
                    if (!TryParseNumber(set, ref index, out end))
                    {
                        return(ParseFailed(tryParse, $"Can not parse number at {index}"));
                    }
                    if (start > end)
                    {
                        return(ParseFailed(tryParse, "End of range is larger than start"));
                    }
                    for (int i = start; i <= end; i++)
                    {
                        cpuList.Add(i);
                    }
                    if (index == set.Length)
                    {
                        break;
                    }
                    else if (set[index] == ',')
                    {
                        index++;
                        continue;
                    }
                    else
                    {
                        return(ParseFailed(tryParse, $"Invalid character at {index}: '{set[index]}'"));
                    }
                }
                else
                {
                    return(ParseFailed(tryParse, $"Invalid character at {index}: '{set[index]}'"));
                }
            } while (index != set.Length);
            var cpuArray = cpuList.ToArray();

            Array.Sort(cpuArray);
            cpus = new CpuSet(cpuArray);
            return(true);
        }
Exemplo n.º 3
0
 public static bool TryParse(string set, out CpuSet cpus)
 {
     return(Parse(set, out cpus, tryParse: true));
 }