1 // =================================
  2 // Copyright (c) 2025 Seppo Laakko
  3 // Distributed under the MIT license
  4 // =================================
  5 
  6 using System;
  7 
  8 namespace System.IO
  9 {
 10     public class StringWriter : TextWriter
 11     {
 12         public StringWriter() : str()
 13         {
 14         }
 15         public string GetString()
 16         {
 17             return Rvalue(str);
 18         }
 19         public const string& Str() const
 20         {
 21             return str;
 22         }
 23         [nodiscard]
 24         public override Result<bool> Write(const string& x)
 25         {
 26             str.Append(x);
 27             return Result<bool>(true);
 28         }
 29         [nodiscard]
 30         public override Result<bool> Write(const wstring& x)
 31         {
 32             auto c = ToUtf8(x);
 33             if (c.Error())
 34             {
 35                 return Result<bool>(ErrorId(c.GetErrorId()));
 36             }
 37             return Write(c.Value());
 38         }
 39         [nodiscard]
 40         public override Result<bool> Write(const ustring& x)
 41         {
 42             auto c = ToUtf8(x);
 43             if (c.Error())
 44             {
 45                 return Result<bool>(ErrorId(c.GetErrorId()));
 46             }
 47             return Write(c.Value());
 48         }
 49         [nodiscard]
 50         public override Result<bool> Write(bool x)
 51         {
 52             return Write(ToString(x));
 53         }
 54         [nodiscard]
 55         public override Result<bool> Write(char x)
 56         {
 57             return Write(ToString(x));
 58         }
 59         [nodiscard]
 60         public override Result<bool> Write(wchar x)
 61         {
 62             auto r = ToString(x);
 63             if (r.Error())
 64             {
 65                 return Result<bool>(ErrorId(r.GetErrorId()));
 66             }
 67             return Write(r.Value());
 68         }
 69         [nodiscard]
 70         public override Result<bool> Write(uchar x)
 71         {
 72             auto r = ToString(x);
 73             if (r.Error())
 74             {
 75                 return Result<bool>(ErrorId(r.GetErrorId()));
 76             }
 77             return Write(r.Value());
 78         }
 79         [nodiscard]
 80         public override Result<bool> Write(sbyte x)
 81         {
 82             return Write(ToString(x));
 83         }
 84         [nodiscard]
 85         public override Result<bool> Write(byte x)
 86         {
 87             return Write(ToString(x));
 88         }
 89         [nodiscard]
 90         public override Result<bool> Write(short x)
 91         {
 92             return Write(ToString(x));
 93         }
 94         [nodiscard]
 95         public override Result<bool> Write(ushort x)
 96         {
 97             return Write(ToString(x));
 98         }
 99         [nodiscard]
100         public override Result<bool> Write(int x)
101         {
102             return Write(ToString(x));
103         }
104         [nodiscard]
105         public override Result<bool> Write(uint x)
106         {
107             return Write(ToString(x));
108         }
109         [nodiscard]
110         public override Result<bool> Write(long x)
111         {
112             return Write(ToString(x));
113         }
114         [nodiscard]
115         public override Result<bool> Write(ulong x)
116         {
117             return Write(ToString(x));
118         }
119         [nodiscard]
120         public override Result<bool> Write(float x)
121         {
122             return Write(ToString(x));
123         }
124         [nodiscard]
125         public override Result<bool> Write(double x)
126         {
127             return Write(ToString(x));
128         }
129         [nodiscard]
130         public override Result<bool> Write(const Date& date)
131         {
132             return Write(date.ToString());
133         }
134         [nodiscard]
135         public override Result<bool> Write(const DateTime& dateTime)
136         {
137             return Write(dateTime.ToString());
138         }
139         [nodiscard]
140         public override Result<bool> WriteLine()
141         {
142             str.Append('\n');
143             return Result<bool>(true);
144         }
145         [nodiscard]
146         public override Result<bool> WriteLine(const string& x)
147         {
148             auto r = Write(x);
149             return r.AndThen(WriteLine());
150         }
151         [nodiscard]
152         public override Result<bool> WriteLine(const wstring& x)
153         {
154             auto r = Write(x);
155             return r.AndThen(WriteLine());
156         }
157         [nodiscard]
158         public override Result<bool> WriteLine(const ustring& x)
159         {
160             auto r = Write(x);
161             return r.AndThen(WriteLine());
162         }
163         [nodiscard]
164         public override Result<bool> WriteLine(bool x)
165         {
166             auto r = Write(x);
167             return r.AndThen(WriteLine());
168         }
169         [nodiscard]
170         public override Result<bool> WriteLine(char x)
171         {
172             auto r = Write(x);
173             return r.AndThen(WriteLine());
174         }
175         [nodiscard]
176         public override Result<bool> WriteLine(wchar x)
177         {
178             auto r = Write(x);
179             return r.AndThen(WriteLine());
180         }
181         [nodiscard]
182         public override Result<bool> WriteLine(uchar x)
183         {
184             auto r = Write(x);
185             return r.AndThen(WriteLine());
186         }
187         [nodiscard]
188         public override Result<bool> WriteLine(sbyte x)
189         {
190             auto r = Write(x);
191             return r.AndThen(WriteLine());
192         }
193         [nodiscard]
194         public override Result<bool> WriteLine(byte x)
195         {
196             auto r = Write(x);
197             return r.AndThen(WriteLine());
198         }
199         [nodiscard]
200         public override Result<bool> WriteLine(short x)
201         {
202             auto r = Write(x);
203             return r.AndThen(WriteLine());
204         }
205         [nodiscard]
206         public override Result<bool>  WriteLine(ushort x)
207         {
208             auto r = Write(x);
209             return r.AndThen(WriteLine());
210         }
211         [nodiscard]
212         public override Result<bool> WriteLine(int x)
213         {
214             auto r = Write(x);
215             return r.AndThen(WriteLine());
216         }
217         [nodiscard]
218         public override Result<bool> WriteLine(uint x)
219         {
220             auto r = Write(x);
221             return r.AndThen(WriteLine());
222         }
223         [nodiscard]
224         public override Result<bool> WriteLine(long x)
225         {
226             auto r = Write(x);
227             return r.AndThen(WriteLine());
228         }
229         [nodiscard]
230         public override Result<bool> WriteLine(ulong x)
231         {
232             auto r = Write(x);
233             return r.AndThen(WriteLine());
234         }
235         [nodiscard]
236         public override Result<bool> WriteLine(float x)
237         {
238             auto r = Write(x);
239             return r.AndThen(WriteLine());
240         }
241         [nodiscard]
242         public override Result<bool> WriteLine(double x)
243         {
244             auto r = Write(x);
245             return r.AndThen(WriteLine());
246         }
247         [nodiscard]
248         public override Result<bool> WriteLine(const Date& date)
249         {
250             auto r = Write(date);
251             return r.AndThen(WriteLine());
252         }
253         [nodiscard]
254         public override Result<bool> WriteLine(const DateTime& dateTime)
255         {
256             auto r = Write(dateTime);
257             return r.AndThen(WriteLine());
258         }
259         private string str;
260     }