Example #1
0
        public static BlockFeature Parse(string str)
        {
            var  input   = str;
            var  feature = new BlockFeature();
            uint height;

            var split = str.Split(new[] { '-', '+' }, StringSplitOptions.None);

            if (split.Length != 1 && split.Length != 2)
            {
                ThrowInvalidFormat();
            }
            str = split[0];
            if (split.Length == 2)
            {
                var offset = TryParse(split[1]);
                feature.Offset = input.Contains("-") ? -offset : offset;
            }


            if (str.Equals("last", StringComparison.OrdinalIgnoreCase) || str.Equals("tip", StringComparison.OrdinalIgnoreCase))
            {
                feature.Special = SpecialFeature.Last;
                return(feature);
            }

            if (uint.TryParse(str, out height))
            {
                feature.Height = (int)height;
                return(feature);
            }

            if (str.Length == 0x40 && str.ToCharArray().All(c => HexEncoder.IsDigit(c) != -1))
            {
                feature.BlockId = new uint256(str);
                return(feature);
            }

            ThrowInvalidFormat();
            return(null);
        }
Example #2
0
        internal void SetHex(string str)
        {
            Array.Clear(pn, 0, pn.Length);
            str = str.TrimStart();

            int i = 0;

            if (str.Length >= 2)
            {
                if (str[0] == '0' && char.ToLower(str[1]) == 'x')
                {
                    i += 2;
                }
            }

            int pBegin = i;

            while (i < str.Length && HexEncoder.IsDigit(str[i]) != -1)
            {
                i++;
            }

            i--;

            int p1   = 0;
            int pend = p1 + WIDTH * 4;

            while (i >= pBegin && p1 < pend)
            {
                SetByte(p1, (byte)HexEncoder.IsDigit(str[i]));
                i--;
                if (i >= pBegin)
                {
                    byte n = (byte)HexEncoder.IsDigit(str[i]);
                    n = (byte)(n << 4);
                    SetByte(p1, (byte)(GetByte(p1) | n));
                    i--;
                    p1++;
                }
            }
        }
Example #3
0
        internal static string HtmlDecode(string s)
        {
            if (s == null)
            {
                return(null);
            }

            if (s.Length == 0)
            {
                return(String.Empty);
            }

            if (s.IndexOf('&') == -1)
            {
                return(s);
            }
#if NET_4_0
            StringBuilder rawEntity = new StringBuilder();
#endif
            StringBuilder entity = new StringBuilder();
            StringBuilder output = new StringBuilder();
            int           len    = s.Length;
            // 0 -> nothing,
            // 1 -> right after '&'
            // 2 -> between '&' and ';' but no '#'
            // 3 -> '#' found after '&' and getting numbers
            int  state                = 0;
            int  number               = 0;
            bool is_hex_value         = false;
            bool have_trailing_digits = false;

            for (int i = 0; i < len; i++)
            {
                char c = s[i];
                if (state == 0)
                {
                    if (c == '&')
                    {
                        entity.Append(c);
#if NET_4_0
                        rawEntity.Append(c);
#endif
                        state = 1;
                    }
                    else
                    {
                        output.Append(c);
                    }
                    continue;
                }

                if (c == '&')
                {
                    state = 1;
                    if (have_trailing_digits)
                    {
                        entity.Append(number.ToString(CultureInfo.InvariantCulture));
                        have_trailing_digits = false;
                    }

                    output.Append(entity.ToString());
                    entity.Length = 0;
                    entity.Append('&');
                    continue;
                }

                if (state == 1)
                {
                    if (c == ';')
                    {
                        state = 0;
                        output.Append(entity.ToString());
                        output.Append(c);
                        entity.Length = 0;
                    }
                    else
                    {
                        number       = 0;
                        is_hex_value = false;
                        if (c != '#')
                        {
                            state = 2;
                        }
                        else
                        {
                            state = 3;
                        }
                        entity.Append(c);
#if NET_4_0
                        rawEntity.Append(c);
#endif
                    }
                }
                else if (state == 2)
                {
                    entity.Append(c);
                    if (c == ';')
                    {
                        string key = entity.ToString();
                        if (key.Length > 1 && Entities.ContainsKey(key.Substring(1, key.Length - 2)))
                        {
                            key = Entities[key.Substring(1, key.Length - 2)].ToString();
                        }

                        output.Append(key);
                        state         = 0;
                        entity.Length = 0;
#if NET_4_0
                        rawEntity.Length = 0;
#endif
                    }
                }
                else if (state == 3)
                {
                    if (c == ';')
                    {
#if NET_4_0
                        if (number == 0)
                        {
                            output.Append(rawEntity.ToString() + ";");
                        }
                        else
#endif
                        if (number > 65535)
                        {
                            output.Append("&#");
                            output.Append(number.ToString(CultureInfo.InvariantCulture));
                            output.Append(";");
                        }
                        else
                        {
                            output.Append((char)number);
                        }
                        state         = 0;
                        entity.Length = 0;
#if NET_4_0
                        rawEntity.Length = 0;
#endif
                        have_trailing_digits = false;
                    }
                    else if (is_hex_value && (HexEncoder.IsDigit(c) != -1))
                    {
                        number = number * 16 + HexEncoder.IsDigit(c);
                        have_trailing_digits = true;
#if NET_4_0
                        rawEntity.Append(c);
#endif
                    }
                    else if (Char.IsDigit(c))
                    {
                        number = number * 10 + ((int)c - '0');
                        have_trailing_digits = true;
#if NET_4_0
                        rawEntity.Append(c);
#endif
                    }
                    else if (number == 0 && (c == 'x' || c == 'X'))
                    {
                        is_hex_value = true;
#if NET_4_0
                        rawEntity.Append(c);
#endif
                    }
                    else
                    {
                        state = 2;
                        if (have_trailing_digits)
                        {
                            entity.Append(number.ToString(CultureInfo.InvariantCulture));
                            have_trailing_digits = false;
                        }
                        entity.Append(c);
                    }
                }
            }

            if (entity.Length > 0)
            {
                output.Append(entity.ToString());
            }
            else if (have_trailing_digits)
            {
                output.Append(number.ToString(CultureInfo.InvariantCulture));
            }
            return(output.ToString());
        }