Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions CSharp/rube.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace Rube
{
class Program
{
private readonly byte[] _word = new byte[] { 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2C, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x2E, 0x0D, 0x0A };
private readonly ConcurrentCharacterConverter<byte, char> _converter = new ConcurrentCharacterConverter<byte, char>();
public static Task Main(string[] args) => new Program().StartAsync();

private async Task StartAsync()
{
await foreach (var c in GetChars(this._word))
{
var output = new CharWrapper(c) switch {
CharWrapper { Char: var x, IsDigit: true } => string.Empty,
CharWrapper { Char: var x, IsControl: true } => x.ToString(),
CharWrapper { Char: var x, IsLower: true } => x.ToString(),
CharWrapper { Char: var x, IsPunctuation: true} => x == '.' ? "." : ",",
CharWrapper { Char: var x } => x.ToString()
};

if (!string.IsNullOrEmpty(output))
Console.Write(output);
}
}

private async IAsyncEnumerable<char> GetChars(byte[] array)
{
var tasks = array.Select(x => this._converter.ConvertAsync(x));

foreach (var task in tasks)
{
yield return await task;
}
}
}

public readonly struct CharWrapper
{
public char Char { get; }
public bool IsDigit => char.IsDigit(this.Char);
public bool IsControl => char.IsControl(this.Char);
public bool IsLower => char.IsLower(this.Char);
public bool IsPunctuation => char.IsPunctuation(this.Char);

public CharWrapper(char c)
=> this.Char = c;
}

public class ConcurrentCharacterConverter<From, To>
{
private readonly SemaphoreSlim _semaphore;

public ConcurrentCharacterConverter()
{
this._semaphore = new SemaphoreSlim(1, 1);
}

public Task<To> ConvertAsync(From input)
{
return Task.Run<To>(async () => {
try
{
await this._semaphore.WaitAsync();
return (To)Convert.ChangeType(input, typeof(To));
}
finally
{
this._semaphore.Release();
}
});
}
}
}