Ejemplo n.º 1
0
        private static String InternalFlagsFormat(EnumInfo entry, ulong result, bool camelCaseText)
        {
            string[] resolvedNames = entry.ResolvedNames;
            ulong[]  values        = entry.Values;

            int           index      = values.Length - 1;
            StringBuilder sb         = new StringBuilder();
            bool          firstTime  = true;
            ulong         saveResult = result;

            // We will not optimize this code further to keep it maintainable. There are some boundary checks that can be applied
            // to minimize the comparsions required. This code works the same for the best/worst case. In general the number of
            // items in an enum are sufficiently small and not worth the optimization.
            while (index >= 0)
            {
                if (index == 0 && values[index] == 0)
                {
                    break;
                }

                if ((result & values[index]) == values[index])
                {
                    result -= values[index];
                    if (!firstTime)
                    {
                        sb.Insert(0, EnumSeparatorString);
                    }

                    string resolvedName = resolvedNames[index];
                    sb.Insert(0, camelCaseText ? StringUtils.ToCamelCase(resolvedName) : resolvedName);
                    firstTime = false;
                }

                index--;
            }

            string returnString;

            if (result != 0)
            {
                // We were unable to represent this number as a bitwise or of valid flags
                returnString = null; // return null so the caller knows to .ToString() the input
            }
            else if (saveResult == 0)
            {
                // For the cases when we have zero
                if (values.Length > 0 && values[0] == 0)
                {
                    returnString = resolvedNames[0]; // Zero was one of the enum values.
                    if (camelCaseText)
                    {
                        returnString = StringUtils.ToCamelCase(returnString);
                    }
                }
                else
                {
                    returnString = null;
                }
            }
            else
            {
                returnString = sb.ToString(); // Return the string representation
            }

            return(returnString);
        }