1 module esstool.stringbuilder;
2 
3 import esstool;
4 import std.conv : to;
5 
6 public class StringBuilder
7 {
8     private string strbuffered = "";
9     this()
10     {
11     }
12 
13     this(string def)
14     {
15         this.strbuffered = def;
16     }
17 
18     public StringBuilder append(byte b)
19     {
20         return this.append(to!char(b));
21     }
22 
23     public StringBuilder append(ubyte b)
24     {
25         return this.append(to!char(b));
26     }
27 
28     public StringBuilder append(char c)
29     {
30         this.strbuffered ~= c;
31         return this;
32     }
33 
34     public StringBuilder append(string s)
35     {
36         this.strbuffered ~= s;
37         return this;
38     }
39 
40     public StringBuilder append(int i)
41     {
42         this.strbuffered ~= i;
43         return this;
44     }
45 
46     public StringBuilder append(uint i)
47     {
48         this.strbuffered ~= i;
49         return this;
50     }
51 
52     public StringBuilder append(float f)
53     {
54         this.strbuffered ~= to!string(f);
55         return this;
56     }
57 
58     public StringBuilder append(double d)
59     {
60         this.strbuffered ~= to!string(d);
61         return this;
62     }
63 
64     public StringBuilder removeAt(int index)
65     {
66         this.strbuffered = this.strbuffered[0 .. index] ~ this.strbuffered[index + 1 .. $];
67         return this;
68     }
69 
70     public StringBuilder removeFirst()
71     {
72         this.strbuffered = this.strbuffered[1 .. $];
73         return this;
74     }
75 
76     public StringBuilder removeLast()
77     {
78         this.strbuffered = this.strbuffered[0 .. $ - 1];
79         return this;
80     }
81 
82     public StringBuilder clean()
83     {
84         this.strbuffered = "";
85         return this;
86     }
87 
88     public StringBuilder appendNewLine()
89     {
90         this.strbuffered ~= "\r\n";
91         return this;
92     }
93 
94     public int indexOf(string str)
95     {
96         int index = 0;
97         int startAt = 0;
98         int maxIndex = len(str) - 1;
99 
100         for (int i = 0; i < len(this.strbuffered); i++)
101         {
102             char c = this.strbuffered[i];
103             char pChar = str[index];
104             if (c != pChar)
105             {
106                 index = 0;
107                 continue;
108             }
109 
110             if (index++ == 0)
111             {
112                 startAt = i;
113             }
114 
115             if (index == maxIndex)
116             {
117                 return startAt;
118             }
119         }
120 
121         return -1;
122     }
123 
124     public StringBuilder replaceFirst(string oldStr, string newStr)
125     {
126         int startAt = this.indexOf(oldStr);
127         if (startAt == -1)
128         {
129             return this;
130         }
131 
132         int endAt = startAt + len(oldStr);
133 
134         string head = this.strbuffered[0 .. startAt - 1];
135         string end = this.strbuffered[endAt .. $ - 1];
136 
137         this.strbuffered = "";
138         this.append(head).append(newStr).append(end);
139 
140         return this;
141     }
142 
143     public StringBuilder remove(char s)
144     {
145         string newStr = "";
146         for (int i = 0; i < len(this.strbuffered); i++)
147         {
148             char c = this.strbuffered[i];
149             if (c == s)
150             {
151                 continue;
152             }
153 
154             newStr ~= c;
155         }
156 
157         this.strbuffered = newStr;
158         return this;
159     }
160 
161     public int size()
162     {
163         import esstool.arrayutil : len;
164 
165         return len(this.strbuffered);
166     }
167 
168     public string asString()
169     {
170         return this.strbuffered;
171     }
172 }