예제 #1
0
        public void GetSetsByStyleTypeTest_Normal()
        {
            List <IUnikodSet> testList = UnicodeSets.GetSetsByStyleType(StyleType.Normal);

            CollectionAssert.AreEquivalent(new List <IUnikodSet>()
            {
                UnicodeSets.LatinNormalUppercaseSet,
                UnicodeSets.LatinNormalLowercaseSet,
                UnicodeSets.NumberNormalSet
            }, testList);
        }
예제 #2
0
        /// <summary>
        /// Stylize the given styled text string
        /// </summary>
        /// <param name="text">A string text to be stylized</param>
        /// <param name="set">A/Some style set(s) </param>
        /// <returns>Stylized text as `string`</returns>
        public static string Stylize(string text, params IUnikodSet[] set)
        {
            if (text == null)
            {
                return(null);
            }
            else
            {
                StringBuilder builder = new StringBuilder();

                foreach (string character in text.ToUnicodeStringArray())
                {
                    if (char.TryParse(character, out char singleCharacter))
                    {
                        if (char.IsControl(singleCharacter) || char.IsWhiteSpace(singleCharacter))
                        {
                            builder.Append(character);
                            continue;
                        }
                    }

                    bool existInNormalSets    = false;
                    int  index                = -1;
                    Type characterSetType     = null;
                    bool?characterIsUppercase = null;

                    foreach (IUnikodSet normalSet in UnicodeSets.GetSetsByStyleType(StyleType.Normal))
                    {
                        for (int i = 0, normalSetLength = normalSet.Data.Length; i < normalSetLength; i++)
                        {
                            if (normalSet.Data[i].Equals(character))
                            {
                                index             = i;
                                existInNormalSets = true;
                                characterSetType  = normalSet.GetType();

                                if (normalSet is AlphabetSet alphabetSet)
                                {
                                    characterIsUppercase = alphabetSet.LetterCaseType == LetterCaseType.Uppercase;
                                }
                            }
                        }
                    }

                    if (existInNormalSets)
                    {
                        bool found = false;

                        foreach (IUnikodSet targetSet in set)
                        {
                            if (characterSetType == targetSet.GetType())
                            {
                                if (targetSet is AlphabetSet alphabetSet)
                                {
                                    if (alphabetSet.LetterCaseType == LetterCaseType.Uppercase != characterIsUppercase)
                                    {
                                        continue;
                                    }
                                }

                                if (targetSet.Data[index] != null)
                                {
                                    builder.Append(targetSet.Data[index]);
                                    found = true;
                                    break;
                                }
                            }
                        }

                        if (!found)
                        {
                            builder.Append(character);
                        }
                    }
                    else
                    {
                        builder.Append(character);
                    }
                }

                return(builder.ToString());
            }
        }