Sign in to follow this  

Writing subjects in present and past is hard

Recommended Posts

I wanted a class which produces a string which is like: Aquiring, Aquired, Initializing, Initialized, Building, Built, Copy, Copied etc.

Just to write "less" code... i thought... why did i do that, i have no idea... maybe because i can???

AddLog(SubjectBuilder.Build("C", false, "file A to B"));
AddLog(SubjectBuilder.Build("C", true, "file A to B successfully"));

 

Anyway this is the result:

    /// <summary>
    /// Simple class for genering a message using a subject as prefix
    /// </summary>
    public static class SubjectBuilder
    {
        private static readonly Dictionary<char, string> _subjectLetterMap = new Dictionary<char, string>() {
            { 'A', "Aquire" },
            { 'B', "Build" },
            { 'I', "Initialize" },
            { 'G', "Generate" },
            { 'D', "Destroy" },
            { 'M', "Make" },
            { 'C', "Copy" },
            { 'P', "Publish" }
        };

        class SubjectRule
        {
            public bool CurCut { get; set; } = true;
            public string Cur { get; set; } = "ing";
            public bool PastCut { get; set; } = false;
            public string Past { get; set; } = "d";
        }

        private static readonly Dictionary<char, SubjectRule> _rules = new Dictionary<char, SubjectRule>()
        {
            { 'e', new SubjectRule() },
            { 'd', new SubjectRule() { CurCut = false, PastCut = true, Past = "t" } },
            { 'y', new SubjectRule() { CurCut = false, PastCut = true, Past = "ied" } },
            { 'h', new SubjectRule() { CurCut = false, PastCut = false, Past = "ed" } }
        };

        /// <summary>
        /// Returns a simple string starting with a subject - like create, generate, copy.
        /// The subject is modified to satisfy the past or current time (creat-ing, creat-ed, etc.)
        /// </summary>
        /// <param name="firstLetterOrSubject">Subject or just the first letter of the subject</param>
        /// <param name="past">Is it now or in the past</param>
        /// <param name="text">The actual text</param>
        /// <returns>Builded message startin with the subject</returns>
        public static string Build(string firstLetterOrSubject, bool past, string text)
        {
            StringBuilder s = new StringBuilder();
            string subject = null;
            if (!string.IsNullOrEmpty(firstLetterOrSubject))
            {
                if (firstLetterOrSubject.Length == 1)
                {
                    // Single letter - lookup into the subject table
                    char upperCh = firstLetterOrSubject.ToUpper()[0];
                    if (_subjectLetterMap.ContainsKey(upperCh))
                        subject = _subjectLetterMap[upperCh];
                }
                else
                    subject = firstLetterOrSubject;
            }
            if (!string.IsNullOrEmpty(subject))
            {
                char lastChar = subject.Substring(subject.Length - 1)[0];
                SubjectRule rule = _rules.ContainsKey(lastChar) ? _rules[lastChar] : new SubjectRule();
                if (past)
                {
                    if (rule.PastCut)
                        s.Append(subject.Substring(0, subject.Length - 1));
                    else
                        s.Append(subject);
                    s.Append(rule.Past);
                }
                else
                {
                    if (rule.CurCut)
                        s.Append(subject.Substring(0, subject.Length - 1));
                    else
                        s.Append(subject);
                    s.Append(rule.Cur);
                }
                s.Append(" ");
            }
            s.Append(text);
            return s.ToString();
        }
    }

 

And yes this is used in a productive every day used application...

Edited by Finalspace

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this