Ionite
1 year ago
3 changed files with 48 additions and 12 deletions
@ -0,0 +1,27 @@
|
||||
using System.CodeDom; |
||||
using System.CodeDom.Compiler; |
||||
|
||||
namespace StabilityMatrix.Core.Extensions; |
||||
|
||||
public static class StringExtensions |
||||
{ |
||||
/// <summary> |
||||
/// Converts string to repr |
||||
/// </summary> |
||||
public static string ToRepr(this string str) |
||||
{ |
||||
using var writer = new StringWriter(); |
||||
using var provider = CodeDomProvider.CreateProvider("CSharp"); |
||||
|
||||
provider.GenerateCodeFromExpression( |
||||
new CodePrimitiveExpression(str), |
||||
writer, |
||||
new CodeGeneratorOptions {IndentString = "\t"}); |
||||
|
||||
var literal = writer.ToString(); |
||||
// Replace split lines |
||||
literal = literal.Replace($"\" +{Environment.NewLine}\t\"", ""); |
||||
|
||||
return literal; |
||||
} |
||||
} |
Loading…
Reference in new issue