Skip to content

Commit 2a7a0be

Browse files
author
Milli
committed
newlines work properly with morse code!
1 parent 2e2cf1b commit 2a7a0be

File tree

1 file changed

+43
-11
lines changed

1 file changed

+43
-11
lines changed

Encoder.cs

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -239,31 +239,63 @@ public static string MorseCode(string encIn, out bool error, bool encode)
239239
}
240240
catch (KeyNotFoundException)
241241
{
242-
// bypass this character, don't decode it
243-
encOut += encIn[i] + " ";
244-
// let Main know that an error has occured
245-
error = true;
242+
if (encIn[i] == '\n' && encIn[i - 1] == '\r')
243+
{
244+
encOut += "\r\n";
245+
}
246+
else if (encIn[i] != '\r')
247+
{
248+
// bypass this character, don't decode it
249+
encOut += encIn[i] + " ";
250+
// let Main know that an error has occured
251+
error = true;
252+
}
246253
}
247254
}
248255
}
249256
// DECODE
250257
else
251258
{
252259
encIn = encIn.Trim() + " ";
260+
for (int i = 1; i < encIn.Length; i++)
261+
{
262+
if (encIn[i] == '\r' && encIn[i - 1] != ' ')
263+
{
264+
encIn = encIn.Insert(i, " ");
265+
i++;
266+
}
267+
}
253268
while (encIn.Length > 1)
254269
{
255270
// current morse code sequence, one character long.
256-
string currentChar = encIn[..encIn.IndexOf(' ')];
257-
try
271+
string currentChar;
272+
if (encIn[0] != '\r')
258273
{
259-
encOut += MorseT[currentChar];
274+
currentChar = encIn[..encIn.IndexOf(' ')];
275+
try
276+
{
277+
encOut += MorseT[currentChar];
278+
}
279+
catch (KeyNotFoundException)
280+
{
281+
if (currentChar == "\r\n")
282+
{
283+
encOut += "\r\n";
284+
}
285+
else
286+
{
287+
encOut += currentChar + " ";
288+
}
289+
// bypass this character, don't decode it
290+
error = true;
291+
}
292+
encIn = encIn.Remove(0, encIn.IndexOf(' ') + 1);
260293
}
261-
catch (KeyNotFoundException)
294+
else
262295
{
263-
// bypass this character, don't decode it
264-
encOut += currentChar + " ";
296+
encOut += "\r\n";
297+
encIn = encIn.Remove(0, encIn.IndexOf('\n') + 1);
265298
}
266-
encIn = encIn.Remove(0, encIn.IndexOf(' ') + 1);
267299
}
268300
}
269301

0 commit comments

Comments
 (0)