Beispiel #1
0
        /// <summary>
        /// Tries to convert the string representation of the name or numeric value of one or more enumerated values to an equivalent enumerated object.
        /// In case of success the return value is <see langword="true"/>&#160;and parsed <see langword="enum"/>&#160;is returned in <paramref name="result"/> parameter.
        /// </summary>
        /// <param name="value">The string representation of the enumerated value or values to parse.</param>
        /// <param name="separator">In case of more values specifies the separator among the values. If empty, then comma (<c>,</c>) separator is used.</param>
        /// <param name="ignoreCase">If <see langword="true"/>, ignores case; otherwise, regards case.</param>
        /// <param name="result">Returns the default value of <typeparamref name="TEnum"/>, if return value is <see langword="false"/>; otherwise, the parsed <see langword="enum"/>&#160;value.</param>
        /// <returns><see langword="false"/>&#160;if the <see cref="ReadOnlySpan{T}"><![CDATA[ReadOnlySpan<char>]]></see> in <paramref name="value"/> parameter cannot be parsed as <typeparamref name="TEnum"/>; otherwise, <see langword="true"/>.</returns>
        public static bool TryParse(ReadOnlySpan <char> value, ReadOnlySpan <char> separator, bool ignoreCase, out TEnum result)
        {
            // simple name match test (always case-sensitive)
            if (NameValuePairs.TryGetValue(value, out result))
            {
                return(true);
            }

            ReadOnlySpan <char> s = value.Trim();

            result = default(TEnum);
            if (s.Length == 0)
            {
                return(false);
            }

            // simple numeric value
            char c = s[0];

            if (((c >= '0' && c <= '9') || c == '-' || c == '+') && s.TryParseIntQuick(underlyingInfo.IsSigned, underlyingInfo.MaxValue, out ulong numericValue))
            {
                result = converter.ToEnum(numericValue);
                return(true);
            }

            // rest: flags enum or ignored case
            if (separator.IsEmpty)
            {
                separator = EnumExtensions.DefaultParseSeparator;
            }

            ulong acc = 0UL;
            StringKeyedDictionary <ulong> dict = ignoreCase ? NameRawValuePairsIgnoreCase : NameRawValuePairs;

            while (s.TryGetNextSegment(separator, out ReadOnlySpan <char> token))
            {
                token = token.Trim();
                if (token.Length == 0)
                {
                    return(false);
                }

                // literal token found in dictionary
                if (dict.TryGetValue(token, out ulong tokens))
                {
                    acc |= tokens;
                    continue;
                }

                // checking if is numeric token
                c = token[0];
                if (((c >= '0' && c <= '9') || c == '-' || c == '+') && token.TryParseIntQuick(underlyingInfo.IsSigned, underlyingInfo.MaxValue, out numericValue))
                {
                    acc |= numericValue;
                    continue;
                }

                // none of above
                return(false);
            }

            result = converter.ToEnum(acc);
            return(true);
        }
Beispiel #2
0
        internal PayPalTransactionResponseForMassPay(PayPalAccount usedAccount, NameValuePairs response)
        {
            RawResponse = response.ToString();
            UsedAccount = usedAccount;
            Note        = string.Empty;

            string refNo;

            response.TryGetValue("CORRELATIONID", out refNo);
            ReferenceNumber = refNo;

            string success = "";
            bool   isAck   = response.TryGetValue("ACK", out success);

            IsSuccess = isAck && String.Equals(success, "Success", StringComparison.OrdinalIgnoreCase);

            if (!IsSuccess)
            {
                string severity, errorCode, message;

                if ((!response.TryGetValue("error.severity", out severity)) || !response.TryGetValue("error(0).severity", out severity))
                {
                    response.TryGetValue("L_SHORTMESSAGE0", out severity);
                }
                if ((!response.TryGetValue("error.errorId", out errorCode)) || !response.TryGetValue("error(0).errorId", out errorCode))
                {
                    response.TryGetValue("L_ERRORCODE0", out errorCode);
                }
                if ((!response.TryGetValue("error.message", out message)) || !response.TryGetValue("error(0).message", out message))
                {
                    response.TryGetValue("L_LONGMESSAGE0", out message);
                }

                Note = severity + " " + errorCode + ": " + message;
            }
            else
            {
                Note = Resources.U4000.MONEYSENT.Replace("%p%", "PayPal");
            }
        }