コード例 #1
0
		private static string ReadWord(TextReader textReader)
		{
			StringBuilder builder = new StringBuilder();
			int r;
			while((r = textReader.Read()) != -1)
			{
				var ch = (char)r;
				bool isSpace = DataEncoder.IsSpace(ch);
				if(isSpace && builder.Length == 0)
					continue;
				if(isSpace && builder.Length != 0)
					break;
				builder.Append((char)r);
			}
			return builder.ToString();
		}
コード例 #2
0
ファイル: Money.cs プロジェクト: jannickj/NBitcoin
        public static bool TryParse(string bitcoin, out Money nRet)
        {
            nRet = new Money(0);
            if (string.IsNullOrEmpty(bitcoin))
            {
                return(false);
            }

            string strWhole = "";
            long   nUnits   = 0;

            int i = 0;

            while (DataEncoder.IsSpace(bitcoin[i]))
            {
                if (i >= bitcoin.Length)
                {
                    return(false);
                }
                i++;
            }
            if (i >= bitcoin.Length)
            {
                return(false);
            }
            bool minus = bitcoin[i] == '-';

            if (minus || bitcoin[i] == '+')
            {
                i++;
            }

            for ( ; i < bitcoin.Length; i++)
            {
                if (bitcoin[i] == '.')
                {
                    i++;
                    if (i >= bitcoin.Length)
                    {
                        break;
                    }
                    long nMult = CENT * 10;
                    while (isdigit(bitcoin[i]) && (nMult > 0))
                    {
                        nUnits += nMult * (bitcoin[i] - '0');
                        i++;
                        if (i >= bitcoin.Length)
                        {
                            break;
                        }
                        nMult /= 10;
                    }
                    break;
                }
                if (DataEncoder.IsSpace(bitcoin[i]))
                {
                    break;
                }
                if (!isdigit(bitcoin[i]))
                {
                    return(false);
                }
                strWhole += bitcoin[i];
            }
            for ( ; i < bitcoin.Length; i++)
            {
                if (!DataEncoder.IsSpace(bitcoin[i]))
                {
                    return(false);
                }
            }
            if (strWhole.Length > 10)            // guard against 63 bit overflow
            {
                return(false);
            }
            if (nUnits < 0 || nUnits > COIN)
            {
                return(false);
            }

            var nWhole = BigInteger.Parse(strWhole);
            var nValue = nWhole * COIN + nUnits;

            nRet = new Money(minus ? -nValue : nValue);
            return(true);
        }