summaryrefslogtreecommitdiff
path: root/modules/mono/editor/GodotTools/GodotTools.IdeConnection/MessageComposer.cs
blob: 9e4cd6ec1a4d57693089236633c9cf5bc1b3f4be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System.Linq;
using System.Text;

namespace GodotTools.IdeConnection
{
    public class MessageComposer
    {
        private readonly StringBuilder stringBuilder = new StringBuilder();

        private static readonly char[] CharsToEscape = { '\\', '"' };

        public void AddArgument(string argument)
        {
            AddArgument(argument, quoted: argument.Contains(","));
        }

        public void AddArgument(string argument, bool quoted)
        {
            if (stringBuilder.Length > 0)
                stringBuilder.Append(',');

            if (quoted)
            {
                stringBuilder.Append('"');
                
                foreach (char @char in argument)
                {
                    if (CharsToEscape.Contains(@char))
                        stringBuilder.Append('\\');
                    stringBuilder.Append(@char);
                }
                
                stringBuilder.Append('"');
            }
            else
            {
                stringBuilder.Append(argument);
            }
        }

        public override string ToString()
        {
            return stringBuilder.ToString();
        }
    }
}