Advertisement Jump to content
Finalspace

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
Advertisement

I believe the true reason for you to do this was boredom combined with a bit of an itch xD
I totally feel you, man :D

Share this post


Link to post
Share on other sites

Really i have no idea why i did that, but hey its still used in a production tool i made, used to deploy updates to our applications.

But this tool is used by developers only, so even when something would be wrong about the messages being written - nobody except me would notice it :D

 

Ah wait, i know why i did it: I was tired of writing "Copy A to B" vs "Copied A to B successfully" vs "Failed to copy A to B" all the time. So i ended up writing that "thing". This saved me a few words of typing. Also this had the advantage to use it for other operations as well, like "Initialize", "Create", "Make", "Destroy", etc.

Edited by Finalspace

Share this post


Link to post
Share on other sites

What about:

  • Current operation: Copy A to B.
  • Operation failed: Copy A to B.
  • Operation succeeded: Copy A to B.
  • Confirm operation: Copy A to B

No problem with irregular verb conjugation at all :) Also works for localisation into languages where verb tenses aren't formed similarly to English, etc.

Edited by pcmaster

Share this post


Link to post
Share on other sites
On 10/25/2018 at 9:31 AM, pcmaster said:

What about:

  • Current operation: Copy A to B.
  • Operation failed: Copy A to B.
  • Operation succeeded: Copy A to B.
  • Confirm operation: Copy A to B

No problem with irregular verb conjugation at all :) Also works for localisation into languages where verb tenses aren't formed similarly to English, etc.

Thats neat. Thanks for the tip.

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!