Skip to content

Instantly share code, notes, and snippets.

@incongruousm
Last active July 29, 2024 20:16
Show Gist options
  • Save incongruousm/509ef8820532883f9899b6e980ef6503 to your computer and use it in GitHub Desktop.
Save incongruousm/509ef8820532883f9899b6e980ef6503 to your computer and use it in GitHub Desktop.
A C# port of the PlantUml Text Encoding logic as given at http://plantuml.com/pte.html
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Text;
namespace PlantUml.Utils
{
public class PlantUmlTextEncoder
{
public string Encode(TextReader reader)
{
using (var output = new MemoryStream())
{
using (var writer = new StreamWriter(new DeflateStream(output, CompressionLevel.Optimal), Encoding.UTF8))
writer.Write(reader.ReadToEnd());
return Encode(output.ToArray());
}
}
private static string Encode(IReadOnlyList<byte> bytes)
{
var length = bytes.Count;
var s = new StringBuilder();
for (var i = 0; i < length; i += 3)
{
var b1 = bytes[i];
var b2 = i + 1 < length ? bytes[i + 1] : (byte) 0;
var b3 = i + 2 < length ? bytes[i + 2] : (byte) 0;
s.Append(Append3Bytes(b1, b2, b3));
}
return s.ToString();
}
private static char[] Append3Bytes(byte b1, byte b2, byte b3)
{
var c1 = b1 >> 2;
var c2 = (b1 & 0x3) << 4 | b2 >> 4;
var c3 = (b2 & 0xF) << 2 | b3 >> 6;
var c4 = b3 & 0x3F;
return new[]
{
EncodeByte((byte) (c1 & 0x3F)),
EncodeByte((byte) (c2 & 0x3F)),
EncodeByte((byte) (c3 & 0x3F)),
EncodeByte((byte) (c4 & 0x3F))
};
}
private static char EncodeByte(byte b)
{
var ascii = Encoding.ASCII;
if (b < 10)
return ascii.GetChars(new[] {(byte) (48 + b)})[0];
b -= 10;
if (b < 26)
return ascii.GetChars(new[] {(byte) (65 + b)})[0];
b -= 26;
if (b < 26)
return ascii.GetChars(new[] {(byte) (97 + b)})[0];
b -= 26;
if (b == 0)
return '-';
if (b == 1)
return '_';
return '?';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment