Created
December 6, 2018 10:45
-
-
Save olvrb/0b950ae5b59e97b029c3ad5dc30c9af6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
namespace FallProject.Utilities { | |
public static class Base64Utilities { | |
// Credits to https://stackoverflow.com/a/11743162/8611114. | |
// Extension method, just in case I need it later. | |
public static string Base64Encode(this string plainText) { | |
// Convert the string to a byte array. | |
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText); | |
// Then convert it to a base64 string. | |
return Convert.ToBase64String(plainTextBytes); | |
} | |
// Extension method, just in case I need it later. | |
public static string Base64Decode(this string base64EncodedData) { | |
// Convert the string to a byte array. | |
byte[] base64EncodedBytes = Convert.FromBase64String(base64EncodedData); | |
// Then convert it to a UTF8 string. | |
return Encoding.UTF8.GetString(base64EncodedBytes); | |
} | |
} | |
public class SimpleList { | |
private string _listAsStrings; | |
public SimpleList(List<string> input) { | |
SetList(input); | |
} | |
private List<string> SetList(List<string> input) { | |
_listAsStrings = input | |
.Select(Base64Utilities.Base64Encode) | |
// Not sure why I'm not using Join here. | |
.Aggregate((x, y) => $"{x},{y},") | |
.Trim(); | |
return GetList(); | |
} | |
// LINQ is beautiful. | |
public List<string> GetList() => _listAsStrings | |
.Split(",") | |
// Remove empty indexes. | |
.Where(x => !string.IsNullOrEmpty(x)) | |
// Decode each index from base64. | |
.Select(Base64Utilities.Base64Decode) | |
.ToList(); | |
// Because we want so enclose everything as much as possible. | |
public string GetRawList() => _listAsStrings; | |
public List<string> Add(string item) { | |
_listAsStrings += $"{item.Base64Encode()},"; | |
return GetList(); | |
} | |
public List<string> Remove(string item) { | |
List<string> tempList = GetList(); | |
// It really does not matter if the method fails to remove the item. | |
tempList.Remove(item); | |
return SetList(tempList); | |
} | |
public static List<string> CreateListFromBase64SimpleList(string input) { | |
return input | |
.Split(",") | |
// Remove empty indexes. | |
.Where(x => !string.IsNullOrEmpty(x)) | |
// Decode each index from base64. | |
.Select(Base64Utilities.Base64Decode) | |
.ToList(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment