Ejemplo n.º 1
0
        //
        // Summary:
        //     Will either:
        //      - toggle current case
        //      - change to upper case
        //      - change to lower case
        //     of the characters of a given string.
        //
        // Parameters:
        //   inputStr:
        //     The string whose characters' case should be changed.
        //
        //   initialInputStringOffset:
        //     The offset of the first character of the given string.
        //
        //   selectionSpan:
        //     The document span which (partially) contains the given string.
        //
        //   operation:
        //     null  - toggle current case
        //     true  - change to upper case
        //     false - change to lower case
        //
        // Returns:
        //     The result string after chaning its character case according to the specified operation.
        //
        internal static string CharacterCaseSwitchingHelper(string inputStr, int initialInputStringOffset, DocumentSpan selectionSpan, bool?operation)
        {
            StringBuilder builder     = new StringBuilder(inputStr.Length);
            int           offsetInRun = initialInputStringOffset;

            foreach (char c in inputStr)
            {
                // check if the offset of the current string character is intersecting with the selection's current document span
                // (a selection may consists of more than one document span)
                if (selectionSpan.Contains(offsetInRun))
                {
                    // if current character is intersecting with the document span -> execute the operation over that character
                    if (!operation.HasValue)
                    {
                        builder.Append(char.IsUpper(c) ? char.ToLower(c) : char.ToUpper(c));
                    }
                    else if (operation.Value)
                    {
                        builder.Append(char.ToUpper(c));
                    }
                    else
                    {
                        builder.Append(char.ToLower(c));
                    }
                }
                else
                {
                    // if current character is not intersecting with the document span -> don't do anything
                    builder.Append(c);
                }
                offsetInRun++;
            }

            return(builder.ToString());
        }